JDK-8269705 : We need Math.unsignedMultiplyHigh
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 18
  • Submitted: 2021-06-30
  • Updated: 2021-07-02
  • Resolved: 2021-07-02
Related Reports
CSR :  
Description
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) {}
Comments
Moving to Approved.
02-07-2021

Looks sensible to me. It might be worth adding cross-reference links with Math.multiplyHigh().
01-07-2021