L64X128StarStar.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 "L64X128StarStarRandom"} 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.  * @see <a href="https://doi.org/10.1145/3485525">Steele &amp; Vigna (2021) Proc. ACM Programming
  43.  *      Languages 5, 1-31</a>
  44.  * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/package-summary.html">
  45.  *      JDK 17 java.util.random javadoc</a>
  46.  * @since 1.5
  47.  */
  48. public class L64X128StarStar extends AbstractL64X128 implements SplittableUniformRandomProvider {
  49.     /**
  50.      * Creates a new instance.
  51.      *
  52.      * @param seed Initial seed.
  53.      * If the length is larger than 4, only the first 4 elements will
  54.      * be used; if smaller, the remaining elements will be automatically
  55.      * set. A seed containing all zeros in the last two elements
  56.      * will create a non-functional XBG sub-generator and a low
  57.      * quality output with a period of 2<sup>64</sup>.
  58.      *
  59.      * <p>The 1st element is used to set the LCG increment; the least significant bit
  60.      * is set to odd to ensure a full period LCG. The 2nd element is used
  61.      * to set the LCG state.</p>
  62.      */
  63.     public L64X128StarStar(long[] seed) {
  64.         super(seed);
  65.     }

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

  84.     /**
  85.      * Creates a copy instance.
  86.      *
  87.      * @param source Source to copy.
  88.      */
  89.     protected L64X128StarStar(L64X128StarStar source) {
  90.         super(source);
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public long next() {
  95.         // LXM generate.
  96.         // Old state is used for the output allowing parallel pipelining
  97.         // on processors that support multiple concurrent instructions.

  98.         final long s0 = x0;
  99.         final long s = ls;

  100.         // Mix
  101.         final long z = Long.rotateLeft((s + s0) * 5, 7) * 9;

  102.         // LCG update
  103.         ls = M * s + la;

  104.         // XBG update
  105.         long s1 = x1;

  106.         s1 ^= s0;
  107.         x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
  108.         x1 = Long.rotateLeft(s1, 37); // c

  109.         return z;
  110.     }

  111.     /**
  112.      * Create a copy.
  113.      *
  114.      * @return the copy
  115.      */
  116.     @Override
  117.     protected L64X128StarStar copy() {
  118.         // This exists to ensure the jump function performed in the super class returns
  119.         // the correct class type. It should not be public.
  120.         return new L64X128StarStar(this);
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public SplittableUniformRandomProvider split(UniformRandomProvider source) {
  125.         return create(source.nextLong(), source);
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public Stream<SplittableUniformRandomProvider> splits(long streamSize, SplittableUniformRandomProvider source) {
  130.         return RandomStreams.generateWithSeed(streamSize, source, L64X128StarStar::create);
  131.     }

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