diff --git a/test/hotspot/jtreg/compiler/vectorization/TestVectorAlgorithms.java b/test/hotspot/jtreg/compiler/vectorization/TestVectorAlgorithms.java index e2ac1f7a9c2..57b91eb29c4 100644 --- a/test/hotspot/jtreg/compiler/vectorization/TestVectorAlgorithms.java +++ b/test/hotspot/jtreg/compiler/vectorization/TestVectorAlgorithms.java @@ -125,6 +125,10 @@ public class TestVectorAlgorithms { testGroups.put("reverseI", new HashMap()); 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()); + 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); + } } diff --git a/test/hotspot/jtreg/compiler/vectorization/VectorAlgorithmsImpl.java b/test/hotspot/jtreg/compiler/vectorization/VectorAlgorithmsImpl.java index 65256934d26..0b818ee69fa 100644 --- a/test/hotspot/jtreg/compiler/vectorization/VectorAlgorithmsImpl.java +++ b/test/hotspot/jtreg/compiler/vectorization/VectorAlgorithmsImpl.java @@ -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; + } } diff --git a/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithms.java b/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithms.java index d524c74b1ad..d15ce22deb9 100644 --- a/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithms.java +++ b/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithms.java @@ -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); + } + } diff --git a/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithmsImpl.java b/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithmsImpl.java index 95a96aabe2b..065a6522d87 100644 --- a/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithmsImpl.java +++ b/test/micro/org/openjdk/bench/vm/compiler/VectorAlgorithmsImpl.java @@ -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; + } }