L64X128Mix.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.rng.core.source64;

  18. import java.util.stream.Stream;
  19. import org.apache.commons.rng.SplittableUniformRandomProvider;
  20. import org.apache.commons.rng.UniformRandomProvider;
  21. import org.apache.commons.rng.core.util.RandomStreams;

  22. /**
  23.  * A 64-bit all purpose generator.
  24.  *
  25.  * <p>This is a member of the LXM family of generators: L=Linear congruential generator;
  26.  * X=Xor based generator; and M=Mix. This member uses a 64-bit LCG and 128-bit Xor-based
  27.  * generator. It is named as {@code "L64X128MixRandom"} in the {@code java.util.random}
  28.  * package introduced in JDK 17; the LXM family is described in further detail in:
  29.  *
  30.  * <blockquote>Steele and Vigna (2021) LXM: better splittable pseudorandom number generators
  31.  * (and almost as fast). Proceedings of the ACM on Programming Languages, Volume 5,
  32.  * Article 148, pp 1–31.</blockquote>
  33.  *
  34.  * <p>Memory footprint is 256 bits and the period is 2<sup>64</sup> (2<sup>128</sup> - 1).
  35.  *
  36.  * <p>This generator implements
  37.  * {@link org.apache.commons.rng.LongJumpableUniformRandomProvider LongJumpableUniformRandomProvider}.
  38.  * In addition instances created with a different additive parameter for the LCG are robust
  39.  * against accidental correlation in a multi-threaded setting. The additive parameters must be
  40.  * different in the most significant 63-bits.
  41.  *
  42.  * <p>This generator implements
  43.  * {@link org.apache.commons.rng.SplittableUniformRandomProvider SplittableUniformRandomProvider}.
  44.  * The stream of generators created using the {@code splits} methods support parallelisation
  45.  * and are robust against accidental correlation by using unique values for the additive parameter
  46.  * for each instance in the same stream. The primitive streaming methods support parallelisation
  47.  * but with no assurances of accidental correlation; each thread uses a new instance with a
  48.  * randomly initialised state.
  49.  *
  50.  * @see <a href="https://doi.org/10.1145/3485525">Steele &amp; Vigna (2021) Proc. ACM Programming
  51.  *      Languages 5, 1-31</a>
  52.  * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/package-summary.html">
  53.  *      JDK 17 java.util.random javadoc</a>
  54.  * @since 1.5
  55.  */
  56. public class L64X128Mix extends AbstractL64X128 implements SplittableUniformRandomProvider {
  57.     /**
  58.      * Creates a new instance.
  59.      *
  60.      * @param seed Initial seed.
  61.      * If the length is larger than 4, only the first 4 elements will
  62.      * be used; if smaller, the remaining elements will be automatically
  63.      * set. A seed containing all zeros in the last two elements
  64.      * will create a non-functional XBG sub-generator and a low
  65.      * quality output with a period of 2<sup>64</sup>.
  66.      *
  67.      * <p>The 1st element is used to set the LCG increment; the least significant bit
  68.      * is set to odd to ensure a full period LCG. The 2nd element is used
  69.      * to set the LCG state.</p>
  70.      */
  71.     public L64X128Mix(long[] seed) {
  72.         super(seed);
  73.     }

  74.     /**
  75.      * Creates a new instance using a 4 element seed.
  76.      * A seed containing all zeros in the last two elements
  77.      * will create a non-functional XBG sub-generator and a low
  78.      * quality output with a period of 2<sup>64</sup>.
  79.      *
  80.      * <p>The 1st element is used to set the LCG increment; the least significant bit
  81.      * is set to odd to ensure a full period LCG. The 2nd element is used
  82.      * to set the LCG state.</p>
  83.      *
  84.      * @param seed0 Initial seed element 0.
  85.      * @param seed1 Initial seed element 1.
  86.      * @param seed2 Initial seed element 2.
  87.      * @param seed3 Initial seed element 3.
  88.      */
  89.     public L64X128Mix(long seed0, long seed1, long seed2, long seed3) {
  90.         super(seed0, seed1, seed2, seed3);
  91.     }

  92.     /**
  93.      * Creates a copy instance.
  94.      *
  95.      * @param source Source to copy.
  96.      */
  97.     protected L64X128Mix(L64X128Mix source) {
  98.         super(source);
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public long next() {
  103.         // LXM generate.
  104.         // Old state is used for the output allowing parallel pipelining
  105.         // on processors that support multiple concurrent instructions.

  106.         final long s0 = x0;
  107.         final long s = ls;

  108.         // Mix
  109.         final long z = LXMSupport.lea64(s + s0);

  110.         // LCG update
  111.         ls = M * s + la;

  112.         // XBG update
  113.         long s1 = x1;

  114.         s1 ^= s0;
  115.         x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
  116.         x1 = Long.rotateLeft(s1, 37); // c

  117.         return z;
  118.     }

  119.     /**
  120.      * Create a copy.
  121.      *
  122.      * @return the copy
  123.      */
  124.     @Override
  125.     protected L64X128Mix copy() {
  126.         // This exists to ensure the jump function performed in the super class returns
  127.         // the correct class type. It should not be public.
  128.         return new L64X128Mix(this);
  129.     }

  130.     /** {@inheritDoc} */
  131.     @Override
  132.     public SplittableUniformRandomProvider split(UniformRandomProvider source) {
  133.         return create(source.nextLong(), source);
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public Stream<SplittableUniformRandomProvider> splits(long streamSize, SplittableUniformRandomProvider source) {
  138.         return RandomStreams.generateWithSeed(streamSize, source, L64X128Mix::create);
  139.     }

  140.     /**
  141.      * Create a new instance using the given {@code seed} and {@code source} of randomness
  142.      * to initialise the instance.
  143.      *
  144.      * @param seed Seed used to initialise the instance.
  145.      * @param source Source of randomness used to initialise the instance.
  146.      * @return A new instance.
  147.      */
  148.     private static SplittableUniformRandomProvider create(long seed, UniformRandomProvider source) {
  149.         // LCG state. The addition uses the input seed.
  150.         // The LCG addition parameter is set to odd so left-shift the seed.
  151.         final long s0 = seed << 1;
  152.         final long s1 = source.nextLong();
  153.         // XBG state must not be all zero
  154.         long x0 = source.nextLong();
  155.         long x1 = source.nextLong();
  156.         if ((x0 | x1) == 0) {
  157.             // SplitMix style seed ensures at least one non-zero value
  158.             x0 = LXMSupport.lea64(s1);
  159.             x1 = LXMSupport.lea64(s1 + LXMSupport.GOLDEN_RATIO_64);
  160.         }
  161.         return new L64X128Mix(s0, s1, x0, x1);
  162.     }
  163. }