You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2023/12/24 17:13:46 UTC

(commons-rng) branch master updated: Consolidate conversion of unsigned long to BigInteger

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git


The following commit(s) were added to refs/heads/master by this push:
     new 26fd7b90 Consolidate conversion of unsigned long to BigInteger
26fd7b90 is described below

commit 26fd7b908bfc457967fac410cb12dc5e6333e734
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Dec 24 17:13:22 2023 +0000

    Consolidate conversion of unsigned long to BigInteger
---
 .../org/apache/commons/rng/core/source64/AbstractLXMTest.java    | 7 ++-----
 .../org/apache/commons/rng/core/source64/LXMSupportTest.java     | 9 ++++-----
 .../apache/commons/rng/examples/jmh/core/LXMBenchmarkTest.java   | 4 ++--
 3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/AbstractLXMTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/AbstractLXMTest.java
index b9648f38..aeb6189d 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/AbstractLXMTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/AbstractLXMTest.java
@@ -664,8 +664,6 @@ abstract class AbstractLXMTest {
      * small jumps that can be verified by an equal number of single state updates.
      */
     static class LCGTest {
-        /** 2^63. */
-        private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63);
         /** 65-bit multiplier for the 128-bit LCG. */
         private static final BigInteger M = BigInteger.ONE.shiftLeft(64).add(toUnsignedBigInteger(LXMSupport.M128L));
         /** 2^128. Used as the modulus for the 128-bit LCG. */
@@ -823,9 +821,8 @@ abstract class AbstractLXMTest {
          * @return the big integer
          */
         private static BigInteger toUnsignedBigInteger(long v) {
-            return v < 0 ?
-                TWO_POW_63.add(BigInteger.valueOf(v & Long.MAX_VALUE)) :
-                BigInteger.valueOf(v);
+            // Delegate
+            return LXMSupportTest.toUnsignedBigInteger(v);
         }
     }
 
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LXMSupportTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LXMSupportTest.java
index 30c3b26b..8a8138cf 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LXMSupportTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/source64/LXMSupportTest.java
@@ -123,9 +123,9 @@ class LXMSupportTest {
      * @param v Value
      * @return the big integer
      */
-    private static BigInteger toUnsignedBigInteger(long v) {
+    static BigInteger toUnsignedBigInteger(long v) {
         return v < 0 ?
-            TWO_POW_63.add(BigInteger.valueOf(v & Long.MAX_VALUE)) :
+            TWO_POW_63.or(BigInteger.valueOf(v & Long.MAX_VALUE)) :
             BigInteger.valueOf(v);
     }
 
@@ -136,11 +136,10 @@ class LXMSupportTest {
      * @param lo High part of value
      * @return the big integer
      */
-    private static BigInteger toUnsignedBigInteger(long hi, long lo) {
-        return toUnsignedBigInteger(hi).shiftLeft(64).add(toUnsignedBigInteger(lo));
+    static BigInteger toUnsignedBigInteger(long hi, long lo) {
+        return toUnsignedBigInteger(hi).shiftLeft(64).or(toUnsignedBigInteger(lo));
     }
 
-
     @Test
     void testUnsignedAddHigh() {
         // This will trigger a carry as the sum is 2^64.
diff --git a/commons-rng-examples/examples-jmh/src/test/java/org/apache/commons/rng/examples/jmh/core/LXMBenchmarkTest.java b/commons-rng-examples/examples-jmh/src/test/java/org/apache/commons/rng/examples/jmh/core/LXMBenchmarkTest.java
index a80d6e51..5bc314b4 100644
--- a/commons-rng-examples/examples-jmh/src/test/java/org/apache/commons/rng/examples/jmh/core/LXMBenchmarkTest.java
+++ b/commons-rng-examples/examples-jmh/src/test/java/org/apache/commons/rng/examples/jmh/core/LXMBenchmarkTest.java
@@ -213,7 +213,7 @@ class LXMBenchmarkTest {
      */
     private static BigInteger toUnsignedBigInteger(long v) {
         return v < 0 ?
-            TWO_POW_63.add(BigInteger.valueOf(v & Long.MAX_VALUE)) :
+            TWO_POW_63.or(BigInteger.valueOf(v & Long.MAX_VALUE)) :
             BigInteger.valueOf(v);
     }
 
@@ -225,7 +225,7 @@ class LXMBenchmarkTest {
      * @return the big integer
      */
     private static BigInteger toUnsignedBigInteger(long hi, long lo) {
-        return toUnsignedBigInteger(hi).shiftLeft(64).add(toUnsignedBigInteger(lo));
+        return toUnsignedBigInteger(hi).shiftLeft(64).or(toUnsignedBigInteger(lo));
     }
 
     /**