| 1 |
// Copyright 2018 Developers of the Rand project. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 |
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 |
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 |
// option. This file may not be copied, modified, or distributed |
| 7 |
// except according to those terms. |
| 8 |
|
| 9 |
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; |
| 10 |
use rand_core::le::read_u32_into; |
| 11 |
use rand_core::{RngCore, SeedableRng}; |
| 12 |
#[cfg(feature = "serde")] |
| 13 |
use serde::{Deserialize, Serialize}; |
| 14 |
|
| 15 |
/// A xoshiro128++ random number generator. |
| 16 |
/// |
| 17 |
/// The xoshiro128++ algorithm is not suitable for cryptographic purposes, but |
| 18 |
/// is very fast and has excellent statistical properties. |
| 19 |
/// |
| 20 |
/// The algorithm used here is translated from [the `xoshiro128plusplus.c` |
| 21 |
/// reference source code](http://xoshiro.di.unimi.it/xoshiro128plusplus.c) by |
| 22 |
/// David Blackman and Sebastiano Vigna. |
| 23 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 24 |
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 25 |
pub struct Xoshiro128PlusPlus { |
| 26 |
s: [u32; 4], |
| 27 |
} |
| 28 |
|
| 29 |
impl SeedableRng for Xoshiro128PlusPlus { |
| 30 |
type Seed = [u8; 16]; |
| 31 |
|
| 32 |
/// Create a new `Xoshiro128PlusPlus`. If `seed` is entirely 0, it will be |
| 33 |
/// mapped to a different seed. |
| 34 |
#[inline] |
| 35 |
fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus { |
| 36 |
let mut state = [0; 4]; |
| 37 |
read_u32_into(&seed, &mut state); |
| 38 |
// Check for zero on aligned integers for better code generation. |
| 39 |
// Furtermore, seed_from_u64(0) will expand to a constant when optimized. |
| 40 |
if state.iter().all(|&x| x == 0) { |
| 41 |
return Self::seed_from_u64(0); |
| 42 |
} |
| 43 |
Xoshiro128PlusPlus { s: state } |
| 44 |
} |
| 45 |
|
| 46 |
/// Create a new `Xoshiro128PlusPlus` from a `u64` seed. |
| 47 |
/// |
| 48 |
/// This uses the SplitMix64 generator internally. |
| 49 |
#[inline] |
| 50 |
fn seed_from_u64(mut state: u64) -> Self { |
| 51 |
const PHI: u64 = 0x9e3779b97f4a7c15; |
| 52 |
let mut s = [0; 4]; |
| 53 |
for i in s.chunks_exact_mut(2) { |
| 54 |
state = state.wrapping_add(PHI); |
| 55 |
let mut z = state; |
| 56 |
z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9); |
| 57 |
z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb); |
| 58 |
z = z ^ (z >> 31); |
| 59 |
i[0] = z as u32; |
| 60 |
i[1] = (z >> 32) as u32; |
| 61 |
} |
| 62 |
// By using a non-zero PHI we are guaranteed to generate a non-zero state |
| 63 |
// Thus preventing a recursion between from_seed and seed_from_u64. |
| 64 |
debug_assert_ne!(s, [0; 4]); |
| 65 |
Xoshiro128PlusPlus { s } |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl RngCore for Xoshiro128PlusPlus { |
| 70 |
#[inline] |
| 71 |
fn next_u32(&mut self) -> u32 { |
| 72 |
let res = self.s[0] |
| 73 |
.wrapping_add(self.s[3]) |
| 74 |
.rotate_left(7) |
| 75 |
.wrapping_add(self.s[0]); |
| 76 |
|
| 77 |
let t = self.s[1] << 9; |
| 78 |
|
| 79 |
self.s[2] ^= self.s[0]; |
| 80 |
self.s[3] ^= self.s[1]; |
| 81 |
self.s[1] ^= self.s[2]; |
| 82 |
self.s[0] ^= self.s[3]; |
| 83 |
|
| 84 |
self.s[2] ^= t; |
| 85 |
|
| 86 |
self.s[3] = self.s[3].rotate_left(11); |
| 87 |
|
| 88 |
res |
| 89 |
} |
| 90 |
|
| 91 |
#[inline] |
| 92 |
fn next_u64(&mut self) -> u64 { |
| 93 |
next_u64_via_u32(self) |
| 94 |
} |
| 95 |
|
| 96 |
#[inline] |
| 97 |
fn fill_bytes(&mut self, dst: &mut [u8]) { |
| 98 |
fill_bytes_via_next(self, dst) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg(test)] |
| 103 |
mod tests { |
| 104 |
use super::Xoshiro128PlusPlus; |
| 105 |
use rand_core::{RngCore, SeedableRng}; |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn reference() { |
| 109 |
let mut rng = |
| 110 |
Xoshiro128PlusPlus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); |
| 111 |
// These values were produced with the reference implementation: |
| 112 |
// http://xoshiro.di.unimi.it/xoshiro128plusplus.c |
| 113 |
let expected = [ |
| 114 |
641, 1573767, 3222811527, 3517856514, 836907274, 4247214768, 3867114732, 1355841295, |
| 115 |
495546011, 621204420, |
| 116 |
]; |
| 117 |
for &e in &expected { |
| 118 |
assert_eq!(rng.next_u32(), e); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn stable_seed_from_u64_and_from_seed() { |
| 124 |
// We don't guarantee value-stability for SmallRng but this |
| 125 |
// could influence keeping stability whenever possible (e.g. after optimizations). |
| 126 |
let mut rng = Xoshiro128PlusPlus::seed_from_u64(0); |
| 127 |
// from_seed([0; 16]) should produce the same state as seed_from_u64(0). |
| 128 |
let mut rng_from_seed_0 = Xoshiro128PlusPlus::from_seed([0; 16]); |
| 129 |
let expected = [ |
| 130 |
1179900579, 1938959192, 3089844957, 3657088315, 1015453891, 479942911, 3433842246, |
| 131 |
669252886, 3985671746, 2737205563, |
| 132 |
]; |
| 133 |
for &e in &expected { |
| 134 |
assert_eq!(rng.next_u32(), e); |
| 135 |
assert_eq!(rng_from_seed_0.next_u32(), e); |
| 136 |
} |
| 137 |
} |
| 138 |
} |