src/java.base/share/classes/java/util/random/L64X1024MixRandom.java
branchJDK-8193209-branch
changeset 59080 1b314be4feb2
parent 57684 7cb325557832
--- a/src/java.base/share/classes/java/util/random/L64X1024MixRandom.java	Thu Aug 29 11:33:26 2019 -0300
+++ b/src/java.base/share/classes/java/util/random/L64X1024MixRandom.java	Thu Nov 14 08:54:56 2019 -0400
@@ -28,7 +28,7 @@
 import java.math.BigInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.random.RandomGenerator.SplittableGenerator;
-import java.util.random.RandomSupport.AbstractSplittableGenerator;
+import java.util.random.RandomSupport.AbstractSplittableWithBrineGenerator;
 
 /**
  * A generator of uniform pseudorandom values applicable for use in
@@ -54,9 +54,10 @@
  * {@link L64X1024MixRandom} is a specific member of the LXM family of algorithms
  * for pseudorandom number generators.  Every LXM generator consists of two
  * subgenerators; one is an LCG (Linear Congruential Generator) and the other is
- * an Xorshift generator.  Each output of an LXM generator is the sum of one
- * output from each subgenerator, possibly processed by a final mixing function
- * (and {@link L64X1024MixRandom} does use a mixing function).
+ * an Xorshift generator.  Each output of an LXM generator is the result of
+ * combining state from the LCG with state from the Xorshift generator by
+ * using a Mixing function (and then the state of the LCG and the state of the
+ * Xorshift generator are advanced).
  * <p>
  * The LCG subgenerator for {@link L64X1024MixRandom} has an update step of the
  * form {@code s = m * s + a}, where {@code s}, {@code m}, and {@code a} are all
@@ -73,7 +74,9 @@
  * which can take on any values provided that they are not all zero.
  * The period of this subgenerator is 2<sup>1024</sup>-1.
  * <p>
- * The mixing function for {@link L64X256MixRandom} is the 64-bit MurmurHash3 finalizer.
+ * The mixing function for {@link L64X1024MixRandom} is {@link RandomSupport.mixLea64}
+ * applied to the argument {@code (s + s0)}, where {@code s0} is the most recently computed
+ * element of {@code x}.
  * <p>
  * Because the periods 2<sup>64</sup> and 2<sup>1024</sup>-1 of the two subgenerators
  * are relatively prime, the <em>period</em> of any single {@link L64X1024MixRandom} object
@@ -98,8 +101,8 @@
  * 2<sup>1024</sup> subsequence values, nearly all of them (2<sup>1024</sup>-2<sup>64</sup>)
  * occur 2<sup>64</sup> times over the course of the entire cycle, and the other
  * 2<sup>64</sup> subsequence values occur only 2<sup>64</sup>-1 times.  So the ratio
- * of the probability of getting one of the less common subsequence values and the
- * probability of getting one of the more common subsequence values is 1-2<sup>-64</sup>.
+ * of the probability of getting any specific one of the less common subsequence values and the
+ * probability of getting any specific one of the more common subsequence values is 1-2<sup>-64</sup>.
  * (Note that the set of 2<sup>64</sup> less-common subsequence values will differ from
  * one instance of {@link L64X1024MixRandom} to another, as a function of the additive
  * parameter of the LCG.)  The values produced by the {@code nextInt()}, {@code nextFloat()},
@@ -136,7 +139,7 @@
  *
  * @since 14
  */
-public final class L64X1024MixRandom extends AbstractSplittableGenerator {
+public final class L64X1024MixRandom extends AbstractSplittableWithBrineGenerator {
 
     /*
      * Implementation Overview.
@@ -185,14 +188,13 @@
         BigInteger.ONE.shiftLeft(N*64).subtract(BigInteger.ONE).shiftLeft(64);
 
     /*
-     * Multiplier used in the LCG portion of the algorithm, taken from
-     * Pierre L'Ecuyer, Tables of linear congruential generators of
-     * different sizes and good lattice structure, <em>Mathematics of
-     * Computation</em> 68, 225 (January 1999), pages 249-260,
-     * Table 4 (first multiplier for size 2<sup>64</sup>).
+     * Multiplier used in the LCG portion of the algorithm.
+     * Chosen based on research by Sebastiano Vigna and Guy Steele (2019).
+     * The spectral scores for dimensions 2 through 8 for the multiplier 0xd1342543de82ef95
+     * are [0.958602, 0.937479, 0.870757, 0.822326, 0.820405, 0.813065, 0.760215].
      */
 
-    private static final long M = 2862933555777941757L;
+    private static final long M = 0xd1342543de82ef95L;
 
     /* ---------------- instance fields ---------------- */
 
@@ -264,9 +266,10 @@
         this.x[15] = x15;
         // If x0, x1, ..., x15 are all zero (very unlikely), we must choose nonzero values.
         if ((x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | x11 | x12 | x13 | x14 | x15) == 0) {
+	    long v = s;
             // At least fifteen of the sixteen values generated here will be nonzero.
             for (int j = 0; j < N; j++) {
-                this.x[j] = RandomSupport.mixStafford13(s += RandomSupport.GOLDEN_RATIO_64);
+                this.x[j] = RandomSupport.mixStafford13(v += RandomSupport.GOLDEN_RATIO_64);
             }
         }
     }
@@ -288,7 +291,7 @@
         // The seed is hashed by mixStafford13 to produce the initial `x[0]`,
         // which will then be used to produce the first generated value.
         // The other x values are filled in as if by a SplitMix PRNG with
-        // GOLDEN_RATIO_64 as the gamma value and Stafford13 as the mixer.
+        // GOLDEN_RATIO_64 as the gamma value and mixStafford13 as the mixer.
         this(RandomSupport.mixMurmur64(seed ^= RandomSupport.SILVER_RATIO_64),
              1,
              RandomSupport.mixStafford13(seed),
@@ -341,27 +344,28 @@
     }
 
     /* ---------------- public methods ---------------- */
-
     /**
-     * Constructs and returns a new instance of {@link L64X1024MixRandom}
-     * that shares no mutable state with this instance.
+     * Given 63 bits of "brine", constructs and returns a new instance of
+     * {@code L64X1024MixRandom} that shares no mutable state with this instance.
      * However, with very high probability, the set of values collectively
      * generated by the two objects has the same statistical properties as if
      * same the quantity of values were generated by a single thread using
-     * a single {@link L64X1024MixRandom} object.  Either or both of the two
+     * a single {@code L64X1024MixRandom} object.  Either or both of the two
      * objects may be further split using the {@code split} method,
      * and the same expected statistical properties apply to the
      * entire set of generators constructed by such recursive splitting.
      *
-     * @param source a {@link SplittableGenerator} instance to be used instead
+     * @param source a {@code SplittableGenerator} instance to be used instead
      *               of this one as a source of pseudorandom bits used to
      *               initialize the state of the new ones.
-     * @return a new instance of {@link L64X1024MixRandom}
+     * @param brine a long value, of which the low 63 bits are used to choose
+     *              the {@code a} parameter for the new instance.
+     * @return a new instance of {@code L64X1024MixRandom}
      */
-    public L64X1024MixRandom split(SplittableGenerator source) {
-        // Literally pick a new instance "at random".
-        return new L64X1024MixRandom(source.nextLong(), source.nextLong(),
-                                     source.nextLong(), source.nextLong(),
+    public SplittableGenerator split(SplittableGenerator source, long brine) {
+	// Pick a new instance "at random", but use the brine for `a`.
+        return new L64X1024MixRandom(brine << 1, source.nextLong(),
+				     source.nextLong(), source.nextLong(),
                                      source.nextLong(), source.nextLong(),
                                      source.nextLong(), source.nextLong(),
                                      source.nextLong(), source.nextLong(),
@@ -382,7 +386,12 @@
         final long s0 = x[p = (p + 1) & (N - 1)];
         long s15 = x[q];
 
-        final long z = s + s0;
+	// Compute the result based on current state information
+	// (this allows the computation to be overlapped with state update).
+
+	final long result = RandomSupport.mixLea64(s + s0);
+	
+	// Update the LCG subgenerator
         s = M * s + a;  // LCG
 
         // Second part of xoroshiro1024: update array data
@@ -390,9 +399,15 @@
         x[q] = Long.rotateLeft(s0, 25) ^ s15 ^ (s15 << 27);
         x[p] = Long.rotateLeft(s15, 36);
 
-        return RandomSupport.mixLea64(z);  // mixing function
+        return result;
     }
 
+    /**
+     * Returns the period of this random generator.
+     *
+     * @return a {@link BigInteger} whose value is the number of distinct possible states of this
+     *         {@link RandomGenerator} object (2<sup>64</sup>(2<sup>1024</sup>-1)).
+     */
     public BigInteger period() {
         return PERIOD;
     }