Skip to content
Snippets Groups Projects
Unverified Commit fcf1104e authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #102 from sajattack/rand

implement rand and srand using the rand crate
parents e34d32ae 0ec7d0bc
No related branches found
No related tags found
No related merge requests found
...@@ -12,3 +12,4 @@ platform = { path = "../platform" } ...@@ -12,3 +12,4 @@ platform = { path = "../platform" }
ralloc = { path = "../../ralloc", default-features = false } ralloc = { path = "../../ralloc", default-features = false }
ctype = { path = "../ctype" } ctype = { path = "../ctype" }
errno = { path = "../errno" } errno = { path = "../errno" }
rand = { git = "https://github.com/rust-lang-nursery/rand/", default-features = false }
...@@ -8,8 +8,10 @@ extern crate ctype; ...@@ -8,8 +8,10 @@ extern crate ctype;
extern crate errno; extern crate errno;
extern crate platform; extern crate platform;
extern crate ralloc; extern crate ralloc;
extern crate rand;
use core::{ptr, str}; use core::{ptr, str};
use rand::{Rng, SeedableRng, XorShiftRng};
use errno::*; use errno::*;
use platform::types::*; use platform::types::*;
...@@ -19,8 +21,10 @@ static ALLOCATOR: ralloc::Allocator = ralloc::Allocator; ...@@ -19,8 +21,10 @@ static ALLOCATOR: ralloc::Allocator = ralloc::Allocator;
pub const EXIT_FAILURE: c_int = 1; pub const EXIT_FAILURE: c_int = 1;
pub const EXIT_SUCCESS: c_int = 0; pub const EXIT_SUCCESS: c_int = 0;
pub const RAND_MAX: c_int = 2147483647;
static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32]; static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32];
static mut RNG: Option<XorShiftRng> = None;
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long { pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long {
...@@ -375,8 +379,16 @@ pub extern "C" fn qsort( ...@@ -375,8 +379,16 @@ pub extern "C" fn qsort(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn rand() -> c_int { pub unsafe extern "C" fn rand() -> c_int {
unimplemented!(); match RNG {
Some(ref mut rng) => rng.gen_range::<c_int>(0, RAND_MAX),
None => {
let mut rng = XorShiftRng::from_seed([1; 16]);
let ret = rng.gen_range::<c_int>(0, RAND_MAX);
RNG = Some(rng);
ret
}
}
} }
#[no_mangle] #[no_mangle]
...@@ -425,8 +437,8 @@ pub extern "C" fn setstate(state: *const c_char) -> *mut c_char { ...@@ -425,8 +437,8 @@ pub extern "C" fn setstate(state: *const c_char) -> *mut c_char {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn srand(seed: c_uint) { pub unsafe extern "C" fn srand(seed: c_uint) {
unimplemented!(); RNG = Some(XorShiftRng::from_seed([seed as u8; 16]));
} }
#[no_mangle] #[no_mangle]
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
/sprintf /sprintf
/stdlib/a64l /stdlib/a64l
/stdlib/bsearch /stdlib/bsearch
/stdlib/rand
/stdlib/strtol /stdlib/strtol
/string/strchr /string/strchr
/string/strcspn /string/strcspn
......
...@@ -23,6 +23,7 @@ EXPECT_BINS=\ ...@@ -23,6 +23,7 @@ EXPECT_BINS=\
stdlib/bsearch \ stdlib/bsearch \
stdlib/strtol \ stdlib/strtol \
stdlib/a64l \ stdlib/a64l \
stdlib/rand \
string/strncmp \ string/strncmp \
string/strcspn \ string/strcspn \
string/strchr \ string/strchr \
......
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
printf("%d\n", rand());
srand(259);
printf("%d\n", rand());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment