1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* Mersenne Twister pseudo-random number generator functions.
Copyright 2002 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include "gmp.h"
#include "gmp-impl.h"
/* This code implements the Mersenne Twister pseudorandom number generator
by Takuji Nishimura and Makoto Matsumoto. The buffer initialization
function is different in order to permit seeds greater than 2^32-1. */
/* Number of extractions used to warm the buffer up. */
#define WARM_UP 2000
/* Default seed to use when the generator is not initialized. */
#define DEFAULT_SEED 5489 /* was 4357 */
/* Period parameters. */
#define N 624
#define M 397
#define MATRIX_A 0x9908B0DF /* Constant vector a. */
/* Tempering masks. */
#define MASK_1 0x9D2C5680
#define MASK_2 0xEFC60000
/* State structure for MT. */
typedef struct
{
unsigned long int mt[N]; /* State array. */
int mti; /* Index of current value. */
} gmp_rand_mt_struct;
static void
recalc_buffer (unsigned long int mt[])
{
static const unsigned long int a_times_y[2] = {0, MATRIX_A};
/* a_times_y[y] = y * MATRIX_A for y=0,1 */
unsigned long int y;
int kk;
for (kk = 0; kk < N - M; kk++)
{
y = (mt[kk] & 0x80000000) | (mt[kk + 1] & 0x7FFFFFFF);
mt[kk] = mt[kk + M] ^ (y >> 1) ^ a_times_y[y & 0x01];
}
for (; kk < N - 1; kk++)
{
y = (mt[kk] & 0x80000000) | (mt[kk + 1] & 0x7FFFFFFF);
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ a_times_y[y & 0x01];
}
y = (mt[N - 1] & 0x80000000) | (mt[0] & 0x7FFFFFFF);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ a_times_y[y & 0x01];
}
/* Get nbits bits of output from the generator into dest.
Note that Mersenne Twister is designed to produce outputs in
32-bit words. */
static void
randget_mt (gmp_randstate_t rstate, mp_ptr dest, unsigned long int nbits)
{
unsigned long int y;
int rbits;
mp_size_t i;
mp_size_t nlimbs;
int *pmti;
unsigned long int *mt;
pmti = &((gmp_rand_mt_struct *) RNG_STATE (rstate))->mti;
mt = ((gmp_rand_mt_struct *) RNG_STATE (rstate))->mt;
/* Use a default seed if randseed_mt has not been yet called. */
if (*pmti == N + 1)
gmp_randseed_ui (rstate, (unsigned long int) DEFAULT_SEED);
nlimbs = nbits / GMP_NUMB_BITS;
rbits = nbits % GMP_NUMB_BITS;
#define NEXT_RANDOM \
do \
{ \
if (*pmti >= N) \
{ \
recalc_buffer (mt); \
*pmti = 0; \
} \
y = mt[(*pmti)++]; \
y ^= (y >> 11); \
y ^= (y << 7) & MASK_1; \
y ^= (y << 15) & MASK_2; \
y ^= (y >> 18); \
} \
while (0)
/* Handle the common cases of 32- or 64-bit limbs with fast,
optimized routines, and the rest of cases with a general
routine. In all cases, no more than 31 bits are rejected
for the last limb so that every version of the code is
consistent with the others. */
#if (GMP_NUMB_BITS == 32)
for (i = 0; i < nlimbs; i++)
{
NEXT_RANDOM;
dest[i] = (mp_limb_t) y;
}
if (rbits)
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) (y & ~(ULONG_MAX << rbits));
}
#else /* GMP_NUMB_BITS != 32 */
#if (GMP_NUMB_BITS == 64)
for (i = 0; i < nlimbs; i++)
{
NEXT_RANDOM;
dest[i] = (mp_limb_t) y;
NEXT_RANDOM;
dest[i] |= (mp_limb_t) y << 32;
}
if (rbits)
{
if (rbits < 32)
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) (y & ~(ULONG_MAX << rbits));
}
else
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) y;
if (rbits > 32)
{
NEXT_RANDOM;
dest[nlimbs] |=
((mp_limb_t) (y & ~(ULONG_MAX << (rbits-32)))) << 32;
}
}
}
#else /* GMP_NUMB_BITS != 64 */
{
/* Fall back to a general algorithm. This algorithm works by
keeping a pool of up to 64 bits (2 outputs from MT) acting
as a shift register from which bits are consumed as needed.
Bits are consumed using the LSB bits of bitpool_l, and
inserted via bitpool_h and shifted to the right place. */
unsigned long int bitpool_h = 0;
unsigned long int bitpool_l = 0;
int bits_in_pool = 0; /* Holds number of valid bits in the pool. */
int bits_to_fill; /* Holds total number of bits to put in
destination. */
int bitidx; /* Holds the destination bit position. */
mp_size_t nlimbs2; /* Number of whole+partial limbs to fill. */
nlimbs2 = nlimbs + (rbits != 0);
for (i = 0; i < nlimbs2; i++)
{
bitidx = 0;
if (i < nlimbs)
bits_to_fill = GMP_NUMB_BITS;
else
bits_to_fill = rbits;
dest[i] = CNST_LIMB (0);
while (bits_to_fill >= 32) /* Process whole 32-bit blocks first. */
{
if (bits_in_pool < 32) /* Need more bits. */
{
/* 64-bit right shift. */
NEXT_RANDOM;
bitpool_h = y;
bitpool_l |= (bitpool_h << bits_in_pool) & 0xFFFFFFFF;
if (bits_in_pool == 0)
bitpool_h = 0;
else
bitpool_h >>= 32 - bits_in_pool;
bits_in_pool += 32; /* We've got 32 more bits. */
}
/* Fill a 32-bit chunk */
dest[i] |= ((mp_limb_t) bitpool_l) << bitidx;
bitpool_l = bitpool_h;
bits_in_pool -= 32;
bits_to_fill -= 32;
bitidx += 32;
}
/* Cover the case where GMP_NUMB_BITS is not a multiple of 32. */
if (bits_to_fill != 0)
{
if (bits_in_pool < bits_to_fill)
{
NEXT_RANDOM;
bitpool_h = y;
bitpool_l |= (bitpool_h << bits_in_pool) & 0xFFFFFFFF;
if (bits_in_pool == 0)
bitpool_h = 0;
else
bitpool_h >>= 32 - bits_in_pool;
bits_in_pool += 32;
}
dest[i] |= (((mp_limb_t) bitpool_l
& ~(~CNST_LIMB (0) << bits_to_fill))
<< bitidx);
bitpool_l = ((bitpool_l >> bits_to_fill)
| (bitpool_h << (32 - bits_to_fill))) & 0xFFFFFFFF;
bitpool_h >>= bits_to_fill;
bits_in_pool -= bits_to_fill;
}
}
}
#endif /* GMP_NUMB_BITS != 64 */
#endif /* GMP_NUMB_BITS != 32 */
}
/* Seeding function. Uses powering modulo a non-Mersenne prime to obtain
a permutation of the input seed space. The modulus is 2^19937-20023,
which is probably prime. The power is 1074888996. In order to avoid
seeds 0 and 1 generating invalid or strange output, the input seed is
first manipulated as follows:
seed1 = seed mod (2^19937-20027) + 2
so that seed1 lies between 2 and 2^19937-20026 inclusive. Then the
powering is performed as follows:
seed2 = (seed1^1074888996) mod (2^19937-20023)
and then seed2 is used to bootstrap the buffer.
This method aims to give guarantees that:
a) seed2 will never be zero,
b) seed2 will very seldom have a very low population of ones in its
binary representation, and
c) every seed between 0 and 2^19937-20028 (inclusive) will yield a
different sequence.
CAVEATS:
The period of the seeding function is 2^19937-20027. This means that
with seeds 2^19937-20027, 2^19937-20026, ... the exact same sequences
are obtained as with seeds 0, 1, etc.; it also means that seed -1
produces the same sequence as seed 2^19937-20028, etc.
*/
static int
randseed_mt (gmp_randstate_t rstate, mpz_srcptr seed)
{
int i;
size_t cnt;
gmp_rand_mt_struct *p;
mpz_t mod; /* Modulus. */
mpz_t seed1; /* Intermediate result. */
p = (gmp_rand_mt_struct *) RNG_STATE (rstate);
mpz_init (mod);
mpz_init (seed1);
mpz_set_ui (mod, 0L);
mpz_setbit (mod, 19937L);
mpz_sub_ui (mod, mod, 20027L);
mpz_mod (seed1, seed, mod); /* Reduce `seed' modulo `mod'. */
mpz_add_ui (seed1, seed1, 2L); /* seed1 is now ready. */
mpz_add_ui (mod, mod, 4L); /* Prepare modulus for powering. */
mpz_powm_ui (seed1, seed1, 1074888996L, mod);
/* Split seed1 into N 32-bit chunks. */
/*FIXME: Remove or reenable:
for (i = 0; i < N; i++)
{
p->mt[i] = mpz_get_ui (seed1) & 0xFFFFFFFF;
mpz_tdiv_q_2exp (seed1, seed1, 32L);
}
*/
mpz_export ((void *) p->mt, &cnt, -1, sizeof (unsigned long int), 0, 0,
seed1);
while (cnt < N)
p->mt[cnt++] = 0;
mpz_clear (mod);
mpz_clear (seed1);
/* The last bit should be placed at the left. */
if (p->mt[N - 1] != 0)
p->mt[N - 1] = 0x80000000;
if (WARM_UP)
{
/* Warm the generator up. */
for (i = 0; i < WARM_UP / N; i++)
recalc_buffer (p->mt);
p->mti = WARM_UP % N;
}
else
p->mti = N;
return 0;
}
static void
randclear_mt (gmp_randstate_t rstate)
{
(*__gmp_free_func) ((void *) RNG_STATE (rstate),
sizeof (gmp_rand_mt_struct));
}
static const gmp_randfnptr_t Mersenne_Twister_Generator = {
randseed_mt,
randget_mt,
randclear_mt
};
/* Initialize MT-specific data. */
void
gmp_randinit_mt (gmp_randstate_t rstate)
{
/* Set the generator functions. */
RNG_FNPTR (rstate) = (void *) &Mersenne_Twister_Generator;
/* Allocate the MT-specific state. */
RNG_STATE (rstate) =
(mp_ptr) (*__gmp_allocate_func) (sizeof (gmp_rand_mt_struct));
/* mti = N + 1 means use default seed. */
((gmp_rand_mt_struct *) RNG_STATE (rstate))->mti = N + 1;
}
|