 # We need Math.unsignedMultiplyHigh

XMLWordPrintable

#### Details

• Type: CSR
• Status: Closed
• Priority: P4
• Resolution: Approved
• Fix Version/s:
• Component/s:
• Labels:
None
• Subcomponent:
• Compatibility Kind:
behavioral
• Compatibility Risk:
minimal
• Compatibility Risk Description:
Adding a new method to a final class should have no compatibility risk.
• Interface Kind:
Java API
• Scope:
SE

## Summary

Add a new method to compute the upper 64 bits of the unsigned 128-bit product of two unsigned 64-bit integers.

## Problem

There is no interface to compute the high half of the product of two unsigned 64-bit integers. Such a function would be useful to cryptographic and other algorithms especially once intrinsified.

## Solution

Add a method `unsignedMultiplyHigh(long x, long y)` to `java.lang.Math` and `java.lang.StrictMath` which computes the upper 64 bits of the 128-bit product of two unsigned 64-bit integers. The algorithm used in `jdk.internal.math.FDBigInteger#mult(int[] src, int srcLen, int v0, int v1, int[] dst)` is effectively unrolled into a loop-free solution.

## Specification

``````--- a/src/java.base/share/classes/java/lang/Math.java
+++ b/src/java.base/share/classes/java/lang/Math.java
@@ -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,29 @@ 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) {}

--- a/src/java.base/share/classes/java/lang/StrictMath.java
+++ b/src/java.base/share/classes/java/lang/StrictMath.java
@@ -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) {}``````

#### People

Assignee: Brian Burkhalter
Reporter: Andrew Haley
Reviewed By:
Roger Riggs, Stuart Marks