13
13
use core:: prelude:: * ;
14
14
use core:: slice;
15
15
use core:: iter:: { range_step, repeat} ;
16
+ use core:: num:: wrapping:: Wrapping ;
16
17
17
18
use { Rng , SeedableRng , Rand } ;
18
19
@@ -60,7 +61,7 @@ impl IsaacRng {
60
61
/// of `rsl` as a seed, otherwise construct one algorithmically (not
61
62
/// randomly).
62
63
fn init ( & mut self , use_rsl : bool ) {
63
- let mut a = 0x9e3779b9 ;
64
+ let mut a = Wrapping ( 0x9e3779b9 ) ;
64
65
let mut b = a;
65
66
let mut c = a;
66
67
let mut d = a;
@@ -71,14 +72,14 @@ impl IsaacRng {
71
72
72
73
macro_rules! mix {
73
74
( ) => { {
74
- a^= b<<11 ; d+= a; b+= c;
75
- b^= c>>2 ; e+= b; c+= d;
76
- c^= d<<8 ; f+= c; d+= e;
77
- d^= e>>16 ; g+= d; e+= f;
78
- e^= f<<10 ; h+= e; f+= g;
79
- f^= g>>4 ; a+= f; g+= h;
80
- g^= h<<8 ; b+= g; h+= a;
81
- h^= a>>9 ; c+= h; a+= b;
75
+ a=a^ ( b<<11 ) ; d=d+ a; b=b+ c;
76
+ b=b^ ( c>>2 ) ; e=e+ b; c=c+ d;
77
+ c=c^ ( d<<8 ) ; f=f+ c; d=d+ e;
78
+ d=d^ ( e>>16 ) ; g=g+ d; e=e+ f;
79
+ e=e^ ( f<<10 ) ; h=h+ e; f=f+ g;
80
+ f=f^ ( g>>4 ) ; a=a+ f; g=g+ h;
81
+ g=g^ ( h<<8 ) ; b=b+ g; h=h+ a;
82
+ h=h^ ( a>>9 ) ; c=c+ h; a=a+ b;
82
83
} }
83
84
}
84
85
@@ -90,15 +91,15 @@ impl IsaacRng {
90
91
macro_rules! memloop {
91
92
( $arr: expr) => { {
92
93
for i in range_step( 0 , RAND_SIZE as uint, 8 ) {
93
- a+= $arr[ i ] ; b+= $arr[ i+1 ] ;
94
- c+= $arr[ i+2 ] ; d+= $arr[ i+3 ] ;
95
- e+= $arr[ i+4 ] ; f+= $arr[ i+5 ] ;
96
- g+= $arr[ i+6 ] ; h+= $arr[ i+7 ] ;
94
+ a=a+ Wrapping ( $arr[ i ] ) ; b=b+ Wrapping ( $arr[ i+1 ] ) ;
95
+ c=c+ Wrapping ( $arr[ i+2 ] ) ; d=d+ Wrapping ( $arr[ i+3 ] ) ;
96
+ e=e+ Wrapping ( $arr[ i+4 ] ) ; f=f+ Wrapping ( $arr[ i+5 ] ) ;
97
+ g=g+ Wrapping ( $arr[ i+6 ] ) ; h=h+ Wrapping ( $arr[ i+7 ] ) ;
97
98
mix!( ) ;
98
- self . mem[ i ] =a; self . mem[ i+1 ] =b;
99
- self . mem[ i+2 ] =c; self . mem[ i+3 ] =d;
100
- self . mem[ i+4 ] =e; self . mem[ i+5 ] =f;
101
- self . mem[ i+6 ] =g; self . mem[ i+7 ] =h;
99
+ self . mem[ i ] =a. 0 ; self . mem[ i+1 ] =b. 0 ;
100
+ self . mem[ i+2 ] =c. 0 ; self . mem[ i+3 ] =d. 0 ;
101
+ self . mem[ i+4 ] =e. 0 ; self . mem[ i+5 ] =f. 0 ;
102
+ self . mem[ i+6 ] =g. 0 ; self . mem[ i+7 ] =h. 0 ;
102
103
}
103
104
} }
104
105
}
@@ -108,10 +109,10 @@ impl IsaacRng {
108
109
} else {
109
110
for i in range_step ( 0 , RAND_SIZE as uint , 8 ) {
110
111
mix ! ( ) ;
111
- self . mem [ i ] =a; self . mem [ i+1 ] =b;
112
- self . mem [ i+2 ] =c; self . mem [ i+3 ] =d;
113
- self . mem [ i+4 ] =e; self . mem [ i+5 ] =f;
114
- self . mem [ i+6 ] =g; self . mem [ i+7 ] =h;
112
+ self . mem [ i ] =a. 0 ; self . mem [ i+1 ] =b. 0 ;
113
+ self . mem [ i+2 ] =c. 0 ; self . mem [ i+3 ] =d. 0 ;
114
+ self . mem [ i+4 ] =e. 0 ; self . mem [ i+5 ] =f. 0 ;
115
+ self . mem [ i+6 ] =g. 0 ; self . mem [ i+7 ] =h. 0 ;
115
116
}
116
117
}
117
118
@@ -130,7 +131,8 @@ impl IsaacRng {
130
131
static MIDPOINT : uint = ( RAND_SIZE / 2 ) as uint ;
131
132
132
133
macro_rules! ind {
133
- ( $x: expr) => ( self . mem[ ( ( $x >> 2 ) as uint & ( ( RAND_SIZE - 1 ) as uint) ) ] )
134
+ ( $x: expr) => ( Wrapping ( self . mem[ ( ( $x >> 2 ) as uint &
135
+ ( ( RAND_SIZE - 1 ) as uint) ) ] ) )
134
136
}
135
137
136
138
let r = [ ( 0 , MIDPOINT ) , ( MIDPOINT , 0 ) ] ;
@@ -142,11 +144,11 @@ impl IsaacRng {
142
144
let mix = a << $shift as uint;
143
145
144
146
let x = self . mem[ base + mr_offset] ;
145
- a = ( a ^ mix) + self . mem[ base + m2_offset] ;
146
- let y = ind!( x) + a + b ;
147
- self . mem[ base + mr_offset] = y;
147
+ a = ( Wrapping ( a ^ mix) + Wrapping ( self . mem[ base + m2_offset] ) ) . 0 ;
148
+ let y = ind!( x) + Wrapping ( a ) + Wrapping ( b ) ;
149
+ self . mem[ base + mr_offset] = y. 0 ;
148
150
149
- b = ind!( y >> RAND_SIZE_LEN as uint) + x ;
151
+ b = ( ind!( y. 0 >> RAND_SIZE_LEN as uint) + Wrapping ( x ) ) . 0 ;
150
152
self . rsl[ base + mr_offset] = b;
151
153
} }
152
154
}
@@ -157,11 +159,11 @@ impl IsaacRng {
157
159
let mix = a >> $shift as uint;
158
160
159
161
let x = self . mem[ base + mr_offset] ;
160
- a = ( a ^ mix) + self . mem[ base + m2_offset] ;
161
- let y = ind!( x) + a + b ;
162
- self . mem[ base + mr_offset] = y;
162
+ a = ( Wrapping ( a ^ mix) + Wrapping ( self . mem[ base + m2_offset] ) ) . 0 ;
163
+ let y = ind!( x) + Wrapping ( a ) + Wrapping ( b ) ;
164
+ self . mem[ base + mr_offset] = y. 0 ;
163
165
164
- b = ind!( y >> RAND_SIZE_LEN as uint) + x ;
166
+ b = ( ind!( y. 0 >> RAND_SIZE_LEN as uint) + Wrapping ( x ) ) . 0 ;
165
167
self . rsl[ base + mr_offset] = b;
166
168
} }
167
169
}
0 commit comments