diff --git a/src/rand.rs b/src/rand.rs index e018af4..0e89d60 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -2,7 +2,7 @@ //! //! Licensed under the Blue Oak Model Licence 1.0.0 use core::{ - ffi::{c_int, c_uint, c_ulong}, + ffi::{c_int, c_uint}, sync::atomic::Ordering, }; @@ -22,7 +22,7 @@ pub extern "C" fn srand(seed: c_uint) { /// Rust implementation of C library function `rand`. #[cfg_attr(feature = "rand", no_mangle)] pub extern "C" fn rand() -> c_int { - let mut state = RAND_STATE.load(Ordering::Relaxed) as c_ulong; + let mut state = RAND_STATE.load(Ordering::Relaxed) as c_uint; let result = unsafe { crate::rand_r(&mut state) }; RAND_STATE.store(state as u32, Ordering::Relaxed); result diff --git a/src/rand_r.rs b/src/rand_r.rs index a31143b..bed95d9 100644 --- a/src/rand_r.rs +++ b/src/rand_r.rs @@ -1,7 +1,7 @@ //! Rust implementation of C library function `rand_r` //! //! Licensed under the Blue Oak Model Licence 1.0.0 -use core::ffi::{c_int, c_uint, c_ulong}; +use core::ffi::{c_int, c_uint}; #[cfg_attr(not(feature = "rand_r"), export_name = "tinyrlibc_RAND_MAX")] #[cfg_attr(feature = "rand_r", no_mangle)] @@ -12,20 +12,20 @@ pub static RAND_MAX: c_int = c_int::MAX; /// Passing NULL (core::ptr::null()) gives undefined behaviour. #[cfg_attr(not(feature = "rand_r"), export_name = "tinyrlibc_rand_r")] #[cfg_attr(feature = "rand_r", no_mangle)] -pub unsafe extern "C" fn rand_r(seedp: *mut c_ulong) -> c_int { - let mut result: c_ulong; +pub unsafe extern "C" fn rand_r(seedp: *mut c_uint) -> c_int { + let mut result: c_int; - fn pump(input: c_ulong) -> c_ulong { + fn pump(input: u32) -> u32 { // This algorithm is mentioned in the ISO C standard input.wrapping_mul(1103515245).wrapping_add(12345) } - fn select_top(state: c_ulong, bits: usize) -> c_ulong { + fn select_top(state: u32, bits: usize) -> c_int { // ignore the lower 16 bits, as they are low quality - (state >> 16) & ((1 << bits) - 1) + ((state >> 16) & ((1 << bits) - 1)) as c_int } - let mut next = *seedp; + let mut next = *seedp as u32; if c_int::MAX == 32767 || cfg!(feature = "rand_max_i16") { // pull 15 bits in one go next = pump(next); @@ -39,7 +39,7 @@ pub unsafe extern "C" fn rand_r(seedp: *mut c_ulong) -> c_int { next = pump(next); result |= select_top(next, 10); } - *seedp = next; + *seedp = next as c_uint; result as c_int }