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 a089841b408de17f19a0dea43f11af2b18a8a304 Author: Alex Herbert <[email protected]> AuthorDate: Thu Apr 15 23:00:54 2021 +0100 UnitBallSampler: Use the sign bit for the sign of the range [-1, 1) Changes the use of the upper 54 bits of the long from 53 bits for the double then 1 bit for the sign to 1 bit for the sign then 53 bits for the double. --- .../examples/jmh/sampling/UnitBallSamplerBenchmark.java | 14 ++++++++------ .../org/apache/commons/rng/sampling/UnitBallSampler.java | 11 +++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitBallSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitBallSamplerBenchmark.java index 9a7f883..450f85c 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitBallSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitBallSamplerBenchmark.java @@ -46,7 +46,6 @@ import java.util.concurrent.TimeUnit; @State(Scope.Benchmark) @Fork(value = 1, jvmArgs = { "-server", "-Xms128M", "-Xmx128M" }) public class UnitBallSamplerBenchmark { - /** Name for the baseline method. */ private static final String BASELINE = "Baseline"; /** Name for the rejection method. */ @@ -61,6 +60,8 @@ public class UnitBallSamplerBenchmark { private static final String HYPERSPHERE_DISCARD = "HypersphereDiscard"; /** Error message for an unknown sampler type. */ private static final String UNKNOWN_SAMPLER = "Unknown sampler type: "; + /** The mask to extract the lower 53-bits from a long. */ + private static final long LOWER_53_BITS = -1L >>> 11; /** * The sampler. @@ -738,10 +739,11 @@ public class UnitBallSamplerBenchmark { * @return the double */ private static double makeSignedDouble(long bits) { - // Upper 53-bits generates a positive number in the range [0, 1). - // This has 1 optionally subtracted. Do not use the lower bits on the - // assumption they are less random. - return ((bits >>> 11) * 0x1.0p-53d) - ((bits >>> 10) & 0x1L); + // Use the upper 54 bits on the assumption they are more random. + // The sign bit generates a value of 0 or 1 for subtraction. + // The next 53 bits generates a positive number in the range [0, 1). + // [0, 1) - (0 or 1) => [-1, 1) + return (((bits >>> 10) & LOWER_53_BITS) * 0x1.0p-53d) - (bits >>> 63); } /** @@ -763,7 +765,7 @@ public class UnitBallSamplerBenchmark { * @param bh Data sink * @param data Input data. */ - //@Benchmark + @Benchmark public void create1D(Blackhole bh, Sampler1D data) { runSampler(bh, data); } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitBallSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitBallSampler.java index e0c2b69..4c359b5 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitBallSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitBallSampler.java @@ -41,6 +41,8 @@ public abstract class UnitBallSampler implements SharedStateSampler<UnitBallSamp private static final int TWO_D = 2; /** The dimension for 3D sampling. */ private static final int THREE_D = 3; + /** The mask to extract the lower 53-bits from a long. */ + private static final long LOWER_53_BITS = -1L >>> 11; /** * Sample uniformly from a 1D unit line. @@ -228,9 +230,10 @@ public abstract class UnitBallSampler implements SharedStateSampler<UnitBallSamp * @return the double */ private static double makeSignedDouble(long bits) { - // Upper 53-bits generates a positive number in the range [0, 1). - // This has 1 optionally subtracted. Do not use the lower bits on the - // assumption they are less random. - return ((bits >>> 11) * 0x1.0p-53d) - ((bits >>> 10) & 0x1L); + // Use the upper 54 bits on the assumption they are more random. + // The sign bit generates a value of 0 or 1 for subtraction. + // The next 53 bits generates a positive number in the range [0, 1). + // [0, 1) - (0 or 1) => [-1, 1) + return (((bits >>> 10) & LOWER_53_BITS) * 0x1.0p-53d) - (bits >>> 63); } }
