Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-random / src / mt19937ar.c
1 /*
2 ** mt19937ar.c - MT Random functions
3 **
4 ** Copyright (C) 1997 - 2016, Makoto Matsumoto and Takuji Nishimura,
5 ** All rights reserved.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining
8 ** a copy of this software and associated documentation files (the
9 ** "Software"), to deal in the Software without restriction, including
10 ** without limitation the rights to use, copy, modify, merge, publish,
11 ** distribute, sublicense, and/or sell copies of the Software, and to
12 ** permit persons to whom the Software is furnished to do so, subject to
13 ** the following conditions:
14 **
15 ** The above copyright notice and this permission notice shall be
16 ** included in all copies or substantial portions of the Software.
17 **
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **
26 ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27 **
28 ** Any feedback is very welcome.
29 ** http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
30 ** email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
31 **
32 ** This version is modified by mruby developers. If you see any problem,
33 ** contact us first at https://github.com/mruby/mruby/issues
34 */
35
36 #include <mruby.h>
37 #include "mt19937ar.h"
38
39 /* Period parameters */
40 /* #define N 624 */
41 #define M 397
42 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
43 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
44 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
45
46 #if 0 /* dead_code */
47 static unsigned long mt[N]; /* the array for the state vector  */
48 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
49 #endif /* dead_code */
50
51 void mrb_random_init_genrand(mt_state *t, unsigned long s)
52 {
53     t->mt[0]= s & 0xffffffffUL;
54     for (t->mti=1; t->mti<N; t->mti++) {
55         t->mt[t->mti] = (1812433253UL * (t->mt[t->mti-1] ^ (t->mt[t->mti-1] >> 30)) + t->mti);
56         t->mt[t->mti] &= 0xffffffffUL;
57     }
58 }
59
60 unsigned long mrb_random_genrand_int32(mt_state *t)
61 {
62     unsigned long y;
63     static const unsigned long mag01[2]={0x0UL, MATRIX_A};
64     /* mag01[x] = x * MATRIX_A  for x=0,1 */
65
66     if (t->mti >= N) { /* generate N words at one time */
67         int kk;
68
69         if (t->mti == N+1)   /* if init_genrand() has not been called, */
70             mrb_random_init_genrand(t, 5489UL); /* a default initial seed is used */
71
72         for (kk=0;kk<N-M;kk++) {
73             y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
74             t->mt[kk] = t->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
75         }
76         for (;kk<N-1;kk++) {
77             y = (t->mt[kk]&UPPER_MASK)|(t->mt[kk+1]&LOWER_MASK);
78             t->mt[kk] = t->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
79         }
80         y = (t->mt[N-1]&UPPER_MASK)|(t->mt[0]&LOWER_MASK);
81         t->mt[N-1] = t->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
82
83         t->mti = 0;
84     }
85
86     y = t->mt[t->mti++];
87
88     /* Tempering */
89     y ^= (y >> 11);
90     y ^= (y << 7) & 0x9d2c5680UL;
91     y ^= (y << 15) & 0xefc60000UL;
92     y ^= (y >> 18);
93
94     t->gen.int_ = y;
95
96     return y;
97 }
98
99 double mrb_random_genrand_real1(mt_state *t)
100 {
101     mrb_random_genrand_int32(t);
102     t->gen.double_ =  t->gen.int_*(1.0/4294967295.0);
103     return t->gen.double_;
104     /* divided by 2^32-1 */
105 }
106
107 #if 0 /* dead_code */
108 /* initializes mt[N] with a seed */
109 void init_genrand(unsigned long s)
110 {
111     mt[0]= s & 0xffffffffUL;
112     for (mti=1; mti<N; mti++) {
113         mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
114         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
115         /* In the previous versions, MSBs of the seed affect   */
116         /* only MSBs of the array mt[].                        */
117         /* 2002/01/09 modified by Makoto Matsumoto             */
118         mt[mti] &= 0xffffffffUL;
119         /* for >32 bit machines */
120     }
121 }
122
123 /* initialize by an array with array-length */
124 /* init_key is the array for initializing keys */
125 /* key_length is its length */
126 /* slight change for C++, 2004/2/26 */
127 void init_by_array(unsigned long init_key[], int key_length)
128 {
129     int i, j, k;
130     init_genrand(19650218UL);
131     i=1; j=0;
132     k = (N>key_length ? N : key_length);
133     for (; k; k--) {
134         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
135           + init_key[j] + j; /* non linear */
136         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
137         i++; j++;
138         if (i>=N) { mt[0] = mt[N-1]; i=1; }
139         if (j>=key_length) j=0;
140     }
141     for (k=N-1; k; k--) {
142         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
143           - i; /* non linear */
144         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
145         i++;
146         if (i>=N) { mt[0] = mt[N-1]; i=1; }
147     }
148
149     mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
150 }
151
152 /* generates a random number on [0,0xffffffff]-interval */
153 unsigned long genrand_int32(void)
154 {
155     unsigned long y;
156     static const unsigned long mag01[2]={0x0UL, MATRIX_A};
157     /* mag01[x] = x * MATRIX_A  for x=0,1 */
158
159     if (mti >= N) { /* generate N words at one time */
160         int kk;
161
162         if (mti == N+1)   /* if init_genrand() has not been called, */
163             init_genrand(5489UL); /* a default initial seed is used */
164
165         for (kk=0;kk<N-M;kk++) {
166             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
167             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
168         }
169         for (;kk<N-1;kk++) {
170             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
171             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
172         }
173         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
174         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
175
176         mti = 0;
177     }
178
179     y = mt[mti++];
180
181     /* Tempering */
182     y ^= (y >> 11);
183     y ^= (y << 7) & 0x9d2c5680UL;
184     y ^= (y << 15) & 0xefc60000UL;
185     y ^= (y >> 18);
186
187     return y;
188 }
189
190 /* generates a random number on [0,0x7fffffff]-interval */
191 long genrand_int31(void)
192 {
193     return (long)(genrand_int32()>>1);
194 }
195
196 /* generates a random number on [0,1]-real-interval */
197 double genrand_real1(void)
198 {
199     return genrand_int32()*(1.0/4294967295.0);
200     /* divided by 2^32-1 */
201 }
202
203 /* generates a random number on [0,1)-real-interval */
204 double genrand_real2(void)
205 {
206     return genrand_int32()*(1.0/4294967296.0);
207     /* divided by 2^32 */
208 }
209
210 /* generates a random number on (0,1)-real-interval */
211 double genrand_real3(void)
212 {
213     return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
214     /* divided by 2^32 */
215 }
216
217 /* generates a random number on [0,1) with 53-bit resolution*/
218 double genrand_res53(void)
219 {
220     unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
221     return(a*67108864.0+b)*(1.0/9007199254740992.0);
222 }
223 /* These real versions are due to Isaku Wada, 2002/01/09 added */
224 #endif /* dead_code */