|
1 // |jit-test| slow; |
|
2 |
|
3 function randomRecursion() { |
|
4 var y = "" |
|
5 if (rnd(2)) { |
|
6 var x = 2; |
|
7 "{" + x + "}"; |
|
8 randomRecursion(); |
|
9 randomRecursion(); |
|
10 return [""]; |
|
11 } |
|
12 return [""]; |
|
13 } |
|
14 |
|
15 function thisFunctionIsNeverCalled() { |
|
16 } |
|
17 |
|
18 function testOne() { |
|
19 ox = newGlobal(); |
|
20 var code = randomRecursion()[rnd(3)]; |
|
21 } |
|
22 |
|
23 initRnd(); |
|
24 gczeal(10, 3); |
|
25 |
|
26 for (var count = 0; count < 20; count++) { |
|
27 print(count); |
|
28 testOne() |
|
29 } |
|
30 |
|
31 // ========================================================================================== |
|
32 |
|
33 // this program is a JavaScript version of Mersenne Twister, with concealment and encapsulation in class, |
|
34 // an almost straight conversion from the original program, mt19937ar.c, |
|
35 // translated by y. okada on July 17, 2006. |
|
36 // Changes by Jesse Ruderman: added "var" keyword in a few spots; added export_mta etc; pasted into fuzz.js. |
|
37 // in this program, procedure descriptions and comments of original source code were not removed. |
|
38 // lines commented with //c// were originally descriptions of c procedure. and a few following lines are appropriate JavaScript descriptions. |
|
39 // lines commented with /* and */ are original comments. |
|
40 // lines commented with // are additional comments in this JavaScript version. |
|
41 // before using this version, create at least one instance of MersenneTwister19937 class, and initialize the each state, given below in c comments, of all the instances. |
|
42 /* |
|
43 A C-program for MT19937, with initialization improved 2002/1/26. |
|
44 Coded by Takuji Nishimura and Makoto Matsumoto. |
|
45 |
|
46 Before using, initialize the state by using init_genrand(seed) |
|
47 or init_by_array(init_key, key_length). |
|
48 |
|
49 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
|
50 All rights reserved. |
|
51 |
|
52 Redistribution and use in source and binary forms, with or without |
|
53 modification, are permitted provided that the following conditions |
|
54 are met: |
|
55 |
|
56 1. Redistributions of source code must retain the above copyright |
|
57 notice, this list of conditions and the following disclaimer. |
|
58 |
|
59 2. Redistributions in binary form must reproduce the above copyright |
|
60 notice, this list of conditions and the following disclaimer in the |
|
61 documentation and/or other materials provided with the distribution. |
|
62 |
|
63 3. The names of its contributors may not be used to endorse or promote |
|
64 products derived from this software without specific prior written |
|
65 permission. |
|
66 |
|
67 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
68 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
69 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
70 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
71 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
72 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
73 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
74 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
75 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
76 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
77 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
78 |
|
79 |
|
80 Any feedback is very welcome. |
|
81 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
|
82 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
|
83 */ |
|
84 |
|
85 function MersenneTwister19937() |
|
86 { |
|
87 /* Period parameters */ |
|
88 //c//#define N 624 |
|
89 //c//#define M 397 |
|
90 //c//#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
|
91 //c//#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
|
92 //c//#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
|
93 var N = 624; |
|
94 var M = 397; |
|
95 var MATRIX_A = 0x9908b0df; /* constant vector a */ |
|
96 var UPPER_MASK = 0x80000000; /* most significant w-r bits */ |
|
97 var LOWER_MASK = 0x7fffffff; /* least significant r bits */ |
|
98 //c//static unsigned long mt[N]; /* the array for the state vector */ |
|
99 //c//static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ |
|
100 var mt = new Array(N); /* the array for the state vector */ |
|
101 var mti = N+1; /* mti==N+1 means mt[N] is not initialized */ |
|
102 |
|
103 function unsigned32 (n1) // returns a 32-bits unsiged integer from an operand to which applied a bit operator. |
|
104 { |
|
105 return n1 < 0 ? (n1 ^ UPPER_MASK) + UPPER_MASK : n1; |
|
106 } |
|
107 |
|
108 function subtraction32 (n1, n2) // emulates lowerflow of a c 32-bits unsiged integer variable, instead of the operator -. these both arguments must be non-negative integers expressible using unsigned 32 bits. |
|
109 { |
|
110 return n1 < n2 ? unsigned32((0x100000000 - (n2 - n1)) & 0xffffffff) : n1 - n2; |
|
111 } |
|
112 |
|
113 function addition32 (n1, n2) // emulates overflow of a c 32-bits unsiged integer variable, instead of the operator +. these both arguments must be non-negative integers expressible using unsigned 32 bits. |
|
114 { |
|
115 return unsigned32((n1 + n2) & 0xffffffff) |
|
116 } |
|
117 |
|
118 function multiplication32 (n1, n2) // emulates overflow of a c 32-bits unsiged integer variable, instead of the operator *. these both arguments must be non-negative integers expressible using unsigned 32 bits. |
|
119 { |
|
120 var sum = 0; |
|
121 for (var i = 0; i < 32; ++i){ |
|
122 if ((n1 >>> i) & 0x1){ |
|
123 sum = addition32(sum, unsigned32(n2 << i)); |
|
124 } |
|
125 } |
|
126 return sum; |
|
127 } |
|
128 |
|
129 /* initializes mt[N] with a seed */ |
|
130 //c//void init_genrand(unsigned long s) |
|
131 this.init_genrand = function (s) |
|
132 { |
|
133 //c//mt[0]= s & 0xffffffff; |
|
134 mt[0]= unsigned32(s & 0xffffffff); |
|
135 for (mti=1; mti<N; mti++) { |
|
136 mt[mti] = |
|
137 //c//(1812433253 * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); |
|
138 addition32(multiplication32(1812433253, unsigned32(mt[mti-1] ^ (mt[mti-1] >>> 30))), mti); |
|
139 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
|
140 /* In the previous versions, MSBs of the seed affect */ |
|
141 /* only MSBs of the array mt[]. */ |
|
142 /* 2002/01/09 modified by Makoto Matsumoto */ |
|
143 //c//mt[mti] &= 0xffffffff; |
|
144 mt[mti] = unsigned32(mt[mti] & 0xffffffff); |
|
145 /* for >32 bit machines */ |
|
146 } |
|
147 } |
|
148 |
|
149 /* initialize by an array with array-length */ |
|
150 /* init_key is the array for initializing keys */ |
|
151 /* key_length is its length */ |
|
152 /* slight change for C++, 2004/2/26 */ |
|
153 //c//void init_by_array(unsigned long init_key[], int key_length) |
|
154 this.init_by_array = function (init_key, key_length) |
|
155 { |
|
156 //c//int i, j, k; |
|
157 var i, j, k; |
|
158 //c//init_genrand(19650218); |
|
159 this.init_genrand(19650218); |
|
160 i=1; j=0; |
|
161 k = (N>key_length ? N : key_length); |
|
162 for (; k; k--) { |
|
163 //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525)) |
|
164 //c// + init_key[j] + j; /* non linear */ |
|
165 mt[i] = addition32(addition32(unsigned32(mt[i] ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1664525)), init_key[j]), j); |
|
166 mt[i] = |
|
167 //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ |
|
168 unsigned32(mt[i] & 0xffffffff); |
|
169 i++; j++; |
|
170 if (i>=N) { mt[0] = mt[N-1]; i=1; } |
|
171 if (j>=key_length) j=0; |
|
172 } |
|
173 for (k=N-1; k; k--) { |
|
174 //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941)) |
|
175 //c//- i; /* non linear */ |
|
176 mt[i] = subtraction32(unsigned32((dbg=mt[i]) ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1566083941)), i); |
|
177 //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ |
|
178 mt[i] = unsigned32(mt[i] & 0xffffffff); |
|
179 i++; |
|
180 if (i>=N) { mt[0] = mt[N-1]; i=1; } |
|
181 } |
|
182 mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ |
|
183 } |
|
184 |
|
185 this.export_state = function() { return [mt, mti]; }; |
|
186 this.import_state = function(s) { mt = s[0]; mti = s[1]; }; |
|
187 this.export_mta = function() { return mt; }; |
|
188 this.import_mta = function(_mta) { mt = _mta }; |
|
189 this.export_mti = function() { return mti; }; |
|
190 this.import_mti = function(_mti) { mti = _mti; } |
|
191 |
|
192 /* generates a random number on [0,0xffffffff]-interval */ |
|
193 //c//unsigned long genrand_int32(void) |
|
194 this.genrand_int32 = function () |
|
195 { |
|
196 //c//unsigned long y; |
|
197 //c//static unsigned long mag01[2]={0x0UL, MATRIX_A}; |
|
198 var y; |
|
199 var mag01 = new Array(0x0, MATRIX_A); |
|
200 /* mag01[x] = x * MATRIX_A for x=0,1 */ |
|
201 |
|
202 if (mti >= N) { /* generate N words at one time */ |
|
203 //c//int kk; |
|
204 var kk; |
|
205 |
|
206 if (mti == N+1) /* if init_genrand() has not been called, */ |
|
207 //c//init_genrand(5489); /* a default initial seed is used */ |
|
208 this.init_genrand(5489); /* a default initial seed is used */ |
|
209 |
|
210 for (kk=0;kk<N-M;kk++) { |
|
211 //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
|
212 //c//mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]; |
|
213 y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); |
|
214 mt[kk] = unsigned32(mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]); |
|
215 } |
|
216 for (;kk<N-1;kk++) { |
|
217 //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
|
218 //c//mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]; |
|
219 y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); |
|
220 mt[kk] = unsigned32(mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]); |
|
221 } |
|
222 //c//y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
|
223 //c//mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]; |
|
224 y = unsigned32((mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)); |
|
225 mt[N-1] = unsigned32(mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]); |
|
226 mti = 0; |
|
227 } |
|
228 |
|
229 y = mt[mti++]; |
|
230 |
|
231 /* Tempering */ |
|
232 //c//y ^= (y >> 11); |
|
233 //c//y ^= (y << 7) & 0x9d2c5680; |
|
234 //c//y ^= (y << 15) & 0xefc60000; |
|
235 //c//y ^= (y >> 18); |
|
236 y = unsigned32(y ^ (y >>> 11)); |
|
237 y = unsigned32(y ^ ((y << 7) & 0x9d2c5680)); |
|
238 y = unsigned32(y ^ ((y << 15) & 0xefc60000)); |
|
239 y = unsigned32(y ^ (y >>> 18)); |
|
240 |
|
241 return y; |
|
242 } |
|
243 |
|
244 /* generates a random number on [0,0x7fffffff]-interval */ |
|
245 //c//long genrand_int31(void) |
|
246 this.genrand_int31 = function () |
|
247 { |
|
248 //c//return (genrand_int32()>>1); |
|
249 return (this.genrand_int32()>>>1); |
|
250 } |
|
251 |
|
252 /* generates a random number on [0,1]-real-interval */ |
|
253 //c//double genrand_real1(void) |
|
254 this.genrand_real1 = function () |
|
255 { |
|
256 //c//return genrand_int32()*(1.0/4294967295.0); |
|
257 return this.genrand_int32()*(1.0/4294967295.0); |
|
258 /* divided by 2^32-1 */ |
|
259 } |
|
260 |
|
261 /* generates a random number on [0,1)-real-interval */ |
|
262 //c//double genrand_real2(void) |
|
263 this.genrand_real2 = function () |
|
264 { |
|
265 //c//return genrand_int32()*(1.0/4294967296.0); |
|
266 return this.genrand_int32()*(1.0/4294967296.0); |
|
267 /* divided by 2^32 */ |
|
268 } |
|
269 |
|
270 /* generates a random number on (0,1)-real-interval */ |
|
271 //c//double genrand_real3(void) |
|
272 this.genrand_real3 = function () |
|
273 { |
|
274 //c//return ((genrand_int32()) + 0.5)*(1.0/4294967296.0); |
|
275 return ((this.genrand_int32()) + 0.5)*(1.0/4294967296.0); |
|
276 /* divided by 2^32 */ |
|
277 } |
|
278 |
|
279 /* generates a random number on [0,1) with 53-bit resolution*/ |
|
280 //c//double genrand_res53(void) |
|
281 this.genrand_res53 = function () |
|
282 { |
|
283 //c//unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; |
|
284 var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6; |
|
285 return(a*67108864.0+b)*(1.0/9007199254740992.0); |
|
286 } |
|
287 /* These real versions are due to Isaku Wada, 2002/01/09 added */ |
|
288 } |
|
289 |
|
290 function initRnd() { |
|
291 var fuzzMT = new MersenneTwister19937; |
|
292 var fuzzSeed = 53; |
|
293 fuzzMT.init_genrand(fuzzSeed); |
|
294 rnd = function (n) { var v = Math.floor(fuzzMT.genrand_real2() * n); return v; }; |
|
295 rnd.rndReal = function() { return fuzzMT.genrand_real2(); }; |
|
296 rnd.fuzzMT = fuzzMT; |
|
297 } |