Alex Herbert created RNG-191:
--------------------------------

             Summary: Use java.lang.invoke to dynamically call Math multiply 
high methods
                 Key: RNG-191
                 URL: https://issues.apache.org/jira/browse/RNG-191
             Project: Commons RNG
          Issue Type: Improvement
          Components: core
            Reporter: Alex Herbert


The following generators rely on a 128-bit multiplication result from two 
64-bit longs:
 * L128_X128_MIX
 * L128_X256_MIX
 * L128_X1024_MIX
 * PHILOX_4X64

Java added support to java.lang.Math to compute the upper 64-bit result with 
potential intrinsic calls to native functions:
||Method||JDK||Notes||
|[multiplyHigh (Javadoc JDK 
21)|https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Math.html#multiplyHigh(long,long)]|9|Convert
 to unsigned using:
Math.multiplyHigh(a, b) + ((a >> 63) & b) + ((b >> 63) & a)|
|[unsignedMultiplyHigh (Javadoc JDK 
21)|https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Math.html#unsignedMultiplyHigh(long,long)]|18|
 |

Since Commons RNG targets Java 8 these methods cannot be used. The current RNGs 
use a software implementation to compose the upper bits of the 128-bit result. 
However the methods can be used with a 
[MethodHandle|https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/invoke/MethodHandle.html]
 introduced in Java 7:
{code:java}
// find the method
MethodHandle h = MethodHandles.publicLookup()
    .findStatic(Math.class,
                "unsignedMultiplyHigh",
                MethodType.methodType(long.class, long.class, long.class));

// invoke (snippet)
long a, b;
try {
    long r = (long) h2.invokeExact(a, b);
} catch (Throwable e) {
    throw new RuntimeException(e);
}
{code}
Investigate the use of MethodHandle within the named RNGs.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to