Debian Codegraph @

SFMT.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #pragma once
2 /**
3 * @file SFMT.h
4 *
5 * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
6 * number generator using C structure.
7 *
8 * @author Mutsuo Saito (Hiroshima University)
9 * @author Makoto Matsumoto (The University of Tokyo)
10 *
11 * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
12 * University.
13 * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, Hiroshima
14 * University and The University of Tokyo.
15 * All rights reserved.
16 *
17 * The 3-clause BSD License is applied to this software:
18 *
19 * * * * * *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are met:
22 *
23 * - Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * - Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * - Neither the names of Hiroshima University, The University of Tokyo nor the
29 * names of its contributors may be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
43 * * * * * *
44 *
45 * @note We assume that your system has inttypes.h. If your system
46 * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
47 * and you have to define PRIu64 and PRIx64 in this file as follows:
48 * @verbatim
49 typedef unsigned int uint32_t
50 typedef unsigned long long uint64_t
51 #define PRIu64 "llu"
52 #define PRIx64 "llx"
53 @endverbatim
54 * uint32_t must be exactly 32-bit unsigned integer type (no more, no
55 * less), and uint64_t must be exactly 64-bit unsigned integer type.
56 * PRIu64 and PRIx64 are used for printf function to print 64-bit
57 * unsigned int and 64-bit unsigned int in hexadecimal format.
58 */
59
60 #ifndef SFMTST_H
61 #define SFMTST_H
62 #if defined(__cplusplus)
63 extern "C" {
64 #endif
65
66 #include <stdio.h>
67 #include <assert.h>
68
69 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
70 #include <inttypes.h>
71 #elif defined(_MSC_VER) || defined(__BORLANDC__)
72 typedef unsigned int uint32_t;
73 typedef unsigned __int64 uint64_t;
74 #define inline __inline
75 #else
76 #include <inttypes.h>
77 #if defined(__GNUC__)
78 #define inline __inline__
79 #endif
80 #endif
81
82 #ifndef PRIu64
83 #if defined(_MSC_VER) || defined(__BORLANDC__)
84 #define PRIu64 "I64u"
85 #define PRIx64 "I64x"
86 #else
87 #define PRIu64 "llu"
88 #define PRIx64 "llx"
89 #endif
90 #endif
91
92
93 // fold in SFMT-params.h, etc. to reduce compilation time a bit
94 #if !defined(SFMT_MEXP)
95 #define SFMT_MEXP 19937
96 #endif
97 /*-----------------
98 BASIC DEFINITIONS
99 -----------------*/
100 /** Mersenne Exponent. The period of the sequence
101 * is a multiple of 2^MEXP-1.
102 * #define SFMT_MEXP 19937 */
103 /** SFMT generator has an internal state array of 128-bit integers,
104 * and N is its size. */
105 #define SFMT_N (SFMT_MEXP / 128 + 1)
106 /** N32 is the size of internal state array when regarded as an array
107 * of 32-bit integers.*/
108 #define SFMT_N32 (SFMT_N * 4)
109 /** N64 is the size of internal state array when regarded as an array
110 * of 64-bit integers.*/
111 #define SFMT_N64 (SFMT_N * 2)
112 #define SFMT_POS1 122
113 #define SFMT_SL1 18
114 #define SFMT_SL2 1
115 #define SFMT_SR1 11
116 #define SFMT_SR2 1
117 #define SFMT_MSK1 0xdfffffefU
118 #define SFMT_MSK2 0xddfecb7fU
119 #define SFMT_MSK3 0xbffaffffU
120 #define SFMT_MSK4 0xbffffff6U
121 #define SFMT_PARITY1 0x00000001U
122 #define SFMT_PARITY2 0x00000000U
123 #define SFMT_PARITY3 0x00000000U
124 #define SFMT_PARITY4 0x13c9e684U
125 #define SFMT_IDSTR "SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6"
126
127
128 /*------------------------------------------
129 128-bit SIMD like data type for standard C
130 ------------------------------------------*/
131 #ifdef __LP64__
132 #ifdef __SSE2__
133 #include <emmintrin.h>
134 #else
135 #define SIMDE_ENABLE_NATIVE_ALIASES
136 #include "x86/sse2.h"
137 #endif
138
139 /** 128-bit data structure */
140 union W128_T {
141 uint32_t u[4];
142 uint64_t u64[2];
143 __m128i si;
144 };
145 #else
146 /** 128-bit data structure */
147 union W128_T {
148 uint32_t u[4];
149 uint64_t u64[2];
150 };
151 #endif
152
153 /** 128-bit data type */
154 typedef union W128_T w128_t;
155
156 /**
157 * SFMT internal state
158 */
159 struct SFMT_T {
160 /** the 128-bit internal state array */
161 w128_t state[SFMT_N];
162 /** index counter to the 32-bit internal state array */
163 int idx;
164 };
165
166 typedef struct SFMT_T sfmt_t;
167
168 void sfmt_fill_array32(sfmt_t * sfmt, uint32_t * array, int size);
169 void sfmt_fill_array64(sfmt_t * sfmt, uint64_t * array, int size);
170 void sfmt_init_gen_rand(sfmt_t * sfmt, uint32_t seed);
171 void sfmt_init_by_array(sfmt_t * sfmt, uint32_t * init_key, int key_length);
172 const char * sfmt_get_idstring(sfmt_t * sfmt);
173 int sfmt_get_min_array_size32(sfmt_t * sfmt);
174 int sfmt_get_min_array_size64(sfmt_t * sfmt);
175 void sfmt_gen_rand_all(sfmt_t * sfmt);
176
177 /**
178 * This function generates and returns 32-bit pseudorandom number.
179 * init_gen_rand or init_by_array must be called before this function.
180 * @param sfmt SFMT internal state
181 * @return 32-bit pseudorandom number
182 */
183 inline static uint32_t sfmt_genrand_uint32(sfmt_t * sfmt) {
184 uint32_t r;
185 uint32_t * psfmt32 = &sfmt->state[0].u[0];
186
187 if (sfmt->idx >= SFMT_N32) {
188 sfmt_gen_rand_all(sfmt);
189 sfmt->idx = 0;
190 }
191 r = psfmt32[sfmt->idx++];
192 return r;
193 }
194 /**
195 * This function generates and returns 64-bit pseudorandom number.
196 * init_gen_rand or init_by_array must be called before this function.
197 * The function gen_rand64 should not be called after gen_rand32,
198 * unless an initialization is again executed.
199 * @param sfmt SFMT internal state
200 * @return 64-bit pseudorandom number
201 */
202 inline static uint64_t sfmt_genrand_uint64(sfmt_t * sfmt) {
203 uint64_t r;
204 uint64_t * psfmt64 = &sfmt->state[0].u64[0];
205 assert(sfmt->idx % 2 == 0);
206
207 if (sfmt->idx >= SFMT_N32) {
208 sfmt_gen_rand_all(sfmt);
209 sfmt->idx = 0;
210 }
211 r = psfmt64[sfmt->idx / 2];
212 sfmt->idx += 2;
213 return r;
214 }
215
216 /* =================================================
217 The following real versions are due to Isaku Wada
218 ================================================= */
219 /**
220 * converts an unsigned 32-bit number to a double on [0,1]-real-interval.
221 * @param v 32-bit unsigned integer
222 * @return double on [0,1]-real-interval
223 */
224 inline static double sfmt_to_real1(uint32_t v)
225 {
226 return v * (1.0/4294967295.0);
227 /* divided by 2^32-1 */
228 }
229
230 /**
231 * generates a random number on [0,1]-real-interval
232 * @param sfmt SFMT internal state
233 * @return double on [0,1]-real-interval
234 */
235 inline static double sfmt_genrand_real1(sfmt_t * sfmt)
236 {
237 return sfmt_to_real1(sfmt_genrand_uint32(sfmt));
238 }
239
240 /**
241 * converts an unsigned 32-bit integer to a double on [0,1)-real-interval.
242 * @param v 32-bit unsigned integer
243 * @return double on [0,1)-real-interval
244 */
245 inline static double sfmt_to_real2(uint32_t v)
246 {
247 return v * (1.0/4294967296.0);
248 /* divided by 2^32 */
249 }
250
251 /**
252 * generates a random number on [0,1)-real-interval
253 * @param sfmt SFMT internal state
254 * @return double on [0,1)-real-interval
255 */
256 inline static double sfmt_genrand_real2(sfmt_t * sfmt)
257 {
258 return sfmt_to_real2(sfmt_genrand_uint32(sfmt));
259 }
260
261 /**
262 * converts an unsigned 32-bit integer to a double on (0,1)-real-interval.
263 * @param v 32-bit unsigned integer
264 * @return double on (0,1)-real-interval
265 */
266 inline static double sfmt_to_real3(uint32_t v)
267 {
268 return (((double)v) + 0.5)*(1.0/4294967296.0);
269 /* divided by 2^32 */
270 }
271
272 /**
273 * generates a random number on (0,1)-real-interval
274 * @param sfmt SFMT internal state
275 * @return double on (0,1)-real-interval
276 */
277 inline static double sfmt_genrand_real3(sfmt_t * sfmt)
278 {
279 return sfmt_to_real3(sfmt_genrand_uint32(sfmt));
280 }
281
282 /**
283 * converts an unsigned 32-bit integer to double on [0,1)
284 * with 53-bit resolution.
285 * @param v 32-bit unsigned integer
286 * @return double on [0,1)-real-interval with 53-bit resolution.
287 */
288 inline static double sfmt_to_res53(uint64_t v)
289 {
290 return v * (1.0/18446744073709551616.0L);
291 }
292
293 /**
294 * generates a random number on [0,1) with 53-bit resolution
295 * @param sfmt SFMT internal state
296 * @return double on [0,1) with 53-bit resolution
297 */
298 inline static double sfmt_genrand_res53(sfmt_t * sfmt)
299 {
300 return sfmt_to_res53(sfmt_genrand_uint64(sfmt));
301 }
302
303
304 /* =================================================
305 The following function are added by Saito.
306 ================================================= */
307 /**
308 * generates a random number on [0,1) with 53-bit resolution from two
309 * 32 bit integers
310 */
311 inline static double sfmt_to_res53_mix(uint32_t x, uint32_t y)
312 {
313 return sfmt_to_res53(x | ((uint64_t)y << 32));
314 }
315
316 /**
317 * generates a random number on [0,1) with 53-bit resolution
318 * using two 32bit integers.
319 * @param sfmt SFMT internal state
320 * @return double on [0,1) with 53-bit resolution
321 */
322 inline static double sfmt_genrand_res53_mix(sfmt_t * sfmt)
323 {
324 uint32_t x, y;
325
326 x = sfmt_genrand_uint32(sfmt);
327 y = sfmt_genrand_uint32(sfmt);
328 return sfmt_to_res53_mix(x, y);
329 }
330
331 #if defined(__cplusplus)
332 }
333 #endif
334
335 #endif