| 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 __x86_64__ |
| 133 |
#include <emmintrin.h> |
| 134 |
#else |
| 135 |
#define SIMDE_ENABLE_NATIVE_ALIASES |
| 136 |
#ifdef IGNORE_BUNDLED_SIMDE |
| 137 |
#include <simde/x86/sse2.h> |
| 138 |
#else |
| 139 |
#include "../simde/x86/sse2.h" |
| 140 |
#endif |
| 141 |
#endif |
| 142 |
|
| 143 |
/** 128-bit data structure */ |
| 144 |
union W128_T { |
| 145 |
uint32_t u[4]; |
| 146 |
uint64_t u64[2]; |
| 147 |
__m128i si; |
| 148 |
}; |
| 149 |
#else |
| 150 |
/** 128-bit data structure */ |
| 151 |
union W128_T { |
| 152 |
uint32_t u[4]; |
| 153 |
uint64_t u64[2]; |
| 154 |
}; |
| 155 |
#endif |
| 156 |
|
| 157 |
/** 128-bit data type */ |
| 158 |
typedef union W128_T w128_t; |
| 159 |
|
| 160 |
/** |
| 161 |
* SFMT internal state |
| 162 |
*/ |
| 163 |
struct SFMT_T { |
| 164 |
/** the 128-bit internal state array */ |
| 165 |
w128_t state[SFMT_N]; |
| 166 |
/** index counter to the 32-bit internal state array */ |
| 167 |
int idx; |
| 168 |
}; |
| 169 |
|
| 170 |
typedef struct SFMT_T sfmt_t; |
| 171 |
|
| 172 |
void sfmt_fill_array32(sfmt_t * sfmt, uint32_t * array, int size); |
| 173 |
void sfmt_fill_array64(sfmt_t * sfmt, uint64_t * array, int size); |
| 174 |
void sfmt_init_gen_rand(sfmt_t * sfmt, uint32_t seed); |
| 175 |
void sfmt_init_by_array(sfmt_t * sfmt, uint32_t * init_key, int key_length); |
| 176 |
const char * sfmt_get_idstring(sfmt_t * sfmt); |
| 177 |
int sfmt_get_min_array_size32(sfmt_t * sfmt); |
| 178 |
int sfmt_get_min_array_size64(sfmt_t * sfmt); |
| 179 |
void sfmt_gen_rand_all(sfmt_t * sfmt); |
| 180 |
|
| 181 |
/** |
| 182 |
* This function generates and returns 32-bit pseudorandom number. |
| 183 |
* init_gen_rand or init_by_array must be called before this function. |
| 184 |
* @param sfmt SFMT internal state |
| 185 |
* @return 32-bit pseudorandom number |
| 186 |
*/ |
| 187 |
inline static uint32_t sfmt_genrand_uint32(sfmt_t * sfmt) { |
| 188 |
uint32_t r; |
| 189 |
uint32_t * psfmt32 = &sfmt->state[0].u[0]; |
| 190 |
|
| 191 |
if (sfmt->idx >= SFMT_N32) { |
| 192 |
sfmt_gen_rand_all(sfmt); |
| 193 |
sfmt->idx = 0; |
| 194 |
} |
| 195 |
r = psfmt32[sfmt->idx++]; |
| 196 |
return r; |
| 197 |
} |
| 198 |
/** |
| 199 |
* This function generates and returns 64-bit pseudorandom number. |
| 200 |
* init_gen_rand or init_by_array must be called before this function. |
| 201 |
* The function gen_rand64 should not be called after gen_rand32, |
| 202 |
* unless an initialization is again executed. |
| 203 |
* @param sfmt SFMT internal state |
| 204 |
* @return 64-bit pseudorandom number |
| 205 |
*/ |
| 206 |
inline static uint64_t sfmt_genrand_uint64(sfmt_t * sfmt) { |
| 207 |
uint64_t r; |
| 208 |
uint64_t * psfmt64 = &sfmt->state[0].u64[0]; |
| 209 |
assert(sfmt->idx % 2 == 0); |
| 210 |
|
| 211 |
if (sfmt->idx >= SFMT_N32) { |
| 212 |
sfmt_gen_rand_all(sfmt); |
| 213 |
sfmt->idx = 0; |
| 214 |
} |
| 215 |
r = psfmt64[sfmt->idx / 2]; |
| 216 |
sfmt->idx += 2; |
| 217 |
return r; |
| 218 |
} |
| 219 |
|
| 220 |
/* ================================================= |
| 221 |
The following real versions are due to Isaku Wada |
| 222 |
================================================= */ |
| 223 |
/** |
| 224 |
* converts an unsigned 32-bit number to a double on [0,1]-real-interval. |
| 225 |
* @param v 32-bit unsigned integer |
| 226 |
* @return double on [0,1]-real-interval |
| 227 |
*/ |
| 228 |
inline static double sfmt_to_real1(uint32_t v) |
| 229 |
{ |
| 230 |
return v * (1.0/4294967295.0); |
| 231 |
/* divided by 2^32-1 */ |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* generates a random number on [0,1]-real-interval |
| 236 |
* @param sfmt SFMT internal state |
| 237 |
* @return double on [0,1]-real-interval |
| 238 |
*/ |
| 239 |
inline static double sfmt_genrand_real1(sfmt_t * sfmt) |
| 240 |
{ |
| 241 |
return sfmt_to_real1(sfmt_genrand_uint32(sfmt)); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* converts an unsigned 32-bit integer to a double on [0,1)-real-interval. |
| 246 |
* @param v 32-bit unsigned integer |
| 247 |
* @return double on [0,1)-real-interval |
| 248 |
*/ |
| 249 |
inline static double sfmt_to_real2(uint32_t v) |
| 250 |
{ |
| 251 |
return v * (1.0/4294967296.0); |
| 252 |
/* divided by 2^32 */ |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* generates a random number on [0,1)-real-interval |
| 257 |
* @param sfmt SFMT internal state |
| 258 |
* @return double on [0,1)-real-interval |
| 259 |
*/ |
| 260 |
inline static double sfmt_genrand_real2(sfmt_t * sfmt) |
| 261 |
{ |
| 262 |
return sfmt_to_real2(sfmt_genrand_uint32(sfmt)); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* converts an unsigned 32-bit integer to a double on (0,1)-real-interval. |
| 267 |
* @param v 32-bit unsigned integer |
| 268 |
* @return double on (0,1)-real-interval |
| 269 |
*/ |
| 270 |
inline static double sfmt_to_real3(uint32_t v) |
| 271 |
{ |
| 272 |
#ifdef __cplusplus |
| 273 |
return (static_cast<double>(v) + 0.5)*(1.0/4294967296.0); |
| 274 |
#else |
| 275 |
return (((double)v) + 0.5)*(1.0/4294967296.0); |
| 276 |
#endif |
| 277 |
/* divided by 2^32 */ |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* generates a random number on (0,1)-real-interval |
| 282 |
* @param sfmt SFMT internal state |
| 283 |
* @return double on (0,1)-real-interval |
| 284 |
*/ |
| 285 |
inline static double sfmt_genrand_real3(sfmt_t * sfmt) |
| 286 |
{ |
| 287 |
return sfmt_to_real3(sfmt_genrand_uint32(sfmt)); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* converts an unsigned 64-bit integer to double on [0,1) |
| 292 |
* with 53-bit resolution. |
| 293 |
* @param v 64-bit unsigned integer |
| 294 |
* @return double on [0,1)-real-interval with 53-bit resolution. |
| 295 |
*/ |
| 296 |
inline static double sfmt_to_res53(uint64_t v) |
| 297 |
{ |
| 298 |
return v * 0.0000000000000000000542101086242752217003726400434970855712890625; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* generates a random number on [0,1) with 53-bit resolution |
| 303 |
* @param sfmt SFMT internal state |
| 304 |
* @return double on [0,1) with 53-bit resolution |
| 305 |
*/ |
| 306 |
inline static double sfmt_genrand_res53(sfmt_t * sfmt) |
| 307 |
{ |
| 308 |
return sfmt_to_res53(sfmt_genrand_uint64(sfmt)); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/* ================================================= |
| 313 |
The following function are added by Saito. |
| 314 |
================================================= */ |
| 315 |
/** |
| 316 |
* generates a random number on [0,1) with 53-bit resolution from two |
| 317 |
* 32 bit integers |
| 318 |
*/ |
| 319 |
inline static double sfmt_to_res53_mix(uint32_t x, uint32_t y) |
| 320 |
{ |
| 321 |
#ifdef __cplusplus |
| 322 |
return sfmt_to_res53(x | (static_cast<uint64_t>(y) << 32)); |
| 323 |
#else |
| 324 |
return sfmt_to_res53(x | ((uint64_t)y << 32)); |
| 325 |
#endif |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* generates a random number on [0,1) with 53-bit resolution |
| 330 |
* using two 32bit integers. |
| 331 |
* @param sfmt SFMT internal state |
| 332 |
* @return double on [0,1) with 53-bit resolution |
| 333 |
*/ |
| 334 |
inline static double sfmt_genrand_res53_mix(sfmt_t * sfmt) |
| 335 |
{ |
| 336 |
uint32_t x, y; |
| 337 |
|
| 338 |
x = sfmt_genrand_uint32(sfmt); |
| 339 |
y = sfmt_genrand_uint32(sfmt); |
| 340 |
return sfmt_to_res53_mix(x, y); |
| 341 |
} |
| 342 |
|
| 343 |
#if defined(__cplusplus) |
| 344 |
} |
| 345 |
#endif |
| 346 |
|
| 347 |
#endif |