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) || defined(__BORLANDC__)
89 # if !defined(DSFMT_UINT32_DEFINED) && !defined(SFMT_UINT32_DEFINED)
90 typedef unsigned int uint32_t;
91 typedef unsigned __int64
uint64_t;
92 # define UINT64_C(v) (v ## ui64)
93 # define DSFMT_UINT32_DEFINED
95 # define inline __inline
99 # include <inttypes.h>
100 # if !defined(inline)
101 # if defined(__GNUC__)
102 # define inline __inline__
110 # if defined(_MSC_VER) || defined(__BORLANDC__)
111 # define PRIu64 "I64u"
112 # define PRIx64 "I64x"
114 # define PRIu64 "llu"
115 # define PRIx64 "llx"
120 # define UINT64_C(v) (v ## ULL)
123 /*------------------------------------------
124 128-bit SIMD like data type for standard C
125 ------------------------------------------*/
126 #if defined(HAVE_ALTIVEC)
127 # if !defined(__APPLE__)
128 # include <altivec.h>
130 /** 128-bit data structure */
132 vector
unsigned int s
;
138 #elif defined(HAVE_SSE2)
139 # include <emmintrin.h>
141 /** 128-bit data structure */
149 #else /* standard C */
150 /** 128-bit data structure */
158 /** 128-bit data type */
159 typedef union W128_T w128_t
;
161 /** the 128-bit internal state array */
163 w128_t status
[DSFMT_N
+ 1] __attribute__((aligned (16)));
166 typedef struct DSFMT_T dsfmt_t
;
168 /** dsfmt internal state vector */
169 extern dsfmt_t dsfmt_global_data
;
170 /** dsfmt mexp for check */
171 extern const int dsfmt_global_mexp
;
173 void dsfmt_gen_rand_all(dsfmt_t
*dsfmt
);
174 void dsfmt_fill_array_open_close(dsfmt_t
*dsfmt
, double array
[], int size
);
175 void dsfmt_fill_array_close_open(dsfmt_t
*dsfmt
, double array
[], int size
);
176 void dsfmt_fill_array_open_open(dsfmt_t
*dsfmt
, double array
[], int size
);
177 void dsfmt_fill_array_close1_open2(dsfmt_t
*dsfmt
, double array
[], int size
);
178 void dsfmt_chk_init_gen_rand(dsfmt_t
*dsfmt
, uint32_t seed
, int mexp
);
179 void dsfmt_chk_init_by_array(dsfmt_t
*dsfmt
, uint32_t init_key
[],
180 int key_length
, int mexp
);
181 const char *dsfmt_get_idstring(void);
182 int dsfmt_get_min_array_size(void);
184 #if defined(__GNUC__)
185 # define DSFMT_PRE_INLINE inline static
186 # define DSFMT_PST_INLINE __attribute__((always_inline))
187 #elif defined(_MSC_VER) && _MSC_VER >= 1200
188 # define DSFMT_PRE_INLINE __forceinline static
189 # define DSFMT_PST_INLINE
191 # define DSFMT_PRE_INLINE inline static
192 # define DSFMT_PST_INLINE
194 DSFMT_PRE_INLINE
uint32_t dsfmt_genrand_uint32(dsfmt_t
*dsfmt
) DSFMT_PST_INLINE
;
195 DSFMT_PRE_INLINE
double dsfmt_genrand_close1_open2(dsfmt_t
*dsfmt
)
197 DSFMT_PRE_INLINE
double dsfmt_genrand_close_open(dsfmt_t
*dsfmt
)
199 DSFMT_PRE_INLINE
double dsfmt_genrand_open_close(dsfmt_t
*dsfmt
)
201 DSFMT_PRE_INLINE
double dsfmt_genrand_open_open(dsfmt_t
*dsfmt
)
203 DSFMT_PRE_INLINE
uint32_t dsfmt_gv_genrand_uint32(void) DSFMT_PST_INLINE
;
204 DSFMT_PRE_INLINE
double dsfmt_gv_genrand_close1_open2(void) DSFMT_PST_INLINE
;
205 DSFMT_PRE_INLINE
double dsfmt_gv_genrand_close_open(void) DSFMT_PST_INLINE
;
206 DSFMT_PRE_INLINE
double dsfmt_gv_genrand_open_close(void) DSFMT_PST_INLINE
;
207 DSFMT_PRE_INLINE
double dsfmt_gv_genrand_open_open(void) DSFMT_PST_INLINE
;
208 DSFMT_PRE_INLINE
void dsfmt_gv_fill_array_open_close(double array
[], int size
)
210 DSFMT_PRE_INLINE
void dsfmt_gv_fill_array_close_open(double array
[], int size
)
212 DSFMT_PRE_INLINE
void dsfmt_gv_fill_array_open_open(double array
[], int size
)
214 DSFMT_PRE_INLINE
void dsfmt_gv_fill_array_close1_open2(double array
[], int size
)
216 DSFMT_PRE_INLINE
void dsfmt_gv_init_gen_rand(uint32_t seed
) DSFMT_PST_INLINE
;
217 DSFMT_PRE_INLINE
void dsfmt_gv_init_by_array(uint32_t init_key
[],
218 int key_length
) DSFMT_PST_INLINE
;
219 DSFMT_PRE_INLINE
void dsfmt_init_gen_rand(dsfmt_t
*dsfmt
, uint32_t seed
)
221 DSFMT_PRE_INLINE
void dsfmt_init_by_array(dsfmt_t
*dsfmt
, uint32_t init_key
[],
222 int key_length
) DSFMT_PST_INLINE
;
225 * This function generates and returns unsigned 32-bit integer.
226 * This is slower than SFMT, only for convenience usage.
227 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
228 * before this function.
229 * @param dsfmt dsfmt internal state date
230 * @return double precision floating point pseudorandom number
232 inline static uint32_t dsfmt_genrand_uint32(dsfmt_t
*dsfmt
) {
234 uint64_t *psfmt64
= &dsfmt
->status
[0].u
[0];
236 if (dsfmt
->idx
>= DSFMT_N64
) {
237 dsfmt_gen_rand_all(dsfmt
);
240 r
= psfmt64
[dsfmt
->idx
++] & 0xffffffffU
;
245 * This function generates and returns double precision pseudorandom
246 * number which distributes uniformly in the range [1, 2). This is
247 * the primitive and faster than generating numbers in other ranges.
248 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
249 * before this function.
250 * @param dsfmt dsfmt internal state date
251 * @return double precision floating point pseudorandom number
253 inline static double dsfmt_genrand_close1_open2(dsfmt_t
*dsfmt
) {
255 double *psfmt64
= &dsfmt
->status
[0].d
[0];
257 if (dsfmt
->idx
>= DSFMT_N64
) {
258 dsfmt_gen_rand_all(dsfmt
);
261 r
= psfmt64
[dsfmt
->idx
++];
266 * This function generates and returns unsigned 32-bit integer.
267 * This is slower than SFMT, only for convenience usage.
268 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
269 * before this function. This function uses \b global variables.
270 * @return double precision floating point pseudorandom number
272 inline static uint32_t dsfmt_gv_genrand_uint32(void) {
273 return dsfmt_genrand_uint32(&dsfmt_global_data
);
277 * This function generates and returns double precision pseudorandom
278 * number which distributes uniformly in the range [1, 2).
279 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
280 * before this function. This function uses \b global variables.
281 * @return double precision floating point pseudorandom number
283 inline static double dsfmt_gv_genrand_close1_open2(void) {
284 return dsfmt_genrand_close1_open2(&dsfmt_global_data
);
288 * This function generates and returns double precision pseudorandom
289 * number which distributes uniformly in the range [0, 1).
290 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
291 * before this function.
292 * @param dsfmt dsfmt internal state date
293 * @return double precision floating point pseudorandom number
295 inline static double dsfmt_genrand_close_open(dsfmt_t
*dsfmt
) {
296 return dsfmt_genrand_close1_open2(dsfmt
) - 1.0;
300 * This function generates and returns double precision pseudorandom
301 * number which distributes uniformly in the range [0, 1).
302 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
303 * before this function. This function uses \b global variables.
304 * @return double precision floating point pseudorandom number
306 inline static double dsfmt_gv_genrand_close_open(void) {
307 return dsfmt_gv_genrand_close1_open2() - 1.0;
311 * This function generates and returns double precision pseudorandom
312 * number which distributes uniformly in the range (0, 1].
313 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
314 * before this function.
315 * @param dsfmt dsfmt internal state date
316 * @return double precision floating point pseudorandom number
318 inline static double dsfmt_genrand_open_close(dsfmt_t
*dsfmt
) {
319 return 2.0 - dsfmt_genrand_close1_open2(dsfmt
);
323 * This function generates and returns double precision pseudorandom
324 * number which distributes uniformly in the range (0, 1].
325 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
326 * before this function. This function uses \b global variables.
327 * @return double precision floating point pseudorandom number
329 inline static double dsfmt_gv_genrand_open_close(void) {
330 return 2.0 - dsfmt_gv_genrand_close1_open2();
334 * This function generates and returns double precision pseudorandom
335 * number which distributes uniformly in the range (0, 1).
336 * dsfmt_init_gen_rand() or dsfmt_init_by_array() must be called
337 * before this function.
338 * @param dsfmt dsfmt internal state date
339 * @return double precision floating point pseudorandom number
341 inline static double dsfmt_genrand_open_open(dsfmt_t
*dsfmt
) {
342 double *dsfmt64
= &dsfmt
->status
[0].d
[0];
348 if (dsfmt
->idx
>= DSFMT_N64
) {
349 dsfmt_gen_rand_all(dsfmt
);
352 r
.d
= dsfmt64
[dsfmt
->idx
++];
358 * This function generates and returns double precision pseudorandom
359 * number which distributes uniformly in the range (0, 1).
360 * dsfmt_gv_init_gen_rand() or dsfmt_gv_init_by_array() must be called
361 * before this function. This function uses \b global variables.
362 * @return double precision floating point pseudorandom number
364 inline static double dsfmt_gv_genrand_open_open(void) {
365 return dsfmt_genrand_open_open(&dsfmt_global_data
);
369 * This function generates double precision floating point
370 * pseudorandom numbers which distribute in the range [1, 2) to the
371 * specified array[] by one call. This function is the same as
372 * dsfmt_fill_array_close1_open2() except that this function uses
373 * \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()
379 inline static void dsfmt_gv_fill_array_close1_open2(double array
[], int size
) {
380 dsfmt_fill_array_close1_open2(&dsfmt_global_data
, array
, size
);
384 * This function generates double precision floating point
385 * pseudorandom numbers which distribute in the range (0, 1] to the
386 * specified array[] by one call. This function is the same as
387 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
388 * This function uses \b global variables.
389 * @param array an array where pseudorandom numbers are filled
391 * @param size the number of pseudorandom numbers to be generated.
392 * see also \sa dsfmt_fill_array_close1_open2() and \sa
393 * dsfmt_gv_fill_array_close1_open2()
395 inline static void dsfmt_gv_fill_array_open_close(double array
[], int size
) {
396 dsfmt_fill_array_open_close(&dsfmt_global_data
, array
, size
);
400 * This function generates double precision floating point
401 * pseudorandom numbers which distribute in the range [0, 1) to the
402 * specified array[] by one call. This function is the same as
403 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
404 * This function uses \b global variables.
405 * @param array an array where pseudorandom numbers are filled
407 * @param size the number of pseudorandom numbers to be generated.
408 * see also \sa dsfmt_fill_array_close1_open2() \sa
409 * dsfmt_gv_fill_array_close1_open2()
411 inline static void dsfmt_gv_fill_array_close_open(double array
[], int size
) {
412 dsfmt_fill_array_close_open(&dsfmt_global_data
, array
, size
);
416 * This function generates double precision floating point
417 * pseudorandom numbers which distribute in the range (0, 1) to the
418 * specified array[] by one call. This function is the same as
419 * dsfmt_gv_fill_array_close1_open2() except the distribution range.
420 * This function uses \b global variables.
421 * @param array an array where pseudorandom numbers are filled
423 * @param size the number of pseudorandom numbers to be generated.
424 * see also \sa dsfmt_fill_array_close1_open2() \sa
425 * dsfmt_gv_fill_array_close1_open2()
427 inline static void dsfmt_gv_fill_array_open_open(double array
[], int size
) {
428 dsfmt_fill_array_open_open(&dsfmt_global_data
, array
, size
);
432 * This function initializes the internal state array with a 32-bit
434 * @param dsfmt dsfmt state vector.
435 * @param seed a 32-bit integer used as the seed.
437 inline static void dsfmt_init_gen_rand(dsfmt_t
*dsfmt
, uint32_t seed
) {
438 dsfmt_chk_init_gen_rand(dsfmt
, seed
, DSFMT_MEXP
);
442 * This function initializes the internal state array with a 32-bit
443 * integer seed. This function uses \b global variables.
444 * @param seed a 32-bit integer used as the seed.
445 * see also \sa dsfmt_init_gen_rand()
447 inline static void dsfmt_gv_init_gen_rand(uint32_t seed
) {
448 dsfmt_init_gen_rand(&dsfmt_global_data
, seed
);
452 * This function initializes the internal state array,
453 * with an array of 32-bit integers used as the seeds.
454 * @param dsfmt dsfmt state vector
455 * @param init_key the array of 32-bit integers, used as a seed.
456 * @param key_length the length of init_key.
458 inline static void dsfmt_init_by_array(dsfmt_t
*dsfmt
, uint32_t init_key
[],
460 dsfmt_chk_init_by_array(dsfmt
, init_key
, key_length
, DSFMT_MEXP
);
464 * This function initializes the internal state array,
465 * with an array of 32-bit integers used as the seeds.
466 * This function uses \b global variables.
467 * @param init_key the array of 32-bit integers, used as a seed.
468 * @param key_length the length of init_key.
469 * see also \sa dsfmt_init_by_array()
471 inline static void dsfmt_gv_init_by_array(uint32_t init_key
[], int key_length
) {
472 dsfmt_init_by_array(&dsfmt_global_data
, init_key
, key_length
);
475 #if !defined(DSFMT_DO_NOT_USE_OLD_NAMES)
476 DSFMT_PRE_INLINE
const char *get_idstring(void) DSFMT_PST_INLINE
;
477 DSFMT_PRE_INLINE
int get_min_array_size(void) DSFMT_PST_INLINE
;
478 DSFMT_PRE_INLINE
void init_gen_rand(uint32_t seed
) DSFMT_PST_INLINE
;
479 DSFMT_PRE_INLINE
void init_by_array(uint32_t init_key
[], int key_length
)
481 DSFMT_PRE_INLINE
double genrand_close1_open2(void) DSFMT_PST_INLINE
;
482 DSFMT_PRE_INLINE
double genrand_close_open(void) DSFMT_PST_INLINE
;
483 DSFMT_PRE_INLINE
double genrand_open_close(void) DSFMT_PST_INLINE
;
484 DSFMT_PRE_INLINE
double genrand_open_open(void) DSFMT_PST_INLINE
;
485 DSFMT_PRE_INLINE
void fill_array_open_close(double array
[], int size
)
487 DSFMT_PRE_INLINE
void fill_array_close_open(double array
[], int size
)
489 DSFMT_PRE_INLINE
void fill_array_open_open(double array
[], int size
)
491 DSFMT_PRE_INLINE
void fill_array_close1_open2(double array
[], int size
)
495 * This function is just the same as dsfmt_get_idstring().
497 * see also \sa dsfmt_get_idstring()
499 inline static const char *get_idstring(void) {
500 return dsfmt_get_idstring();
504 * This function is just the same as dsfmt_get_min_array_size().
505 * @return minimum size of array used for fill_array functions.
506 * see also \sa dsfmt_get_min_array_size()
508 inline static int get_min_array_size(void) {
509 return dsfmt_get_min_array_size();
513 * This function is just the same as dsfmt_gv_init_gen_rand().
514 * @param seed a 32-bit integer used as the seed.
515 * see also \sa dsfmt_gv_init_gen_rand(), \sa dsfmt_init_gen_rand().
517 inline static void init_gen_rand(uint32_t seed
) {
518 dsfmt_gv_init_gen_rand(seed
);
522 * This function is just the same as dsfmt_gv_init_by_array().
523 * @param init_key the array of 32-bit integers, used as a seed.
524 * @param key_length the length of init_key.
525 * see also \sa dsfmt_gv_init_by_array(), \sa dsfmt_init_by_array().
527 inline static void init_by_array(uint32_t init_key
[], int key_length
) {
528 dsfmt_gv_init_by_array(init_key
, key_length
);
532 * This function is just the same as dsfmt_gv_genrand_close1_open2().
533 * @return double precision floating point number.
534 * see also \sa dsfmt_genrand_close1_open2() \sa
535 * dsfmt_gv_genrand_close1_open2()
537 inline static double genrand_close1_open2(void) {
538 return dsfmt_gv_genrand_close1_open2();
542 * This function is just the same as dsfmt_gv_genrand_close_open().
543 * @return double precision floating point number.
544 * see also \sa dsfmt_genrand_close_open() \sa
545 * dsfmt_gv_genrand_close_open()
547 inline static double genrand_close_open(void) {
548 return dsfmt_gv_genrand_close_open();
552 * This function is just the same as dsfmt_gv_genrand_open_close().
553 * @return double precision floating point number.
554 * see also \sa dsfmt_genrand_open_close() \sa
555 * dsfmt_gv_genrand_open_close()
557 inline static double genrand_open_close(void) {
558 return dsfmt_gv_genrand_open_close();
562 * This function is just the same as dsfmt_gv_genrand_open_open().
563 * @return double precision floating point number.
564 * see also \sa dsfmt_genrand_open_open() \sa
565 * dsfmt_gv_genrand_open_open()
567 inline static double genrand_open_open(void) {
568 return dsfmt_gv_genrand_open_open();
572 * This function is juset the same as dsfmt_gv_fill_array_open_close().
573 * @param array an array where pseudorandom numbers are filled
575 * @param size the number of pseudorandom numbers to be generated.
576 * see also \sa dsfmt_gv_fill_array_open_close(), \sa
577 * dsfmt_fill_array_close1_open2(), \sa
578 * dsfmt_gv_fill_array_close1_open2()
580 inline static void fill_array_open_close(double array
[], int size
) {
581 dsfmt_gv_fill_array_open_close(array
, size
);
585 * This function is juset the same as dsfmt_gv_fill_array_close_open().
586 * @param array an array where pseudorandom numbers are filled
588 * @param size the number of pseudorandom numbers to be generated.
589 * see also \sa dsfmt_gv_fill_array_close_open(), \sa
590 * dsfmt_fill_array_close1_open2(), \sa
591 * dsfmt_gv_fill_array_close1_open2()
593 inline static void fill_array_close_open(double array
[], int size
) {
594 dsfmt_gv_fill_array_close_open(array
, size
);
598 * This function is juset the same as dsfmt_gv_fill_array_open_open().
599 * @param array an array where pseudorandom numbers are filled
601 * @param size the number of pseudorandom numbers to be generated.
602 * see also \sa dsfmt_gv_fill_array_open_open(), \sa
603 * dsfmt_fill_array_close1_open2(), \sa
604 * dsfmt_gv_fill_array_close1_open2()
606 inline static void fill_array_open_open(double array
[], int size
) {
607 dsfmt_gv_fill_array_open_open(array
, size
);
611 * This function is juset the same as dsfmt_gv_fill_array_close1_open2().
612 * @param array an array where pseudorandom numbers are filled
614 * @param size the number of pseudorandom numbers to be generated.
615 * see also \sa dsfmt_fill_array_close1_open2(), \sa
616 * dsfmt_gv_fill_array_close1_open2()
618 inline static void fill_array_close1_open2(double array
[], int size
) {
619 dsfmt_gv_fill_array_close1_open2(array
, size
);
621 #endif /* DSFMT_DO_NOT_USE_OLD_NAMES */