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