Debian Codegraph @

src/xorshift.rs

raw symbols SCIP
1 use Source;
2
3 /// An instance of the Xorshift128+ algorithm.
4 ///
5 /// ## References
6 ///
7 /// 1. Sebastiano Vigna, “Further Scramblings of Marsaglia’s Xorshift
8 /// Generators,” CoRR, 2014.
9 ///
10 /// 2. https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
11 #[derive(Clone, Copy)]
12 pub struct Xorshift128Plus(u64, u64);
13
14 impl Xorshift128Plus {
15 /// Create an instance of the algorithm.
16 ///
17 /// At least one bit of the seed should be one.
18 #[inline(always)]
19 pub fn new(seed: [u64; 2]) -> Xorshift128Plus {
20 debug_assert!(seed[0] | seed[1] != 0, "at least one bit of the seed should be one");
21 Xorshift128Plus(seed[0], seed[1])
22 }
23 }
24
25 impl Source for Xorshift128Plus {
26 #[inline(always)]
27 fn read_u64(&mut self) -> u64 {
28 let (mut x, y) = (self.0, self.1);
29 self.0 = y;
30 x = x ^ (x << 23);
31 x = x ^ (x >> 17);
32 x = x ^ y ^ (y >> 26);
33 self.1 = x;
34 x.wrapping_add(y)
35 }
36 }
37
38 #[cfg(test)]
39 mod tests {
40 use Xorshift128Plus;
41
42 #[test]
43 #[should_panic]
44 fn new_zero_seed() {
45 let _ = Xorshift128Plus::new([0, 0]);
46 }
47 }