1cfg_rt! {
2 mod rt;
3 pub(crate) use rt::RngSeedGenerator;
4
5 cfg_unstable! {
6 mod rt_unstable;
7 }
8}
9
10#[allow(unreachable_pub)]
15#[derive(Clone, Debug)]
16pub struct RngSeed {
17 s: u32,
18 r: u32,
19}
20
21#[derive(Clone, Copy, Debug)]
29pub(crate) struct FastRand {
30 one: u32,
31 two: u32,
32}
33
34impl RngSeed {
35 pub(crate) fn new() -> Self {
37 Self::from_u64(crate::loom::rand::seed())
38 }
39
40 fn from_u64(seed: u64) -> Self {
41 let one = (seed >> 32) as u32;
42 let mut two = seed as u32;
43
44 if two == 0 {
45 two = 1;
47 }
48
49 Self::from_pair(one, two)
50 }
51
52 fn from_pair(s: u32, r: u32) -> Self {
53 Self { s, r }
54 }
55}
56
57impl FastRand {
58 pub(crate) fn new() -> FastRand {
60 FastRand::from_seed(RngSeed::new())
61 }
62
63 pub(crate) fn from_seed(seed: RngSeed) -> FastRand {
65 FastRand {
66 one: seed.s,
67 two: seed.r,
68 }
69 }
70
71 #[cfg(any(
72 feature = "macros",
73 feature = "rt-multi-thread",
74 feature = "time",
75 all(feature = "sync", feature = "rt")
76 ))]
77 pub(crate) fn fastrand_n(&mut self, n: u32) -> u32 {
78 let mul = (self.fastrand() as u64).wrapping_mul(n as u64);
81 (mul >> 32) as u32
82 }
83
84 fn fastrand(&mut self) -> u32 {
85 let mut s1 = self.one;
86 let s0 = self.two;
87
88 s1 ^= s1 << 17;
89 s1 = s1 ^ s0 ^ s1 >> 7 ^ s0 >> 16;
90
91 self.one = s0;
92 self.two = s1;
93
94 s0.wrapping_add(s1)
95 }
96}