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