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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::prng_iter;
use crate::smol_core;
use crate::smol_core::Algorithm;
#[derive(Default)]
pub struct XorShift32 {
pub(crate) data: u32,
}
#[derive(Default)]
pub struct XorShift64 {
pub(crate) data: u64,
}
#[derive(Default)]
pub struct XorShift128 {
pub(crate) data: [u32; 4],
}
#[derive(Default)]
pub struct XorShift128Plus {
pub(crate) data: [u64; 2],
}
prng_iter! {XorShift32}
prng_iter! {XorShift64}
prng_iter! {XorShift128}
prng_iter! {XorShift128Plus}
impl Algorithm for XorShift32 {
type Output = u32;
fn gen(&mut self) -> Self::Output {
let mut x = self.data;
x ^= x.overflowing_shl(13).0;
x ^= x >> 17;
x ^= x.overflowing_shl(5).0;
self.data = x;
x
}
}
impl Algorithm for XorShift64 {
type Output = u64;
fn gen(&mut self) -> Self::Output {
let mut x = self.data;
x ^= x.overflowing_shl(13).0;
x ^= x >> 7;
x ^= x.overflowing_shl(17).0;
self.data = x;
x
}
}
impl Algorithm for XorShift128 {
type Output = u32;
fn gen(&mut self) -> Self::Output {
let mut t = self.data[3];
let s_ = self.data[0];
self.data[3] = self.data[2];
self.data[2] = self.data[1];
self.data[1] = s_;
t ^= t.overflowing_shl(11).0;
t ^= t >> 8;
self.data[0] = t ^ s_ ^ (s_ >> 19);
self.data[0]
}
}
impl Algorithm for XorShift128Plus {
type Output = u64;
fn gen(&mut self) -> Self::Output {
let mut s1 = self.data[0];
let s0 = self.data[1];
self.data[0] = s0;
s1 ^= s1.overflowing_shl(23).0;
s1 ^= s1 >> 17;
s1 ^= s0;
s1 ^= s0 >> 26;
self.data[1] = s1;
s1.overflowing_add(s0).0
}
}