use crate::rng64::Rng64;
use std::slice::from_raw_parts_mut;
#[repr(C)]
pub struct Philox64 {
c: [u64; 2],
k: [u64; 2],
}
impl Philox64 {
const fn chunk_size() -> usize {
2
}
const fn m0() -> u64 {
0xD2B74407B1CE6E93
}
pub fn new(seed: [u64; 2]) -> Self {
Self { c: [1, 0], k: seed }
}
pub fn warm(&mut self, count: usize) {
for _i in 0..count {
let _ = self.nextu();
}
}
#[inline]
pub fn nextu(&mut self) -> [u64; 2] {
let mut out = [0u64; 2];
let prod = (self.c[0] as u128).wrapping_mul(Self::m0() as u128);
out[0] = (prod as u64) ^ self.c[1] ^ self.k[0];
out[1] = (prod >> 64) as u64;
self.c[0] = self.c[0].wrapping_add(1);
self.c[1] = self.c[1].wrapping_add(1);
out
}
#[inline]
pub fn nextf(&mut self) -> f64 {
self.nextu()[0] as f64 * (1.0 / (u64::MAX as f64 + 1.0))
}
#[inline]
pub fn randi(&mut self, min: i64, max: i64) -> i64 {
let range = (max as i128 - min as i128 + 1) as u128;
let x = self.nextu()[0];
((x as u128 * range) >> 64) as i64 + min
}
#[inline]
pub fn randf(&mut self, min: f64, max: f64) -> f64 {
self.nextf() * (max - min) + min
}
#[inline]
pub fn choice<'a, T>(&mut self, choices: &'a [T]) -> &'a T {
let index = self.randi(0, choices.len() as i64 - 1) as usize;
&choices[index]
}
}
impl Rng64 for Philox64 {
#[inline]
fn randi(&mut self, min: i64, max: i64) -> i64 {
self.randi(min, max)
}
#[inline]
fn randf(&mut self, min: f64, max: f64) -> f64 {
self.randf(min, max)
}
#[inline]
fn choice<'a, T>(&mut self, choices: &'a [T]) -> &'a T {
self.choice(choices)
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_new(seed1: u64, seed2: u64) -> *mut Philox64 {
Box::into_raw(Box::new(Philox64::new([seed1, seed2])))
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_warm(ptr: *mut Philox64, count: usize) {
unsafe {
let rng = &mut *ptr;
rng.warm(count);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_free(ptr: *mut Philox64) {
if !ptr.is_null() {
unsafe { drop(Box::from_raw(ptr)) }
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_next_u64s(ptr: *mut Philox64, out: *mut u64, count: usize) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
let mut i = 0;
while i < count {
let chunk = rng.nextu();
let take = (count - i).min(Philox64::chunk_size());
buffer[i..i + take].copy_from_slice(&chunk[..take]);
i += take;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_next_f64s(ptr: *mut Philox64, out: *mut f64, count: usize) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
let mut i = 0;
while i < count {
let chunk = rng.nextu();
let take = (count - i).min(Philox64::chunk_size());
for j in 0..take {
buffer[i + j] = chunk[j] as f64 * (1.0 / (u64::MAX as f64 + 1.0));
}
i += take;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_rand_i64s(
ptr: *mut Philox64,
out: *mut i64,
count: usize,
min: i64,
max: i64,
) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
let mut i = 0;
while i < count {
let chunk = rng.nextu();
let take = (count - i).min(Philox64::chunk_size());
for j in 0..take {
let range = (max as i128 - min as i128 + 1) as u128;
buffer[i + j] = ((chunk[j] as u128 * range) >> 64) as i64 + min;
}
i += take;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn philox64_rand_f64s(
ptr: *mut Philox64,
out: *mut f64,
count: usize,
min: f64,
max: f64,
) {
unsafe {
let rng = &mut *ptr;
let buffer = from_raw_parts_mut(out, count);
let mut i = 0;
while i < count {
let chunk = rng.nextu();
let take = (count - i).min(Philox64::chunk_size());
for j in 0..take {
let val_01 = chunk[j] as f64 * (1.0 / (u64::MAX as f64 + 1.0));
buffer[i + j] = val_01 * (max - min) + min;
}
i += take;
}
}
}
#[cfg(test)]
mod tests {
use super::Philox64;
#[test]
fn it_works() {
let mut rng = Philox64::new([1, 2]);
assert_eq!(rng.nextu(), [15183679468541472402, 0]);
assert_eq!(rng.nextf(), 0.6462178266116214);
assert_eq!(rng.randi(10, 20), 15);
assert_eq!(rng.randf(10.0, 20.0), 12.924356532232428);
assert_eq!(*rng.choice(&[0, 1, 2, 3, 4]), 0);
assert_eq!(*rng.choice(&[1, 2, 3, 4, 5]), 5);
assert_eq!(*rng.choice(&[2, 3, 4, 5, 6]), 5);
assert_eq!(*rng.choice(&[3, 4, 5, 6, 7]), 5);
}
}