1use crate::uniform_generator::UniformGenerator;
8
9const INIT_A: u32 = 0x43b0_d7e5;
10const MULT_A: u32 = 0x931e_8875;
11const INIT_B: u32 = 0x8b51_f9dd;
12const MULT_B: u32 = 0x58f3_8ded;
13const MIX_MULT_L: u32 = 0xca01_f9dd;
14const MIX_MULT_R: u32 = 0x4973_f715;
15const XSHIFT: u32 = 16;
16const POOL_SIZE: usize = 4;
17
18fn hashmix(value: u32, hash_const: &mut u32) -> u32 {
19 let mut v = value ^ *hash_const;
20 *hash_const = hash_const.wrapping_mul(MULT_A);
21 v = v.wrapping_mul(*hash_const);
22 v ^ (v >> XSHIFT)
23}
24
25fn mix(x: u32, y: u32) -> u32 {
26 let r = MIX_MULT_L.wrapping_mul(x).wrapping_sub(MIX_MULT_R.wrapping_mul(y));
27 r ^ (r >> XSHIFT)
28}
29
30#[derive(Clone, Debug)]
33pub struct SeedSequence {
34 pool: [u32; POOL_SIZE],
35}
36
37impl SeedSequence {
38 pub fn new(seed: u128) -> Self {
40 let mut words: Vec<u32> = Vec::new();
42 let mut s = seed;
43 while s > 0 {
44 words.push((s & 0xffff_ffff) as u32);
45 s >>= 32;
46 }
47 if words.is_empty() {
48 words.push(0);
49 }
50 let mut pool = [0u32; POOL_SIZE];
51 let mut hash_const = INIT_A;
52 for (i, p) in pool.iter_mut().enumerate() {
53 *p = hashmix(words.get(i).copied().unwrap_or(0), &mut hash_const);
54 }
55 for i_src in 0..POOL_SIZE {
56 for i_dst in 0..POOL_SIZE {
57 if i_src != i_dst {
58 let h = hashmix(pool[i_src], &mut hash_const);
59 pool[i_dst] = mix(pool[i_dst], h);
60 }
61 }
62 }
63 for &w in words.iter().skip(POOL_SIZE) {
64 for p in pool.iter_mut() {
65 let h = hashmix(w, &mut hash_const);
66 *p = mix(*p, h);
67 }
68 }
69 SeedSequence { pool }
70 }
71
72 pub fn generate_state_u32(&self, n_words: usize) -> Vec<u32> {
74 let mut hash_const = INIT_B;
75 (0..n_words)
76 .map(|i| {
77 let mut v = self.pool[i % POOL_SIZE] ^ hash_const;
78 hash_const = hash_const.wrapping_mul(MULT_B);
79 v = v.wrapping_mul(hash_const);
80 v ^ (v >> XSHIFT)
81 })
82 .collect()
83 }
84
85 pub fn generate_state_u64(&self, n_words: usize) -> Vec<u64> {
87 self.generate_state_u32(2 * n_words).chunks_exact(2).map(|w| u64::from(w[0]) | (u64::from(w[1]) << 32)).collect()
88 }
89}
90
91const PCG_MULT: u128 = 0x2360_ed05_1fc6_5da4_4385_df64_9fcc_f645;
92
93#[derive(Clone, Debug)]
96pub struct Pcg64 {
97 state: u128,
98 inc: u128,
99}
100
101impl Pcg64 {
102 pub fn from_seed(seed: u128) -> Self {
104 Self::from_seed_sequence(&SeedSequence::new(seed))
105 }
106
107 pub fn from_seed_sequence(ss: &SeedSequence) -> Self {
109 let v = ss.generate_state_u64(4);
110 let initstate = (u128::from(v[0]) << 64) | u128::from(v[1]);
111 let initseq = (u128::from(v[2]) << 64) | u128::from(v[3]);
112 let mut g = Pcg64 { state: 0, inc: (initseq << 1) | 1 };
113 g.step();
114 g.state = g.state.wrapping_add(initstate);
115 g.step();
116 g
117 }
118
119 fn step(&mut self) {
120 self.state = self.state.wrapping_mul(PCG_MULT).wrapping_add(self.inc);
121 }
122}
123
124impl UniformGenerator for Pcg64 {
125 fn next_u64(&mut self) -> u64 {
126 self.step();
127 let s = self.state;
128 let (hi, lo) = ((s >> 64) as u64, s as u64);
129 (hi ^ lo).rotate_right((s >> 122) as u32)
130 }
131
132 fn next_f64(&mut self) -> f64 {
134 (self.next_u64() >> 11) as f64 * (1.0 / 9_007_199_254_740_992.0)
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 #[test]
144 fn matches_numpy_streams() {
145 let draw = |seed: u128| {
146 let mut g = Pcg64::from_seed(seed);
147 (0..3).map(|_| g.next_f64()).collect::<Vec<f64>>()
148 };
149 assert_eq!(draw(0), vec![0.6369616873214543, 0.2697867137638703, 0.04097352393619469]);
150 assert_eq!(draw(12345), vec![0.22733602246716966, 0.31675833970975287, 0.7973654573327341]);
151 assert_eq!(draw((1 << 40) + 7), vec![0.829812891829413, 0.25530515834910417, 0.15379071585298754]);
153 assert_eq!(draw((1 << 70) + 3), vec![0.4203871190878119, 0.2858736967365757, 0.10627311818295038]);
154 let mut g = Pcg64::from_seed((1 << 40) + 7);
155 assert_eq!([g.next_u64(), g.next_u64()], [15307346044642009514, 4709548916763817589]);
156 let mut g = Pcg64::from_seed((1 << 70) + 3);
157 assert_eq!([g.next_u64(), g.next_u64()], [7754773597696927362, 5273438921104871389]);
158 }
159}