/* * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
staticvoid assertArraysEquals(long[] r, long[] a, FUnOp f) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(r[i], f.apply(a[i]));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a[i]), "at index #" + i + ", input = " + a[i]);
}
}
interface FUnArrayOp { long[] apply(long a);
}
staticvoid assertArraysEquals(long[] r, long[] a, FUnArrayOp f) { int i = 0; try { for (; i < a.length; i += SPECIES.length()) { Assert.assertEquals(Arrays.copyOfRange(r, i, i+SPECIES.length()),
f.apply(a[i]));
}
} catch (AssertionError e) { long[] ref = f.apply(a[i]); long[] res = Arrays.copyOfRange(r, i, i+SPECIES.length()); Assert.assertEquals(res, ref, "(ref: " + Arrays.toString(ref)
+ ", res: " + Arrays.toString(res)
+ "), at index #" + i);
}
}
staticvoid assertArraysEquals(long[] r, long[] a, boolean[] mask, FUnOp f) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? f.apply(a[i]) : a[i]);
}
} catch (AssertionError e) { Assert.assertEquals(r[i], mask[i % SPECIES.length()] ? f.apply(a[i]) : a[i], "at index #" + i + ", input = " + a[i] + ", mask = " + mask[i % SPECIES.length()]);
}
}
interface FReductionOp { long apply(long[] a, int idx);
}
interface FReductionAllOp { long apply(long[] a);
}
staticvoid assertReductionArraysEquals(long[] r, long rc, long[] a,
FReductionOp f, FReductionAllOp fa) { int i = 0; try { Assert.assertEquals(rc, fa.apply(a)); for (; i < a.length; i += SPECIES.length()) { Assert.assertEquals(r[i], f.apply(a, i));
}
} catch (AssertionError e) { Assert.assertEquals(rc, fa.apply(a), "Final result is incorrect!"); Assert.assertEquals(r[i], f.apply(a, i), "at index #" + i);
}
}
interface FReductionMaskedOp { long apply(long[] a, int idx, boolean[] mask);
}
interface FReductionAllMaskedOp { long apply(long[] a, boolean[] mask);
}
staticvoid assertReductionArraysEqualsMasked(long[] r, long rc, long[] a, boolean[] mask,
FReductionMaskedOp f, FReductionAllMaskedOp fa) { int i = 0; try { Assert.assertEquals(rc, fa.apply(a, mask)); for (; i < a.length; i += SPECIES.length()) { Assert.assertEquals(r[i], f.apply(a, i, mask));
}
} catch (AssertionError e) { Assert.assertEquals(rc, fa.apply(a, mask), "Final result is incorrect!"); Assert.assertEquals(r[i], f.apply(a, i, mask), "at index #" + i);
}
}
interface FBoolReductionOp { boolean apply(boolean[] a, int idx);
}
staticvoid assertReductionBoolArraysEquals(boolean[] r, boolean[] a, FBoolReductionOp f) { int i = 0; try { for (; i < a.length; i += SPECIES.length()) { Assert.assertEquals(r[i], f.apply(a, i));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a, i), "at index #" + i);
}
}
interface FMaskReductionOp { int apply(boolean[] a, int idx);
}
staticvoid assertMaskReductionArraysEquals(int[] r, boolean[] a, FMaskReductionOp f) { int i = 0; try { for (; i < a.length; i += SPECIES.length()) { Assert.assertEquals(r[i], f.apply(a, i));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a, i), "at index #" + i);
}
}
staticvoid assertInsertArraysEquals(long[] r, long[] a, long element, int index, int start, int end) { int i = start; try { for (; i < end; i += 1) { if(i%SPECIES.length() == index) { Assert.assertEquals(r[i], element);
} else { Assert.assertEquals(r[i], a[i]);
}
}
} catch (AssertionError e) { if (i%SPECIES.length() == index) { Assert.assertEquals(r[i], element, "at index #" + i);
} else { Assert.assertEquals(r[i], a[i], "at index #" + i);
}
}
}
staticvoid assertRearrangeArraysEquals(long[] r, long[] a, int[] order, int vector_len) { int i = 0, j = 0; try { for (; i < a.length; i += vector_len) { for (j = 0; j < vector_len; j++) { Assert.assertEquals(r[i+j], a[i+order[i+j]]);
}
}
} catch (AssertionError e) { int idx = i + j; Assert.assertEquals(r[i+j], a[i+order[i+j]], "at index #" + idx + ", input = " + a[i+order[i+j]]);
}
}
staticvoid assertcompressArraysEquals(long[] r, long[] a, boolean[] m, int vector_len) { int i = 0, j = 0, k = 0; try { for (; i < a.length; i += vector_len) {
k = 0; for (j = 0; j < vector_len; j++) { if (m[(i + j) % SPECIES.length()]) { Assert.assertEquals(r[i + k], a[i + j]);
k++;
}
} for (; k < vector_len; k++) { Assert.assertEquals(r[i + k], (long)0);
}
}
} catch (AssertionError e) { int idx = i + k; if (m[(i + j) % SPECIES.length()]) { Assert.assertEquals(r[idx], a[i + j], "at index #" + idx);
} else { Assert.assertEquals(r[idx], (long)0, "at index #" + idx);
}
}
}
staticvoid assertexpandArraysEquals(long[] r, long[] a, boolean[] m, int vector_len) { int i = 0, j = 0, k = 0; try { for (; i < a.length; i += vector_len) {
k = 0; for (j = 0; j < vector_len; j++) { if (m[(i + j) % SPECIES.length()]) { Assert.assertEquals(r[i + j], a[i + k]);
k++;
} else { Assert.assertEquals(r[i + j], (long)0);
}
}
}
} catch (AssertionError e) { int idx = i + j; if (m[idx % SPECIES.length()]) { Assert.assertEquals(r[idx], a[i + k], "at index #" + idx);
} else { Assert.assertEquals(r[idx], (long)0, "at index #" + idx);
}
}
}
staticvoid assertSelectFromArraysEquals(long[] r, long[] a, long[] order, int vector_len) { int i = 0, j = 0; try { for (; i < a.length; i += vector_len) { for (j = 0; j < vector_len; j++) { Assert.assertEquals(r[i+j], a[i+(int)order[i+j]]);
}
}
} catch (AssertionError e) { int idx = i + j; Assert.assertEquals(r[i+j], a[i+(int)order[i+j]], "at index #" + idx + ", input = " + a[i+(int)order[i+j]]);
}
}
staticvoid assertRearrangeArraysEquals(long[] r, long[] a, int[] order, boolean[] mask, int vector_len) { int i = 0, j = 0; try { for (; i < a.length; i += vector_len) { for (j = 0; j < vector_len; j++) { if (mask[j % SPECIES.length()]) Assert.assertEquals(r[i+j], a[i+order[i+j]]); else Assert.assertEquals(r[i+j], (long)0);
}
}
} catch (AssertionError e) { int idx = i + j; if (mask[j % SPECIES.length()]) Assert.assertEquals(r[i+j], a[i+order[i+j]], "at index #" + idx + ", input = " + a[i+order[i+j]] + ", mask = " + mask[j % SPECIES.length()]); else Assert.assertEquals(r[i+j], (long)0, "at index #" + idx + ", input = " + a[i+order[i+j]] + ", mask = " + mask[j % SPECIES.length()]);
}
}
staticvoid assertSelectFromArraysEquals(long[] r, long[] a, long[] order, boolean[] mask, int vector_len) { int i = 0, j = 0; try { for (; i < a.length; i += vector_len) { for (j = 0; j < vector_len; j++) { if (mask[j % SPECIES.length()]) Assert.assertEquals(r[i+j], a[i+(int)order[i+j]]); else Assert.assertEquals(r[i+j], (long)0);
}
}
} catch (AssertionError e) { int idx = i + j; if (mask[j % SPECIES.length()]) Assert.assertEquals(r[i+j], a[i+(int)order[i+j]], "at index #" + idx + ", input = " + a[i+(int)order[i+j]] + ", mask = " + mask[j % SPECIES.length()]); else Assert.assertEquals(r[i+j], (long)0, "at index #" + idx + ", input = " + a[i+(int)order[i+j]] + ", mask = " + mask[j % SPECIES.length()]);
}
}
staticvoid assertBroadcastArraysEquals(long[] r, long[] a) { int i = 0; for (; i < a.length; i += SPECIES.length()) { int idx = i; for (int j = idx; j < (idx + SPECIES.length()); j++)
a[j]=a[idx];
}
try { for (i = 0; i < a.length; i++) { Assert.assertEquals(r[i], a[i]);
}
} catch (AssertionError e) { Assert.assertEquals(r[i], a[i], "at index #" + i + ", input = " + a[i]);
}
}
interface FBinOp { long apply(long a, long b);
}
interface FBinMaskOp { long apply(long a, long b, boolean m);
static FBinMaskOp lift(FBinOp f) { return (a, b, m) -> m ? f.apply(a, b) : a;
}
}
staticvoid assertArraysEquals(long[] r, long[] a, long[] b, FBinOp f) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(r[i], f.apply(a[i], b[i]));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a[i], b[i]), "(" + a[i] + ", " + b[i] + ") at index #" + i);
}
}
staticvoid assertBroadcastArraysEquals(long[] r, long[] a, long[] b, FBinOp f) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(r[i], f.apply(a[i], b[(i / SPECIES.length()) * SPECIES.length()]));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a[i], b[(i / SPECIES.length()) * SPECIES.length()]), "(" + a[i] + ", " + b[(i / SPECIES.length()) * SPECIES.length()] + ") at index #" + i);
}
}
staticvoid assertBroadcastLongArraysEquals(long[] r, long[] a, long[] b, FBinOp f) { int i = 0; try { for (; i < a.length; i++) { Assert.assertEquals(r[i], f.apply(a[i], (long)((long)b[(i / SPECIES.length()) * SPECIES.length()])));
}
} catch (AssertionError e) { Assert.assertEquals(r[i], f.apply(a[i], (long)((long)b[(i / SPECIES.length()) * SPECIES.length()])), "(" + a[i] + ", " + b[(i / SPECIES.length()) * SPECIES.length()] + ") at index #" + i);
}
}
staticvoid assertArraysEquals(long[] r, long[] a, long[] b, boolean[] mask, FBinOp f) {
assertArraysEquals(r, a, b, mask, FBinMaskOp.lift(f));
}
// Create combinations of pairs // @@@ Might be sensitive to order e.g. div by 0 staticfinal List<List<IntFunction<long[]>>> LONG_GENERATOR_PAIRS =
Stream.of(LONG_GENERATORS.get(0)).
flatMap(fa -> LONG_GENERATORS.stream().skip(1).map(fb -> List.of(fa, fb))).
collect(Collectors.toList());
@DataProvider public Object[][] boolUnaryOpProvider() { return BOOL_ARRAY_GENERATORS.stream().
map(f -> new Object[]{f}).
toArray(Object[][]::new);
}
@Test // Test all shuffle related operations. staticvoid shuffleTest() { // To test backend instructions, make sure that C2 is used. for (int loop = 0; loop < INVOC_COUNT * INVOC_COUNT; loop++) {
iotaShuffle();
}
}
@Test // Test div by 0. staticvoid bitwiseDivByZeroSmokeTest() { try {
LongVector a = (LongVector) SPECIES.broadcast(0).addIndex(1);
LongVector b = (LongVector) SPECIES.broadcast(0);
a.div(b); Assert.fail();
} catch (ArithmeticException e) {
}
try {
LongVector a = (LongVector) SPECIES.broadcast(0).addIndex(1);
LongVector b = (LongVector) SPECIES.broadcast(0);
VectorMask<Long> m = a.lt((long) 1);
a.div(b, m); Assert.fail();
} catch (ArithmeticException e) {
}
}
staticlong ADD(long a, long b) { return (long)(a + b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid ADDLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.ADD, bv).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, Long64VectorTests::ADD);
}
staticlong add(long a, long b) { return (long)(a + b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid addLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.add(bv).intoArray(r, i);
}
assertArraysEquals(r, a, b, Long64VectorTests::add);
}
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.ADD, bv, vmask).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::ADD);
}
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.add(bv, vmask).intoArray(r, i);
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::add);
}
staticlong SUB(long a, long b) { return (long)(a - b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid SUBLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.SUB, bv).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, Long64VectorTests::SUB);
}
staticlong sub(long a, long b) { return (long)(a - b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid subLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.sub(bv).intoArray(r, i);
}
assertArraysEquals(r, a, b, Long64VectorTests::sub);
}
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.SUB, bv, vmask).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::SUB);
}
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.sub(bv, vmask).intoArray(r, i);
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::sub);
}
staticlong MUL(long a, long b) { return (long)(a * b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid MULLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.MUL, bv).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, Long64VectorTests::MUL);
}
staticlong mul(long a, long b) { return (long)(a * b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid mulLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.mul(bv).intoArray(r, i);
}
assertArraysEquals(r, a, b, Long64VectorTests::mul);
}
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.MUL, bv, vmask).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::MUL);
}
for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.mul(bv, vmask).intoArray(r, i);
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::mul);
}
staticlong DIV(long a, long b) { return (long)(a / b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid DIVLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
replaceZero(b, (long) 1);
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.DIV, bv).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, Long64VectorTests::DIV);
}
staticlong div(long a, long b) { return (long)(a / b);
}
@Test(dataProvider = "longBinaryOpProvider") staticvoid divLong64VectorTests(IntFunction<long[]> fa, IntFunction<long[]> fb) { long[] a = fa.apply(SPECIES.length()); long[] b = fb.apply(SPECIES.length()); long[] r = fr.apply(SPECIES.length());
replaceZero(b, (long) 1);
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.div(bv).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, Long64VectorTests::div);
}
for (int ic = 0; ic < INVOC_COUNT; ic++) { for (int i = 0; i < a.length; i += SPECIES.length()) {
LongVector av = LongVector.fromArray(SPECIES, a, i);
LongVector bv = LongVector.fromArray(SPECIES, b, i);
av.lanewise(VectorOperators.DIV, bv, vmask).intoArray(r, i);
}
}
assertArraysEquals(r, a, b, mask, Long64VectorTests::DIV);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.