Implement lcong48() and seed48()

This commit is contained in:
Peter Limkilde Svendsen
2019-05-23 20:36:13 +02:00
parent 767cf86b38
commit b2a9cdf930
4 changed files with 114 additions and 15 deletions
+14 -2
View File
@@ -7,10 +7,16 @@ use platform::types::*;
* set. */
pub static mut XI: u64 = 0;
// Used by seed48() (returns a pointer to this array).
pub static mut STASHED_XI: [c_ushort; 3] = [0; 3];
/* Multiplier and addend, which may be set through lcong48(). Default values as
* specified in POSIX. */
pub static mut A: u64 = 0x5deece66d;
pub static mut C: u16 = 0xb;
const A_DEFAULT: u64 = 0x5deece66d;
const C_DEFAULT: u16 = 0xb;
pub static mut A: u64 = A_DEFAULT;
pub static mut C: u16 = C_DEFAULT;
/// Gets the next element in the linear congruential generator's
/// sequence.
@@ -61,3 +67,9 @@ pub unsafe fn set_ushort_arr3_from_uint48(arr_ptr: *mut c_ushort, value: u64) {
*arr_ptr.offset(1) = c_ushort::from((value >> 16) as u16);
*arr_ptr.offset(2) = c_ushort::from((value >> 32) as u16);
}
/// Used by `srand48()` and `seed48()`.
pub unsafe fn reset_a_and_c() {
A = A_DEFAULT;
C = C_DEFAULT;
}
+20 -7
View File
@@ -382,9 +382,12 @@ pub extern "C" fn labs(i: c_long) -> c_long {
i.abs()
}
// #[no_mangle]
pub extern "C" fn lcong48(param: [c_ushort; 7]) {
unimplemented!();
#[no_mangle]
pub unsafe extern "C" fn lcong48(param: *mut c_ushort) {
// Input should be a size-7 array.
lcg48::XI = lcg48::ushort_arr3_to_uint48(param.offset(0));
lcg48::A = lcg48::ushort_arr3_to_uint48(param.offset(3));
lcg48::C = *param.offset(6) as u16; // c_ushort may be more than 16 bits
}
#[repr(C)]
@@ -703,9 +706,17 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char
ptr
}
// #[no_mangle]
pub extern "C" fn seed48(seed16v: [c_ushort; 3]) -> c_ushort {
unimplemented!();
#[no_mangle]
pub unsafe extern "C" fn seed48(seed16v: *mut c_ushort) -> *mut c_ushort {
lcg48::reset_a_and_c();
//lcg48::STASHED_XI = lcg48::XI;
//lcg48::set_ushort_arr3_from_uint48(&mut lcg48::STASHED_XI[0] as *mut c_ushort, lcg48::XI);
lcg48::set_ushort_arr3_from_uint48(lcg48::STASHED_XI.as_mut_ptr(), lcg48::XI);
lcg48::XI = lcg48::ushort_arr3_to_uint48(seed16v);
//&mut lcg48::STASHED_XI[0]
lcg48::STASHED_XI.as_mut_ptr()
}
#[no_mangle]
@@ -792,10 +803,12 @@ 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. */
lcg48::XI = (((seedval & 0xffff_ffff) as u64) << 16) | 0x330e_u64;
lcg48::XI = (((seedval as u32) as u64) << 16) | 0x330e_u64;
}
// #[no_mangle]