jdk/test/hotspot/jtreg/compiler/c2/TestMultiplyHighLowFusion.java
2026-03-13 19:36:19 +11:00

136 lines
4.6 KiB
Java

/*
* Copyright (c) 2026, 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.
*/
/*
* @test
* @bug 8379327
* @summary Verify correctness for combined low/high 64-bit multiplication patterns.
* @library /test/lib /
* @run driver compiler.c2.TestMultiplyHighLowFusion
*/
package compiler.c2;
import compiler.lib.ir_framework.*;
import java.math.BigInteger;
public class TestMultiplyHighLowFusion {
private static final BigInteger MASK_64 = BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE);
private static final long[] CORNER_CASES = {
0L,
1L,
-1L,
Long.MIN_VALUE,
Long.MAX_VALUE,
0x00000000FFFFFFFFL,
0xFFFFFFFF00000000L,
0x123456789ABCDEFL,
0xFEDCBA9876543210L
};
private static final java.util.Random RANDOM = new java.util.Random(0x8379327L);
private int warmupCount = 0;
public static void main(String[] args) {
TestFramework.run();
}
@Test
@IR(applyIfPlatform = {"x64", "true"}, phase = CompilePhase.PRINT_IDEAL, counts = {"\\bMulHiLoL\\b", "1"})
public static long doMath(long a, long b) {
long low = a * b;
long high = Math.multiplyHigh(a, b);
return low + high;
}
@Test
@IR(applyIfPlatform = {"x64", "true"}, phase = CompilePhase.PRINT_IDEAL, counts = {"\\bMulHiLoL\\b", "1"})
public static long doMathSwapped(long a, long b) {
long low = b * a;
long high = Math.multiplyHigh(b, a);
return low + high;
}
@Test
@IR(applyIfPlatform = {"x64", "true"}, phase = CompilePhase.PRINT_IDEAL, counts = {"\\bUMulHiLoL\\b", "1"})
public static long doUnsignedMath(long a, long b) {
long low = a * b;
long high = Math.unsignedMultiplyHigh(a, b);
return low + high;
}
@Test
@IR(applyIfPlatform = {"x64", "true"}, phase = CompilePhase.PRINT_IDEAL, counts = {"\\bUMulHiLoL\\b", "1"})
public static long doUnsignedMathSwapped(long a, long b) {
long low = b * a;
long high = Math.unsignedMultiplyHigh(b, a);
return low + high;
}
@Run(test = {"doMath", "doMathSwapped", "doUnsignedMath", "doUnsignedMathSwapped"})
public void runTests() {
if (warmupCount < CORNER_CASES.length * CORNER_CASES.length) {
int aIdx = warmupCount / CORNER_CASES.length;
int bIdx = warmupCount % CORNER_CASES.length;
verifyPair(CORNER_CASES[aIdx], CORNER_CASES[bIdx]);
warmupCount++;
} else {
verifyPair(RANDOM.nextLong(), RANDOM.nextLong());
}
}
private void verifyPair(long a, long b) {
long expectedSigned = expectedSigned(a, b);
long expectedUnsigned = expectedUnsigned(a, b);
if (doMath(a, b) != expectedSigned) {
throw new RuntimeException("Signed mismatch for a=" + a + ", b=" + b);
}
if (doMathSwapped(a, b) != expectedSigned) {
throw new RuntimeException("Signed swapped mismatch for a=" + a + ", b=" + b);
}
if (doUnsignedMath(a, b) != expectedUnsigned) {
throw new RuntimeException("Unsigned mismatch for a=" + a + ", b=" + b);
}
if (doUnsignedMathSwapped(a, b) != expectedUnsigned) {
throw new RuntimeException("Unsigned swapped mismatch for a=" + a + ", b=" + b);
}
}
private static long expectedSigned(long a, long b) {
BigInteger product = BigInteger.valueOf(a).multiply(BigInteger.valueOf(b));
long low = product.longValue();
long high = product.shiftRight(64).longValue();
return low + high;
}
private static long expectedUnsigned(long a, long b) {
BigInteger ua = BigInteger.valueOf(a).and(MASK_64);
BigInteger ub = BigInteger.valueOf(b).and(MASK_64);
BigInteger product = ua.multiply(ub);
long low = product.longValue();
long high = product.shiftRight(64).longValue();
return low + high;
}
}