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.Arrays;
021import org.apache.commons.rng.core.util.NumberFactory;
022
023/**
024 * A fast RNG.
025 *
026 * @see <a href="http://xorshift.di.unimi.it/xorshift1024star.c">
027 * Original source code</a>
028 *
029 * @see <a href="https://en.wikipedia.org/wiki/Xorshift">Xorshift (Wikipedia)</a>
030 * @since 1.0
031 */
032public class XorShift1024Star extends LongProvider {
033    /** Size of the state vector. */
034    private static final int SEED_SIZE = 16;
035    /** State. */
036    private final long[] state = new long[SEED_SIZE];
037    /** Index in "state" array. */
038    private int index;
039
040    /**
041     * Creates a new instance.
042     *
043     * @param seed Initial seed.
044     * If the length is larger than 16, only the first 16 elements will
045     * be used; if smaller, the remaining elements will be automatically
046     * set.
047     */
048    public XorShift1024Star(long[] seed) {
049        setSeedInternal(seed);
050    }
051
052    /** {@inheritDoc} */
053    @Override
054    protected byte[] getStateInternal() {
055        final long[] s = Arrays.copyOf(state, SEED_SIZE + 1);
056        s[SEED_SIZE] = index;
057
058        return NumberFactory.makeByteArray(s);
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    protected void setStateInternal(byte[] s) {
064        checkStateSize(s, (SEED_SIZE + 1) * 8);
065
066        final long[] tmp = NumberFactory.makeLongArray(s);
067
068        System.arraycopy(tmp, 0, state, 0, SEED_SIZE);
069        index = (int) tmp[SEED_SIZE];
070    }
071
072    /**
073     * Seeds the RNG.
074     *
075     * @param seed Seed.
076     */
077    private void setSeedInternal(long[] seed) {
078        // Reset the whole state of this RNG (i.e. "state" and "index").
079        // Filling procedure is not part of the reference code.
080        fillState(state, seed);
081        index = 0;
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public long next() {
087        final long s0 = state[index];
088        long s1 = state[index = (index + 1) & 15];
089        s1 ^= s1 << 31; // a
090        state[index] = s1 ^ s0 ^ (s1 >>> 11) ^ (s0 >>> 30); // b,c
091        return state[index] * 1181783497276652981L;
092    }
093}