Skip to content

Commit

Permalink
Use an 'unsigned int' as the rand_r seed.
Browse files Browse the repository at this point in the history
This then matches the stdlib.h declaration.

Looking at the numbers generated, having that be a 16-bit value doesn't
appear to affect the quality of the output significantly. It's certainly
good enough for typical use cases.
  • Loading branch information
jonathanpallant committed Nov 14, 2024
1 parent daa172e commit 4c01b21
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/rand_r.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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;

Check warning on line 28 in src/rand_r.rs

View workflow job for this annotation

GitHub Actions / clippy

casting to the same type is unnecessary (`u32` -> `u32`)

warning: casting to the same type is unnecessary (`u32` -> `u32`) --> src/rand_r.rs:28:17 | 28 | let mut next = *seedp as u32; | ^^^^^^^^^^^^^ help: try: `*seedp` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default
if c_int::MAX == 32767 || cfg!(feature = "rand_max_i16") {
// pull 15 bits in one go
next = pump(next);
Expand All @@ -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
}
Expand Down

0 comments on commit 4c01b21

Please sign in to comment.