Skip to content

Commit e069a28

Browse files
Use an 'unsigned int' as the rand_r seed.
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.
1 parent 91e0612 commit e069a28

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/rand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! Licensed under the Blue Oak Model Licence 1.0.0
44
use core::{
5-
ffi::{c_int, c_uint, c_ulong},
5+
ffi::{c_int, c_uint},
66
sync::atomic::Ordering,
77
};
88

@@ -22,7 +22,7 @@ pub extern "C" fn srand(seed: c_uint) {
2222
/// Rust implementation of C library function `rand`.
2323
#[cfg_attr(feature = "rand", no_mangle)]
2424
pub extern "C" fn rand() -> c_int {
25-
let mut state = RAND_STATE.load(Ordering::Relaxed) as c_ulong;
25+
let mut state = RAND_STATE.load(Ordering::Relaxed) as c_uint;
2626
let result = unsafe { crate::rand_r(&mut state) };
2727
RAND_STATE.store(state as u32, Ordering::Relaxed);
2828
result

src/rand_r.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Rust implementation of C library function `rand_r`
22
//!
33
//! Licensed under the Blue Oak Model Licence 1.0.0
4-
use core::ffi::{c_int, c_uint, c_ulong};
4+
use core::ffi::{c_int, c_uint};
55

66
#[cfg_attr(not(feature = "rand_r"), export_name = "tinyrlibc_RAND_MAX")]
77
#[cfg_attr(feature = "rand_r", no_mangle)]
@@ -12,20 +12,20 @@ pub static RAND_MAX: c_int = c_int::MAX;
1212
/// Passing NULL (core::ptr::null()) gives undefined behaviour.
1313
#[cfg_attr(not(feature = "rand_r"), export_name = "tinyrlibc_rand_r")]
1414
#[cfg_attr(feature = "rand_r", no_mangle)]
15-
pub unsafe extern "C" fn rand_r(seedp: *mut c_ulong) -> c_int {
16-
let mut result: c_ulong;
15+
pub unsafe extern "C" fn rand_r(seedp: *mut c_uint) -> c_int {
16+
let mut result: c_int;
1717

18-
fn pump(input: c_ulong) -> c_ulong {
18+
fn pump(input: u32) -> u32 {
1919
// This algorithm is mentioned in the ISO C standard
2020
input.wrapping_mul(1103515245).wrapping_add(12345)
2121
}
2222

23-
fn select_top(state: c_ulong, bits: usize) -> c_ulong {
23+
fn select_top(state: u32, bits: usize) -> c_int {
2424
// ignore the lower 16 bits, as they are low quality
25-
(state >> 16) & ((1 << bits) - 1)
25+
((state >> 16) & ((1 << bits) - 1)) as c_int
2626
}
2727

28-
let mut next = *seedp;
28+
let mut next = *seedp as u32;
2929
if c_int::MAX == 32767 || cfg!(feature = "rand_max_i16") {
3030
// pull 15 bits in one go
3131
next = pump(next);
@@ -39,7 +39,7 @@ pub unsafe extern "C" fn rand_r(seedp: *mut c_ulong) -> c_int {
3939
next = pump(next);
4040
result |= select_top(next, 10);
4141
}
42-
*seedp = next;
42+
*seedp = next as c_uint;
4343

4444
result as c_int
4545
}

0 commit comments

Comments
 (0)