1 /*
2 * This file derives from SFMT 1.3.3
3 * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was
4 * released under the terms of the following license:
5 *
6 * Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
7 * University. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the Hiroshima University nor the names of
20 * its contributors may be used to endorse or promote products
21 * derived from this software without specific prior written
22 * permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /**
37 * @file SFMT.c
38 * @brief SIMD oriented Fast Mersenne Twister(SFMT)
39 *
40 * @author Mutsuo Saito (Hiroshima University)
41 * @author Makoto Matsumoto (Hiroshima University)
42 *
43 * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
44 * University. All rights reserved.
45 *
46 * The new BSD License is applied to this software, see LICENSE.txt
47 */
48 #define SFMT_C_
49 #include "test/jemalloc_test.h"
50 #include "test/SFMT-params.h"
51
52 #if defined(JEMALLOC_BIG_ENDIAN) && !defined(BIG_ENDIAN64)
53 #define BIG_ENDIAN64 1
54 #endif
55 #if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
56 #define BIG_ENDIAN64 1
57 #endif
58 #if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)
59 #define BIG_ENDIAN64 1
60 #endif
61 #if defined(ONLY64) && !defined(BIG_ENDIAN64)
62 #if defined(__GNUC__)
63 #error "-DONLY64 must be specified with -DBIG_ENDIAN64"
64 #endif
65 #undef ONLY64
66 #endif
67 /*------------------------------------------------------
68 128-bit SIMD data type for Altivec, SSE2 or standard C
69 ------------------------------------------------------*/
70 #if defined(HAVE_ALTIVEC)
71 /** 128-bit data structure */
72 union W128_T {
73 vector unsigned int s;
74 uint32_t u[4];
75 };
76 /** 128-bit data type */
77 typedef union W128_T w128_t;
78
79 #elif defined(HAVE_SSE2)
80 /** 128-bit data structure */
81 union W128_T {
82 __m128i si;
83 uint32_t u[4];
84 };
85 /** 128-bit data type */
86 typedef union W128_T w128_t;
87
88 #else
89
90 /** 128-bit data structure */
91 struct W128_T {
92 uint32_t u[4];
93 };
94 /** 128-bit data type */
95 typedef struct W128_T w128_t;
96
97 #endif
98
99 struct sfmt_s {
100 /** the 128-bit internal state array */
101 w128_t sfmt[N];
102 /** index counter to the 32-bit internal state array */
103 int idx;
104 /** a flag: it is 0 if and only if the internal state is not yet
105 * initialized. */
106 int initialized;
107 };
108
109 /*--------------------------------------
110 FILE GLOBAL VARIABLES
111 internal state, index counter and flag
112 --------------------------------------*/
113
114 /** a parity check vector which certificate the period of 2^{MEXP} */
115 static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};
116
117 /*----------------
118 STATIC FUNCTIONS
119 ----------------*/
120 JEMALLOC_INLINE_C int idxof(int i);
121 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
122 JEMALLOC_INLINE_C void rshift128(w128_t *out, w128_t const *in, int shift);
123 JEMALLOC_INLINE_C void lshift128(w128_t *out, w128_t const *in, int shift);
124 #endif
125 JEMALLOC_INLINE_C void gen_rand_all(sfmt_t *ctx);
126 JEMALLOC_INLINE_C void gen_rand_array(sfmt_t *ctx, w128_t *array, int size);
127 JEMALLOC_INLINE_C uint32_t func1(uint32_t x);
128 JEMALLOC_INLINE_C uint32_t func2(uint32_t x);
129 static void period_certification(sfmt_t *ctx);
130 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
131 JEMALLOC_INLINE_C void swap(w128_t *array, int size);
132 #endif
133
134 #if defined(HAVE_ALTIVEC)
135 #include "test/SFMT-alti.h"
136 #elif defined(HAVE_SSE2)
137 #include "test/SFMT-sse2.h"
138 #endif
139
140 /**
141 * This function simulate a 64-bit index of LITTLE ENDIAN
142 * in BIG ENDIAN machine.
143 */
144 #ifdef ONLY64
idxof(int i)145 JEMALLOC_INLINE_C int idxof(int i) {
146 return i ^ 1;
147 }
148 #else
idxof(int i)149 JEMALLOC_INLINE_C int idxof(int i) {
150 return i;
151 }
152 #endif
153 /**
154 * This function simulates SIMD 128-bit right shift by the standard C.
155 * The 128-bit integer given in in is shifted by (shift * 8) bits.
156 * This function simulates the LITTLE ENDIAN SIMD.
157 * @param out the output of this function
158 * @param in the 128-bit data to be shifted
159 * @param shift the shift value
160 */
161 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
162 #ifdef ONLY64
rshift128(w128_t * out,w128_t const * in,int shift)163 JEMALLOC_INLINE_C void rshift128(w128_t *out, w128_t const *in, int shift) {
164 uint64_t th, tl, oh, ol;
165
166 th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
167 tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
168
169 oh = th >> (shift * 8);
170 ol = tl >> (shift * 8);
171 ol |= th << (64 - shift * 8);
172 out->u[0] = (uint32_t)(ol >> 32);
173 out->u[1] = (uint32_t)ol;
174 out->u[2] = (uint32_t)(oh >> 32);
175 out->u[3] = (uint32_t)oh;
176 }
177 #else
rshift128(w128_t * out,w128_t const * in,int shift)178 JEMALLOC_INLINE_C void rshift128(w128_t *out, w128_t const *in, int shift) {
179 uint64_t th, tl, oh, ol;
180
181 th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
182 tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
183
184 oh = th >> (shift * 8);
185 ol = tl >> (shift * 8);
186 ol |= th << (64 - shift * 8);
187 out->u[1] = (uint32_t)(ol >> 32);
188 out->u[0] = (uint32_t)ol;
189 out->u[3] = (uint32_t)(oh >> 32);
190 out->u[2] = (uint32_t)oh;
191 }
192 #endif
193 /**
194 * This function simulates SIMD 128-bit left shift by the standard C.
195 * The 128-bit integer given in in is shifted by (shift * 8) bits.
196 * This function simulates the LITTLE ENDIAN SIMD.
197 * @param out the output of this function
198 * @param in the 128-bit data to be shifted
199 * @param shift the shift value
200 */
201 #ifdef ONLY64
lshift128(w128_t * out,w128_t const * in,int shift)202 JEMALLOC_INLINE_C void lshift128(w128_t *out, w128_t const *in, int shift) {
203 uint64_t th, tl, oh, ol;
204
205 th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
206 tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
207
208 oh = th << (shift * 8);
209 ol = tl << (shift * 8);
210 oh |= tl >> (64 - shift * 8);
211 out->u[0] = (uint32_t)(ol >> 32);
212 out->u[1] = (uint32_t)ol;
213 out->u[2] = (uint32_t)(oh >> 32);
214 out->u[3] = (uint32_t)oh;
215 }
216 #else
lshift128(w128_t * out,w128_t const * in,int shift)217 JEMALLOC_INLINE_C void lshift128(w128_t *out, w128_t const *in, int shift) {
218 uint64_t th, tl, oh, ol;
219
220 th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
221 tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
222
223 oh = th << (shift * 8);
224 ol = tl << (shift * 8);
225 oh |= tl >> (64 - shift * 8);
226 out->u[1] = (uint32_t)(ol >> 32);
227 out->u[0] = (uint32_t)ol;
228 out->u[3] = (uint32_t)(oh >> 32);
229 out->u[2] = (uint32_t)oh;
230 }
231 #endif
232 #endif
233
234 /**
235 * This function represents the recursion formula.
236 * @param r output
237 * @param a a 128-bit part of the internal state array
238 * @param b a 128-bit part of the internal state array
239 * @param c a 128-bit part of the internal state array
240 * @param d a 128-bit part of the internal state array
241 */
242 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
243 #ifdef ONLY64
do_recursion(w128_t * r,w128_t * a,w128_t * b,w128_t * c,w128_t * d)244 JEMALLOC_INLINE_C void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
245 w128_t *d) {
246 w128_t x;
247 w128_t y;
248
249 lshift128(&x, a, SL2);
250 rshift128(&y, c, SR2);
251 r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]
252 ^ (d->u[0] << SL1);
253 r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]
254 ^ (d->u[1] << SL1);
255 r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]
256 ^ (d->u[2] << SL1);
257 r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]
258 ^ (d->u[3] << SL1);
259 }
260 #else
do_recursion(w128_t * r,w128_t * a,w128_t * b,w128_t * c,w128_t * d)261 JEMALLOC_INLINE_C void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
262 w128_t *d) {
263 w128_t x;
264 w128_t y;
265
266 lshift128(&x, a, SL2);
267 rshift128(&y, c, SR2);
268 r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]
269 ^ (d->u[0] << SL1);
270 r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]
271 ^ (d->u[1] << SL1);
272 r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]
273 ^ (d->u[2] << SL1);
274 r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]
275 ^ (d->u[3] << SL1);
276 }
277 #endif
278 #endif
279
280 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
281 /**
282 * This function fills the internal state array with pseudorandom
283 * integers.
284 */
gen_rand_all(sfmt_t * ctx)285 JEMALLOC_INLINE_C void gen_rand_all(sfmt_t *ctx) {
286 int i;
287 w128_t *r1, *r2;
288
289 r1 = &ctx->sfmt[N - 2];
290 r2 = &ctx->sfmt[N - 1];
291 for (i = 0; i < N - POS1; i++) {
292 do_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1,
293 r2);
294 r1 = r2;
295 r2 = &ctx->sfmt[i];
296 }
297 for (; i < N; i++) {
298 do_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1 - N], r1,
299 r2);
300 r1 = r2;
301 r2 = &ctx->sfmt[i];
302 }
303 }
304
305 /**
306 * This function fills the user-specified array with pseudorandom
307 * integers.
308 *
309 * @param array an 128-bit array to be filled by pseudorandom numbers.
310 * @param size number of 128-bit pseudorandom numbers to be generated.
311 */
gen_rand_array(sfmt_t * ctx,w128_t * array,int size)312 JEMALLOC_INLINE_C void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {
313 int i, j;
314 w128_t *r1, *r2;
315
316 r1 = &ctx->sfmt[N - 2];
317 r2 = &ctx->sfmt[N - 1];
318 for (i = 0; i < N - POS1; i++) {
319 do_recursion(&array[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1, r2);
320 r1 = r2;
321 r2 = &array[i];
322 }
323 for (; i < N; i++) {
324 do_recursion(&array[i], &ctx->sfmt[i], &array[i + POS1 - N], r1, r2);
325 r1 = r2;
326 r2 = &array[i];
327 }
328 for (; i < size - N; i++) {
329 do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
330 r1 = r2;
331 r2 = &array[i];
332 }
333 for (j = 0; j < 2 * N - size; j++) {
334 ctx->sfmt[j] = array[j + size - N];
335 }
336 for (; i < size; i++, j++) {
337 do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
338 r1 = r2;
339 r2 = &array[i];
340 ctx->sfmt[j] = array[i];
341 }
342 }
343 #endif
344
345 #if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)
swap(w128_t * array,int size)346 JEMALLOC_INLINE_C void swap(w128_t *array, int size) {
347 int i;
348 uint32_t x, y;
349
350 for (i = 0; i < size; i++) {
351 x = array[i].u[0];
352 y = array[i].u[2];
353 array[i].u[0] = array[i].u[1];
354 array[i].u[2] = array[i].u[3];
355 array[i].u[1] = x;
356 array[i].u[3] = y;
357 }
358 }
359 #endif
360 /**
361 * This function represents a function used in the initialization
362 * by init_by_array
363 * @param x 32-bit integer
364 * @return 32-bit integer
365 */
func1(uint32_t x)366 static uint32_t func1(uint32_t x) {
367 return (x ^ (x >> 27)) * (uint32_t)1664525UL;
368 }
369
370 /**
371 * This function represents a function used in the initialization
372 * by init_by_array
373 * @param x 32-bit integer
374 * @return 32-bit integer
375 */
func2(uint32_t x)376 static uint32_t func2(uint32_t x) {
377 return (x ^ (x >> 27)) * (uint32_t)1566083941UL;
378 }
379
380 /**
381 * This function certificate the period of 2^{MEXP}
382 */
period_certification(sfmt_t * ctx)383 static void period_certification(sfmt_t *ctx) {
384 int inner = 0;
385 int i, j;
386 uint32_t work;
387 uint32_t *psfmt32 = &ctx->sfmt[0].u[0];
388
389 for (i = 0; i < 4; i++)
390 inner ^= psfmt32[idxof(i)] & parity[i];
391 for (i = 16; i > 0; i >>= 1)
392 inner ^= inner >> i;
393 inner &= 1;
394 /* check OK */
395 if (inner == 1) {
396 return;
397 }
398 /* check NG, and modification */
399 for (i = 0; i < 4; i++) {
400 work = 1;
401 for (j = 0; j < 32; j++) {
402 if ((work & parity[i]) != 0) {
403 psfmt32[idxof(i)] ^= work;
404 return;
405 }
406 work = work << 1;
407 }
408 }
409 }
410
411 /*----------------
412 PUBLIC FUNCTIONS
413 ----------------*/
414 /**
415 * This function returns the identification string.
416 * The string shows the word size, the Mersenne exponent,
417 * and all parameters of this generator.
418 */
get_idstring(void)419 const char *get_idstring(void) {
420 return IDSTR;
421 }
422
423 /**
424 * This function returns the minimum size of array used for \b
425 * fill_array32() function.
426 * @return minimum size of array used for fill_array32() function.
427 */
get_min_array_size32(void)428 int get_min_array_size32(void) {
429 return N32;
430 }
431
432 /**
433 * This function returns the minimum size of array used for \b
434 * fill_array64() function.
435 * @return minimum size of array used for fill_array64() function.
436 */
get_min_array_size64(void)437 int get_min_array_size64(void) {
438 return N64;
439 }
440
441 #ifndef ONLY64
442 /**
443 * This function generates and returns 32-bit pseudorandom number.
444 * init_gen_rand or init_by_array must be called before this function.
445 * @return 32-bit pseudorandom number
446 */
gen_rand32(sfmt_t * ctx)447 uint32_t gen_rand32(sfmt_t *ctx) {
448 uint32_t r;
449 uint32_t *psfmt32 = &ctx->sfmt[0].u[0];
450
451 assert(ctx->initialized);
452 if (ctx->idx >= N32) {
453 gen_rand_all(ctx);
454 ctx->idx = 0;
455 }
456 r = psfmt32[ctx->idx++];
457 return r;
458 }
459
460 /* Generate a random integer in [0..limit). */
gen_rand32_range(sfmt_t * ctx,uint32_t limit)461 uint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit) {
462 uint32_t ret, above;
463
464 above = 0xffffffffU - (0xffffffffU % limit);
465 while (1) {
466 ret = gen_rand32(ctx);
467 if (ret < above) {
468 ret %= limit;
469 break;
470 }
471 }
472 return ret;
473 }
474 #endif
475 /**
476 * This function generates and returns 64-bit pseudorandom number.
477 * init_gen_rand or init_by_array must be called before this function.
478 * The function gen_rand64 should not be called after gen_rand32,
479 * unless an initialization is again executed.
480 * @return 64-bit pseudorandom number
481 */
gen_rand64(sfmt_t * ctx)482 uint64_t gen_rand64(sfmt_t *ctx) {
483 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
484 uint32_t r1, r2;
485 uint32_t *psfmt32 = &ctx->sfmt[0].u[0];
486 #else
487 uint64_t r;
488 uint64_t *psfmt64 = (uint64_t *)&ctx->sfmt[0].u[0];
489 #endif
490
491 assert(ctx->initialized);
492 assert(ctx->idx % 2 == 0);
493
494 if (ctx->idx >= N32) {
495 gen_rand_all(ctx);
496 ctx->idx = 0;
497 }
498 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
499 r1 = psfmt32[ctx->idx];
500 r2 = psfmt32[ctx->idx + 1];
501 ctx->idx += 2;
502 return ((uint64_t)r2 << 32) | r1;
503 #else
504 r = psfmt64[ctx->idx / 2];
505 ctx->idx += 2;
506 return r;
507 #endif
508 }
509
510 /* Generate a random integer in [0..limit). */
gen_rand64_range(sfmt_t * ctx,uint64_t limit)511 uint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit) {
512 uint64_t ret, above;
513
514 above = KQU(0xffffffffffffffff) - (KQU(0xffffffffffffffff) % limit);
515 while (1) {
516 ret = gen_rand64(ctx);
517 if (ret < above) {
518 ret %= limit;
519 break;
520 }
521 }
522 return ret;
523 }
524
525 #ifndef ONLY64
526 /**
527 * This function generates pseudorandom 32-bit integers in the
528 * specified array[] by one call. The number of pseudorandom integers
529 * is specified by the argument size, which must be at least 624 and a
530 * multiple of four. The generation by this function is much faster
531 * than the following gen_rand function.
532 *
533 * For initialization, init_gen_rand or init_by_array must be called
534 * before the first call of this function. This function can not be
535 * used after calling gen_rand function, without initialization.
536 *
537 * @param array an array where pseudorandom 32-bit integers are filled
538 * by this function. The pointer to the array must be \b "aligned"
539 * (namely, must be a multiple of 16) in the SIMD version, since it
540 * refers to the address of a 128-bit integer. In the standard C
541 * version, the pointer is arbitrary.
542 *
543 * @param size the number of 32-bit pseudorandom integers to be
544 * generated. size must be a multiple of 4, and greater than or equal
545 * to (MEXP / 128 + 1) * 4.
546 *
547 * @note \b memalign or \b posix_memalign is available to get aligned
548 * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
549 * returns the pointer to the aligned memory block.
550 */
fill_array32(sfmt_t * ctx,uint32_t * array,int size)551 void fill_array32(sfmt_t *ctx, uint32_t *array, int size) {
552 assert(ctx->initialized);
553 assert(ctx->idx == N32);
554 assert(size % 4 == 0);
555 assert(size >= N32);
556
557 gen_rand_array(ctx, (w128_t *)array, size / 4);
558 ctx->idx = N32;
559 }
560 #endif
561
562 /**
563 * This function generates pseudorandom 64-bit integers in the
564 * specified array[] by one call. The number of pseudorandom integers
565 * is specified by the argument size, which must be at least 312 and a
566 * multiple of two. The generation by this function is much faster
567 * than the following gen_rand function.
568 *
569 * For initialization, init_gen_rand or init_by_array must be called
570 * before the first call of this function. This function can not be
571 * used after calling gen_rand function, without initialization.
572 *
573 * @param array an array where pseudorandom 64-bit integers are filled
574 * by this function. The pointer to the array must be "aligned"
575 * (namely, must be a multiple of 16) in the SIMD version, since it
576 * refers to the address of a 128-bit integer. In the standard C
577 * version, the pointer is arbitrary.
578 *
579 * @param size the number of 64-bit pseudorandom integers to be
580 * generated. size must be a multiple of 2, and greater than or equal
581 * to (MEXP / 128 + 1) * 2
582 *
583 * @note \b memalign or \b posix_memalign is available to get aligned
584 * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
585 * returns the pointer to the aligned memory block.
586 */
fill_array64(sfmt_t * ctx,uint64_t * array,int size)587 void fill_array64(sfmt_t *ctx, uint64_t *array, int size) {
588 assert(ctx->initialized);
589 assert(ctx->idx == N32);
590 assert(size % 2 == 0);
591 assert(size >= N64);
592
593 gen_rand_array(ctx, (w128_t *)array, size / 2);
594 ctx->idx = N32;
595
596 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
597 swap((w128_t *)array, size /2);
598 #endif
599 }
600
601 /**
602 * This function initializes the internal state array with a 32-bit
603 * integer seed.
604 *
605 * @param seed a 32-bit integer used as the seed.
606 */
init_gen_rand(uint32_t seed)607 sfmt_t *init_gen_rand(uint32_t seed) {
608 void *p;
609 sfmt_t *ctx;
610 int i;
611 uint32_t *psfmt32;
612
613 if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {
614 return NULL;
615 }
616 ctx = (sfmt_t *)p;
617 psfmt32 = &ctx->sfmt[0].u[0];
618
619 psfmt32[idxof(0)] = seed;
620 for (i = 1; i < N32; i++) {
621 psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]
622 ^ (psfmt32[idxof(i - 1)] >> 30))
623 + i;
624 }
625 ctx->idx = N32;
626 period_certification(ctx);
627 ctx->initialized = 1;
628
629 return ctx;
630 }
631
632 /**
633 * This function initializes the internal state array,
634 * with an array of 32-bit integers used as the seeds
635 * @param init_key the array of 32-bit integers, used as a seed.
636 * @param key_length the length of init_key.
637 */
init_by_array(uint32_t * init_key,int key_length)638 sfmt_t *init_by_array(uint32_t *init_key, int key_length) {
639 void *p;
640 sfmt_t *ctx;
641 int i, j, count;
642 uint32_t r;
643 int lag;
644 int mid;
645 int size = N * 4;
646 uint32_t *psfmt32;
647
648 if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {
649 return NULL;
650 }
651 ctx = (sfmt_t *)p;
652 psfmt32 = &ctx->sfmt[0].u[0];
653
654 if (size >= 623) {
655 lag = 11;
656 } else if (size >= 68) {
657 lag = 7;
658 } else if (size >= 39) {
659 lag = 5;
660 } else {
661 lag = 3;
662 }
663 mid = (size - lag) / 2;
664
665 memset(ctx->sfmt, 0x8b, sizeof(ctx->sfmt));
666 if (key_length + 1 > N32) {
667 count = key_length + 1;
668 } else {
669 count = N32;
670 }
671 r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)]
672 ^ psfmt32[idxof(N32 - 1)]);
673 psfmt32[idxof(mid)] += r;
674 r += key_length;
675 psfmt32[idxof(mid + lag)] += r;
676 psfmt32[idxof(0)] = r;
677
678 count--;
679 for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
680 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
681 ^ psfmt32[idxof((i + N32 - 1) % N32)]);
682 psfmt32[idxof((i + mid) % N32)] += r;
683 r += init_key[j] + i;
684 psfmt32[idxof((i + mid + lag) % N32)] += r;
685 psfmt32[idxof(i)] = r;
686 i = (i + 1) % N32;
687 }
688 for (; j < count; j++) {
689 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
690 ^ psfmt32[idxof((i + N32 - 1) % N32)]);
691 psfmt32[idxof((i + mid) % N32)] += r;
692 r += i;
693 psfmt32[idxof((i + mid + lag) % N32)] += r;
694 psfmt32[idxof(i)] = r;
695 i = (i + 1) % N32;
696 }
697 for (j = 0; j < N32; j++) {
698 r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]
699 + psfmt32[idxof((i + N32 - 1) % N32)]);
700 psfmt32[idxof((i + mid) % N32)] ^= r;
701 r -= i;
702 psfmt32[idxof((i + mid + lag) % N32)] ^= r;
703 psfmt32[idxof(i)] = r;
704 i = (i + 1) % N32;
705 }
706
707 ctx->idx = N32;
708 period_certification(ctx);
709 ctx->initialized = 1;
710
711 return ctx;
712 }
713
fini_gen_rand(sfmt_t * ctx)714 void fini_gen_rand(sfmt_t *ctx) {
715 assert(ctx != NULL);
716
717 ctx->initialized = 0;
718 free(ctx);
719 }
720