Rollup merge of #23105 - alexcrichton:rand-wrapping, r=brson

There was a buildbot failure recently of an arithmetic overflow in the isaac
module of the rand crate, so I've imported the isaac implementation from
out-of-tree which makes somewhat more liberal usage of the wrapping primitives.
Hopefull this quelches any future overflow!
This commit is contained in:
Manish Goregaokar 2015-03-10 14:59:13 +05:30
commit 7206fec9c3

View File

@ -10,16 +10,21 @@
//! The ISAAC random number generator.
#![allow(non_camel_case_types)]
use core::prelude::*;
use core::slice;
use core::iter::{range_step, repeat};
use core::num::wrapping::Wrapping;
use core::num::wrapping::Wrapping as w;
use {Rng, SeedableRng, Rand};
const RAND_SIZE_LEN: u32 = 8;
const RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
type w32 = w<u32>;
type w64 = w<u64>;
const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
const RAND_SIZE_USIZE: usize = 1 << RAND_SIZE_LEN;
/// A random number generator that uses the ISAAC algorithm[1].
///
@ -33,18 +38,18 @@ const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
#[derive(Copy)]
pub struct IsaacRng {
cnt: u32,
rsl: [u32; RAND_SIZE_UINT],
mem: [u32; RAND_SIZE_UINT],
a: u32,
b: u32,
c: u32
rsl: [w32; RAND_SIZE_USIZE],
mem: [w32; RAND_SIZE_USIZE],
a: w32,
b: w32,
c: w32,
}
static EMPTY: IsaacRng = IsaacRng {
cnt: 0,
rsl: [0; RAND_SIZE_UINT],
mem: [0; RAND_SIZE_UINT],
a: 0, b: 0, c: 0
rsl: [w(0); RAND_SIZE_USIZE],
mem: [w(0); RAND_SIZE_USIZE],
a: w(0), b: w(0), c: w(0),
};
impl IsaacRng {
@ -61,7 +66,7 @@ impl IsaacRng {
/// of `rsl` as a seed, otherwise construct one algorithmically (not
/// randomly).
fn init(&mut self, use_rsl: bool) {
let mut a = Wrapping(0x9e3779b9);
let mut a = w(0x9e3779b9);
let mut b = a;
let mut c = a;
let mut d = a;
@ -90,16 +95,16 @@ impl IsaacRng {
if use_rsl {
macro_rules! memloop {
($arr:expr) => {{
for i in range_step(0, RAND_SIZE as uint, 8) {
a=a+Wrapping($arr[i ]); b=b+Wrapping($arr[i+1]);
c=c+Wrapping($arr[i+2]); d=d+Wrapping($arr[i+3]);
e=e+Wrapping($arr[i+4]); f=f+Wrapping($arr[i+5]);
g=g+Wrapping($arr[i+6]); h=h+Wrapping($arr[i+7]);
for i in range_step(0, RAND_SIZE_USIZE, 8) {
a=a+$arr[i ]; b=b+$arr[i+1];
c=c+$arr[i+2]; d=d+$arr[i+3];
e=e+$arr[i+4]; f=f+$arr[i+5];
g=g+$arr[i+6]; h=h+$arr[i+7];
mix!();
self.mem[i ]=a.0; self.mem[i+1]=b.0;
self.mem[i+2]=c.0; self.mem[i+3]=d.0;
self.mem[i+4]=e.0; self.mem[i+5]=f.0;
self.mem[i+6]=g.0; self.mem[i+7]=h.0;
self.mem[i ]=a; self.mem[i+1]=b;
self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h;
}
}}
}
@ -107,12 +112,12 @@ impl IsaacRng {
memloop!(self.rsl);
memloop!(self.mem);
} else {
for i in range_step(0, RAND_SIZE as uint, 8) {
for i in range_step(0, RAND_SIZE_USIZE, 8) {
mix!();
self.mem[i ]=a.0; self.mem[i+1]=b.0;
self.mem[i+2]=c.0; self.mem[i+3]=d.0;
self.mem[i+4]=e.0; self.mem[i+5]=f.0;
self.mem[i+6]=g.0; self.mem[i+7]=h.0;
self.mem[i ]=a; self.mem[i+1]=b;
self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h;
}
}
@ -123,32 +128,31 @@ impl IsaacRng {
#[inline]
#[allow(unsigned_negation)]
fn isaac(&mut self) {
self.c += 1;
self.c = self.c + w(1);
// abbreviations
let mut a = self.a;
let mut b = self.b + self.c;
const MIDPOINT: uint = (RAND_SIZE / 2) as uint;
const MIDPOINT: usize = RAND_SIZE_USIZE / 2;
macro_rules! ind {
($x:expr) => (Wrapping( self.mem[(($x >> 2) as uint &
((RAND_SIZE - 1) as uint))] ))
($x:expr) => ( self.mem[($x >> 2).0 as usize & (RAND_SIZE_USIZE - 1)] )
}
let r = [(0, MIDPOINT), (MIDPOINT, 0)];
for &(mr_offset, m2_offset) in &r {
for &(mr_offset, m2_offset) in r.iter() {
macro_rules! rngstepp {
($j:expr, $shift:expr) => {{
let base = $j;
let mix = a << $shift as uint;
let mix = a << $shift;
let x = self.mem[base + mr_offset];
a = (Wrapping(a ^ mix) + Wrapping(self.mem[base + m2_offset])).0;
let y = ind!(x) + Wrapping(a) + Wrapping(b);
self.mem[base + mr_offset] = y.0;
a = (a ^ mix) + self.mem[base + m2_offset];
let y = ind!(x) + a + b;
self.mem[base + mr_offset] = y;
b = (ind!(y.0 >> RAND_SIZE_LEN as uint) + Wrapping(x)).0;
b = ind!(y >> RAND_SIZE_LEN) + x;
self.rsl[base + mr_offset] = b;
}}
}
@ -156,14 +160,14 @@ impl IsaacRng {
macro_rules! rngstepn {
($j:expr, $shift:expr) => {{
let base = $j;
let mix = a >> $shift as uint;
let mix = a >> $shift;
let x = self.mem[base + mr_offset];
a = (Wrapping(a ^ mix) + Wrapping(self.mem[base + m2_offset])).0;
let y = ind!(x) + Wrapping(a) + Wrapping(b);
self.mem[base + mr_offset] = y.0;
a = (a ^ mix) + self.mem[base + m2_offset];
let y = ind!(x) + a + b;
self.mem[base + mr_offset] = y;
b = (ind!(y.0 >> RAND_SIZE_LEN as uint) + Wrapping(x)).0;
b = ind!(y >> RAND_SIZE_LEN) + x;
self.rsl[base + mr_offset] = b;
}}
}
@ -209,7 +213,7 @@ impl Rng for IsaacRng {
// (the % is cheaply telling the optimiser that we're always
// in bounds, without unsafe. NB. this is a power of two, so
// it optimises to a bitwise mask).
self.rsl[(self.cnt % RAND_SIZE) as uint]
self.rsl[(self.cnt % RAND_SIZE) as usize].0
}
}
@ -220,12 +224,12 @@ impl<'a> SeedableRng<&'a [u32]> for IsaacRng {
let seed_iter = seed.iter().cloned().chain(repeat(0));
for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
*rsl_elem = seed_elem;
*rsl_elem = w(seed_elem);
}
self.cnt = 0;
self.a = 0;
self.b = 0;
self.c = 0;
self.a = w(0);
self.b = w(0);
self.c = w(0);
self.init(true);
}
@ -248,21 +252,21 @@ impl Rand for IsaacRng {
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
let slice = slice::from_raw_parts_mut(ptr, (RAND_SIZE * 4) as uint);
let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE_USIZE * 4);
other.fill_bytes(slice);
}
ret.cnt = 0;
ret.a = 0;
ret.b = 0;
ret.c = 0;
ret.a = w(0);
ret.b = w(0);
ret.c = w(0);
ret.init(true);
return ret;
}
}
const RAND_SIZE_64_LEN: uint = 8;
const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
const RAND_SIZE_64_LEN: usize = 8;
const RAND_SIZE_64: usize = 1 << RAND_SIZE_64_LEN;
/// A random number generator that uses ISAAC-64[1], the 64-bit
/// variant of the ISAAC algorithm.
@ -276,19 +280,19 @@ const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
#[derive(Copy)]
pub struct Isaac64Rng {
cnt: uint,
rsl: [u64; RAND_SIZE_64],
mem: [u64; RAND_SIZE_64],
a: u64,
b: u64,
c: u64,
cnt: usize,
rsl: [w64; RAND_SIZE_64],
mem: [w64; RAND_SIZE_64],
a: w64,
b: w64,
c: w64,
}
static EMPTY_64: Isaac64Rng = Isaac64Rng {
cnt: 0,
rsl: [0; RAND_SIZE_64],
mem: [0; RAND_SIZE_64],
a: 0, b: 0, c: 0,
rsl: [w(0); RAND_SIZE_64],
mem: [w(0); RAND_SIZE_64],
a: w(0), b: w(0), c: w(0),
};
impl Isaac64Rng {
@ -306,7 +310,7 @@ impl Isaac64Rng {
fn init(&mut self, use_rsl: bool) {
macro_rules! init {
($var:ident) => (
let mut $var = Wrapping(0x9e3779b97f4a7c13);
let mut $var = w(0x9e3779b97f4a7c13);
)
}
init!(a); init!(b); init!(c); init!(d);
@ -314,14 +318,14 @@ impl Isaac64Rng {
macro_rules! mix {
() => {{
a=a-e; f=f^h>>9; h=h+a;
b=b-f; g=g^a<<9; a=a+b;
c=c-g; h=h^b>>23; b=b+c;
d=d-h; a=a^c<<15; c=c+d;
e=e-a; b=b^d>>14; d=d+e;
f=f-b; c=c^e<<20; e=e+f;
g=g-c; d=d^f>>17; f=f+g;
h=h-d; e=e^g<<14; g=g+h;
a=a-e; f=f^(h>>9); h=h+a;
b=b-f; g=g^(a<<9); a=a+b;
c=c-g; h=h^(b>>23); b=b+c;
d=d-h; a=a^(c<<15); c=c+d;
e=e-a; b=b^(d>>14); d=d+e;
f=f-b; c=c^(e<<20); e=e+f;
g=g-c; d=d^(f>>17); f=f+g;
h=h-d; e=e^(g<<14); g=g+h;
}}
}
@ -333,15 +337,15 @@ impl Isaac64Rng {
macro_rules! memloop {
($arr:expr) => {{
for i in (0..RAND_SIZE_64 / 8).map(|i| i * 8) {
a=a+Wrapping($arr[i ]); b=b+Wrapping($arr[i+1]);
c=c+Wrapping($arr[i+2]); d=d+Wrapping($arr[i+3]);
e=e+Wrapping($arr[i+4]); f=f+Wrapping($arr[i+5]);
g=g+Wrapping($arr[i+6]); h=h+Wrapping($arr[i+7]);
a=a+$arr[i ]; b=b+$arr[i+1];
c=c+$arr[i+2]; d=d+$arr[i+3];
e=e+$arr[i+4]; f=f+$arr[i+5];
g=g+$arr[i+6]; h=h+$arr[i+7];
mix!();
self.mem[i ]=a.0; self.mem[i+1]=b.0;
self.mem[i+2]=c.0; self.mem[i+3]=d.0;
self.mem[i+4]=e.0; self.mem[i+5]=f.0;
self.mem[i+6]=g.0; self.mem[i+7]=h.0;
self.mem[i ]=a; self.mem[i+1]=b;
self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h;
}
}}
}
@ -351,10 +355,10 @@ impl Isaac64Rng {
} else {
for i in (0..RAND_SIZE_64 / 8).map(|i| i * 8) {
mix!();
self.mem[i ]=a.0; self.mem[i+1]=b.0;
self.mem[i+2]=c.0; self.mem[i+3]=d.0;
self.mem[i+4]=e.0; self.mem[i+5]=f.0;
self.mem[i+6]=g.0; self.mem[i+7]=h.0;
self.mem[i ]=a; self.mem[i+1]=b;
self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h;
}
}
@ -363,35 +367,35 @@ impl Isaac64Rng {
/// Refills the output buffer (`self.rsl`)
fn isaac64(&mut self) {
self.c += 1;
self.c = self.c + w(1);
// abbreviations
let mut a = Wrapping(self.a);
let mut b = Wrapping(self.b) + Wrapping(self.c);
const MIDPOINT: uint = RAND_SIZE_64 / 2;
const MP_VEC: [(uint, uint); 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
let mut a = self.a;
let mut b = self.b + self.c;
const MIDPOINT: usize = RAND_SIZE_64 / 2;
const MP_VEC: [(usize, usize); 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
macro_rules! ind {
($x:expr) => {
*self.mem.get_unchecked(($x as uint >> 3) & (RAND_SIZE_64 - 1))
*self.mem.get_unchecked((($x >> 3).0 as usize) & (RAND_SIZE_64 - 1))
}
}
for &(mr_offset, m2_offset) in &MP_VEC {
for &(mr_offset, m2_offset) in MP_VEC.iter() {
for base in (0..MIDPOINT / 4).map(|i| i * 4) {
macro_rules! rngstepp {
($j:expr, $shift:expr) => {{
let base = base + $j;
let mix = a ^ (a << $shift as uint);
let mix = a ^ (a << $shift);
let mix = if $j == 0 {!mix} else {mix};
unsafe {
let x = Wrapping(*self.mem.get_unchecked(base + mr_offset));
a = mix + Wrapping(*self.mem.get_unchecked(base + m2_offset));
let y = Wrapping(ind!(x.0)) + a + b;
*self.mem.get_unchecked_mut(base + mr_offset) = y.0;
let x = *self.mem.get_unchecked(base + mr_offset);
a = mix + *self.mem.get_unchecked(base + m2_offset);
let y = ind!(x) + a + b;
*self.mem.get_unchecked_mut(base + mr_offset) = y;
b = Wrapping(ind!(y.0 >> RAND_SIZE_64_LEN)) + x;
*self.rsl.get_unchecked_mut(base + mr_offset) = b.0;
b = ind!(y >> RAND_SIZE_64_LEN) + x;
*self.rsl.get_unchecked_mut(base + mr_offset) = b;
}
}}
}
@ -399,17 +403,17 @@ impl Isaac64Rng {
macro_rules! rngstepn {
($j:expr, $shift:expr) => {{
let base = base + $j;
let mix = a ^ (a >> $shift as uint);
let mix = a ^ (a >> $shift);
let mix = if $j == 0 {!mix} else {mix};
unsafe {
let x = Wrapping(*self.mem.get_unchecked(base + mr_offset));
a = mix + Wrapping(*self.mem.get_unchecked(base + m2_offset));
let y = Wrapping(ind!(x.0)) + a + b;
*self.mem.get_unchecked_mut(base + mr_offset) = y.0;
let x = *self.mem.get_unchecked(base + mr_offset);
a = mix + *self.mem.get_unchecked(base + m2_offset);
let y = ind!(x) + a + b;
*self.mem.get_unchecked_mut(base + mr_offset) = y;
b = Wrapping(ind!(y.0 >> RAND_SIZE_64_LEN)) + x;
*self.rsl.get_unchecked_mut(base + mr_offset) = b.0;
b = ind!(y >> RAND_SIZE_64_LEN) + x;
*self.rsl.get_unchecked_mut(base + mr_offset) = b;
}
}}
}
@ -421,8 +425,8 @@ impl Isaac64Rng {
}
}
self.a = a.0;
self.b = b.0;
self.a = a;
self.b = b;
self.cnt = RAND_SIZE_64;
}
}
@ -452,7 +456,7 @@ impl Rng for Isaac64Rng {
// See corresponding location in IsaacRng.next_u32 for
// explanation.
debug_assert!(self.cnt < RAND_SIZE_64);
self.rsl[(self.cnt % RAND_SIZE_64) as uint]
self.rsl[(self.cnt % RAND_SIZE_64) as usize].0
}
}
@ -463,12 +467,12 @@ impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
let seed_iter = seed.iter().cloned().chain(repeat(0));
for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
*rsl_elem = seed_elem;
*rsl_elem = w(seed_elem);
}
self.cnt = 0;
self.a = 0;
self.b = 0;
self.c = 0;
self.a = w(0);
self.b = w(0);
self.c = w(0);
self.init(true);
}
@ -491,13 +495,13 @@ impl Rand for Isaac64Rng {
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
let slice = slice::from_raw_parts_mut(ptr, (RAND_SIZE_64 * 8) as uint);
let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE_64 * 8);
other.fill_bytes(slice);
}
ret.cnt = 0;
ret.a = 0;
ret.b = 0;
ret.c = 0;
ret.a = w(0);
ret.b = w(0);
ret.c = w(0);
ret.init(true);
return ret;
@ -516,16 +520,16 @@ mod test {
#[test]
fn test_rng_32_rand_seeded() {
let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
let mut ra: IsaacRng = SeedableRng::from_seed(&*s);
let mut rb: IsaacRng = SeedableRng::from_seed(&*s);
let mut ra: IsaacRng = SeedableRng::from_seed(&s[..]);
let mut rb: IsaacRng = SeedableRng::from_seed(&s[..]);
assert!(order::equals(ra.gen_ascii_chars().take(100),
rb.gen_ascii_chars().take(100)));
}
#[test]
fn test_rng_64_rand_seeded() {
let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
let mut ra: Isaac64Rng = SeedableRng::from_seed(&*s);
let mut rb: Isaac64Rng = SeedableRng::from_seed(&*s);
let mut ra: Isaac64Rng = SeedableRng::from_seed(&s[..]);
let mut rb: Isaac64Rng = SeedableRng::from_seed(&s[..]);
assert!(order::equals(ra.gen_ascii_chars().take(100),
rb.gen_ascii_chars().take(100)));
}
@ -550,7 +554,7 @@ mod test {
#[test]
fn test_rng_32_reseed() {
let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
let mut r: IsaacRng = SeedableRng::from_seed(&*s);
let mut r: IsaacRng = SeedableRng::from_seed(&s[..]);
let string1: String = r.gen_ascii_chars().take(100).collect();
r.reseed(&s);
@ -561,7 +565,7 @@ mod test {
#[test]
fn test_rng_64_reseed() {
let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
let mut r: Isaac64Rng = SeedableRng::from_seed(&*s);
let mut r: Isaac64Rng = SeedableRng::from_seed(&s[..]);
let string1: String = r.gen_ascii_chars().take(100).collect();
r.reseed(&s);