[ 
https://issues.apache.org/jira/browse/RNG-191?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18062479#comment-18062479
 ] 

Alex Herbert commented on RNG-191:
----------------------------------

I have prepared a modification of the code to use the Math multiply methods for 
the PHILOX_4X64 generator only.

JMH results on a Mac M2 Max in ns/operation:
| |nextLong| | |nextLongN| | |
|JDK|Original|UM|Relative|Original|UM|Relative|
|1.8.0_473|15.709|15.569|0.991|16.503|16.669|1.010|
|11.0.29|9.754|5.390|0.553|11.014|5.604|0.509|
|17.0.17|9.087|3.621|0.398|10.240|4.420|0.432|
|21.0.9|9.053|2.172|0.240|10.170|2.797|0.275|
|25.0.2|9.104|2.164|0.238|10.207|2.792|0.274|

Performance agrees with previous results from the testing benchmarks. On JDK 11 
performance doubles and increases even more on higher JDKs. 

PR is here: [PR 192|https://github.com/apache/commons-rng/pull/192]

> 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
>            Priority: Minor
>
> 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