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

commit 678bfb434f208047e53424c32a13ca05f37bf31c
Author: Alex Herbert <aherb...@apache.org>
AuthorDate: Mon Jul 5 18:13:06 2021 +0100

    Testing more methods for a signed double of -1 or 1
---
 .../jmh/sampling/UnitSphereSamplerBenchmark.java   | 32 +++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git 
a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
 
b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
index bede36f..2911155 100644
--- 
a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
+++ 
b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
@@ -125,11 +125,15 @@ public class UnitSphereSamplerBenchmark {
     public static class Sampler1D extends SamplerData {
         /** Name for the signed double method. */
         private static final String SIGNED_DOUBLE = "signedDouble";
+        /** Name for the masked int method. */
+        private static final String MASKED_INT = "maskedInt";
+        /** Name for the masked long method. */
+        private static final String MASKED_LONG = "maskedLong";
         /** Name for the boolean method. */
         private static final String BOOLEAN = "boolean";
 
         /** The sampler type. */
-        @Param({BASELINE, SIGNED_DOUBLE, BOOLEAN, ARRAY})
+        @Param({BASELINE, SIGNED_DOUBLE, MASKED_INT, MASKED_LONG, BOOLEAN, 
ARRAY})
         private String type;
 
         /** {@inheritDoc} */
@@ -151,6 +155,32 @@ public class UnitSphereSamplerBenchmark {
                         return new double[] {1.0 - ((rng.nextInt() >>> 30) & 
0x2)};
                     }
                 };
+            } else if (MASKED_INT.equals(type)) {
+                return new Sampler() {
+                    // The value 1.0 in raw long bits
+                    private final long one = Double.doubleToRawLongBits(1.0);
+                    // Mask to extract the sign bit from an integer (as a long)
+                    private final long signBit = 1L << 31;
+
+                    @Override
+                    public double[] sample() {
+                        // Shift the sign bit and combine with the bits for a 
double of 1.0
+                        return new double[] {Double.longBitsToDouble(one | 
((rng.nextInt() & signBit) << 32))};
+                    }
+                };
+            } else if (MASKED_LONG.equals(type)) {
+                return new Sampler() {
+                    // The value 1.0 in raw long bits
+                    private final long one = Double.doubleToRawLongBits(1.0);
+                    // Mask to extract the sign bit from a long
+                    private final long signBit = 1L << 63;
+
+                    @Override
+                    public double[] sample() {
+                        // Combine the sign bit with the bits for a double of 
1.0
+                        return new double[] {Double.longBitsToDouble(one | 
(rng.nextLong() & signBit))};
+                    }
+                };
             } else if (BOOLEAN.equals(type)) {
                 return new Sampler() {
                     @Override

Reply via email to