4 * @brief double precision SIMD oriented Fast Mersenne Twister(dSFMT)
5 * pseudorandom number generator based on IEEE 754 format.
7 * @author Mutsuo Saito (Hiroshima University)
8 * @author Makoto Matsumoto (Hiroshima University)
10 * Copyright (C) 2007, 2008 Mutsuo Saito, Makoto Matsumoto and
11 * Hiroshima University. All rights reserved.
13 * The new BSD License is applied to this software.
16 * @note We assume that your system has inttypes.h. If your system
17 * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
18 * and you have to define PRIu64 and PRIx64 in this file as follows:
20 typedef unsigned int uint32_t
21 typedef unsigned long long uint64_t
25 * uint32_t must be exactly 32-bit unsigned integer type (no more, no
26 * less), and uint64_t must be exactly 64-bit unsigned integer type.
27 * PRIu64 and PRIx64 are used for printf function to print 64-bit
28 * unsigned int and 64-bit unsigned int in hexadecimal format.
37 #if !defined(DSFMT_MEXP)
39 #warning "DSFMT_MEXP is not defined. I assume DSFMT_MEXP is 19937."
41 #define DSFMT_MEXP 19937
46 /** Mersenne Exponent. The period of the sequence
47 * is a multiple of 2^DSFMT_MEXP-1.
48 * #define DSFMT_MEXP 19937 */
49 /** DSFMT generator has an internal state array of 128-bit integers,
50 * and N is its size. */
51 #define DSFMT_N ((DSFMT_MEXP - 128) / 104 + 1)
52 /** N32 is the size of internal state array when regarded as an array
53 * of 32-bit integers.*/
54 #define DSFMT_N32 (DSFMT_N * 4)
55 /** N64 is the size of internal state array when regarded as an array
56 * of 64-bit integers.*/
57 #define DSFMT_N64 (DSFMT_N * 2)
59 #if !defined(DSFMT_BIG_ENDIAN)
60 # if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN)
61 # if __BYTE_ORDER == __BIG_ENDIAN
62 # define DSFMT_BIG_ENDIAN 1
64 # elif defined(_BYTE_ORDER) && defined(_BIG_ENDIAN)
65 # if _BYTE_ORDER == _BIG_ENDIAN
66 # define DSFMT_BIG_ENDIAN 1
68 # elif defined(__BYTE_ORDER__) && defined(__BIG_ENDIAN__)
69 # if __BYTE_ORDER__ == __BIG_ENDIAN__
70 # define DSFMT_BIG_ENDIAN 1
72 # elif defined(BYTE_ORDER) && defined(BIG_ENDIAN)
73 # if BYTE_ORDER == BIG_ENDIAN
74 # define DSFMT_BIG_ENDIAN 1
76 # elif defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN) \
77 || defined(__BIG_ENDIAN__) || defined(BIG_ENDIAN)
78 # define DSFMT_BIG_ENDIAN 1
82 #if defined(DSFMT_BIG_ENDIAN) && defined(__amd64)
83 # undef DSFMT_BIG_ENDIAN
86 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
87 # include <inttypes.h>
88 #elif defined(_MSC_VER)
89 # if !defined(DSFMT_UINT32_DEFINED) && !defined(SFMT_UINT32_DEFINED)
90 typedef unsigned int uint32_t;
91 typedef unsigned long long uint64_t;
92 # define DSFMT_UINT32_DEFINED
94 # define inline __inline
97 #elif defined(__BORLANDC__)
98 # if !defined(DSFMT_UINT32_DEFINED) && !defined(SFMT_UINT32_DEFINED)
99 typedef unsigned int uint32_t;
100 typedef unsigned __int64 uint64_t;
101 # define DSFMT_UINT32_DEFINED
102 # if !defined(inline)
103 # define inline __inline
107 # include <inttypes.h>
108 # if !defined(inline)
109 # if defined(__GNUC__)
110 # define inline __inline__
118 # if defined(_MSC_VER) || defined(__BORLANDC__)
119 # define PRIu64 "I64u"
120 # define PRIx64 "I64x"
122 # define PRIu64 "llu"
123 # define PRIx64 "llx"
128 # if defined(__BORLANDC__)
129 # define UINT64_C(v) (v ## UI64)
131 # define UINT64_C(v) (v ## ULL)
136 /*------------------------------------------
137 128-bit SIMD like data type for standard C
138 ------------------------------------------*/
139 #if defined(HAVE_ALTIVEC)
140 # if !defined(__APPLE__)
141 # include <altivec.h>
143 /** 128-bit data structure */
145 vector unsigned int s;
151 #elif defined(HAVE_SSE2)
152 # include <emmintrin.h>
154 /** 128-bit data structure */
162 #else /* standard C */
163 /** 128-bit data structure */
171 /** 128-bit data type */
172 typedef union W128_T w128_t;
174 /** the 128-bit internal state array */
176 w128_t status[DSFMT_N + 1];
179 typedef struct DSFMT_T dsfmt_t;
181 /** dsfmt internal state vector */
182 extern dsfmt_t dsfmt_global_data;
183 /** dsfmt mexp for check */
184 extern const int dsfmt_global_mexp;
190 void dsfmt_gen_rand_all(dsfmt_t *dsfmt);
191 void dsfmt_fill_array_open_close(dsfmt_t *dsfmt, double array[], int size);
192 void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size);
193 void dsfmt_fill_array_open_open(dsfmt_t *dsfmt, double array[], int size);
194 void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size);
195 void dsfmt_chk_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed, int mexp);
196 void dsfmt_chk_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
197 int key_length, int mexp);
198 const char *dsfmt_get_idstring(void);
199 int dsfmt_get_min_array_size(void);
201 #if defined(__GNUC__)
202 # define DSFMT_PRE_INLINE inline static
203 # define DSFMT_PST_INLINE __attribute__((always_inline))
204 #elif defined(_MSC_VER) && _MSC_VER >= 1200
205 # define DSFMT_PRE_INLINE __forceinline
206 # define DSFMT_PST_INLINE
208 # define DSFMT_PRE_INLINE inline static
209 # define DSFMT_PST_INLINE
211 DSFMT_PRE_INLINE double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt)
213 DSFMT_PRE_INLINE double dsfmt_genrand_close_open(dsfmt_t *dsfmt)
215 DSFMT_PRE_INLINE double dsfmt_genrand_open_close(dsfmt_t *dsfmt)
217 DSFMT_PRE_INLINE double dsfmt_genrand_open_open(dsfmt_t *dsfmt)
219 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close1_open2(void) DSFMT_PST_INLINE;
220 DSFMT_PRE_INLINE double dsfmt_gv_genrand_close_open(void) DSFMT_PST_INLINE;
221 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_close(void) DSFMT_PST_INLINE;
222 DSFMT_PRE_INLINE double dsfmt_gv_genrand_open_open(void) DSFMT_PST_INLINE;
223 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_close(double array[], int size)
225 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close_open(double array[], int size)
227 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_open_open(double array[], int size)
229 DSFMT_PRE_INLINE void dsfmt_gv_fill_array_close1_open2(double array[], int size)
231 DSFMT_PRE_INLINE void dsfmt_gv_init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
232 DSFMT_PRE_INLINE void dsfmt_gv_init_by_array(uint32_t init_key[],
233 int key_length) DSFMT_PST_INLINE;
234 DSFMT_PRE_INLINE void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed)
236 DSFMT_PRE_INLINE void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
237 int key_length) DSFMT_PST_INLINE;
240 * This function generates and returns double precision pseudorandom
241 * number which distributes uniformly in the range [1, 2). This is
242 * the primitive and faster than generating numbers in other ranges.
243 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
244 * before this function.
245 * @param dsfmt dsfmt internal state date
246 * @return double precision floating point pseudorandom number
248 inline static double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt) {
250 double *psfmt64 = &dsfmt->status[0].d[0];
252 if (dsfmt->idx >= DSFMT_N64) {
253 dsfmt_gen_rand_all(dsfmt);
256 r = psfmt64[dsfmt->idx++];
261 * This function generates and returns double precision pseudorandom
262 * number which distributes uniformly in the range [1, 2). This is
263 * the primitive and faster than generating numbers in other ranges.
264 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
265 * before this function. This function uses \b global variables.
266 * @return double precision floating point pseudorandom number
268 inline static double dsfmt_gv_genrand_close1_open2(void) {
269 return dsfmt_genrand_close1_open2(&dsfmt_global_data);
273 * This function generates and returns double precision pseudorandom
274 * number which distributes uniformly in the range [0, 1).
275 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
276 * before this function.
277 * @param dsfmt dsfmt internal state date
278 * @return double precision floating point pseudorandom number
280 inline static double dsfmt_genrand_close_open(dsfmt_t *dsfmt) {
281 return dsfmt_genrand_close1_open2(dsfmt) - 1.0;
285 * This function generates and returns double precision pseudorandom
286 * number which distributes uniformly in the range [0, 1).
287 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
288 * before this function. This function uses \b global variables.
289 * @return double precision floating point pseudorandom number
291 inline static double dsfmt_gv_genrand_close_open(void) {
292 return dsfmt_gv_genrand_close1_open2() - 1.0;
296 * This function generates and returns double precision pseudorandom
297 * number which distributes uniformly in the range (0, 1].
298 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
299 * before this function.
300 * @param dsfmt dsfmt internal state date
301 * @return double precision floating point pseudorandom number
303 inline static double dsfmt_genrand_open_close(dsfmt_t *dsfmt) {
304 return 2.0 - dsfmt_genrand_close1_open2(dsfmt);
308 * This function generates and returns double precision pseudorandom
309 * number which distributes uniformly in the range (0, 1].
310 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
311 * before this function. This function uses \b global variables.
312 * @return double precision floating point pseudorandom number
314 inline static double dsfmt_gv_genrand_open_close(void) {
315 return 2.0 - dsfmt_gv_genrand_close1_open2();
319 * This function generates and returns double precision pseudorandom
320 * number which distributes uniformly in the range (0, 1).
321 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
322 * before this function.
323 * @param dsfmt dsfmt internal state date
324 * @return double precision floating point pseudorandom number
326 inline static double dsfmt_genrand_open_open(dsfmt_t *dsfmt) {
327 double *dsfmt64 = &dsfmt->status[0].d[0];
333 if (dsfmt->idx >= DSFMT_N64) {
334 dsfmt_gen_rand_all(dsfmt);
337 r.d = dsfmt64[dsfmt->idx++];
343 * This function generates and returns double precision pseudorandom
344 * number which distributes uniformly in the range (0, 1).
345 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
346 * before this function. This function uses \b global variables.
347 * @return double precision floating point pseudorandom number
349 inline static double dsfmt_gv_genrand_open_open(void) {
350 return dsfmt_genrand_open_open(&dsfmt_global_data);
354 * This function generates double precision floating point
355 * pseudorandom numbers which distribute in the range [1, 2) to the
356 * specified array[] by one call. This function is the same as
357 * dsfmt_fill_array_close1_open2() except that this function uses
358 * \b global variables.
359 * @param array an array where pseudorandom numbers are filled
361 * @param size the number of pseudorandom numbers to be generated.
362 * see also \sa dsfmt_fill_array_close1_open2()
364 inline static void dsfmt_gv_fill_array_close1_open2(double array[], int size) {
365 dsfmt_fill_array_close1_open2(&dsfmt_global_data, array, size);
369 * This function generates double precision floating point
370 * pseudorandom numbers which distribute in the range (0, 1] to the
371 * specified array[] by one call. This function is the same as
372 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
373 * This function uses \b global variables.
374 * @param array an array where pseudorandom numbers are filled
376 * @param size the number of pseudorandom numbers to be generated.
377 * see also \sa dsfmt_fill_array_close1_open2() and \sa
378 * dsfmt_gv_fill_array_close1_open2()
380 inline static void dsfmt_gv_fill_array_open_close(double array[], int size) {
381 dsfmt_fill_array_open_close(&dsfmt_global_data, array, size);
385 * This function generates double precision floating point
386 * pseudorandom numbers which distribute in the range [0, 1) to the
387 * specified array[] by one call. This function is the same as
388 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
389 * This function uses \b global variables.
390 * @param array an array where pseudorandom numbers are filled
392 * @param size the number of pseudorandom numbers to be generated.
393 * see also \sa dsfmt_fill_array_close1_open2() \sa
394 * dsfmt_gv_fill_array_close1_open2()
396 inline static void dsfmt_gv_fill_array_close_open(double array[], int size) {
397 dsfmt_fill_array_close_open(&dsfmt_global_data, array, size);
401 * This function generates double precision floating point
402 * pseudorandom numbers which distribute in the range (0, 1) to the
403 * specified array[] by one call. This function is the same as
404 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
405 * This function uses \b global variables.
406 * @param array an array where pseudorandom numbers are filled
408 * @param size the number of pseudorandom numbers to be generated.
409 * see also \sa dsfmt_fill_array_close1_open2() \sa
410 * dsfmt_gv_fill_array_close1_open2()
412 inline static void dsfmt_gv_fill_array_open_open(double array[], int size) {
413 dsfmt_fill_array_open_open(&dsfmt_global_data, array, size);
417 * This function initializes the internal state array with a 32-bit
419 * @param dsfmt dsfmt state vector.
420 * @param seed a 32-bit integer used as the seed.
422 inline static void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed) {
423 dsfmt_chk_init_gen_rand(dsfmt, seed, DSFMT_MEXP);
427 * This function initializes the internal state array with a 32-bit
428 * integer seed. This function uses \b global variables.
429 * @param seed a 32-bit integer used as the seed.
430 * see also \sa dsfmt_init_gen_rand()
432 inline static void dsfmt_gv_init_gen_rand(uint32_t seed) {
433 dsfmt_init_gen_rand(&dsfmt_global_data, seed);
437 * This function initializes the internal state array,
438 * with an array of 32-bit integers used as the seeds.
439 * @param dsfmt dsfmt state vector
440 * @param init_key the array of 32-bit integers, used as a seed.
441 * @param key_length the length of init_key.
443 inline static void dsfmt_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
445 dsfmt_chk_init_by_array(dsfmt, init_key, key_length, DSFMT_MEXP);
449 * This function initializes the internal state array,
450 * with an array of 32-bit integers used as the seeds.
451 * This function uses \b global variables.
452 * @param init_key the array of 32-bit integers, used as a seed.
453 * @param key_length the length of init_key.
454 * see also \sa dsfmt_init_by_array()
456 inline static void dsfmt_gv_init_by_array(uint32_t init_key[], int key_length) {
457 dsfmt_init_by_array(&dsfmt_global_data, init_key, key_length);
460 #if !defined(DSFMT_DO_NOT_USE_OLD_NAMES)
461 DSFMT_PRE_INLINE const char *get_idstring(void) DSFMT_PST_INLINE;
462 DSFMT_PRE_INLINE int get_min_array_size(void) DSFMT_PST_INLINE;
463 DSFMT_PRE_INLINE void init_gen_rand(uint32_t seed) DSFMT_PST_INLINE;
464 DSFMT_PRE_INLINE void init_by_array(uint32_t init_key[], int key_length)
466 DSFMT_PRE_INLINE double genrand_close1_open2(void) DSFMT_PST_INLINE;
467 DSFMT_PRE_INLINE double genrand_close_open(void) DSFMT_PST_INLINE;
468 DSFMT_PRE_INLINE double genrand_open_close(void) DSFMT_PST_INLINE;
469 DSFMT_PRE_INLINE double genrand_open_open(void) DSFMT_PST_INLINE;
470 DSFMT_PRE_INLINE void fill_array_open_close(double array[], int size)
472 DSFMT_PRE_INLINE void fill_array_close_open(double array[], int size)
474 DSFMT_PRE_INLINE void fill_array_open_open(double array[], int size)
476 DSFMT_PRE_INLINE void fill_array_close1_open2(double array[], int size)
480 * This function is just the same as dsfmt_get_idstring().
482 * see also \sa dsfmt_get_idstring()
484 inline static const char *get_idstring(void) {
485 return dsfmt_get_idstring();
489 * This function is just the same as dsfmt_get_min_array_size().
490 * @return minimum size of array used for fill_array functions.
491 * see also \sa dsfmt_get_min_array_size()
493 inline static int get_min_array_size(void) {
494 return dsfmt_get_min_array_size();
498 * This function is just the same as dsfmt_gv_init_gen_rand().
499 * @param seed a 32-bit integer used as the seed.
500 * see also \sa dsfmt_gv_init_gen_rand(), \sa dsfmt_init_gen_rand().
502 inline static void init_gen_rand(uint32_t seed) {
503 dsfmt_gv_init_gen_rand(seed);
507 * This function is just the same as dsfmt_gv_init_by_array().
508 * @param init_key the array of 32-bit integers, used as a seed.
509 * @param key_length the length of init_key.
510 * see also \sa dsfmt_gv_init_by_array(), \sa dsfmt_init_by_array().
512 inline static void init_by_array(uint32_t init_key[], int key_length) {
513 dsfmt_gv_init_by_array(init_key, key_length);
517 * This function is just the same as dsfmt_gv_genrand_close1_open2().
518 * @return double precision floating point number.
519 * see also \sa dsfmt_genrand_close1_open2() \sa
520 * dsfmt_gv_genrand_close1_open2()
522 inline static double genrand_close1_open2(void) {
523 return dsfmt_gv_genrand_close1_open2();
527 * This function is just the same as dsfmt_gv_genrand_close_open().
528 * @return double precision floating point number.
529 * see also \sa dsfmt_genrand_close_open() \sa
530 * dsfmt_gv_genrand_close_open()
532 inline static double genrand_close_open(void) {
533 return dsfmt_gv_genrand_close_open();
537 * This function is just the same as dsfmt_gv_genrand_open_close().
538 * @return double precision floating point number.
539 * see also \sa dsfmt_genrand_open_close() \sa
540 * dsfmt_gv_genrand_open_close()
542 inline static double genrand_open_close(void) {
543 return dsfmt_gv_genrand_open_close();
547 * This function is just the same as dsfmt_gv_genrand_open_open().
548 * @return double precision floating point number.
549 * see also \sa dsfmt_genrand_open_open() \sa
550 * dsfmt_gv_genrand_open_open()
552 inline static double genrand_open_open(void) {
553 return dsfmt_gv_genrand_open_open();
557 * This function is juset the same as dsfmt_gv_fill_array_open_close().
558 * @param array an array where pseudorandom numbers are filled
560 * @param size the number of pseudorandom numbers to be generated.
561 * see also \sa dsfmt_gv_fill_array_open_close(), \sa
562 * dsfmt_fill_array_close1_open2(), \sa
563 * dsfmt_gv_fill_array_close1_open2()
565 inline static void fill_array_open_close(double array[], int size) {
566 dsfmt_gv_fill_array_open_close(array, size);
570 * This function is juset the same as dsfmt_gv_fill_array_close_open().
571 * @param array an array where pseudorandom numbers are filled
573 * @param size the number of pseudorandom numbers to be generated.
574 * see also \sa dsfmt_gv_fill_array_close_open(), \sa
575 * dsfmt_fill_array_close1_open2(), \sa
576 * dsfmt_gv_fill_array_close1_open2()
578 inline static void fill_array_close_open(double array[], int size) {
579 dsfmt_gv_fill_array_close_open(array, size);
583 * This function is juset the same as dsfmt_gv_fill_array_open_open().
584 * @param array an array where pseudorandom numbers are filled
586 * @param size the number of pseudorandom numbers to be generated.
587 * see also \sa dsfmt_gv_fill_array_open_open(), \sa
588 * dsfmt_fill_array_close1_open2(), \sa
589 * dsfmt_gv_fill_array_close1_open2()
591 inline static void fill_array_open_open(double array[], int size) {
592 dsfmt_gv_fill_array_open_open(array, size);
596 * This function is juset the same as dsfmt_gv_fill_array_close1_open2().
597 * @param array an array where pseudorandom numbers are filled
599 * @param size the number of pseudorandom numbers to be generated.
600 * see also \sa dsfmt_fill_array_close1_open2(), \sa
601 * dsfmt_gv_fill_array_close1_open2()
603 inline static void fill_array_close1_open2(double array[], int size) {
604 dsfmt_gv_fill_array_close1_open2(array, size);
606 #endif /* DSFMT_DO_NOT_USE_OLD_NAMES */