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.
89use rand_core::impls::fill_bytes_via_next;
10use rand_core::le::read_u64_into;
11use rand_core::{RngCore, SeedableRng};
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
1415/// A xoshiro256++ random number generator.
16///
17/// The xoshiro256++ 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 `xoshiro256plusplus.c`
21/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plusplus.c) by
22/// David Blackman and Sebastiano Vigna.
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub struct Xoshiro256PlusPlus {
26 s: [u64; 4],
27}
2829impl SeedableRng for Xoshiro256PlusPlus {
30type Seed = [u8; 32];
3132/// Create a new `Xoshiro256PlusPlus`. If `seed` is entirely 0, it will be
33 /// mapped to a different seed.
34#[inline]
35fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus {
36let mut state = [0; 4];
37 read_u64_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.
40if state.iter().all(|&x| x == 0) {
41return Self::seed_from_u64(0);
42 }
43 Xoshiro256PlusPlus { s: state }
44 }
4546/// Create a new `Xoshiro256PlusPlus` from a `u64` seed.
47 ///
48 /// This uses the SplitMix64 generator internally.
49#[inline]
50fn seed_from_u64(mut state: u64) -> Self {
51const PHI: u64 = 0x9e3779b97f4a7c15;
52let mut s = [0; 4];
53for i in s.iter_mut() {
54 state = state.wrapping_add(PHI);
55let 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 = z;
60 }
61// By using a non-zero PHI we are guaranteed to generate a non-zero state
62 // Thus preventing a recursion between from_seed and seed_from_u64.
63debug_assert_ne!(s, [0; 4]);
64 Xoshiro256PlusPlus { s }
65 }
66}
6768impl RngCore for Xoshiro256PlusPlus {
69#[inline]
70fn next_u32(&mut self) -> u32 {
71// The lowest bits have some linear dependencies, so we use the
72 // upper bits instead.
73let val = self.next_u64();
74 (val >> 32) as u32
75 }
7677#[inline]
78fn next_u64(&mut self) -> u64 {
79let res = self.s[0]
80 .wrapping_add(self.s[3])
81 .rotate_left(23)
82 .wrapping_add(self.s[0]);
8384let t = self.s[1] << 17;
8586self.s[2] ^= self.s[0];
87self.s[3] ^= self.s[1];
88self.s[1] ^= self.s[2];
89self.s[0] ^= self.s[3];
9091self.s[2] ^= t;
9293self.s[3] = self.s[3].rotate_left(45);
9495 res
96 }
9798#[inline]
99fn fill_bytes(&mut self, dst: &mut [u8]) {
100 fill_bytes_via_next(self, dst)
101 }
102}
103104#[cfg(test)]
105mod tests {
106use super::Xoshiro256PlusPlus;
107use rand_core::{RngCore, SeedableRng};
108109#[test]
110fn reference() {
111let mut rng = Xoshiro256PlusPlus::from_seed([
1121, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0,
1130, 0, 0,
114 ]);
115// These values were produced with the reference implementation:
116 // http://xoshiro.di.unimi.it/xoshiro256plusplus.c
117let expected = [
11841943041,
11958720359,
1203588806011781223,
1213591011842654386,
1229228616714210784205,
1239973669472204895162,
12414011001112246962877,
12512406186145184390807,
12615849039046786891736,
12710450023813501588000,
128 ];
129for &e in &expected {
130assert_eq!(rng.next_u64(), e);
131 }
132 }
133134#[test]
135fn stable_seed_from_u64() {
136// We don't guarantee value-stability for SmallRng but this
137 // could influence keeping stability whenever possible (e.g. after optimizations).
138let mut rng = Xoshiro256PlusPlus::seed_from_u64(0);
139let expected = [
1405987356902031041503,
1417051070477665621255,
1426633766593972829180,
143211316841551650330,
1449136120204379184874,
145379361710973160858,
14615813423377499357806,
14715596884590815070553,
1485439680534584881407,
1491369371744833522710,
150 ];
151for &e in &expected {
152assert_eq!(rng.next_u64(), e);
153 }
154 }
155}