use crate::{Cast, ConstInit, Own, Slice, read_at, slice};
use crate::{Infallible, InfallibleResult, RandQualities, RandSeedable, RandTry};
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_meta!{location("num/prob/rand")}]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct XorShift128p([u64; 2]);
impl Default for XorShift128p {
fn default() -> Self {
Self::INIT
}
}
impl ConstInit for XorShift128p {
const INIT: Self = Self::new_unchecked(Self::DEFAULT_SEED);
}
impl XorShift128p {
#[doc(hidden)]
pub const DEFAULT_SEED: [u64; 2] = [0xDEFA_0017_DEFA_0017; 2];
#[cold] #[allow(dead_code)] #[rustfmt::skip]
const fn cold_path_default() -> Self { Self::new_unchecked(Self::DEFAULT_SEED) }
}
impl XorShift128p {
pub const fn new(seeds: [u64; 2]) -> Self {
if (seeds[0] | seeds[1]) == 0 { Self::cold_path_default() } else { Self(seeds) }
}
pub const fn new_unchecked(seeds: [u64; 2]) -> Self {
debug_assert![(seeds[0] | seeds[1]) != 0, "Seeds must be non-zero"];
Self(seeds)
}
#[inline(never)]
pub fn from_stack() -> Self {
let (a, b) = (0, 0);
let seed: [u64; 2] = [&a as *const _ as u64, &b as *const _ as u64];
Self::new_unchecked(seed)
}
#[must_use]
pub const fn inner_state(self) -> [u64; 2] {
self.0
}
pub const fn from_state(state: [u64; 2]) -> Self {
Self(state)
}
#[must_use]
pub const fn current_u64(&self) -> u64 {
self.0[0].wrapping_add(self.0[1])
}
#[must_use]
pub const fn next_u64(&mut self) -> u64 {
let [s0, mut s1] = [self.0[0], self.0[1]];
let result = s0.wrapping_add(s1);
s1 ^= s0;
self.0[0] = s0.rotate_left(55) ^ s1 ^ (s1 << 14); self.0[1] = s1.rotate_left(36); result
}
pub const fn peek_next_state(&self) -> Self {
let mut x = self.0;
let [s0, mut s1] = [x[0], x[1]];
s1 ^= s0;
x[0] = s0.rotate_left(55) ^ s1 ^ (s1 << 14); x[1] = s1.rotate_left(36); Self(x)
}
pub const fn own_next_u64(self) -> Own<Self, u64> {
let s = self.peek_next_state();
let v = s.current_u64();
Own::new(s, v)
}
pub const fn fill_bytes(&mut self, buffer: &mut [u8]) {
let mut i = 0;
while i < buffer.len() {
let random_u64 = self.next_u64();
let bytes = random_u64.to_le_bytes();
let remaining = buffer.len() - i;
if remaining >= 8 {
Slice::copy(slice!(mut buffer, i, ..i + 8), &bytes);
i += 8;
} else {
Slice::copy(slice!(mut buffer, i, ..), slice!(&bytes, ..remaining));
break;
}
}
}
}
impl XorShift128p {
pub const fn new1_u128(seed: u128) -> Self {
Self::new(Cast(seed).into_u64_le())
}
pub const fn new2_u64(seeds: [u64; 2]) -> Self {
Self::new(seeds)
}
pub const fn new4_u32(seeds: [u32; 4]) -> Self {
Self::new([
Cast::<u64>::from_u32_le([seeds[0], seeds[1]]),
Cast::<u64>::from_u32_le([seeds[2], seeds[3]]),
])
}
pub const fn new8_u16(seeds: [u16; 8]) -> Self {
Self::new([
Cast::<u64>::from_u16_le([seeds[0], seeds[1], seeds[2], seeds[3]]),
Cast::<u64>::from_u16_le([seeds[4], seeds[5], seeds[6], seeds[7]]),
])
}
pub const fn new16_u8(seeds: [u8; 16]) -> Self {
let s = seeds;
Self::new([u64::from_le_bytes(read_at![s, 0, @8]), u64::from_le_bytes(read_at![s, 8, @8])])
}
}
crate::items! {
impl RandTry for XorShift128p {
type Error = Infallible;
const RAND_OUTPUT_BITS: u32 = 64;
const RAND_STATE_BITS: u32 = 128;
const RAND_QUALITIES: RandQualities = RandQualities::WEAK_PRNG;
fn rand_try_next_u64(&mut self) -> InfallibleResult<u64> { Ok(self.next_u64()) }
fn rand_try_fill_bytes(&mut self, buffer: &mut [u8]) -> InfallibleResult<()> {
self.fill_bytes(buffer); Ok(())
}
}
impl RandSeedable for XorShift128p {
type RandSeed = [u8; 16];
#[inline(always)]
fn rand_from_seed(seed: Self::RandSeed) -> Self { Self::new16_u8(seed) }
}
}
crate::__impl_dep_rand_core!(XorShift128p);