Skip to content

Commit 1a52f31

Browse files
committed
Migrate SmallRng::seed_from_u64 from PCG32 to SplitMix64
1 parent 900ba4a commit 1a52f31

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/rngs/xoshiro128plusplus.rs

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ impl SeedableRng for Xoshiro128PlusPlus {
3939
read_u32_into(&seed, &mut state);
4040
Xoshiro128PlusPlus { s: state }
4141
}
42+
43+
/// Create a new `Xoshiro128PlusPlus` from a `u64` seed.
44+
///
45+
/// This uses the SplitMix64 generator internally.
46+
fn seed_from_u64(mut state: u64) -> Self {
47+
const PHI: u64 = 0x9e3779b97f4a7c15;
48+
let mut seed = Self::Seed::default();
49+
for chunk in seed.as_mut().chunks_mut(8) {
50+
state = state.wrapping_add(PHI);
51+
let mut z = state;
52+
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
53+
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
54+
z = z ^ (z >> 31);
55+
chunk.copy_from_slice(&z.to_le_bytes());
56+
}
57+
Self::from_seed(seed)
58+
}
4259
}
4360

4461
impl RngCore for Xoshiro128PlusPlus {

src/rngs/xoshiro256plusplus.rs

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ impl SeedableRng for Xoshiro256PlusPlus {
3939
read_u64_into(&seed, &mut state);
4040
Xoshiro256PlusPlus { s: state }
4141
}
42+
43+
/// Create a new `Xoshiro256PlusPlus` from a `u64` seed.
44+
///
45+
/// This uses the SplitMix64 generator internally.
46+
fn seed_from_u64(mut state: u64) -> Self {
47+
const PHI: u64 = 0x9e3779b97f4a7c15;
48+
let mut seed = Self::Seed::default();
49+
for chunk in seed.as_mut().chunks_mut(8) {
50+
state = state.wrapping_add(PHI);
51+
let mut z = state;
52+
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
53+
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
54+
z = z ^ (z >> 31);
55+
chunk.copy_from_slice(&z.to_le_bytes());
56+
}
57+
Self::from_seed(seed)
58+
}
4259
}
4360

4461
impl RngCore for Xoshiro256PlusPlus {

0 commit comments

Comments
 (0)