use crate::rng32::Rng32;
use std::slice::from_raw_parts_mut;
#[repr(C)]
pub struct Pcg32 {
state: u64,
inc: u64,
}
impl Pcg32 {
pub fn new(seed: u64) -> Self {
Pcg32 {
state: seed.wrapping_add(0xDA3E39CB94B95BDB),
inc: seed | 1,
}
}
#[inline]
pub fn nextu(&mut self) -> u32 {
let oldstate = self.state;
self.state = oldstate
.wrapping_mul(6364136223846793005)
.wrapping_add(self.inc);
let xorshifted = (((oldstate >> 18) ^ oldstate) >> 27) as u32;
let rot = (oldstate >> 59) as u32;
(xorshifted >> rot) | (xorshifted << (rot.wrapping_neg() & 31))
}
#[inline]
pub fn nextf(&mut self) -> f32 {
self.nextu() as f32 * (1.0 / (u32::MAX as f32 + 1.0))
}
#[inline]
pub fn randi(&mut self, min: i32, max: i32) -> i32 {
let range = (max as i64 - min as i64 + 1) as u64;
let x = self.nextu();
((x as u64 * range) >> 32) as i32 + min
}
#[inline]
pub fn randf(&mut self, min: f32, max: f32) -> f32 {
let range = max - min;
let scale = range * (1.0 / (u32::MAX as f32 + 1.0));
(self.nextu() as f32 * scale) + min
}
#[inline]
pub fn choice<'a, T>(&mut self, choices: &'a [T]) -> &'a T {
let index = self.randi(0, choices.len() as i32 - 1);
&choices[index as usize]
}
}
impl Rng32 for Pcg32 {
#[inline]
fn randi(&mut self, min: i32, max: i32) -> i32 {
self.randi(min, max)
}
#[inline]
fn randf(&mut self, min: f32, max: f32) -> f32 {
self.randf(min, max)
}
#[inline]
fn choice<'a, T>(&'a mut self, choices: &'a [T]) -> &'a T {
self.choice(choices)
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_new(seed: u64) -> *mut Pcg32 {
Box::into_raw(Box::new(Pcg32::new(seed)))
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_free(ptr: *mut Pcg32) {
if !ptr.is_null() {
unsafe { drop(Box::from_raw(ptr)) };
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_next_u32s(ptr: *mut Pcg32, out: *mut u32, count: usize) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
for v in buffer {
*v = rng.nextu();
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_next_f32s(ptr: *mut Pcg32, out: *mut f32, count: usize) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
for v in buffer {
*v = rng.nextf();
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_rand_i32s(
ptr: *mut Pcg32,
out: *mut i32,
count: usize,
min: i32,
max: i32,
) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
for v in buffer {
*v = rng.randi(min, max);
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn pcg32_rand_f32s(
ptr: *mut Pcg32,
out: *mut f32,
count: usize,
min: f32,
max: f32,
) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
for v in buffer {
*v = rng.randf(min, max);
}
}
}
#[cfg(test)]
mod tests {
use super::Pcg32;
#[test]
fn it_works() {
let mut rng = Pcg32::new(1);
assert_eq!(rng.nextu(), 4164751464);
assert_eq!(rng.nextf(), 0.9784193);
assert_eq!(rng.randi(10, 20), 17);
assert_eq!(rng.randf(10.0, 20.0), 12.517176);
assert_eq!(*rng.choice(&[0, 1, 2, 3, 4]), 1);
assert_eq!(*rng.choice(&[1, 2, 3, 4, 5]), 5);
assert_eq!(*rng.choice(&[2, 3, 4, 5, 6]), 2);
assert_eq!(*rng.choice(&[3, 4, 5, 6, 7]), 7);
}
}