1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use Source;

/// The Xorshift128+ algorithm.
///
/// ## References
///
/// 1. Sebastiano Vigna, “Further Scramblings of Marsaglia’s Xorshift
///    Generators,” CoRR, 2014.
///
/// 2. https://en.wikipedia.org/wiki/Xorshift#Xorshift.2B
#[derive(Clone, Copy)]
pub struct Xorshift128Plus {
    state: [u64; 2],
}

impl Xorshift128Plus {
    /// Create an instance of the algorithm.
    ///
    /// At least one bit of the seed should be nonzero.
    #[inline(always)]
    pub fn new(seed: [u64; 2]) -> Xorshift128Plus {
        debug_assert!(seed[0] | seed[1] != 0, "at least one bit of the seed should be nonzero");
        Xorshift128Plus { state: seed }
    }
}

impl Source for Xorshift128Plus {
    #[inline(always)]
    fn read_u64(&mut self) -> u64 {
        let (mut x, y) = (self.state[0], self.state[1]);

        self.state[0] = y;
        x = x ^ (x << 23);
        x = x ^ (x >> 17);
        x = x ^ y ^ (y >> 26);
        self.state[1] = x;

        x.wrapping_add(y)
    }
}

#[cfg(test)]
mod tests {
    use super::Xorshift128Plus;

    #[test]
    #[should_panic]
    fn new_zero_seed() {
        let _ = Xorshift128Plus::new([0, 0]);
    }
}