use crate::{ConstInit, Own, whilst};
use crate::{Infallible, InfallibleResult, RandQualities, RandSeedable, RandTry};
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_meta!{location("num/prob/rand")}]
#[doc = crate::_doc_vendor!("Xabc")]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Xabc {
a: u8,
b: u8,
c: u8,
x: u8,
}
impl Default for Xabc {
fn default() -> Self {
Self::INIT
}
}
impl ConstInit for Xabc {
const INIT: Self = Self::new(Self::DEFAULT_SEED);
}
impl Xabc {
#[doc(hidden)]
pub const DEFAULT_SEED: [u8; 3] = [0xDE, 0xFA, 0x17];
}
impl Xabc {
pub const fn new(seeds: [u8; 3]) -> Self {
let a = seeds[0];
let b = seeds[1];
let c = seeds[2];
let x = 1;
let a = a ^ c ^ x;
let b = b.wrapping_add(a);
let c = c.wrapping_add(b >> 1) ^ a;
Self { a, b, c, x }
}
pub const fn reseed(&mut self, seeds: [u8; 3]) {
self.a ^= seeds[0];
self.b ^= seeds[1];
self.c ^= seeds[2];
self.x += 1;
self.a = self.a ^ self.c ^ self.x;
self.b = self.b.wrapping_add(self.a);
self.c = self.c.wrapping_add(self.b >> 1) ^ self.a;
}
#[must_use]
pub const fn inner_state(self) -> [u8; 4] {
[self.a, self.b, self.c, self.x]
}
pub const fn from_state(state: [u8; 4]) -> Self {
Self { a: state[0], b: state[1], c: state[2], x: state[3] }
}
#[must_use]
pub const fn current_u8(&self) -> u8 {
self.c
}
#[must_use]
pub const fn next_u8(&mut self) -> u8 {
self.x = self.x.wrapping_add(1);
self.a = self.a ^ self.c ^ self.x;
self.b = self.b.wrapping_add(self.a);
self.c = self.c.wrapping_add(self.b >> 1) ^ self.a;
self.c
}
pub const fn peek_next_state(&self) -> Self {
let [mut a, mut b, mut c, mut x] = [self.a, self.b, self.c, self.x];
x += 1;
a = a ^ c ^ x;
b = b.wrapping_add(a);
c = c.wrapping_add(b >> 1) ^ a;
Self { a, b, c, x }
}
pub const fn own_next_u8(self) -> Own<Self, u8> {
let s = self.peek_next_state();
let v = s.current_u8();
Own::new(s, v)
}
pub const fn fill_bytes(&mut self, buffer: &mut [u8]) {
whilst! { i in 0..buffer.len(); {
buffer[i] = self.next_u8();
}}
}
}
impl Xabc {
pub const fn new3_u8(seeds: [u8; 3]) -> Self {
Self::new(seeds)
}
pub const fn next_u16(&mut self) -> u16 {
u16::from_le_bytes([self.next_u8(), self.next_u8()])
}
pub const fn next_u32(&mut self) -> u32 {
u32::from_le_bytes([self.next_u8(), self.next_u8(), self.next_u8(), self.next_u8()])
}
pub const fn next_u64(&mut self) -> u64 {
u64::from_le_bytes([
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
])
}
}
crate::items! {
impl RandTry for Xabc {
type Error = Infallible;
const RAND_OUTPUT_BITS: u32 = 8;
const RAND_STATE_BITS: u32 = 32;
const RAND_QUALITIES: RandQualities = RandQualities::WEAK_PRNG;
fn rand_try_next_u8(&mut self) -> InfallibleResult<u8> { Ok(self.next_u8()) }
fn rand_try_next_u16(&mut self) -> InfallibleResult<u16> { Ok(self.next_u16()) }
fn rand_try_next_u32(&mut self) -> InfallibleResult<u32> { Ok(self.next_u32()) }
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 Xabc {
type RandSeed = [u8; 3];
#[inline(always)]
fn rand_from_seed(seed: Self::RandSeed) -> Self { Self::new(seed) }
}
}
crate::__impl_dep_rand_core!(Xabc);