8188044: We need Math.unsignedMultiplyHigh
Reviewed-by: rriggs, aph, darcy
This commit is contained in:
parent
3d84398d12
commit
ca4bea4665
src/java.base/share/classes/java/lang
test/jdk/java/lang/Math
@ -1156,6 +1156,7 @@ public final class Math {
|
||||
* @param x the first value
|
||||
* @param y the second value
|
||||
* @return the result
|
||||
* @see #unsignedMultiplyHigh
|
||||
* @since 9
|
||||
*/
|
||||
@IntrinsicCandidate
|
||||
@ -1187,6 +1188,24 @@ public final class Math {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns as a {@code long} the most significant 64 bits of the unsigned
|
||||
* 128-bit product of two unsigned 64-bit factors.
|
||||
*
|
||||
* @param x the first value
|
||||
* @param y the second value
|
||||
* @return the result
|
||||
* @see #multiplyHigh
|
||||
* @since 18
|
||||
*/
|
||||
public static long unsignedMultiplyHigh(long x, long y) {
|
||||
// Compute via multiplyHigh() to leverage the intrinsic
|
||||
long result = Math.multiplyHigh(x, y);
|
||||
result += (y & (x >> 63)); // equivalent to `if (x < 0) result += y;`
|
||||
result += (x & (y >> 63)); // equivalent to `if (y < 0) result += x;`
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the largest (closest to positive infinity)
|
||||
* {@code int} value that is less than or equal to the algebraic quotient.
|
||||
|
@ -982,6 +982,7 @@ public final class StrictMath {
|
||||
* @param x the first value
|
||||
* @param y the second value
|
||||
* @return the result
|
||||
* @see #unsignedMultiplyHigh
|
||||
* @see Math#multiplyHigh(long,long)
|
||||
* @since 9
|
||||
*/
|
||||
@ -989,6 +990,21 @@ public final class StrictMath {
|
||||
return Math.multiplyHigh(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns as a {@code long} the most significant 64 bits of the unsigned
|
||||
* 128-bit product of two unsigned 64-bit factors.
|
||||
*
|
||||
* @param x the first value
|
||||
* @param y the second value
|
||||
* @return the result
|
||||
* @see #multiplyHigh
|
||||
* @see Math#unsignedMultiplyHigh(long,long)
|
||||
* @since 18
|
||||
*/
|
||||
public static long unsignedMultiplyHigh(long x, long y) {
|
||||
return Math.unsignedMultiplyHigh(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the largest (closest to positive infinity)
|
||||
* {@code int} value that is less than or equal to the algebraic quotient.
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 2021, 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
|
||||
@ -26,12 +26,13 @@
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.RandomFactory
|
||||
* @run main MultiplicationTests
|
||||
* @bug 5100935
|
||||
* @bug 5100935 8188044
|
||||
* @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed)
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.function.BiFunction;
|
||||
import jdk.test.lib.RandomFactory;
|
||||
|
||||
public class MultiplicationTests {
|
||||
@ -49,10 +50,25 @@ public class MultiplicationTests {
|
||||
.shiftRight(64).longValue();
|
||||
}
|
||||
|
||||
// Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y)
|
||||
private static boolean check(long x, long y) {
|
||||
long p1 = multiplyHighBigInt(x, y);
|
||||
long p2 = Math.multiplyHigh(x, y);
|
||||
// Calculate high 64 bits of unsigned 128 product using signed multiply
|
||||
private static long unsignedMultiplyHigh(long x, long y) {
|
||||
long x0 = x & 0xffffffffL;
|
||||
long x1 = x >>> 32;
|
||||
long y0 = y & 0xffffffffL;
|
||||
long y1 = y >>> 32;
|
||||
|
||||
long t = x1 * y0 + ((x0 * y0) >>> 32);
|
||||
long z0 = x0 * y1 + (t & 0xffffffffL);
|
||||
long z1 = t >>> 32;
|
||||
|
||||
return x1 * y1 + z1 + (z0 >>> 32);
|
||||
}
|
||||
|
||||
// Compare results of two functions for a pair of values
|
||||
private static boolean check(BiFunction<Long,Long,Long> reference,
|
||||
BiFunction<Long,Long,Long> multiply, long x, long y) {
|
||||
long p1 = reference.apply(x, y);
|
||||
long p2 = multiply.apply(x, y);
|
||||
if (p1 != p2) {
|
||||
System.err.printf("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2);
|
||||
return false;
|
||||
@ -61,7 +77,19 @@ public class MultiplicationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static int testMultiplyHigh() {
|
||||
// Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y)
|
||||
private static boolean checkSigned(long x, long y) {
|
||||
return check((a,b) -> multiplyHighBigInt(a,b),
|
||||
(a,b) -> Math.multiplyHigh(a, b), x, y);
|
||||
}
|
||||
|
||||
// Check Math.unsignedMultiplyHigh(x,y) against unsignedMultiplyHigh(x,y)
|
||||
private static boolean checkUnsigned(long x, long y) {
|
||||
return check((a,b) -> unsignedMultiplyHigh(a,b),
|
||||
(a,b) -> Math.unsignedMultiplyHigh(a, b), x, y);
|
||||
}
|
||||
|
||||
private static int test(BiFunction<Long,Long,Boolean> chk) {
|
||||
int failures = 0;
|
||||
|
||||
// check some boundary cases
|
||||
@ -84,14 +112,14 @@ public class MultiplicationTests {
|
||||
};
|
||||
|
||||
for (long[] xy : v) {
|
||||
if(!check(xy[0], xy[1])) {
|
||||
if(!chk.apply(xy[0], xy[1])) {
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
|
||||
// check some random values
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
if (!check(rnd.nextLong(), rnd.nextLong())) {
|
||||
if (!chk.apply(rnd.nextLong(), rnd.nextLong())) {
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
@ -99,8 +127,16 @@ public class MultiplicationTests {
|
||||
return failures;
|
||||
}
|
||||
|
||||
private static int testMultiplyHigh() {
|
||||
return test((x,y) -> checkSigned(x,y));
|
||||
}
|
||||
|
||||
private static int testUnsignedMultiplyHigh() {
|
||||
return test((x,y) -> checkUnsigned(x,y));
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
int failures = testMultiplyHigh();
|
||||
int failures = testMultiplyHigh() + testUnsignedMultiplyHigh();
|
||||
|
||||
if (failures > 0) {
|
||||
System.err.println("Multiplication testing encountered "
|
||||
|
Loading…
x
Reference in New Issue
Block a user