michael@0: // |jit-test| slow; michael@0: michael@0: function randomRecursion() { michael@0: var y = "" michael@0: if (rnd(2)) { michael@0: var x = 2; michael@0: "{" + x + "}"; michael@0: randomRecursion(); michael@0: randomRecursion(); michael@0: return [""]; michael@0: } michael@0: return [""]; michael@0: } michael@0: michael@0: function thisFunctionIsNeverCalled() { michael@0: } michael@0: michael@0: function testOne() { michael@0: ox = newGlobal(); michael@0: var code = randomRecursion()[rnd(3)]; michael@0: } michael@0: michael@0: initRnd(); michael@0: gczeal(10, 3); michael@0: michael@0: for (var count = 0; count < 20; count++) { michael@0: print(count); michael@0: testOne() michael@0: } michael@0: michael@0: // ========================================================================================== michael@0: michael@0: // this program is a JavaScript version of Mersenne Twister, with concealment and encapsulation in class, michael@0: // an almost straight conversion from the original program, mt19937ar.c, michael@0: // translated by y. okada on July 17, 2006. michael@0: // Changes by Jesse Ruderman: added "var" keyword in a few spots; added export_mta etc; pasted into fuzz.js. michael@0: // in this program, procedure descriptions and comments of original source code were not removed. michael@0: // lines commented with //c// were originally descriptions of c procedure. and a few following lines are appropriate JavaScript descriptions. michael@0: // lines commented with /* and */ are original comments. michael@0: // lines commented with // are additional comments in this JavaScript version. michael@0: // 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: /* michael@0: A C-program for MT19937, with initialization improved 2002/1/26. michael@0: Coded by Takuji Nishimura and Makoto Matsumoto. michael@0: michael@0: Before using, initialize the state by using init_genrand(seed) michael@0: or init_by_array(init_key, key_length). michael@0: michael@0: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, michael@0: All rights reserved. michael@0: michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: 1. Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: 2. Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: 3. The names of its contributors may not be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: michael@0: Any feedback is very welcome. michael@0: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html michael@0: email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) michael@0: */ michael@0: michael@0: function MersenneTwister19937() michael@0: { michael@0: /* Period parameters */ michael@0: //c//#define N 624 michael@0: //c//#define M 397 michael@0: //c//#define MATRIX_A 0x9908b0dfUL /* constant vector a */ michael@0: //c//#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ michael@0: //c//#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ michael@0: var N = 624; michael@0: var M = 397; michael@0: var MATRIX_A = 0x9908b0df; /* constant vector a */ michael@0: var UPPER_MASK = 0x80000000; /* most significant w-r bits */ michael@0: var LOWER_MASK = 0x7fffffff; /* least significant r bits */ michael@0: //c//static unsigned long mt[N]; /* the array for the state vector */ michael@0: //c//static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ michael@0: var mt = new Array(N); /* the array for the state vector */ michael@0: var mti = N+1; /* mti==N+1 means mt[N] is not initialized */ michael@0: michael@0: function unsigned32 (n1) // returns a 32-bits unsiged integer from an operand to which applied a bit operator. michael@0: { michael@0: return n1 < 0 ? (n1 ^ UPPER_MASK) + UPPER_MASK : n1; michael@0: } michael@0: michael@0: 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: { michael@0: return n1 < n2 ? unsigned32((0x100000000 - (n2 - n1)) & 0xffffffff) : n1 - n2; michael@0: } michael@0: michael@0: 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: { michael@0: return unsigned32((n1 + n2) & 0xffffffff) michael@0: } michael@0: michael@0: 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: { michael@0: var sum = 0; michael@0: for (var i = 0; i < 32; ++i){ michael@0: if ((n1 >>> i) & 0x1){ michael@0: sum = addition32(sum, unsigned32(n2 << i)); michael@0: } michael@0: } michael@0: return sum; michael@0: } michael@0: michael@0: /* initializes mt[N] with a seed */ michael@0: //c//void init_genrand(unsigned long s) michael@0: this.init_genrand = function (s) michael@0: { michael@0: //c//mt[0]= s & 0xffffffff; michael@0: mt[0]= unsigned32(s & 0xffffffff); michael@0: for (mti=1; mti> 30)) + mti); michael@0: addition32(multiplication32(1812433253, unsigned32(mt[mti-1] ^ (mt[mti-1] >>> 30))), mti); michael@0: /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ michael@0: /* In the previous versions, MSBs of the seed affect */ michael@0: /* only MSBs of the array mt[]. */ michael@0: /* 2002/01/09 modified by Makoto Matsumoto */ michael@0: //c//mt[mti] &= 0xffffffff; michael@0: mt[mti] = unsigned32(mt[mti] & 0xffffffff); michael@0: /* for >32 bit machines */ michael@0: } michael@0: } michael@0: michael@0: /* initialize by an array with array-length */ michael@0: /* init_key is the array for initializing keys */ michael@0: /* key_length is its length */ michael@0: /* slight change for C++, 2004/2/26 */ michael@0: //c//void init_by_array(unsigned long init_key[], int key_length) michael@0: this.init_by_array = function (init_key, key_length) michael@0: { michael@0: //c//int i, j, k; michael@0: var i, j, k; michael@0: //c//init_genrand(19650218); michael@0: this.init_genrand(19650218); michael@0: i=1; j=0; michael@0: k = (N>key_length ? N : key_length); michael@0: for (; k; k--) { michael@0: //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525)) michael@0: //c// + init_key[j] + j; /* non linear */ michael@0: mt[i] = addition32(addition32(unsigned32(mt[i] ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1664525)), init_key[j]), j); michael@0: mt[i] = michael@0: //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ michael@0: unsigned32(mt[i] & 0xffffffff); michael@0: i++; j++; michael@0: if (i>=N) { mt[0] = mt[N-1]; i=1; } michael@0: if (j>=key_length) j=0; michael@0: } michael@0: for (k=N-1; k; k--) { michael@0: //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941)) michael@0: //c//- i; /* non linear */ michael@0: mt[i] = subtraction32(unsigned32((dbg=mt[i]) ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1566083941)), i); michael@0: //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */ michael@0: mt[i] = unsigned32(mt[i] & 0xffffffff); michael@0: i++; michael@0: if (i>=N) { mt[0] = mt[N-1]; i=1; } michael@0: } michael@0: mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */ michael@0: } michael@0: michael@0: this.export_state = function() { return [mt, mti]; }; michael@0: this.import_state = function(s) { mt = s[0]; mti = s[1]; }; michael@0: this.export_mta = function() { return mt; }; michael@0: this.import_mta = function(_mta) { mt = _mta }; michael@0: this.export_mti = function() { return mti; }; michael@0: this.import_mti = function(_mti) { mti = _mti; } michael@0: michael@0: /* generates a random number on [0,0xffffffff]-interval */ michael@0: //c//unsigned long genrand_int32(void) michael@0: this.genrand_int32 = function () michael@0: { michael@0: //c//unsigned long y; michael@0: //c//static unsigned long mag01[2]={0x0UL, MATRIX_A}; michael@0: var y; michael@0: var mag01 = new Array(0x0, MATRIX_A); michael@0: /* mag01[x] = x * MATRIX_A for x=0,1 */ michael@0: michael@0: if (mti >= N) { /* generate N words at one time */ michael@0: //c//int kk; michael@0: var kk; michael@0: michael@0: if (mti == N+1) /* if init_genrand() has not been called, */ michael@0: //c//init_genrand(5489); /* a default initial seed is used */ michael@0: this.init_genrand(5489); /* a default initial seed is used */ michael@0: michael@0: for (kk=0;kk> 1) ^ mag01[y & 0x1]; michael@0: y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); michael@0: mt[kk] = unsigned32(mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]); michael@0: } michael@0: for (;kk> 1) ^ mag01[y & 0x1]; michael@0: y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); michael@0: mt[kk] = unsigned32(mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]); michael@0: } michael@0: //c//y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); michael@0: //c//mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]; michael@0: y = unsigned32((mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)); michael@0: mt[N-1] = unsigned32(mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]); michael@0: mti = 0; michael@0: } michael@0: michael@0: y = mt[mti++]; michael@0: michael@0: /* Tempering */ michael@0: //c//y ^= (y >> 11); michael@0: //c//y ^= (y << 7) & 0x9d2c5680; michael@0: //c//y ^= (y << 15) & 0xefc60000; michael@0: //c//y ^= (y >> 18); michael@0: y = unsigned32(y ^ (y >>> 11)); michael@0: y = unsigned32(y ^ ((y << 7) & 0x9d2c5680)); michael@0: y = unsigned32(y ^ ((y << 15) & 0xefc60000)); michael@0: y = unsigned32(y ^ (y >>> 18)); michael@0: michael@0: return y; michael@0: } michael@0: michael@0: /* generates a random number on [0,0x7fffffff]-interval */ michael@0: //c//long genrand_int31(void) michael@0: this.genrand_int31 = function () michael@0: { michael@0: //c//return (genrand_int32()>>1); michael@0: return (this.genrand_int32()>>>1); michael@0: } michael@0: michael@0: /* generates a random number on [0,1]-real-interval */ michael@0: //c//double genrand_real1(void) michael@0: this.genrand_real1 = function () michael@0: { michael@0: //c//return genrand_int32()*(1.0/4294967295.0); michael@0: return this.genrand_int32()*(1.0/4294967295.0); michael@0: /* divided by 2^32-1 */ michael@0: } michael@0: michael@0: /* generates a random number on [0,1)-real-interval */ michael@0: //c//double genrand_real2(void) michael@0: this.genrand_real2 = function () michael@0: { michael@0: //c//return genrand_int32()*(1.0/4294967296.0); michael@0: return this.genrand_int32()*(1.0/4294967296.0); michael@0: /* divided by 2^32 */ michael@0: } michael@0: michael@0: /* generates a random number on (0,1)-real-interval */ michael@0: //c//double genrand_real3(void) michael@0: this.genrand_real3 = function () michael@0: { michael@0: //c//return ((genrand_int32()) + 0.5)*(1.0/4294967296.0); michael@0: return ((this.genrand_int32()) + 0.5)*(1.0/4294967296.0); michael@0: /* divided by 2^32 */ michael@0: } michael@0: michael@0: /* generates a random number on [0,1) with 53-bit resolution*/ michael@0: //c//double genrand_res53(void) michael@0: this.genrand_res53 = function () michael@0: { michael@0: //c//unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; michael@0: var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6; michael@0: return(a*67108864.0+b)*(1.0/9007199254740992.0); michael@0: } michael@0: /* These real versions are due to Isaku Wada, 2002/01/09 added */ michael@0: } michael@0: michael@0: function initRnd() { michael@0: var fuzzMT = new MersenneTwister19937; michael@0: var fuzzSeed = 53; michael@0: fuzzMT.init_genrand(fuzzSeed); michael@0: rnd = function (n) { var v = Math.floor(fuzzMT.genrand_real2() * n); return v; }; michael@0: rnd.rndReal = function() { return fuzzMT.genrand_real2(); }; michael@0: rnd.fuzzMT = fuzzMT; michael@0: }