From 58e7685cd3adac2808edca26f057d21019776762 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 15 Nov 2016 08:21:04 -0700 Subject: [PATCH] Update ion, update libc --- libstd_real/libc/Cargo.toml | 1 + libstd_real/libc/src/funcs.rs | 35 ++++++++++++++++++++++++++++------- libstd_real/libc/src/lib.rs | 2 ++ programs/ion | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libstd_real/libc/Cargo.toml b/libstd_real/libc/Cargo.toml index 7137b05..26deaeb 100644 --- a/libstd_real/libc/Cargo.toml +++ b/libstd_real/libc/Cargo.toml @@ -6,3 +6,4 @@ build = "build.rs" [dependencies] redox_syscall = { path = "../../syscall/" } +spin = "*" diff --git a/libstd_real/libc/src/funcs.rs b/libstd_real/libc/src/funcs.rs index 45b1930..4bb3fed 100644 --- a/libstd_real/libc/src/funcs.rs +++ b/libstd_real/libc/src/funcs.rs @@ -8,12 +8,33 @@ pub unsafe extern fn strlen(ptr: *const c_char) -> size_t { i } +extern crate spin; +use core::cell::Cell; +use self::spin::Mutex; + +/// The randomness state. +/// +/// This is updated when a new random integer is read. +static STATE: Mutex<[u64; 2]> = Mutex::new([0xBADF00D1, 0xDEADBEEF]); + +/// Get a pseudorandom integer. +/// +/// Note that this is full-cycle, so apply a modulo when true equidistribution is needed. pub unsafe extern fn random() -> u64 { - let rand; - asm!("rdrand rax" - : "={rax}"(rand) - : - : - : "intel", "volatile"); - rand + // Fetch the state. + let mut state = STATE.lock(); + + // Store the first and second part. + let mut x = state[0]; + let y = state[1]; + + // Put the second part into the first slot. + state[0] = y; + // Twist the first slot. + x ^= x << 23; + // Update the second slot. + state[1] = x ^ y ^ (x >> 17) ^ (y >> 26); + + // Generate the final integer. + state[1].wrapping_add(y) } diff --git a/libstd_real/libc/src/lib.rs b/libstd_real/libc/src/lib.rs index a8f72bf..c4f9732 100644 --- a/libstd_real/libc/src/lib.rs +++ b/libstd_real/libc/src/lib.rs @@ -1,7 +1,9 @@ #![no_std] #![allow(non_camel_case_types)] #![feature(asm)] +#![feature(const_fn)] #![feature(naked_functions)] +#![feature(thread_local)] #![cfg_attr(stdbuild, feature(no_std, core, core_slice_ext, staged_api, custom_attribute, cfg_target_vendor))] #![cfg_attr(stdbuild, no_std)] diff --git a/programs/ion b/programs/ion index fad8108..e594412 160000 --- a/programs/ion +++ b/programs/ion @@ -1 +1 @@ -Subproject commit fad8108a164732ff63950b49ac78d76bb3834a87 +Subproject commit e594412a99c87daa619f09a673b288beecd275ca