This commit is contained in:
Peter Limkilde Svendsen
2019-05-23 21:46:11 +02:00
parent dcff3fd836
commit 7a48107080
2 changed files with 11 additions and 11 deletions
+5 -5
View File
@@ -26,13 +26,13 @@ pub unsafe fn reset_a_and_c() {
}
/// Build a 48-bit integer from a size-3 array of unsigned short.
///
///
/// Takes a pointer argument due to the inappropriate C function
/// signatures generated from Rust's sized arrays, see
/// https://github.com/eqrion/cbindgen/issues/171
pub unsafe fn uint48_from_ushort_arr3(arr_ptr: *const c_ushort) -> u64 {
let arr = [*arr_ptr.offset(0), *arr_ptr.offset(1), *arr_ptr.offset(2)];
/* Cast via u16 to ensure we get only the lower 16 bits of each
* element, as specified by POSIX. */
u64::from(arr[0] as u16) | (u64::from(arr[1] as u16) << 16) | (u64::from(arr[2] as u16) << 32)
@@ -47,18 +47,18 @@ pub unsafe fn set_ushort_arr3_from_uint48(arr_ptr: *mut c_ushort, value: u64) {
/// Advances the buffer from the input argument to the next element in
/// the linear congruential generator's sequence.
///
///
/// Modifies the passed argument in-place and returns the new value as a
/// u64. The input argument must be a size-3 array.
pub unsafe fn generator_step(xi_arr_ptr: *mut c_ushort) -> u64 {
let old_xi: u64 = uint48_from_ushort_arr3(xi_arr_ptr);
/* The recurrence relation of the linear congruential generator,
* X_(n+1) = (a * X_n + c) % m,
* with m = 2**48. The multiplication and addition can overflow a
* u64, but we just let it wrap since we take mod 2**48 anyway. */
let new_xi: u64 = A.wrapping_mul(old_xi).wrapping_add(u64::from(C)) & 0xffff_ffff_ffff;
set_ushort_arr3_from_uint48(xi_arr_ptr, new_xi);
new_xi
}
+6 -6
View File
@@ -1,7 +1,7 @@
//! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html
use core::{intrinsics, iter, mem, ptr, slice};
use core::convert::TryFrom;
use core::{intrinsics, iter, mem, ptr, slice};
use rand::distributions::{Alphanumeric, Distribution, Uniform};
use rand::prng::XorShiftRng;
use rand::rngs::JitterRng;
@@ -381,7 +381,7 @@ pub extern "C" fn labs(i: c_long) -> c_long {
#[no_mangle]
pub unsafe extern "C" fn lcong48(param: *mut c_ushort) {
// Input should be a size-7 array.
/* Go through this ptr -> u64 -> ptr conversion to ensure we only
* get the lower 16 bits of each element. */
let new_xi = lcg48::uint48_from_ushort_arr3(param.offset(0));
@@ -707,12 +707,12 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char
#[no_mangle]
pub unsafe extern "C" fn seed48(seed16v: *mut c_ushort) -> *mut c_ushort {
lcg48::reset_a_and_c();
lcg48::STASHED_XI = lcg48::DEFAULT_XI;
let new_xi = lcg48::uint48_from_ushort_arr3(seed16v);
lcg48::set_ushort_arr3_from_uint48(lcg48::DEFAULT_XI.as_mut_ptr(), new_xi);
lcg48::STASHED_XI.as_mut_ptr()
}
@@ -801,7 +801,7 @@ pub unsafe extern "C" fn srand(seed: c_uint) {
#[no_mangle]
pub unsafe extern "C" fn srand48(seedval: c_long) {
lcg48::reset_a_and_c();
/* Set the high 32 bits of the 48-bit X_i value to the lower 32 bits
* of the input argument, and the lower 16 bits to 0x330e, as
* specified in POSIX. */