001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.rng.core.source64;
019
020import java.util.stream.Stream;
021import org.apache.commons.rng.JumpableUniformRandomProvider;
022import org.apache.commons.rng.SplittableUniformRandomProvider;
023import org.apache.commons.rng.UniformRandomProvider;
024import org.apache.commons.rng.core.util.NumberFactory;
025import org.apache.commons.rng.core.util.RandomStreams;
026
027/**
028 * A 64-bit all purpose generator.
029 *
030 * <p>This is a member of the LXM family of generators: L=Linear congruential generator;
031 * X=Xor based generator; and M=Mix. This member uses a 128-bit LCG and 128-bit Xor-based
032 * generator. It is named as {@code "L128X128MixRandom"} in the {@code java.util.random}
033 * package introduced in JDK 17; the LXM family is described in further detail in:
034 *
035 * <blockquote>Steele and Vigna (2021) LXM: better splittable pseudorandom number generators
036 * (and almost as fast). Proceedings of the ACM on Programming Languages, Volume 5,
037 * Article 148, pp 1–31.</blockquote>
038 *
039 * <p>Memory footprint is 384 bits and the period is 2<sup>128</sup> (2<sup>128</sup> - 1).
040 *
041 * <p>This generator implements
042 * {@link org.apache.commons.rng.LongJumpableUniformRandomProvider LongJumpableUniformRandomProvider}.
043 * In addition instances created with a different additive parameter for the LCG are robust
044 * against accidental correlation in a multi-threaded setting. The additive parameters must be
045 * different in the most significant 127-bits.
046 *
047 * <p>This generator implements
048 * {@link org.apache.commons.rng.SplittableUniformRandomProvider SplittableUniformRandomProvider}.
049 * The stream of generators created using the {@code splits} methods support parallelisation
050 * and are robust against accidental correlation by using unique values for the additive parameter
051 * for each instance in the same stream. The primitive streaming methods support parallelisation
052 * but with no assurances of accidental correlation; each thread uses a new instance with a
053 * randomly initialised state.
054 *
055 * @see <a href="https://doi.org/10.1145/3485525">Steele &amp; Vigna (2021) Proc. ACM Programming
056 *      Languages 5, 1-31</a>
057 * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/package-summary.html">
058 *      JDK 17 java.util.random javadoc</a>
059 * @since 1.5
060 */
061public class L128X128Mix extends AbstractL128 implements SplittableUniformRandomProvider {
062    /** Size of the seed vector. */
063    private static final int SEED_SIZE = 6;
064    /** Size of the XBG state vector. */
065    private static final int XBG_STATE_SIZE = 2;
066    /** Low half of 128-bit LCG multiplier. */
067    private static final long ML = LXMSupport.M128L;
068
069    /** State 0 of the XBG. */
070    private long x0;
071    /** State 1 of the XBG. */
072    private long x1;
073
074    /**
075     * Creates a new instance.
076     *
077     * @param seed Initial seed.
078     * If the length is larger than 6, only the first 6 elements will
079     * be used; if smaller, the remaining elements will be automatically
080     * set. A seed containing all zeros in the last four elements
081     * will create a non-functional XBG sub-generator and a low
082     * quality output with a period of 2<sup>128</sup>.
083     *
084     * <p>The 1st and 2nd elements are used to set the LCG increment; the least significant bit
085     * is set to odd to ensure a full period LCG. The 3rd and 4th elements are used
086     * to set the LCG state.</p>
087     */
088    public L128X128Mix(long[] seed) {
089        super(seed = extendSeed(seed, SEED_SIZE));
090        x0 = seed[4];
091        x1 = seed[5];
092    }
093
094    /**
095     * Creates a new instance using a 6 element seed.
096     * A seed containing all zeros in the last four elements
097     * will create a non-functional XBG sub-generator and a low
098     * quality output with a period of 2<sup>128</sup>.
099     *
100     * <p>The 1st and 2nd elements are used to set the LCG increment; the least significant bit
101     * is set to odd to ensure a full period LCG. The 3rd and 4th elements are used
102     * to set the LCG state.</p>
103     *
104     * @param seed0 Initial seed element 0.
105     * @param seed1 Initial seed element 1.
106     * @param seed2 Initial seed element 2.
107     * @param seed3 Initial seed element 3.
108     * @param seed4 Initial seed element 4.
109     * @param seed5 Initial seed element 5.
110     */
111    public L128X128Mix(long seed0, long seed1, long seed2, long seed3,
112                       long seed4, long seed5) {
113        super(seed0, seed1, seed2, seed3);
114        x0 = seed4;
115        x1 = seed5;
116    }
117
118    /**
119     * Creates a copy instance.
120     *
121     * @param source Source to copy.
122     */
123    protected L128X128Mix(L128X128Mix source) {
124        super(source);
125        x0 = source.x0;
126        x1 = source.x1;
127    }
128
129    /** {@inheritDoc} */
130    @Override
131    protected byte[] getStateInternal() {
132        return composeStateInternal(NumberFactory.makeByteArray(
133                                        new long[] {x0, x1}),
134                                    super.getStateInternal());
135    }
136
137    /** {@inheritDoc} */
138    @Override
139    protected void setStateInternal(byte[] s) {
140        final byte[][] c = splitStateInternal(s, XBG_STATE_SIZE * Long.BYTES);
141        final long[] tmp = NumberFactory.makeLongArray(c[0]);
142        x0 = tmp[0];
143        x1 = tmp[1];
144        super.setStateInternal(c[1]);
145    }
146
147    /** {@inheritDoc} */
148    @Override
149    public long next() {
150        // LXM generate.
151        // Old state is used for the output allowing parallel pipelining
152        // on processors that support multiple concurrent instructions.
153
154        final long s0 = x0;
155        final long sh = lsh;
156
157        // Mix
158        final long z = LXMSupport.lea64(sh + s0);
159
160        // LCG update
161        // The LCG is, in effect, "s = m * s + a" where m = ((1LL << 64) + ML)
162        final long sl = lsl;
163        final long al = lal;
164        final long u = ML * sl;
165        // High half
166        lsh = ML * sh + LXMSupport.unsignedMultiplyHigh(ML, sl) + sl + lah +
167              // Carry propagation
168              LXMSupport.unsignedAddHigh(u, al);
169        // Low half
170        lsl = u + al;
171
172        // XBG update
173        long s1 = x1;
174
175        s1 ^= s0;
176        x0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
177        x1 = Long.rotateLeft(s1, 37); // c
178
179        return z;
180    }
181
182    /**
183     * {@inheritDoc}
184     *
185     * <p>The jump size is the equivalent of moving the state <em>backwards</em> by
186     * (2<sup>128</sup> - 1) positions. It can provide up to 2<sup>128</sup>
187     * non-overlapping subsequences.
188     */
189    @Override
190    public UniformRandomProvider jump() {
191        return super.jump();
192    }
193
194    /**
195     * {@inheritDoc}
196     *
197     * <p>The jump size is the equivalent of moving the state <em>backwards</em> by
198     * 2<sup>64</sup> (2<sup>128</sup> - 1) positions. It can provide up to
199     * 2<sup>64</sup> non-overlapping subsequences of length 2<sup>64</sup>
200     * (2<sup>128</sup> - 1); each subsequence can provide up to 2<sup>64</sup>
201     * non-overlapping subsequences of length (2<sup>128</sup> - 1) using the
202     * {@link #jump()} method.
203     */
204    @Override
205    public JumpableUniformRandomProvider longJump() {
206        return super.longJump();
207    }
208
209    /** {@inheritDoc} */
210    @Override
211    AbstractL128 copy() {
212        // This exists to ensure the jump function performed in the super class returns
213        // the correct class type. It should not be public.
214        return new L128X128Mix(this);
215    }
216
217    /** {@inheritDoc} */
218    @Override
219    public SplittableUniformRandomProvider split(UniformRandomProvider source) {
220        return create(source.nextLong(), source);
221    }
222
223    /** {@inheritDoc} */
224    @Override
225    public Stream<SplittableUniformRandomProvider> splits(long streamSize, SplittableUniformRandomProvider source) {
226        return RandomStreams.generateWithSeed(streamSize, source, L128X128Mix::create);
227    }
228
229    /**
230     * Create a new instance using the given {@code seed} and {@code source} of randomness
231     * to initialise the instance.
232     *
233     * @param seed Seed used to initialise the instance.
234     * @param source Source of randomness used to initialise the instance.
235     * @return A new instance.
236     */
237    private static SplittableUniformRandomProvider create(long seed, UniformRandomProvider source) {
238        // LCG state. The addition lower-half uses the input seed.
239        // The LCG addition parameter is set to odd so left-shift the seed.
240        final long s0 = source.nextLong();
241        final long s1 = seed << 1;
242        final long s2 = source.nextLong();
243        final long s3 = source.nextLong();
244        // XBG state must not be all zero
245        long x0 = source.nextLong();
246        long x1 = source.nextLong();
247        if ((x0 | x1) == 0) {
248            // SplitMix style seed ensures at least one non-zero value
249            final long z = s3;
250            x0 = LXMSupport.lea64(z);
251            x1 = LXMSupport.lea64(z + LXMSupport.GOLDEN_RATIO_64);
252        }
253        return new L128X128Mix(s0, s1, s2, s3, x0, x1);
254    }
255}