This commit is contained in:
Emanuel Peter 2025-12-10 10:55:52 +01:00
parent bb4b177b77
commit 5e3b880066
4 changed files with 108 additions and 1 deletions

View File

@ -125,6 +125,10 @@ public class TestVectorAlgorithms {
testGroups.put("reverseI", new HashMap<String,TestFunction>());
testGroups.get("reverseI").put("reverseI_loop", () -> { return reverseI_loop(aI, rI1); });
testGroups.get("reverseI").put("reverseI_VectorAPI", () -> { return reverseI_VectorAPI(aI, rI2); });
testGroups.put("filterI", new HashMap<String,TestFunction>());
testGroups.get("filterI").put("filterI_loop", () -> { return filterI_loop(aI, rI1, eI); });
testGroups.get("filterI").put("filterI_VectorAPI", () -> { return filterI_VectorAPI(aI, rI2, eI); });
}
@Warmup(100)
@ -150,7 +154,9 @@ public class TestVectorAlgorithms {
"findI_loop",
"findI_VectorAPI",
"reverseI_loop",
"reverseI_VectorAPI"})
"reverseI_VectorAPI",
"filterI_loop",
"filterI_VectorAPI"})
public void runTests(RunInfo info) {
// Repeat many times, so that we also have multiple iterations for post-warmup to potentially recompile
int iters = info.isWarmUp() ? 1 : 20;
@ -325,4 +331,14 @@ public class TestVectorAlgorithms {
public Object reverseI_VectorAPI(int[] a, int[] r) {
return VectorAlgorithmsImpl.reverseI_VectorAPI(a, r);
}
@Test
public Object filterI_loop(int[] a, int[] r, int threshold) {
return VectorAlgorithmsImpl.filterI_loop(a, r, threshold);
}
@Test
public Object filterI_VectorAPI(int[] a, int[] r, int threshold) {
return VectorAlgorithmsImpl.filterI_VectorAPI(a, r, threshold);
}
}

View File

@ -335,4 +335,42 @@ public class VectorAlgorithmsImpl {
}
return r;
}
public static Object filterI_loop(int[] a, int[] r, int threshold) {
int j = 0;
for (int i = 0; i < a.length; i++) {
int ai = a[i];
if (ai >= threshold) {
r[j++] = ai;
}
}
// Just force the resulting length onto the same array.
r[r.length - 1] = j;
return r;
}
public static Object filterI_VectorAPI(int[] a, int[] r, int threshold) {
var thresholds = IntVector.broadcast(SPECIES_I, threshold);
int j = 0;
int i = 0;
for (; i < SPECIES_I.loopBound(a.length); i += SPECIES_I.length()) {
IntVector v = IntVector.fromArray(SPECIES_I, a, i);
var mask = v.compare(VectorOperators.GE, thresholds);
v = v.compress(mask);
int trueCount = mask.trueCount();
var prefixMask = VectorMask.fromLong(SPECIES_I, (1L << trueCount) - 1);
v.intoArray(r, j, prefixMask);
j += trueCount;
}
for (; i < a.length; i++) {
int ai = a[i];
if (ai >= threshold) {
r[j++] = ai;
}
}
// Just force the resulting length onto the same array.
r[r.length - 1] = j;
return r;
}
}

View File

@ -202,4 +202,19 @@ public class VectorAlgorithms {
public Object reverseI_VectorAPI() {
return VectorAlgorithmsImpl.reverseI_VectorAPI(aI, rI);
}
@Benchmark
public Object filterI_loop() {
idx = (idx + 1) & 0xffff;
int e = aI[idx];
return VectorAlgorithmsImpl.filterI_loop(aI, rI, e);
}
@Benchmark
public Object filterI_VectorAPI() {
idx = (idx + 1) & 0xffff;
int e = aI[idx];
return VectorAlgorithmsImpl.filterI_VectorAPI(aI, rI, e);
}
}

View File

@ -335,4 +335,42 @@ public class VectorAlgorithmsImpl {
}
return r;
}
public static Object filterI_loop(int[] a, int[] r, int threshold) {
int j = 0;
for (int i = 0; i < a.length; i++) {
int ai = a[i];
if (ai >= threshold) {
r[j++] = ai;
}
}
// Just force the resulting length onto the same array.
r[r.length - 1] = j;
return r;
}
public static Object filterI_VectorAPI(int[] a, int[] r, int threshold) {
var thresholds = IntVector.broadcast(SPECIES_I, threshold);
int j = 0;
int i = 0;
for (; i < SPECIES_I.loopBound(a.length); i += SPECIES_I.length()) {
IntVector v = IntVector.fromArray(SPECIES_I, a, i);
var mask = v.compare(VectorOperators.GE, thresholds);
v = v.compress(mask);
int trueCount = mask.trueCount();
var prefixMask = VectorMask.fromLong(SPECIES_I, (1L << trueCount) - 1);
v.intoArray(r, j, prefixMask);
j += trueCount;
}
for (; i < a.length; i++) {
int ai = a[i];
if (ai >= threshold) {
r[j++] = ai;
}
}
// Just force the resulting length onto the same array.
r[r.length - 1] = j;
return r;
}
}