AbstractXoRoShiRo128.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 org.apache.commons.rng.JumpableUniformRandomProvider;
  19. import org.apache.commons.rng.LongJumpableUniformRandomProvider;
  20. import org.apache.commons.rng.UniformRandomProvider;
  21. import org.apache.commons.rng.core.util.NumberFactory;

  22. /**
  23.  * This abstract class is a base for algorithms from the Xor-Shift-Rotate family of 64-bit
  24.  * generators with 128-bits of state.
  25.  *
  26.  * @see <a href="http://xoshiro.di.unimi.it/">xorshiro / xoroshiro generators</a>
  27.  * @since 1.3
  28.  */
  29. abstract class AbstractXoRoShiRo128 extends LongProvider implements LongJumpableUniformRandomProvider {
  30.     /** Size of the state vector. */
  31.     private static final int SEED_SIZE = 2;
  32.     /** The coefficients for the jump function. */
  33.     private static final long[] JUMP_COEFFICIENTS = {
  34.         0xdf900294d8f554a5L, 0x170865df4b3201fcL
  35.     };
  36.     /** The coefficients for the long jump function. */
  37.     private static final long[] LONG_JUMP_COEFFICIENTS = {
  38.         0xd2a98b26625eee7bL, 0xdddf9b1090aa7ac1L
  39.     };

  40.     // State is maintained using variables rather than an array for performance

  41.     /** State 0 of the generator. */
  42.     protected long state0;
  43.     /** State 1 of the generator. */
  44.     protected long state1;

  45.     /**
  46.      * Creates a new instance.
  47.      *
  48.      * @param seed Initial seed.
  49.      * If the length is larger than 2, only the first 2 elements will
  50.      * be used; if smaller, the remaining elements will be automatically
  51.      * set. A seed containing all zeros will create a non-functional generator.
  52.      */
  53.     AbstractXoRoShiRo128(long[] seed) {
  54.         if (seed.length < SEED_SIZE) {
  55.             final long[] state = new long[SEED_SIZE];
  56.             fillState(state, seed);
  57.             setState(state);
  58.         } else {
  59.             setState(seed);
  60.         }
  61.     }

  62.     /**
  63.      * Creates a new instance using a 2 element seed.
  64.      * A seed containing all zeros will create a non-functional generator.
  65.      *
  66.      * @param seed0 Initial seed element 0.
  67.      * @param seed1 Initial seed element 1.
  68.      */
  69.     AbstractXoRoShiRo128(long seed0, long seed1) {
  70.         state0 = seed0;
  71.         state1 = seed1;
  72.     }

  73.     /**
  74.      * Creates a copy instance.
  75.      *
  76.      * @param source Source to copy.
  77.      */
  78.     protected AbstractXoRoShiRo128(AbstractXoRoShiRo128 source) {
  79.         super(source);
  80.         state0 = source.state0;
  81.         state1 = source.state1;
  82.     }

  83.     /**
  84.      * Copies the state from the array into the generator state.
  85.      *
  86.      * @param state the new state
  87.      */
  88.     private void setState(long[] state) {
  89.         state0 = state[0];
  90.         state1 = state[1];
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     protected byte[] getStateInternal() {
  95.         return composeStateInternal(NumberFactory.makeByteArray(new long[] {state0, state1}),
  96.                                     super.getStateInternal());
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     protected void setStateInternal(byte[] s) {
  101.         final byte[][] c = splitStateInternal(s, SEED_SIZE * 8);

  102.         setState(NumberFactory.makeLongArray(c[0]));

  103.         super.setStateInternal(c[1]);
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public long next() {
  108.         final long result = nextOutput();

  109.         final long s0 = state0;
  110.         long s1 = state1;

  111.         s1 ^= s0;
  112.         state0 = Long.rotateLeft(s0, 24) ^ s1 ^ (s1 << 16); // a, b
  113.         state1 = Long.rotateLeft(s1, 37); // c

  114.         return result;
  115.     }

  116.     /**
  117.      * Use the current state to compute the next output from the generator.
  118.      * The output function shall vary with respect to different generators.
  119.      * This method is called from {@link #next()} before the current state is updated.
  120.      *
  121.      * @return the next output
  122.      */
  123.     protected abstract long nextOutput();

  124.     /**
  125.      * {@inheritDoc}
  126.      *
  127.      * <p>The jump size is the equivalent of 2<sup>64</sup>
  128.      * calls to {@link UniformRandomProvider#nextLong() nextLong()}. It can provide
  129.      * up to 2<sup>64</sup> non-overlapping subsequences.</p>
  130.      */
  131.     @Override
  132.     public UniformRandomProvider jump() {
  133.         final UniformRandomProvider copy = copy();
  134.         performJump(JUMP_COEFFICIENTS);
  135.         return copy;
  136.     }

  137.     /**
  138.      * {@inheritDoc}
  139.      *
  140.      * <p>The jump size is the equivalent of 2<sup>96</sup> calls to
  141.      * {@link UniformRandomProvider#nextLong() nextLong()}. It can provide up to
  142.      * 2<sup>32</sup> non-overlapping subsequences of length 2<sup>96</sup>; each
  143.      * subsequence can provide up to 2<sup>32</sup> non-overlapping subsequences of
  144.      * length 2<sup>64</sup> using the {@link #jump()} method.</p>
  145.      */
  146.     @Override
  147.     public JumpableUniformRandomProvider longJump() {
  148.         final JumpableUniformRandomProvider copy = copy();
  149.         performJump(LONG_JUMP_COEFFICIENTS);
  150.         return copy;
  151.     }

  152.     /**
  153.      * Create a copy.
  154.      *
  155.      * @return the copy
  156.      */
  157.     protected abstract AbstractXoRoShiRo128 copy();

  158.     /**
  159.      * Perform the jump to advance the generator state. Resets the cached state of the generator.
  160.      *
  161.      * @param jumpCoefficients Jump coefficients.
  162.      */
  163.     final void performJump(long[] jumpCoefficients) {
  164.         long s0 = 0;
  165.         long s1 = 0;
  166.         for (final long jc : jumpCoefficients) {
  167.             for (int b = 0; b < 64; b++) {
  168.                 if ((jc & (1L << b)) != 0) {
  169.                     s0 ^= state0;
  170.                     s1 ^= state1;
  171.                 }
  172.                 next();
  173.             }
  174.         }
  175.         state0 = s0;
  176.         state1 = s1;
  177.         resetCachedState();
  178.     }
  179. }