SFMT  1.4
SFMT.h
Go to the documentation of this file.
1 #pragma once
2 
35 #ifndef SFMTST_H
36 #define SFMTST_H
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40 
41 #include <stdio.h>
42 #include <assert.h>
43 
44 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
45  #include <inttypes.h>
46 #elif defined(_MSC_VER) || defined(__BORLANDC__)
47  typedef unsigned int uint32_t;
48  typedef unsigned __int64 uint64_t;
49  #define inline __inline
50 #else
51  #include <inttypes.h>
52  #if defined(__GNUC__)
53  #define inline __inline__
54  #endif
55 #endif
56 
57 #ifndef PRIu64
58  #if defined(_MSC_VER) || defined(__BORLANDC__)
59  #define PRIu64 "I64u"
60  #define PRIx64 "I64x"
61  #else
62  #define PRIu64 "llu"
63  #define PRIx64 "llx"
64  #endif
65 #endif
66 
67 #include "SFMT-params.h"
68 
69 /*------------------------------------------
70  128-bit SIMD like data type for standard C
71  ------------------------------------------*/
72 #if defined(HAVE_ALTIVEC)
73  #if !defined(__APPLE__)
74  #include <altivec.h>
75  #endif
76 
77 union W128_T {
78  vector unsigned int s;
79  uint32_t u[4];
80  uint64_t u64[2];
81 };
82 #elif defined(HAVE_NEON)
83  #include <arm_neon.h>
84 
86 union W128_T {
87  uint32_t u[4];
88  uint64_t u64[2];
89  uint32x4_t si;
90 };
91 #elif defined(HAVE_SSE2)
92  #include <emmintrin.h>
93 
95 union W128_T {
96  uint32_t u[4];
97  uint64_t u64[2];
98  __m128i si;
99 };
100 #else
101 
102 union W128_T {
103  uint32_t u[4];
104  uint64_t u64[2];
105 };
106 #endif
107 
109 typedef union W128_T w128_t;
110 
114 struct SFMT_T {
116  w128_t state[SFMT_N];
118  int idx;
119 };
120 
121 typedef struct SFMT_T sfmt_t;
122 
123 void sfmt_fill_array32(sfmt_t * sfmt, uint32_t * array, int size);
124 void sfmt_fill_array64(sfmt_t * sfmt, uint64_t * array, int size);
125 void sfmt_init_gen_rand(sfmt_t * sfmt, uint32_t seed);
126 void sfmt_init_by_array(sfmt_t * sfmt, uint32_t * init_key, int key_length);
127 const char * sfmt_get_idstring(sfmt_t * sfmt);
130 void sfmt_gen_rand_all(sfmt_t * sfmt);
131 
132 #ifndef ONLY64
133 
139 inline static uint32_t sfmt_genrand_uint32(sfmt_t * sfmt) {
140  uint32_t r;
141  uint32_t * psfmt32 = &sfmt->state[0].u[0];
142 
143  if (sfmt->idx >= SFMT_N32) {
144  sfmt_gen_rand_all(sfmt);
145  sfmt->idx = 0;
146  }
147  r = psfmt32[sfmt->idx++];
148  return r;
149 }
150 #endif
151 
159 inline static uint64_t sfmt_genrand_uint64(sfmt_t * sfmt) {
160 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
161  uint32_t * psfmt32 = &sfmt->state[0].u[0];
162  uint32_t r1, r2;
163 #else
164  uint64_t r;
165 #endif
166  uint64_t * psfmt64 = &sfmt->state[0].u64[0];
167  assert(sfmt->idx % 2 == 0);
168 
169  if (sfmt->idx >= SFMT_N32) {
170  sfmt_gen_rand_all(sfmt);
171  sfmt->idx = 0;
172  }
173 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
174  r1 = psfmt32[sfmt->idx];
175  r2 = psfmt32[sfmt->idx + 1];
176  sfmt->idx += 2;
177  return ((uint64_t)r2 << 32) | r1;
178 #else
179  r = psfmt64[sfmt->idx / 2];
180  sfmt->idx += 2;
181  return r;
182 #endif
183 }
184 
185 /* =================================================
186  The following real versions are due to Isaku Wada
187  ================================================= */
193 inline static double sfmt_to_real1(uint32_t v)
194 {
195  return v * (1.0/4294967295.0);
196  /* divided by 2^32-1 */
197 }
198 
204 inline static double sfmt_genrand_real1(sfmt_t * sfmt)
205 {
206  return sfmt_to_real1(sfmt_genrand_uint32(sfmt));
207 }
208 
214 inline static double sfmt_to_real2(uint32_t v)
215 {
216  return v * (1.0/4294967296.0);
217  /* divided by 2^32 */
218 }
219 
225 inline static double sfmt_genrand_real2(sfmt_t * sfmt)
226 {
227  return sfmt_to_real2(sfmt_genrand_uint32(sfmt));
228 }
229 
235 inline static double sfmt_to_real3(uint32_t v)
236 {
237  return (((double)v) + 0.5)*(1.0/4294967296.0);
238  /* divided by 2^32 */
239 }
240 
246 inline static double sfmt_genrand_real3(sfmt_t * sfmt)
247 {
248  return sfmt_to_real3(sfmt_genrand_uint32(sfmt));
249 }
250 
257 inline static double sfmt_to_res53(uint64_t v)
258 {
259  return (v >> 11) * (1.0/9007199254740992.0);
260 }
261 
267 inline static double sfmt_genrand_res53(sfmt_t * sfmt)
268 {
269  return sfmt_to_res53(sfmt_genrand_uint64(sfmt));
270 }
271 
272 
273 /* =================================================
274  The following function are added by Saito.
275  ================================================= */
280 inline static double sfmt_to_res53_mix(uint32_t x, uint32_t y)
281 {
282  return sfmt_to_res53(x | ((uint64_t)y << 32));
283 }
284 
291 inline static double sfmt_genrand_res53_mix(sfmt_t * sfmt)
292 {
293  uint32_t x, y;
294 
295  x = sfmt_genrand_uint32(sfmt);
296  y = sfmt_genrand_uint32(sfmt);
297  return sfmt_to_res53_mix(x, y);
298 }
299 
300 #if defined(__cplusplus)
301 }
302 #endif
303 
304 #endif
uint64_t u64[2]
Definition: SFMT.h:104
static double sfmt_to_res53_mix(uint32_t x, uint32_t y)
generates a random number on [0,1) with 53-bit resolution from two 32 bit integers ...
Definition: SFMT.h:280
void sfmt_fill_array32(sfmt_t *sfmt, uint32_t *array, int size)
This function generates pseudorandom 32-bit integers in the specified array[] by one call...
Definition: SFMT.c:291
uint32_t u[4]
Definition: SFMT.h:103
int idx
index counter to the 32-bit internal state array
Definition: SFMT.h:118
int sfmt_get_min_array_size32(sfmt_t *sfmt)
This function returns the minimum size of array used for fill_array32() function. ...
Definition: SFMT.c:221
SFMT internal state.
Definition: SFMT.h:114
static double sfmt_genrand_real2(sfmt_t *sfmt)
generates a random number on [0,1)-real-interval
Definition: SFMT.h:225
static double sfmt_genrand_real3(sfmt_t *sfmt)
generates a random number on (0,1)-real-interval
Definition: SFMT.h:246
static uint64_t sfmt_genrand_uint64(sfmt_t *sfmt)
This function generates and returns 64-bit pseudorandom number.
Definition: SFMT.h:159
static double sfmt_genrand_res53(sfmt_t *sfmt)
generates a random number on [0,1) with 53-bit resolution
Definition: SFMT.h:267
static double sfmt_genrand_res53_mix(sfmt_t *sfmt)
generates a random number on [0,1) with 53-bit resolution using two 32bit integers.
Definition: SFMT.h:291
w128_t state[SFMT_N]
the 128-bit internal state array
Definition: SFMT.h:116
static uint32_t sfmt_genrand_uint32(sfmt_t *sfmt)
This function generates and returns 32-bit pseudorandom number.
Definition: SFMT.h:139
const char * sfmt_get_idstring(sfmt_t *sfmt)
This function returns the identification string.
Definition: SFMT.c:210
128-bit data structure
Definition: SFMT.h:102
static double sfmt_to_real3(uint32_t v)
converts an unsigned 32-bit integer to a double on (0,1)-real-interval.
Definition: SFMT.h:235
int sfmt_get_min_array_size64(sfmt_t *sfmt)
This function returns the minimum size of array used for fill_array64() function. ...
Definition: SFMT.c:232
void sfmt_init_by_array(sfmt_t *sfmt, uint32_t *init_key, int key_length)
This function initializes the internal state array, with an array of 32-bit integers used as the seed...
Definition: SFMT.c:369
void sfmt_init_gen_rand(sfmt_t *sfmt, uint32_t seed)
This function initializes the internal state array with a 32-bit integer seed.
Definition: SFMT.c:347
void sfmt_fill_array64(sfmt_t *sfmt, uint64_t *array, int size)
This function generates pseudorandom 64-bit integers in the specified array[] by one call...
Definition: SFMT.c:327
static double sfmt_genrand_real1(sfmt_t *sfmt)
generates a random number on [0,1]-real-interval
Definition: SFMT.h:204
static double sfmt_to_real2(uint32_t v)
converts an unsigned 32-bit integer to a double on [0,1)-real-interval.
Definition: SFMT.h:214
static double sfmt_to_res53(uint64_t v)
converts an unsigned 32-bit integer to double on [0,1) with 53-bit resolution.
Definition: SFMT.h:257
void sfmt_gen_rand_all(sfmt_t *sfmt)
This function fills the internal state array with pseudorandom integers.
Definition: SFMT.c:243
static double sfmt_to_real1(uint32_t v)
converts an unsigned 32-bit number to a double on [0,1]-real-interval.
Definition: SFMT.h:193