|
| 1 | +/* |
| 2 | + * * For inclusion in the SPEC cpu benchmarks |
| 3 | + * This file implements the random number generation necessary for the SPEC cpu benchmarks. The functions |
| 4 | + * defined here are used in vtr_random.h/cpp |
| 5 | + * |
| 6 | + * |
| 7 | + * A C-program for MT19937, with initialization improved 2002/1/26. |
| 8 | + * Coded by Takuji Nishimura and Makoto Matsumoto. |
| 9 | + * |
| 10 | + * Before using, initialize the state by using init_genrand(seed) |
| 11 | + * or init_by_array(init_key, key_length). |
| 12 | + * |
| 13 | + * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
| 14 | + * All rights reserved. |
| 15 | + * Copyright (C) 2005, Mutsuo Saito, |
| 16 | + * All rights reserved. |
| 17 | + * |
| 18 | + * Redistribution and use in source and binary forms, with or without |
| 19 | + * modification, are permitted provided that the following conditions |
| 20 | + * are met: |
| 21 | + * |
| 22 | + * 1. Redistributions of source code must retain the above copyright |
| 23 | + * notice, this list of conditions and the following disclaimer. |
| 24 | + * |
| 25 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 26 | + * notice, this list of conditions and the following disclaimer in the |
| 27 | + * documentation and/or other materials provided with the distribution. |
| 28 | + * |
| 29 | + * 3. The names of its contributors may not be used to endorse or promote |
| 30 | + * products derived from this software without specific prior written |
| 31 | + * permission. |
| 32 | + * |
| 33 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 34 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 35 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 36 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 37 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 38 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 39 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 40 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 41 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 42 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 43 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 | + * |
| 45 | + * |
| 46 | + * Any feedback is very welcome. |
| 47 | + * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
| 48 | + * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
| 49 | + */ |
| 50 | +/* Slightly modified for use in SPEC CPU by Cloyce D. Spradling (5 Nov 2009) |
| 51 | + */ |
| 52 | + |
| 53 | +#include "specrand.h" |
| 54 | + |
| 55 | +/* Period parameters */ |
| 56 | +#define N 624 |
| 57 | +#define M 397 |
| 58 | +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
| 59 | +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 60 | +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 61 | + |
| 62 | +static unsigned long mt[N]; /* the array for the state vector */ |
| 63 | +static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */ |
| 64 | + |
| 65 | +void spec_srand(int seed) { |
| 66 | + spec_init_genrand((unsigned long)seed); |
| 67 | +} |
| 68 | + |
| 69 | +/* Just a copy of spec_genrand_real2() */ |
| 70 | +double spec_rand() { |
| 71 | + return spec_genrand_int32() * (1.0 / 4294967296.0); |
| 72 | +} |
| 73 | + |
| 74 | +/* Just a copy of spec_genrand_int31() */ |
| 75 | +long spec_lrand48() { |
| 76 | + return (long)(spec_genrand_int32() >> 1); |
| 77 | +} |
| 78 | + |
| 79 | +/* initializes mt[N] with a seed */ |
| 80 | +void spec_init_genrand(unsigned long s) { |
| 81 | + mt[0] = s & 0xffffffffUL; |
| 82 | + for (mti = 1; mti < N; mti++) { |
| 83 | + mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti); |
| 84 | + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 85 | + /* In the previous versions, MSBs of the seed affect */ |
| 86 | + /* only MSBs of the array mt[]. */ |
| 87 | + /* 2002/01/09 modified by Makoto Matsumoto */ |
| 88 | + mt[mti] &= 0xffffffffUL; |
| 89 | + /* for >32 bit machines */ |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/* initialize by an array with array-length */ |
| 94 | +/* init_key is the array for initializing keys */ |
| 95 | +/* key_length is its length */ |
| 96 | +/* slight change for C++, 2004/2/26 */ |
| 97 | +void spec_init_by_array(unsigned long init_key[], int key_length) { |
| 98 | + int i, j, k; |
| 99 | + spec_init_genrand(19650218UL); |
| 100 | + i = 1; |
| 101 | + j = 0; |
| 102 | + k = (N > key_length ? N : key_length); |
| 103 | + for (; k; k--) { |
| 104 | + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) |
| 105 | + + init_key[j] + j; /* non linear */ |
| 106 | + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 107 | + i++; |
| 108 | + j++; |
| 109 | + if (i >= N) { |
| 110 | + mt[0] = mt[N - 1]; |
| 111 | + i = 1; |
| 112 | + } |
| 113 | + if (j >= key_length) j = 0; |
| 114 | + } |
| 115 | + for (k = N - 1; k; k--) { |
| 116 | + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) |
| 117 | + - i; /* non linear */ |
| 118 | + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 119 | + i++; |
| 120 | + if (i >= N) { |
| 121 | + mt[0] = mt[N - 1]; |
| 122 | + i = 1; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ |
| 127 | +} |
| 128 | + |
| 129 | +/* generates a random number on [0,0xffffffff]-interval */ |
| 130 | +unsigned long spec_genrand_int32() { |
| 131 | + unsigned long y; |
| 132 | + static unsigned long mag01[2] = {0x0UL, MATRIX_A}; |
| 133 | + /* mag01[x] = x * MATRIX_A for x=0,1 */ |
| 134 | + |
| 135 | + if (mti >= N) { /* generate N words at one time */ |
| 136 | + int kk; |
| 137 | + |
| 138 | + if (mti == N + 1) /* if init_genrand() has not been called, */ |
| 139 | + spec_init_genrand(5489UL); /* a default initial seed is used */ |
| 140 | + |
| 141 | + for (kk = 0; kk < N - M; kk++) { |
| 142 | + y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 143 | + mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 144 | + } |
| 145 | + for (; kk < N - 1; kk++) { |
| 146 | + y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 147 | + mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 148 | + } |
| 149 | + y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
| 150 | + mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 151 | + |
| 152 | + mti = 0; |
| 153 | + } |
| 154 | + |
| 155 | + y = mt[mti++]; |
| 156 | + |
| 157 | + /* Tempering */ |
| 158 | + y ^= (y >> 11); |
| 159 | + y ^= (y << 7) & 0x9d2c5680UL; |
| 160 | + y ^= (y << 15) & 0xefc60000UL; |
| 161 | + y ^= (y >> 18); |
| 162 | + |
| 163 | + return y; |
| 164 | +} |
| 165 | + |
| 166 | +/* generates a random number on [0,0x7fffffff]-interval */ |
| 167 | +long spec_genrand_int31() { |
| 168 | + return (long)(spec_genrand_int32() >> 1); |
| 169 | +} |
| 170 | + |
| 171 | +/* generates a random number on [0,1]-real-interval */ |
| 172 | +double spec_genrand_real1() { |
| 173 | + return spec_genrand_int32() * (1.0 / 4294967295.0); |
| 174 | + /* divided by 2^32-1 */ |
| 175 | +} |
| 176 | + |
| 177 | +/* generates a random number on [0,1)-real-interval */ |
| 178 | +double spec_genrand_real2() { |
| 179 | + return spec_genrand_int32() * (1.0 / 4294967296.0); |
| 180 | + /* divided by 2^32 */ |
| 181 | +} |
| 182 | + |
| 183 | +/* generates a random number on (0,1)-real-interval */ |
| 184 | +double spec_genrand_real3() { |
| 185 | + return (((double)spec_genrand_int32()) + 0.5) * (1.0 / 4294967296.0); |
| 186 | + /* divided by 2^32 */ |
| 187 | +} |
| 188 | + |
| 189 | +/* generates a random number on [0,1) with 53-bit resolution*/ |
| 190 | +double spec_genrand_res53() { |
| 191 | + unsigned long a = spec_genrand_int32() >> 5, b = spec_genrand_int32() >> 6; |
| 192 | + return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); |
| 193 | +} |
| 194 | +/* These real versions are due to Isaku Wada, 2002/01/09 added */ |
0 commit comments