| 1 |
//! Xorshift RNG used for tests. Based on the `rand_xorshift` crate. |
| 2 |
use core::num::Wrapping; |
| 3 |
|
| 4 |
/// Initial RNG state used in tests. |
| 5 |
// chosen by fair dice roll. guaranteed to be random. |
| 6 |
pub(crate) const RNG: XorShiftRng = XorShiftRng { |
| 7 |
x: Wrapping(0x0787_3B4A), |
| 8 |
y: Wrapping(0xFAAB_8FFE), |
| 9 |
z: Wrapping(0x1745_980F), |
| 10 |
w: Wrapping(0xB0AD_B4F3), |
| 11 |
}; |
| 12 |
|
| 13 |
/// Xorshift RNG instance/ |
| 14 |
pub(crate) struct XorShiftRng { |
| 15 |
x: Wrapping<u32>, |
| 16 |
y: Wrapping<u32>, |
| 17 |
z: Wrapping<u32>, |
| 18 |
w: Wrapping<u32>, |
| 19 |
} |
| 20 |
|
| 21 |
impl XorShiftRng { |
| 22 |
pub(crate) fn fill(&mut self, buf: &mut [u8; 1024]) { |
| 23 |
for chunk in buf.chunks_exact_mut(4) { |
| 24 |
chunk.copy_from_slice(&self.next_u32().to_le_bytes()); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
fn next_u32(&mut self) -> u32 { |
| 29 |
let x = self.x; |
| 30 |
let t = x ^ (x << 11); |
| 31 |
self.x = self.y; |
| 32 |
self.y = self.z; |
| 33 |
self.z = self.w; |
| 34 |
let w = self.w; |
| 35 |
self.w = w ^ (w >> 19) ^ (t ^ (t >> 8)); |
| 36 |
self.w.0 |
| 37 |
} |
| 38 |
} |