AbstractXoRoShiRo64.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.source32;

  18. import org.apache.commons.rng.core.util.NumberFactory;

  19. /**
  20.  * This abstract class is a base for algorithms from the Xor-Shift-Rotate family of 32-bit
  21.  * generators with 64-bits of state.
  22.  *
  23.  * @see <a href="http://xoshiro.di.unimi.it/">xorshiro / xoroshiro generators</a>
  24.  * @since 1.3
  25.  */
  26. abstract class AbstractXoRoShiRo64 extends IntProvider {
  27.     /** Size of the state vector. */
  28.     private static final int SEED_SIZE = 2;

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

  30.     /** State 0 of the generator. */
  31.     protected int state0;
  32.     /** State 1 of the generator. */
  33.     protected int state1;

  34.     /**
  35.      * Creates a new instance.
  36.      *
  37.      * @param seed Initial seed.
  38.      * If the length is larger than 2, only the first 2 elements will
  39.      * be used; if smaller, the remaining elements will be automatically
  40.      * set. A seed containing all zeros will create a non-functional generator.
  41.      */
  42.     AbstractXoRoShiRo64(int[] seed) {
  43.         if (seed.length < SEED_SIZE) {
  44.             final int[] state = new int[SEED_SIZE];
  45.             fillState(state, seed);
  46.             setState(state);
  47.         } else {
  48.             setState(seed);
  49.         }
  50.     }

  51.     /**
  52.      * Creates a new instance using a 2 element seed.
  53.      * A seed containing all zeros will create a non-functional generator.
  54.      *
  55.      * @param seed0 Initial seed element 0.
  56.      * @param seed1 Initial seed element 1.
  57.      */
  58.     AbstractXoRoShiRo64(int seed0, int seed1) {
  59.         state0 = seed0;
  60.         state1 = seed1;
  61.     }

  62.     /**
  63.      * Copies the state from the array into the generator state.
  64.      *
  65.      * @param state the new state
  66.      */
  67.     private void setState(int[] state) {
  68.         state0 = state[0];
  69.         state1 = state[1];
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     protected byte[] getStateInternal() {
  74.         return composeStateInternal(NumberFactory.makeByteArray(new int[] {state0, state1}),
  75.                                     super.getStateInternal());
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected void setStateInternal(byte[] s) {
  80.         final byte[][] c = splitStateInternal(s, SEED_SIZE * 4);

  81.         setState(NumberFactory.makeIntArray(c[0]));

  82.         super.setStateInternal(c[1]);
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public int next() {
  87.         final int result = nextOutput();

  88.         final int s0 = state0;
  89.         int s1 = state1;

  90.         s1 ^= s0;
  91.         state0 = Integer.rotateLeft(s0, 26) ^ s1 ^ (s1 << 9); // a, b
  92.         state1 = Integer.rotateLeft(s1, 13); // c

  93.         return result;
  94.     }

  95.     /**
  96.      * Use the current state to compute the next output from the generator.
  97.      * The output function shall vary with respect to different generators.
  98.      * This method is called from {@link #next()} before the current state is updated.
  99.      *
  100.      * @return the next output
  101.      */
  102.     protected abstract int nextOutput();
  103. }