From d314ac6da294f7a117ccdbca8bd6c596c230335a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Galder=20Zamarre=C3=B1o?= Date: Thu, 24 Jul 2025 15:37:54 +0100 Subject: [PATCH 2/2] Branch instead of CMov: MinMaxVector benchmark changes --- .../openjdk/bench/java/lang/MinMaxVector.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/test/micro/org/openjdk/bench/java/lang/MinMaxVector.java b/test/micro/org/openjdk/bench/java/lang/MinMaxVector.java index fa42d0be834..86d9cd2493c 100644 --- a/test/micro/org/openjdk/bench/java/lang/MinMaxVector.java +++ b/test/micro/org/openjdk/bench/java/lang/MinMaxVector.java @@ -35,13 +35,21 @@ public static class LoopState { /** * Probability of one of the min/max branches being taken. * For max, this value represents the percentage of branches in which - * the value will be bigger or equal than the current max. + * the value will be bigger (or equal if includeEquals is enabled) than the current max. * For min, this value represents the percentage of branches in which - * the value will be smaller or equal than the current min. + * the value will be smaller (or equal if includeEquals is enabled) than the current min. */ @Param({"50", "80", "100"}) int probability; + /** + * If false, values in the min/max branches are strictly bigger/smaller than + * the current min/max values. + * If true, values in the arrays branches could be equals to the current min/max values. + */ + @Param({"true", "false"}) + boolean includeEquals; + int[] minIntA; int[] minIntB; long[] minLongA; @@ -55,7 +63,7 @@ public static class LoopState { @Setup public void setup() { - final long[][] longs = distributeLongRandomIncrement(size, probability); + final long[][] longs = distributeLongRandomIncrement(size, probability, includeEquals); maxLongA = longs[0]; maxLongB = longs[1]; maxIntA = toInts(maxLongA); @@ -76,9 +84,11 @@ static int[] toInts(long[] nums) { return Arrays.stream(nums).mapToInt(i -> (int) i).toArray(); } - static long[][] distributeLongRandomIncrement(int size, int probability) { + static long[][] distributeLongRandomIncrement(int size, int probability, boolean includeEquals) { + final long aboveOrEqualsLowerBound = includeEquals ? 0 : 1; + long[][] result; - int aboveCount, abovePercent; + int aboveOrEqualsCount, aboveOrEqualsPercent; // This algorithm generates 2 arrays of numbers. // The first array is created such that as the array is iterated, @@ -95,16 +105,22 @@ static long[][] distributeLongRandomIncrement(int size, int probability) { result[0][0] = max; result[1][0] = max - 1; - aboveCount = 0; + // Assume that the first value is above or equals to the current max + aboveOrEqualsCount = 1; for (int i = 1; i < result[0].length; i++) { long value; - if (ThreadLocalRandom.current().nextLong(101) <= probability) { - long increment = ThreadLocalRandom.current().nextLong(10); + if (i == 1 && includeEquals) { + // If include equals make sure there is at least one element that is equals to the current min/max. + // So, in that case make the 2nd element in the array the same as the current min/max. + value = max; + aboveOrEqualsCount++; + } else if (ThreadLocalRandom.current().nextLong(101) <= probability) { + long increment = ThreadLocalRandom.current().nextLong(aboveOrEqualsLowerBound, 10); value = max + increment; - aboveCount++; + aboveOrEqualsCount++; } else { - // Decrement by at least 1 - long diffToMax = ThreadLocalRandom.current().nextLong(10) + 1; + // To generate a value strictly lower value, it must generate at least 1 element lower + long diffToMax = ThreadLocalRandom.current().nextLong(1, 10); value = max - diffToMax; } result[0][i] = value; @@ -112,8 +128,8 @@ static long[][] distributeLongRandomIncrement(int size, int probability) { max = Math.max(max, value); } - abovePercent = ((aboveCount + 1) * 100) / size; - } while (abovePercent != probability); + aboveOrEqualsPercent = (aboveOrEqualsCount * 100) / size; + } while (aboveOrEqualsPercent != probability); return result; } @@ -199,7 +215,7 @@ public int intReductionMultiplyMin(LoopState state) { int result = 0; for (int i = 0; i < state.size; i++) { final int v = 11 * state.minIntA[i]; - result = Math.min(result, v); + result = Math.min(v, result); } return result; } @@ -209,7 +225,7 @@ public int intReductionSimpleMin(LoopState state) { int result = 0; for (int i = 0; i < state.size; i++) { final int v = state.minIntA[i]; - result = Math.min(result, v); + result = Math.min(v, result); } return result; } @@ -219,7 +235,7 @@ public int intReductionMultiplyMax(LoopState state) { int result = 0; for (int i = 0; i < state.size; i++) { final int v = 11 * state.maxIntA[i]; - result = Math.max(result, v); + result = Math.max(v, result); } return result; } @@ -229,7 +245,7 @@ public int intReductionSimpleMax(LoopState state) { int result = 0; for (int i = 0; i < state.size; i++) { final int v = state.maxIntA[i]; - result = Math.max(result, v); + result = Math.max(v, result); } return result; } @@ -263,7 +279,7 @@ public long longReductionMultiplyMin(LoopState state) { long result = 0; for (int i = 0; i < state.size; i++) { final long v = 11 * state.minLongA[i]; - result = Math.min(result, v); + result = Math.min(v, result); } return result; } @@ -273,7 +289,7 @@ public long longReductionSimpleMin(LoopState state) { long result = 0; for (int i = 0; i < state.size; i++) { final long v = state.minLongA[i]; - result = Math.min(result, v); + result = Math.min(v, result); } return result; } @@ -283,7 +299,8 @@ public long longReductionMultiplyMax(LoopState state) { long result = 0; for (int i = 0; i < state.size; i++) { final long v = 11 * state.maxLongA[i]; - result = Math.max(result, v); + // System.out.println("Math.max(v: " + v + ", result: " + result + ")"); + result = Math.max(v, result); } return result; } @@ -293,7 +310,7 @@ public long longReductionSimpleMax(LoopState state) { long result = 0; for (int i = 0; i < state.size; i++) { final long v = state.maxLongA[i]; - result = Math.max(result, v); + result = Math.max(v, result); } return result; } -- 2.49.0