5 * @brief double precision SIMD oriented Fast Mersenne Twister(dSFMT)
6 * pseudorandom number generator based on IEEE 754 format.
8 * @author Mutsuo Saito (Hiroshima University)
9 * @author Makoto Matsumoto (Hiroshima University)
11 * Copyright (C) 2007, 2008 Mutsuo Saito, Makoto Matsumoto and
12 * Hiroshima University. All rights reserved.
13 * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto,
14 * Hiroshima University and The University of Tokyo.
15 * All rights reserved.
17 * The new BSD License is applied to this software.
20 * @note We assume that your system has inttypes.h. If your system
21 * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
22 * and you have to define PRIu64 and PRIx64 in this file as follows:
24 typedef unsigned int uint32_t
25 typedef unsigned long long uint64_t
29 * uint32_t must be exactly 32-bit unsigned integer type (no more, no
30 * less), and uint64_t must be exactly 64-bit unsigned integer type.
31 * PRIu64 and PRIx64 are used for printf function to print 64-bit
32 * unsigned int and 64-bit unsigned int in hexadecimal format.
37 #if defined(__cplusplus)
44 #if !defined(DSFMT_MEXP)
46 #warning "DSFMT_MEXP is not defined. I assume DSFMT_MEXP is 19937."
48 #define DSFMT_MEXP 19937
53 /* Mersenne Exponent. The period of the sequence
54 * is a multiple of 2^DSFMT_MEXP-1.
55 * #define DSFMT_MEXP 19937 */
56 /** DSFMT generator has an internal state array of 128-bit integers,
57 * and N is its size. */
58 #define DSFMT_N ((DSFMT_MEXP - 128) / 104 + 1)
59 /** N32 is the size of internal state array when regarded as an array
60 * of 32-bit integers.*/
61 #define DSFMT_N32 (DSFMT_N * 4)
62 /** N64 is the size of internal state array when regarded as an array
63 * of 64-bit integers.*/
64 #define DSFMT_N64 (DSFMT_N * 2)
66 #if !defined(DSFMT_BIG_ENDIAN)
67 # if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
68 # if __BYTE_ORDER == __BIG_ENDIAN
69 # define DSFMT_BIG_ENDIAN 1
71 # elif defined(_BYTE_ORDER) && defined(_BIG_ENDIAN)
72 # if _BYTE_ORDER == _BIG_ENDIAN
73 # define DSFMT_BIG_ENDIAN 1
75 # elif defined(__BYTE_ORDER__) && defined(__BIG_ENDIAN__)
76 # if __BYTE_ORDER__ == __BIG_ENDIAN__
77 # define DSFMT_BIG_ENDIAN 1
79 # elif defined(BYTE_ORDER) && defined(BIG_ENDIAN)
80 # if BYTE_ORDER == BIG_ENDIAN
81 # define DSFMT_BIG_ENDIAN 1
83 # elif defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN) \
84 || defined(__BIG_ENDIAN__) || defined(BIG_ENDIAN)
85 # define DSFMT_BIG_ENDIAN 1
89 #if defined(DSFMT_BIG_ENDIAN) && defined(__amd64)
90 # undef DSFMT_BIG_ENDIAN
93 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
94 # include <inttypes.h>
95 #elif defined(_MSC_VER) || defined(__BORLANDC__)
96 # if !defined(DSFMT_UINT32_DEFINED) && !defined(SFMT_UINT32_DEFINED)
97 typedef unsigned int uint32_t;
98 typedef unsigned __int64 uint64_t;
100 # define UINT64_C(v) (v ## ui64)
102 # define DSFMT_UINT32_DEFINED
103 # if !defined(inline) && !defined(__cplusplus)
104 # define inline __inline
108 # include <inttypes.h>
109 # if !defined(inline) && !defined(__cplusplus)
110 # if defined(__GNUC__)
111 # define inline __inline__
119 # if defined(_MSC_VER) || defined(__BORLANDC__)
120 # define PRIu64 "I64u"
121 # define PRIx64 "I64x"
123 # define PRIu64 "llu"
124 # define PRIx64 "llx"
129 # define UINT64_C(v) (v ## ULL)
132 /*------------------------------------------
133 128-bit SIMD like data type for standard C
134 ------------------------------------------*/
135 #if defined(HAVE_ALTIVEC)
136 # if !defined(__APPLE__)
137 # include <altivec.h>
139 /** 128-bit data structure */
141 vector unsigned int s;
147 #elif defined(HAVE_SSE2)
148 # include <emmintrin.h>
150 /** 128-bit data structure */
158 #else /* standard C */
159 /** 128-bit data structure */
167 /** 128-bit data type */
168 typedef union W128_T w128_t;
170 /** the 128-bit internal state array */
172 w128_t status[DSFMT_N + 1];
175 typedef struct DSFMT_T dsfmt_t;
177 /** dsfmt internal state vector */
178 extern dsfmt_t dsfmt_global_data;
179 /** dsfmt mexp for check */
180 extern const int dsfmt_global_mexp;
182 void dsfmt_gen_rand_all(dsfmt_t *dsfmt);
183 void dsfmt_fill_array_open_close(dsfmt_t *dsfmt, double array[], int size);
184 void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size);
185 void dsfmt_fill_array_open_open(dsfmt_t *dsfmt, double array[], int size);
186 void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size);
187 void dsfmt_chk_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed, int mexp);
188 void dsfmt_chk_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
189 int key_length, int mexp);
190 const char *dsfmt_get_idstring(void);
191 int dsfmt_get_min_array_size(void);
193 #if defined(__GNUC__)
194 # define DSFMT_PRE_INLINE inline static
195 # define DSFMT_PST_INLINE __attribute__((always_inline))
196 #elif defined(_MSC_VER) && _MSC_VER >= 1200
197 # define DSFMT_PRE_INLINE __forceinline static
198 # define DSFMT_PST_INLINE
200 # define DSFMT_PRE_INLINE inline static
201 # define DSFMT_PST_INLINE
203 DSFMT_PRE_INLINE uint32_t dsfmt_genrand_uint32(dsfmt_t *dsfmt) DSFMT_PST_INLINE;
204 DSFMT_PRE_INLINE double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt)
206 DSFMT_PRE_INLINE double dsfmt_genrand_close_open(dsfmt_t *dsfmt)
208 DSFMT_PRE_INLINE double dsfmt_genrand_open_close(dsfmt_t *dsfmt)
210 DSFMT_PRE_INLINE double dsfmt_genrand_open_open(dsfmt_t *dsfmt)
212 DSFMT_PRE_INLINE uint32_t dsfmt_gv_genrand_uint32(void) DSFMT_PST_INLINE;
213 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close1_open2(void) DSFMT_PST_INLINE;
214 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close_open(void) DSFMT_PST_INLINE;
215 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_close(void) DSFMT_PST_INLINE;
216 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_open(void) DSFMT_PST_INLINE;
217 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_close(double array[], int size)
219 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close_open(double array[], int size)
221 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_open(double array[], int size)
223 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close1_open2(double array[], int size)
225 DSFMT_PRE_INLINE void dsfmt_gv_init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
226 DSFMT_PRE_INLINE void dsfmt_gv_init_by_array(uint32_t init_key[],
227 int key_length) DSFMT_PST_INLINE;
228 DSFMT_PRE_INLINE void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed)
230 DSFMT_PRE_INLINE void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
231 int key_length) DSFMT_PST_INLINE;
234 * This function generates and returns unsigned 32-bit integer.
235 * This is slower than SFMT, only for convenience usage.
236 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
237 * before this function.
238 * @param dsfmt dsfmt internal state date
239 * @return double precision floating point pseudorandom number
241 inline static uint32_t dsfmt_genrand_uint32(dsfmt_t *dsfmt) {
243 uint64_t *psfmt64 = &dsfmt->status[0].u[0];
245 if (dsfmt->idx >= DSFMT_N64) {
246 dsfmt_gen_rand_all(dsfmt);
249 r = psfmt64[dsfmt->idx++] & 0xffffffffU;
254 * This function generates and returns double precision pseudorandom
255 * number which distributes uniformly in the range [1, 2). This is
256 * the primitive and faster than generating numbers in other ranges.
257 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
258 * before this function.
259 * @param dsfmt dsfmt internal state date
260 * @return double precision floating point pseudorandom number
262 inline static double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt) {
264 double *psfmt64 = &dsfmt->status[0].d[0];
266 if (dsfmt->idx >= DSFMT_N64) {
267 dsfmt_gen_rand_all(dsfmt);
270 r = psfmt64[dsfmt->idx++];
275 * This function generates and returns unsigned 32-bit integer.
276 * This is slower than SFMT, only for convenience usage.
277 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
278 * before this function. This function uses \b global variables.
279 * @return double precision floating point pseudorandom number
281 inline static uint32_t dsfmt_gv_genrand_uint32(void) {
282 return dsfmt_genrand_uint32(&dsfmt_global_data);
286 * This function generates and returns double precision pseudorandom
287 * number which distributes uniformly in the range [1, 2).
288 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
289 * before this function. This function uses \b global variables.
290 * @return double precision floating point pseudorandom number
292 inline static double dsfmt_gv_genrand_close1_open2(void) {
293 return dsfmt_genrand_close1_open2(&dsfmt_global_data);
297 * This function generates and returns double precision pseudorandom
298 * number which distributes uniformly in the range [0, 1).
299 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
300 * before this function.
301 * @param dsfmt dsfmt internal state date
302 * @return double precision floating point pseudorandom number
304 inline static double dsfmt_genrand_close_open(dsfmt_t *dsfmt) {
305 return dsfmt_genrand_close1_open2(dsfmt) - 1.0;
309 * This function generates and returns double precision pseudorandom
310 * number which distributes uniformly in the range [0, 1).
311 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
312 * before this function. This function uses \b global variables.
313 * @return double precision floating point pseudorandom number
315 inline static double dsfmt_gv_genrand_close_open(void) {
316 return dsfmt_gv_genrand_close1_open2() - 1.0;
320 * This function generates and returns double precision pseudorandom
321 * number which distributes uniformly in the range (0, 1].
322 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
323 * before this function.
324 * @param dsfmt dsfmt internal state date
325 * @return double precision floating point pseudorandom number
327 inline static double dsfmt_genrand_open_close(dsfmt_t *dsfmt) {
328 return 2.0 - dsfmt_genrand_close1_open2(dsfmt);
332 * This function generates and returns double precision pseudorandom
333 * number which distributes uniformly in the range (0, 1].
334 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
335 * before this function. This function uses \b global variables.
336 * @return double precision floating point pseudorandom number
338 inline static double dsfmt_gv_genrand_open_close(void) {
339 return 2.0 - dsfmt_gv_genrand_close1_open2();
343 * This function generates and returns double precision pseudorandom
344 * number which distributes uniformly in the range (0, 1).
345 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
346 * before this function.
347 * @param dsfmt dsfmt internal state date
348 * @return double precision floating point pseudorandom number
350 inline static double dsfmt_genrand_open_open(dsfmt_t *dsfmt) {
351 double *dsfmt64 = &dsfmt->status[0].d[0];
357 if (dsfmt->idx >= DSFMT_N64) {
358 dsfmt_gen_rand_all(dsfmt);
361 r.d = dsfmt64[dsfmt->idx++];
367 * This function generates and returns double precision pseudorandom
368 * number which distributes uniformly in the range (0, 1).
369 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
370 * before this function. This function uses \b global variables.
371 * @return double precision floating point pseudorandom number
373 inline static double dsfmt_gv_genrand_open_open(void) {
374 return dsfmt_genrand_open_open(&dsfmt_global_data);
378 * This function generates double precision floating point
379 * pseudorandom numbers which distribute in the range [1, 2) to the
380 * specified array[] by one call. This function is the same as
381 * dsfmt_fill_array_close1_open2() except that this function uses
382 * \b global variables.
383 * @param array an array where pseudorandom numbers are filled
385 * @param size the number of pseudorandom numbers to be generated.
386 * see also \sa dsfmt_fill_array_close1_open2()
388 inline static void dsfmt_gv_fill_array_close1_open2(double array[], int size) {
389 dsfmt_fill_array_close1_open2(&dsfmt_global_data, array, size);
393 * This function generates double precision floating point
394 * pseudorandom numbers which distribute in the range (0, 1] to the
395 * specified array[] by one call. This function is the same as
396 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
397 * This function uses \b global variables.
398 * @param array an array where pseudorandom numbers are filled
400 * @param size the number of pseudorandom numbers to be generated.
401 * see also \sa dsfmt_fill_array_close1_open2() and \sa
402 * dsfmt_gv_fill_array_close1_open2()
404 inline static void dsfmt_gv_fill_array_open_close(double array[], int size) {
405 dsfmt_fill_array_open_close(&dsfmt_global_data, array, size);
409 * This function generates double precision floating point
410 * pseudorandom numbers which distribute in the range [0, 1) to the
411 * specified array[] by one call. This function is the same as
412 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
413 * This function uses \b global variables.
414 * @param array an array where pseudorandom numbers are filled
416 * @param size the number of pseudorandom numbers to be generated.
417 * see also \sa dsfmt_fill_array_close1_open2() \sa
418 * dsfmt_gv_fill_array_close1_open2()
420 inline static void dsfmt_gv_fill_array_close_open(double array[], int size) {
421 dsfmt_fill_array_close_open(&dsfmt_global_data, array, size);
425 * This function generates double precision floating point
426 * pseudorandom numbers which distribute in the range (0, 1) to the
427 * specified array[] by one call. This function is the same as
428 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
429 * This function uses \b global variables.
430 * @param array an array where pseudorandom numbers are filled
432 * @param size the number of pseudorandom numbers to be generated.
433 * see also \sa dsfmt_fill_array_close1_open2() \sa
434 * dsfmt_gv_fill_array_close1_open2()
436 inline static void dsfmt_gv_fill_array_open_open(double array[], int size) {
437 dsfmt_fill_array_open_open(&dsfmt_global_data, array, size);
441 * This function initializes the internal state array with a 32-bit
443 * @param dsfmt dsfmt state vector.
444 * @param seed a 32-bit integer used as the seed.
446 inline static void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed) {
447 dsfmt_chk_init_gen_rand(dsfmt, seed, DSFMT_MEXP);
451 * This function initializes the internal state array with a 32-bit
452 * integer seed. This function uses \b global variables.
453 * @param seed a 32-bit integer used as the seed.
454 * see also \sa dsfmt_init_gen_rand()
456 inline static void dsfmt_gv_init_gen_rand(uint32_t seed) {
457 dsfmt_init_gen_rand(&dsfmt_global_data, seed);
461 * This function initializes the internal state array,
462 * with an array of 32-bit integers used as the seeds.
463 * @param dsfmt dsfmt state vector
464 * @param init_key the array of 32-bit integers, used as a seed.
465 * @param key_length the length of init_key.
467 inline static void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
469 dsfmt_chk_init_by_array(dsfmt, init_key, key_length, DSFMT_MEXP);
473 * This function initializes the internal state array,
474 * with an array of 32-bit integers used as the seeds.
475 * This function uses \b global variables.
476 * @param init_key the array of 32-bit integers, used as a seed.
477 * @param key_length the length of init_key.
478 * see also \sa dsfmt_init_by_array()
480 inline static void dsfmt_gv_init_by_array(uint32_t init_key[], int key_length) {
481 dsfmt_init_by_array(&dsfmt_global_data, init_key, key_length);
484 #if !defined(DSFMT_DO_NOT_USE_OLD_NAMES)
485 DSFMT_PRE_INLINE const char *get_idstring(void) DSFMT_PST_INLINE;
486 DSFMT_PRE_INLINE int get_min_array_size(void) DSFMT_PST_INLINE;
487 DSFMT_PRE_INLINE void init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
488 DSFMT_PRE_INLINE void init_by_array(uint32_t init_key[], int key_length)
490 DSFMT_PRE_INLINE double genrand_close1_open2(void) DSFMT_PST_INLINE;
491 DSFMT_PRE_INLINE double genrand_close_open(void) DSFMT_PST_INLINE;
492 DSFMT_PRE_INLINE double genrand_open_close(void) DSFMT_PST_INLINE;
493 DSFMT_PRE_INLINE double genrand_open_open(void) DSFMT_PST_INLINE;
494 DSFMT_PRE_INLINE void fill_array_open_close(double array[], int size)
496 DSFMT_PRE_INLINE void fill_array_close_open(double array[], int size)
498 DSFMT_PRE_INLINE void fill_array_open_open(double array[], int size)
500 DSFMT_PRE_INLINE void fill_array_close1_open2(double array[], int size)
504 * This function is just the same as dsfmt_get_idstring().
506 * see also \sa dsfmt_get_idstring()
508 inline static const char *get_idstring(void) {
509 return dsfmt_get_idstring();
513 * This function is just the same as dsfmt_get_min_array_size().
514 * @return minimum size of array used for fill_array functions.
515 * see also \sa dsfmt_get_min_array_size()
517 inline static int get_min_array_size(void) {
518 return dsfmt_get_min_array_size();
522 * This function is just the same as dsfmt_gv_init_gen_rand().
523 * @param seed a 32-bit integer used as the seed.
524 * see also \sa dsfmt_gv_init_gen_rand(), \sa dsfmt_init_gen_rand().
526 inline static void init_gen_rand(uint32_t seed) {
527 dsfmt_gv_init_gen_rand(seed);
531 * This function is just the same as dsfmt_gv_init_by_array().
532 * @param init_key the array of 32-bit integers, used as a seed.
533 * @param key_length the length of init_key.
534 * see also \sa dsfmt_gv_init_by_array(), \sa dsfmt_init_by_array().
536 inline static void init_by_array(uint32_t init_key[], int key_length) {
537 dsfmt_gv_init_by_array(init_key, key_length);
541 * This function is just the same as dsfmt_gv_genrand_close1_open2().
542 * @return double precision floating point number.
543 * see also \sa dsfmt_genrand_close1_open2() \sa
544 * dsfmt_gv_genrand_close1_open2()
546 inline static double genrand_close1_open2(void) {
547 return dsfmt_gv_genrand_close1_open2();
551 * This function is just the same as dsfmt_gv_genrand_close_open().
552 * @return double precision floating point number.
553 * see also \sa dsfmt_genrand_close_open() \sa
554 * dsfmt_gv_genrand_close_open()
556 inline static double genrand_close_open(void) {
557 return dsfmt_gv_genrand_close_open();
561 * This function is just the same as dsfmt_gv_genrand_open_close().
562 * @return double precision floating point number.
563 * see also \sa dsfmt_genrand_open_close() \sa
564 * dsfmt_gv_genrand_open_close()
566 inline static double genrand_open_close(void) {
567 return dsfmt_gv_genrand_open_close();
571 * This function is just the same as dsfmt_gv_genrand_open_open().
572 * @return double precision floating point number.
573 * see also \sa dsfmt_genrand_open_open() \sa
574 * dsfmt_gv_genrand_open_open()
576 inline static double genrand_open_open(void) {
577 return dsfmt_gv_genrand_open_open();
581 * This function is juset the same as dsfmt_gv_fill_array_open_close().
582 * @param array an array where pseudorandom numbers are filled
584 * @param size the number of pseudorandom numbers to be generated.
585 * see also \sa dsfmt_gv_fill_array_open_close(), \sa
586 * dsfmt_fill_array_close1_open2(), \sa
587 * dsfmt_gv_fill_array_close1_open2()
589 inline static void fill_array_open_close(double array[], int size) {
590 dsfmt_gv_fill_array_open_close(array, size);
594 * This function is juset the same as dsfmt_gv_fill_array_close_open().
595 * @param array an array where pseudorandom numbers are filled
597 * @param size the number of pseudorandom numbers to be generated.
598 * see also \sa dsfmt_gv_fill_array_close_open(), \sa
599 * dsfmt_fill_array_close1_open2(), \sa
600 * dsfmt_gv_fill_array_close1_open2()
602 inline static void fill_array_close_open(double array[], int size) {
603 dsfmt_gv_fill_array_close_open(array, size);
607 * This function is juset the same as dsfmt_gv_fill_array_open_open().
608 * @param array an array where pseudorandom numbers are filled
610 * @param size the number of pseudorandom numbers to be generated.
611 * see also \sa dsfmt_gv_fill_array_open_open(), \sa
612 * dsfmt_fill_array_close1_open2(), \sa
613 * dsfmt_gv_fill_array_close1_open2()
615 inline static void fill_array_open_open(double array[], int size) {
616 dsfmt_gv_fill_array_open_open(array, size);
620 * This function is juset the same as dsfmt_gv_fill_array_close1_open2().
621 * @param array an array where pseudorandom numbers are filled
623 * @param size the number of pseudorandom numbers to be generated.
624 * see also \sa dsfmt_fill_array_close1_open2(), \sa
625 * dsfmt_gv_fill_array_close1_open2()
627 inline static void fill_array_close1_open2(double array[], int size) {
628 dsfmt_gv_fill_array_close1_open2(array, size);
630 #endif /* DSFMT_DO_NOT_USE_OLD_NAMES */
632 #if defined(__cplusplus)