view src/SFMT/SFMT.h @ 23:bf9169ad4222

add SMID-oriented fast mersenne twister
author Robert McIntyre <rlm@mit.edu>
date Sun, 04 Mar 2012 17:38:32 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 /**
2 * @file SFMT.h
3 *
4 * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
5 * number generator
6 *
7 * @author Mutsuo Saito (Hiroshima University)
8 * @author Makoto Matsumoto (Hiroshima University)
9 *
10 * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
11 * University. All rights reserved.
12 *
13 * The new BSD License is applied to this software.
14 * see LICENSE.txt
15 *
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:
19 * @verbatim
20 typedef unsigned int uint32_t
21 typedef unsigned long long uint64_t
22 #define PRIu64 "llu"
23 #define PRIx64 "llx"
24 @endverbatim
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.
29 */
31 #ifndef SFMT_H
32 #define SFMT_H
34 #include <stdio.h>
36 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
37 #include <inttypes.h>
38 #elif defined(_MSC_VER) || defined(__BORLANDC__)
39 typedef unsigned int uint32_t;
40 typedef unsigned __int64 uint64_t;
41 #define inline __inline
42 #else
43 #include <inttypes.h>
44 #if defined(__GNUC__)
45 #define inline __inline__
46 #endif
47 #endif
49 #ifndef PRIu64
50 #if defined(_MSC_VER) || defined(__BORLANDC__)
51 #define PRIu64 "I64u"
52 #define PRIx64 "I64x"
53 #else
54 #define PRIu64 "llu"
55 #define PRIx64 "llx"
56 #endif
57 #endif
59 #if defined(__GNUC__)
60 #define ALWAYSINLINE __attribute__((always_inline))
61 #else
62 #define ALWAYSINLINE
63 #endif
65 #if defined(_MSC_VER)
66 #if _MSC_VER >= 1200
67 #define PRE_ALWAYS __forceinline
68 #else
69 #define PRE_ALWAYS inline
70 #endif
71 #else
72 #define PRE_ALWAYS inline
73 #endif
75 #if defined(__cplusplus) || defined(c_plusplus)
76 //extern "C" {
77 #endif
79 uint32_t gen_rand32(void);
80 uint64_t gen_rand64(void);
81 void fill_array32(uint32_t *array, int size);
82 void fill_array64(uint64_t *array, int size);
83 void init_gen_rand(uint32_t seed);
84 void init_by_array(uint32_t *init_key, int key_length);
85 const char *get_idstring(void);
86 int get_min_array_size32(void);
87 int get_min_array_size64(void);
89 #if defined(__cplusplus) || defined(c_plusplus)
90 //} // end extern
91 #endif
93 /* These real versions are due to Isaku Wada */
94 /** generates a random number on [0,1]-real-interval */
95 inline static double to_real1(uint32_t v)
96 {
97 return v * (1.0/4294967295.0);
98 /* divided by 2^32-1 */
99 }
101 /** generates a random number on [0,1]-real-interval */
102 inline static double genrand_real1(void)
103 {
104 return to_real1(gen_rand32());
105 }
107 /** generates a random number on [0,1)-real-interval */
108 inline static double to_real2(uint32_t v)
109 {
110 return v * (1.0/4294967296.0);
111 /* divided by 2^32 */
112 }
114 /** generates a random number on [0,1)-real-interval */
115 inline static double genrand_real2(void)
116 {
117 return to_real2(gen_rand32());
118 }
120 /** generates a random number on (0,1)-real-interval */
121 inline static double to_real3(uint32_t v)
122 {
123 return (((double)v) + 0.5)*(1.0/4294967296.0);
124 /* divided by 2^32 */
125 }
127 /** generates a random number on (0,1)-real-interval */
128 inline static double genrand_real3(void)
129 {
130 return to_real3(gen_rand32());
131 }
132 /** These real versions are due to Isaku Wada */
134 /** generates a random number on [0,1) with 53-bit resolution*/
135 inline static double to_res53(uint64_t v)
136 {
137 return v * (1.0/18446744073709551616.0L);
138 }
140 /** generates a random number on [0,1) with 53-bit resolution from two
141 * 32 bit integers */
142 inline static double to_res53_mix(uint32_t x, uint32_t y)
143 {
144 return to_res53(x | ((uint64_t)y << 32));
145 }
147 /** generates a random number on [0,1) with 53-bit resolution
148 */
149 inline static double genrand_res53(void)
150 {
151 return to_res53(gen_rand64());
152 }
154 /** generates a random number on [0,1) with 53-bit resolution
155 using 32bit integer.
156 */
157 inline static double genrand_res53_mix(void)
158 {
159 uint32_t x, y;
161 x = gen_rand32();
162 y = gen_rand32();
163 return to_res53_mix(x, y);
164 }
165 #endif