Skip to content

Commit 26d8739

Browse files
8371446: VectorAPI: Add unit tests for masks from various long values
1 parent e5da752 commit 26d8739

33 files changed

Lines changed: 3854 additions & 26 deletions

test/jdk/jdk/incubator/vector/AbstractVectorTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ static int[] fillInts(int[] a, IntOp f) {
122122
Arrays.fill(a, true);
123123
return a;
124124
}),
125-
withToString("mask[false]", boolean[]::new)
125+
withToString("mask[false]", boolean[]::new),
126+
withToString("mask[random]", (int l) -> {
127+
boolean[] a = new boolean[l];
128+
for (int i = 0; i < l; i++) {
129+
a[i] = RAND.nextBoolean();
130+
}
131+
return a;
132+
})
126133
);
127134

128135
static final List<List<IntFunction<boolean[]>>>
@@ -131,6 +138,45 @@ static int[] fillInts(int[] a, IntOp f) {
131138
flatMap(fa -> BOOLEAN_MASK_GENERATORS.stream().skip(1).map(
132139
fb -> List.of(fa, fb))).collect(Collectors.toList());
133140

141+
static long pack_booleans_to_long(boolean[] mask) {
142+
long bits = 0L;
143+
// pack up to 64 bits/lane indices, least significant bit is lane 0
144+
int bound = Math.min(mask.length, 64);
145+
for (int i = 0; i < bound; i++) {
146+
if (mask[i]) {
147+
bits |= 1L << i;
148+
}
149+
}
150+
return bits;
151+
}
152+
153+
static final List<IntFunction<Long>> LONG_MASK_GENERATORS = List.of(
154+
withToString("mask[random]", (int l) -> {
155+
boolean[] a = new boolean[l];
156+
for (int i = 0; i < l; i++) {
157+
a[i] = RAND.nextBoolean();
158+
}
159+
return pack_booleans_to_long(a);
160+
}),
161+
withToString("mask[i % 2]", (int l) -> {
162+
boolean[] a = new boolean[l];
163+
for (int i = 0; i < l; i++) {
164+
a[i] = (i % 2 == 0);
165+
}
166+
return pack_booleans_to_long(a);
167+
}),
168+
withToString("mask[true]", (int l) -> {
169+
boolean[] a = new boolean[l];
170+
Arrays.fill(a, true);
171+
return pack_booleans_to_long(a);
172+
}),
173+
withToString("mask[false]", Long::new),
174+
withToString("mask[0xFFFFFFFFFFFFFFFFL]", (_l) -> 0xFFFFFFFFFFFFFFFFL), // true
175+
withToString("mask[0x0000000000000000L]", (_l) -> 0x0000000000000000L), // false
176+
withToString("mask[0x5555555555555555L]", (_l) -> 0x5555555555555555L),
177+
withToString("mask[0x0123456789abcdefL]", (_l) -> 0x0123456789abcdefL)
178+
);
179+
134180
static final List<BiFunction<Integer,Integer,int[]>> INT_SHUFFLE_GENERATORS = List.of(
135181
withToStringBi("shuffle[random]",
136182
(Integer l, Integer m) -> RAND.ints(l, 0, m).toArray())

test/jdk/jdk/incubator/vector/Byte128VectorTests.java

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,13 @@ public Object[][] maskProvider() {
12311231
toArray(Object[][]::new);
12321232
}
12331233

1234+
@DataProvider
1235+
public Object[][] maskLongProvider() {
1236+
return LONG_MASK_GENERATORS.stream().
1237+
map(f -> new Object[]{f}).
1238+
toArray(Object[][]::new);
1239+
}
1240+
12341241
@DataProvider
12351242
public Object[][] maskCompareOpProvider() {
12361243
return BOOLEAN_MASK_COMPARE_GENERATOR_PAIRS.stream().map(List::toArray).
@@ -6809,6 +6816,107 @@ static void maskEqByte128VectorTestsSmokeTest(IntFunction<boolean[]> fa, IntFunc
68096816
assertArraysEquals(r, a, b, Byte128VectorTests::beq);
68106817
}
68116818

6819+
@Test(dataProvider = "maskCompareOpProvider")
6820+
static void maskEqualsByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6821+
boolean[] a = fa.apply(SPECIES.length());
6822+
boolean[] b = fb.apply(SPECIES.length());
6823+
6824+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6825+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6826+
var av = SPECIES.loadMask(a, i);
6827+
var bv = SPECIES.loadMask(b, i);
6828+
boolean equals = av.equals(bv);
6829+
int to = i + SPECIES.length();
6830+
Assert.assertEquals(equals, Arrays.equals(a, i, to, b, i, to));
6831+
}
6832+
}
6833+
}
6834+
6835+
@Test(dataProvider = "maskCompareOpProvider")
6836+
static void maskAndByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6837+
boolean[] a = fa.apply(SPECIES.length());
6838+
boolean[] b = fb.apply(SPECIES.length());
6839+
boolean[] r = new boolean[a.length];
6840+
6841+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6842+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6843+
var av = SPECIES.loadMask(a, i);
6844+
var bv = SPECIES.loadMask(b, i);
6845+
var cv = av.and(bv);
6846+
cv.intoArray(r, i);
6847+
}
6848+
}
6849+
assertArraysEquals(r, a, b, Byte128VectorTests::band);
6850+
}
6851+
6852+
@Test(dataProvider = "maskCompareOpProvider")
6853+
static void maskOrByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6854+
boolean[] a = fa.apply(SPECIES.length());
6855+
boolean[] b = fb.apply(SPECIES.length());
6856+
boolean[] r = new boolean[a.length];
6857+
6858+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6859+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6860+
var av = SPECIES.loadMask(a, i);
6861+
var bv = SPECIES.loadMask(b, i);
6862+
var cv = av.or(bv);
6863+
cv.intoArray(r, i);
6864+
}
6865+
}
6866+
assertArraysEquals(r, a, b, Byte128VectorTests::bor);
6867+
}
6868+
6869+
@Test(dataProvider = "maskCompareOpProvider")
6870+
static void maskXorByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6871+
boolean[] a = fa.apply(SPECIES.length());
6872+
boolean[] b = fb.apply(SPECIES.length());
6873+
boolean[] r = new boolean[a.length];
6874+
6875+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6876+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6877+
var av = SPECIES.loadMask(a, i);
6878+
var bv = SPECIES.loadMask(b, i);
6879+
var cv = av.xor(bv);
6880+
cv.intoArray(r, i);
6881+
}
6882+
}
6883+
assertArraysEquals(r, a, b, Byte128VectorTests::bxor);
6884+
}
6885+
6886+
@Test(dataProvider = "maskCompareOpProvider")
6887+
static void maskAndNotByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6888+
boolean[] a = fa.apply(SPECIES.length());
6889+
boolean[] b = fb.apply(SPECIES.length());
6890+
boolean[] r = new boolean[a.length];
6891+
6892+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6893+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6894+
var av = SPECIES.loadMask(a, i);
6895+
var bv = SPECIES.loadMask(b, i);
6896+
var cv = av.andNot(bv);
6897+
cv.intoArray(r, i);
6898+
}
6899+
}
6900+
assertArraysEquals(r, a, b, Byte128VectorTests::bandNot);
6901+
}
6902+
6903+
@Test(dataProvider = "maskCompareOpProvider")
6904+
static void maskEqByte128VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6905+
boolean[] a = fa.apply(SPECIES.length());
6906+
boolean[] b = fb.apply(SPECIES.length());
6907+
boolean[] r = new boolean[a.length];
6908+
6909+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6910+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6911+
var av = SPECIES.loadMask(a, i);
6912+
var bv = SPECIES.loadMask(b, i);
6913+
var cv = av.eq(bv);
6914+
cv.intoArray(r, i);
6915+
}
6916+
}
6917+
assertArraysEquals(r, a, b, Byte128VectorTests::beq);
6918+
}
6919+
68126920
@Test(dataProvider = "maskProvider")
68136921
static void maskHashCodeByte128VectorTestsSmokeTest(IntFunction<boolean[]> fa) {
68146922
boolean[] a = fa.apply(SPECIES.length());
@@ -6913,6 +7021,20 @@ static void maskCompressByte128VectorTestsSmokeTest(IntFunction<boolean[]> fa) {
69137021
}
69147022
}
69157023

7024+
private static final long LONG_MASK_BITS = 0xFFFFFFFFFFFFFFFFL >>> (64 - SPECIES.length());
7025+
7026+
@Test(dataProvider = "maskLongProvider")
7027+
static void maskFromToLongByte128VectorTests(IntFunction<Long> fa) {
7028+
long a = fa.apply(SPECIES.length());
7029+
long r = -1;
7030+
7031+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
7032+
var vmask = VectorMask.fromLong(SPECIES, a);
7033+
r = vmask.toLong();
7034+
}
7035+
Assert.assertEquals(r, (a & LONG_MASK_BITS));
7036+
}
7037+
69167038
@DataProvider
69177039
public static Object[][] longMaskProvider() {
69187040
return new Object[][]{
@@ -6927,7 +7049,7 @@ public static Object[][] longMaskProvider() {
69277049
static void maskFromToLongByte128VectorTestsSmokeTest(long inputLong) {
69287050
var vmask = VectorMask.fromLong(SPECIES, inputLong);
69297051
long outputLong = vmask.toLong();
6930-
Assert.assertEquals(outputLong, (inputLong & (((0xFFFFFFFFFFFFFFFFL >>> (64 - SPECIES.length()))))));
7052+
Assert.assertEquals(outputLong, (inputLong & LONG_MASK_BITS));
69317053
}
69327054

69337055
@DataProvider

test/jdk/jdk/incubator/vector/Byte256VectorTests.java

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,13 @@ public Object[][] maskProvider() {
12311231
toArray(Object[][]::new);
12321232
}
12331233

1234+
@DataProvider
1235+
public Object[][] maskLongProvider() {
1236+
return LONG_MASK_GENERATORS.stream().
1237+
map(f -> new Object[]{f}).
1238+
toArray(Object[][]::new);
1239+
}
1240+
12341241
@DataProvider
12351242
public Object[][] maskCompareOpProvider() {
12361243
return BOOLEAN_MASK_COMPARE_GENERATOR_PAIRS.stream().map(List::toArray).
@@ -6809,6 +6816,107 @@ static void maskEqByte256VectorTestsSmokeTest(IntFunction<boolean[]> fa, IntFunc
68096816
assertArraysEquals(r, a, b, Byte256VectorTests::beq);
68106817
}
68116818

6819+
@Test(dataProvider = "maskCompareOpProvider")
6820+
static void maskEqualsByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6821+
boolean[] a = fa.apply(SPECIES.length());
6822+
boolean[] b = fb.apply(SPECIES.length());
6823+
6824+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6825+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6826+
var av = SPECIES.loadMask(a, i);
6827+
var bv = SPECIES.loadMask(b, i);
6828+
boolean equals = av.equals(bv);
6829+
int to = i + SPECIES.length();
6830+
Assert.assertEquals(equals, Arrays.equals(a, i, to, b, i, to));
6831+
}
6832+
}
6833+
}
6834+
6835+
@Test(dataProvider = "maskCompareOpProvider")
6836+
static void maskAndByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6837+
boolean[] a = fa.apply(SPECIES.length());
6838+
boolean[] b = fb.apply(SPECIES.length());
6839+
boolean[] r = new boolean[a.length];
6840+
6841+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6842+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6843+
var av = SPECIES.loadMask(a, i);
6844+
var bv = SPECIES.loadMask(b, i);
6845+
var cv = av.and(bv);
6846+
cv.intoArray(r, i);
6847+
}
6848+
}
6849+
assertArraysEquals(r, a, b, Byte256VectorTests::band);
6850+
}
6851+
6852+
@Test(dataProvider = "maskCompareOpProvider")
6853+
static void maskOrByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6854+
boolean[] a = fa.apply(SPECIES.length());
6855+
boolean[] b = fb.apply(SPECIES.length());
6856+
boolean[] r = new boolean[a.length];
6857+
6858+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6859+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6860+
var av = SPECIES.loadMask(a, i);
6861+
var bv = SPECIES.loadMask(b, i);
6862+
var cv = av.or(bv);
6863+
cv.intoArray(r, i);
6864+
}
6865+
}
6866+
assertArraysEquals(r, a, b, Byte256VectorTests::bor);
6867+
}
6868+
6869+
@Test(dataProvider = "maskCompareOpProvider")
6870+
static void maskXorByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6871+
boolean[] a = fa.apply(SPECIES.length());
6872+
boolean[] b = fb.apply(SPECIES.length());
6873+
boolean[] r = new boolean[a.length];
6874+
6875+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6876+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6877+
var av = SPECIES.loadMask(a, i);
6878+
var bv = SPECIES.loadMask(b, i);
6879+
var cv = av.xor(bv);
6880+
cv.intoArray(r, i);
6881+
}
6882+
}
6883+
assertArraysEquals(r, a, b, Byte256VectorTests::bxor);
6884+
}
6885+
6886+
@Test(dataProvider = "maskCompareOpProvider")
6887+
static void maskAndNotByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6888+
boolean[] a = fa.apply(SPECIES.length());
6889+
boolean[] b = fb.apply(SPECIES.length());
6890+
boolean[] r = new boolean[a.length];
6891+
6892+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6893+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6894+
var av = SPECIES.loadMask(a, i);
6895+
var bv = SPECIES.loadMask(b, i);
6896+
var cv = av.andNot(bv);
6897+
cv.intoArray(r, i);
6898+
}
6899+
}
6900+
assertArraysEquals(r, a, b, Byte256VectorTests::bandNot);
6901+
}
6902+
6903+
@Test(dataProvider = "maskCompareOpProvider")
6904+
static void maskEqByte256VectorTests(IntFunction<boolean[]> fa, IntFunction<boolean[]> fb) {
6905+
boolean[] a = fa.apply(SPECIES.length());
6906+
boolean[] b = fb.apply(SPECIES.length());
6907+
boolean[] r = new boolean[a.length];
6908+
6909+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
6910+
for (int i = 0; i < a.length; i += SPECIES.length()) {
6911+
var av = SPECIES.loadMask(a, i);
6912+
var bv = SPECIES.loadMask(b, i);
6913+
var cv = av.eq(bv);
6914+
cv.intoArray(r, i);
6915+
}
6916+
}
6917+
assertArraysEquals(r, a, b, Byte256VectorTests::beq);
6918+
}
6919+
68126920
@Test(dataProvider = "maskProvider")
68136921
static void maskHashCodeByte256VectorTestsSmokeTest(IntFunction<boolean[]> fa) {
68146922
boolean[] a = fa.apply(SPECIES.length());
@@ -6913,6 +7021,20 @@ static void maskCompressByte256VectorTestsSmokeTest(IntFunction<boolean[]> fa) {
69137021
}
69147022
}
69157023

7024+
private static final long LONG_MASK_BITS = 0xFFFFFFFFFFFFFFFFL >>> (64 - SPECIES.length());
7025+
7026+
@Test(dataProvider = "maskLongProvider")
7027+
static void maskFromToLongByte256VectorTests(IntFunction<Long> fa) {
7028+
long a = fa.apply(SPECIES.length());
7029+
long r = -1;
7030+
7031+
for (int ic = 0; ic < INVOC_COUNT * INVOC_COUNT; ic++) {
7032+
var vmask = VectorMask.fromLong(SPECIES, a);
7033+
r = vmask.toLong();
7034+
}
7035+
Assert.assertEquals(r, (a & LONG_MASK_BITS));
7036+
}
7037+
69167038
@DataProvider
69177039
public static Object[][] longMaskProvider() {
69187040
return new Object[][]{
@@ -6927,7 +7049,7 @@ public static Object[][] longMaskProvider() {
69277049
static void maskFromToLongByte256VectorTestsSmokeTest(long inputLong) {
69287050
var vmask = VectorMask.fromLong(SPECIES, inputLong);
69297051
long outputLong = vmask.toLong();
6930-
Assert.assertEquals(outputLong, (inputLong & (((0xFFFFFFFFFFFFFFFFL >>> (64 - SPECIES.length()))))));
7052+
Assert.assertEquals(outputLong, (inputLong & LONG_MASK_BITS));
69317053
}
69327054

69337055
@DataProvider

0 commit comments

Comments
 (0)