[e57de69] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // random.hfa -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Fri Jan 14 07:18:11 2022 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[a6bb5fc] | 12 | // Last Modified On : Mon Mar 20 10:01:40 2023 |
---|
| 13 | // Update Count : 180 |
---|
[e57de69] | 14 | // |
---|
| 15 | |
---|
[13c5e19] | 16 | #pragma once |
---|
| 17 | |
---|
[d9585291] | 18 | #include <stdint.h> // uintXX_t |
---|
[13c5e19] | 19 | |
---|
[dd46fd3] | 20 | #define GLUE2( x, y ) x##y |
---|
| 21 | #define GLUE( x, y ) GLUE2( x, y ) |
---|
| 22 | |
---|
[9fce2572] | 23 | // Set default PRNG for architecture size. |
---|
[d2ad151] | 24 | #ifdef __x86_64__ // 64-bit architecture |
---|
[261e107] | 25 | // 64-bit generators |
---|
[b797d97] | 26 | //#define LEHMER64 |
---|
[261e107] | 27 | //#define XORSHIFT_12_25_27 |
---|
[b797d97] | 28 | #define XOSHIRO256PP |
---|
[261e107] | 29 | //#define KISS_64 |
---|
[09965e5] | 30 | // #define SPLITMIX_64 |
---|
[261e107] | 31 | |
---|
| 32 | // 32-bit generators |
---|
[b797d97] | 33 | //#define XORSHIFT_6_21_7 |
---|
| 34 | #define XOSHIRO128PP |
---|
[4c6ba5a] | 35 | // #define SPLITMIX_32 |
---|
[d2ad151] | 36 | #else // 32-bit architecture |
---|
[261e107] | 37 | // 64-bit generators |
---|
[b797d97] | 38 | //#define XORSHIFT_13_7_17 |
---|
| 39 | #define XOSHIRO256PP |
---|
[09965e5] | 40 | // #define SPLITMIX_64 |
---|
[261e107] | 41 | |
---|
| 42 | // 32-bit generators |
---|
[b797d97] | 43 | //#define XORSHIFT_6_21_7 |
---|
| 44 | #define XOSHIRO128PP |
---|
[4c6ba5a] | 45 | // #define SPLITMIX_32 |
---|
[d2ad151] | 46 | #endif // __x86_64__ |
---|
| 47 | |
---|
[4020f09] | 48 | // Define C/CFA PRNG name and random-state. |
---|
| 49 | |
---|
[261e107] | 50 | #ifdef XOSHIRO256PP |
---|
| 51 | #define PRNG_NAME_64 xoshiro256pp |
---|
| 52 | #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t) |
---|
[a6bb5fc] | 53 | typedef struct { uint64_t s0, s1, s2, s3; } PRNG_STATE_64_T; |
---|
[261e107] | 54 | #endif // XOSHIRO256PP |
---|
| 55 | |
---|
| 56 | #ifdef XOSHIRO128PP |
---|
| 57 | #define PRNG_NAME_32 xoshiro128pp |
---|
| 58 | #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t) |
---|
[a6bb5fc] | 59 | typedef struct { uint32_t s0, s1, s2, s3; } PRNG_STATE_32_T; |
---|
[261e107] | 60 | #endif // XOSHIRO128PP |
---|
| 61 | |
---|
[9fce2572] | 62 | #ifdef LEHMER64 |
---|
[dd46fd3] | 63 | #define PRNG_NAME_64 lehmer64 |
---|
| 64 | #define PRNG_STATE_64_T __uint128_t |
---|
[9fce2572] | 65 | #endif // LEHMER64 |
---|
| 66 | |
---|
[261e107] | 67 | #ifdef WYHASH64 |
---|
| 68 | #define PRNG_NAME_64 wyhash64 |
---|
| 69 | #define PRNG_STATE_64_T uint64_t |
---|
| 70 | #endif // LEHMER64 |
---|
| 71 | |
---|
[c8238c0] | 72 | #ifdef XORSHIFT_13_7_17 |
---|
| 73 | #define PRNG_NAME_64 xorshift_13_7_17 |
---|
| 74 | #define PRNG_STATE_64_T uint64_t |
---|
| 75 | #endif // XORSHIFT_13_7_17 |
---|
| 76 | |
---|
[9fce2572] | 77 | #ifdef XORSHIFT_6_21_7 |
---|
[dd46fd3] | 78 | #define PRNG_NAME_32 xorshift_6_21_7 |
---|
| 79 | #define PRNG_STATE_32_T uint32_t |
---|
[9fce2572] | 80 | #endif // XORSHIFT_6_21_7 |
---|
| 81 | |
---|
[261e107] | 82 | #ifdef XORSHIFT_12_25_27 |
---|
| 83 | #define PRNG_NAME_64 xorshift_12_25_27 |
---|
| 84 | #define PRNG_STATE_64_T uint64_t |
---|
| 85 | #endif // XORSHIFT_12_25_27 |
---|
[dd46fd3] | 86 | |
---|
[09965e5] | 87 | #ifdef SPLITMIX_64 |
---|
| 88 | #define PRNG_NAME_64 splitmix64 |
---|
| 89 | #define PRNG_STATE_64_T uint64_t |
---|
| 90 | #endif // SPLITMIX32 |
---|
| 91 | |
---|
| 92 | #ifdef SPLITMIX_32 |
---|
| 93 | #define PRNG_NAME_32 splitmix32 |
---|
| 94 | #define PRNG_STATE_32_T uint32_t |
---|
| 95 | #endif // SPLITMIX32 |
---|
| 96 | |
---|
[261e107] | 97 | #ifdef KISS_64 |
---|
| 98 | #define PRNG_NAME_64 kiss_64 |
---|
| 99 | #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t) |
---|
[a6bb5fc] | 100 | typedef struct { uint64_t z, w, jsr, jcong; } PRNG_STATE_64_T; |
---|
[261e107] | 101 | #endif // KISS_^64 |
---|
[4020f09] | 102 | |
---|
| 103 | #ifdef XORWOW |
---|
| 104 | #define PRNG_NAME_32 xorwow |
---|
| 105 | #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t) |
---|
[a6bb5fc] | 106 | typedef struct { uint32_t a, b, c, d, counter; } PRNG_STATE_32_T; |
---|
[dd46fd3] | 107 | #endif // XOSHIRO128PP |
---|
| 108 | |
---|
| 109 | #define PRNG_SET_SEED_64 GLUE(PRNG_NAME_64,_set_seed) |
---|
| 110 | #define PRNG_SET_SEED_32 GLUE(PRNG_NAME_32,_set_seed) |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | // Default PRNG used by runtime. |
---|
| 114 | #ifdef __x86_64__ // 64-bit architecture |
---|
| 115 | #define PRNG_NAME PRNG_NAME_64 |
---|
| 116 | #define PRNG_STATE_T PRNG_STATE_64_T |
---|
| 117 | #else // 32-bit architecture |
---|
| 118 | #define PRNG_NAME PRNG_NAME_32 |
---|
| 119 | #define PRNG_STATE_T PRNG_STATE_32_T |
---|
| 120 | #endif // __x86_64__ |
---|
| 121 | |
---|
| 122 | #define PRNG_SET_SEED GLUE(PRNG_NAME,_set_seed) |
---|
| 123 | |
---|
| 124 | |
---|
[261e107] | 125 | // ALL PRNG ALGORITHMS ARE OPTIMIZED SO THAT THE PRNG LOGIC CAN HAPPEN IN PARALLEL WITH THE USE OF THE RESULT. |
---|
[b797d97] | 126 | // Specifically, the current random state is copied for returning, before computing the next value. As a consequence, |
---|
| 127 | // the set_seed routine primes the PRNG by calling it with the state so the seed is not return as the first random |
---|
| 128 | // value. |
---|
| 129 | |
---|
[261e107] | 130 | |
---|
[9fce2572] | 131 | #ifdef __cforall // don't include in C code (invoke.h) |
---|
| 132 | |
---|
[09965e5] | 133 | // Splitmix64 |
---|
| 134 | // https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64 |
---|
| 135 | // Splitmix64 is not recommended for demanding random number requirements, |
---|
| 136 | // but is often used to calculate initial states for other more complex |
---|
| 137 | // pseudo-random number generators. |
---|
| 138 | static inline uint64_t splitmix64( uint64_t & state ) { |
---|
[12b006c] | 139 | state += 0x9e3779b97f4a7c15; |
---|
| 140 | uint64_t z = state; |
---|
| 141 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; |
---|
| 142 | z = (z ^ (z >> 27)) * 0x94d049bb133111eb; |
---|
| 143 | return z ^ (z >> 31); |
---|
| 144 | } // splitmix64 |
---|
[09965e5] | 145 | |
---|
| 146 | static inline void splitmix64_set_seed( uint64_t & state , uint64_t seed ) { |
---|
| 147 | state = seed; |
---|
| 148 | splitmix64( state ); // prime |
---|
[12b006c] | 149 | } // splitmix64_set_seed |
---|
[09965e5] | 150 | |
---|
[4c6ba5a] | 151 | // Splitmix32 |
---|
| 152 | // https://github.com/bryc/code/blob/master/jshash/PRNGs.md#splitmix32 |
---|
| 153 | // Splitmix32 is not recommended for demanding random number requirements, |
---|
| 154 | // but is often used to calculate initial states for other more complex |
---|
| 155 | // pseudo-random number generators. |
---|
| 156 | // SplitMix32 is a 32 bit variant of Splitmix64 |
---|
| 157 | static inline uint32_t splitmix32( uint32_t & state ) { |
---|
| 158 | state += 0x9e3779b9; |
---|
| 159 | uint64_t z = state; |
---|
| 160 | z = (z ^ (z >> 15)) * 0x85ebca6b; |
---|
| 161 | z = (z ^ (z >> 13)) * 0xc2b2ae35; |
---|
| 162 | return z ^ (z >> 16); |
---|
[12b006c] | 163 | } // splitmix32 |
---|
[4c6ba5a] | 164 | |
---|
[12b006c] | 165 | static inline void splitmix32_set_seed( uint32_t & state, uint64_t seed ) { |
---|
[4c6ba5a] | 166 | state = seed; |
---|
| 167 | splitmix32( state ); // prime |
---|
| 168 | } // splitmix32_set_seed |
---|
| 169 | |
---|
| 170 | #ifdef __SIZEOF_INT128__ |
---|
| 171 | //-------------------------------------------------- |
---|
| 172 | static inline uint64_t lehmer64( __uint128_t & state ) { |
---|
| 173 | __uint128_t ret = state; |
---|
| 174 | state *= 0x_da94_2042_e4dd_58b5; |
---|
| 175 | return ret >> 64; |
---|
| 176 | } // lehmer64 |
---|
| 177 | |
---|
| 178 | static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) { |
---|
| 179 | // The seed needs to be coprime with the 2^64 modulus to get the largest period, so no factors of 2 in the seed. |
---|
| 180 | state = seed; |
---|
| 181 | lehmer64( state ); // prime |
---|
| 182 | } // lehmer64_set_seed |
---|
| 183 | |
---|
| 184 | //-------------------------------------------------- |
---|
| 185 | static inline uint64_t wyhash64( uint64_t & state ) { |
---|
| 186 | uint64_t ret = state; |
---|
| 187 | state += 0x_60be_e2be_e120_fc15; |
---|
| 188 | __uint128_t tmp; |
---|
| 189 | tmp = (__uint128_t) ret * 0x_a3b1_9535_4a39_b70d; |
---|
| 190 | uint64_t m1 = (tmp >> 64) ^ tmp; |
---|
| 191 | tmp = (__uint128_t)m1 * 0x_1b03_7387_12fa_d5c9; |
---|
| 192 | uint64_t m2 = (tmp >> 64) ^ tmp; |
---|
| 193 | return m2; |
---|
| 194 | } // wyhash64 |
---|
| 195 | #endif // __SIZEOF_INT128__ |
---|
| 196 | |
---|
[4020f09] | 197 | // https://prng.di.unimi.it/xoshiro256starstar.c |
---|
[dd46fd3] | 198 | // |
---|
| 199 | // This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state |
---|
| 200 | // (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of. |
---|
| 201 | // |
---|
| 202 | // For generating just floating-point numbers, xoshiro256+ is even faster. |
---|
| 203 | // |
---|
| 204 | // The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a |
---|
| 205 | // splitmix64 generator and use its output to fill s. |
---|
| 206 | |
---|
| 207 | #ifndef XOSHIRO256PP |
---|
[a6bb5fc] | 208 | typedef struct { uint64_t s0, s1, s2, s3; } xoshiro256pp_t; |
---|
[dd46fd3] | 209 | #endif // ! XOSHIRO256PP |
---|
| 210 | |
---|
| 211 | static inline uint64_t xoshiro256pp( xoshiro256pp_t & rs ) with(rs) { |
---|
[b797d97] | 212 | inline uint64_t rotl( const uint64_t x, int k ) { |
---|
[dd46fd3] | 213 | return (x << k) | (x >> (64 - k)); |
---|
[4020f09] | 214 | } // rotl |
---|
[dd46fd3] | 215 | |
---|
[b797d97] | 216 | const uint64_t result = rotl( s0 + s3, 23 ) + s0; |
---|
| 217 | const uint64_t t = s1 << 17; |
---|
[dd46fd3] | 218 | |
---|
[b797d97] | 219 | s2 ^= s0; |
---|
| 220 | s3 ^= s1; |
---|
| 221 | s1 ^= s2; |
---|
| 222 | s0 ^= s3; |
---|
| 223 | s2 ^= t; |
---|
| 224 | s3 = rotl( s3, 45 ); |
---|
[dd46fd3] | 225 | return result; |
---|
[4020f09] | 226 | } // xoshiro256pp |
---|
[dd46fd3] | 227 | |
---|
[b797d97] | 228 | static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) { |
---|
[4c6ba5a] | 229 | // these are done explicitly in this order to attain repeatable seeding. |
---|
[12b006c] | 230 | // do not call splitmix64 directly in the state init since order of argument evaluation |
---|
[4c6ba5a] | 231 | // may not be consistent leading to irreproducible seeding |
---|
[09965e5] | 232 | uint64_t seed1 = splitmix64( seed ); |
---|
| 233 | uint64_t seed2 = splitmix64( seed ); |
---|
| 234 | uint64_t seed3 = splitmix64( seed ); |
---|
| 235 | uint64_t seed4 = splitmix64( seed ); |
---|
[4c6ba5a] | 236 | state = (xoshiro256pp_t){ seed1, seed2, seed3, seed4 }; |
---|
[a6bb5fc] | 237 | xoshiro256pp( state ); // prime |
---|
[dd46fd3] | 238 | } // xoshiro256pp_set_seed |
---|
| 239 | |
---|
[4020f09] | 240 | // https://prng.di.unimi.it/xoshiro128plusplus.c |
---|
| 241 | // |
---|
| 242 | // This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid generators. It has excellent speed, a state size |
---|
| 243 | // (128 bits) that is large enough for mild parallelism, and it passes all tests we are aware of. |
---|
| 244 | // |
---|
| 245 | // For generating just single-precision (i.e., 32-bit) floating-point numbers, xoshiro128+ is even faster. |
---|
| 246 | // |
---|
| 247 | // The state must be seeded so that it is not everywhere zero. |
---|
| 248 | |
---|
| 249 | #ifndef XOSHIRO128PP |
---|
[a6bb5fc] | 250 | typedef struct { uint32_t s0, s1, s2, s3; } xoshiro128pp_t; |
---|
[4020f09] | 251 | #endif // ! XOSHIRO128PP |
---|
| 252 | |
---|
| 253 | static inline uint32_t xoshiro128pp( xoshiro128pp_t & rs ) with(rs) { |
---|
| 254 | inline uint32_t rotl( const uint32_t x, int k ) { |
---|
| 255 | return (x << k) | (x >> (32 - k)); |
---|
| 256 | } // rotl |
---|
| 257 | |
---|
[b797d97] | 258 | const uint32_t result = rotl( s0 + s3, 7 ) + s0; |
---|
| 259 | const uint32_t t = s1 << 9; |
---|
[4020f09] | 260 | |
---|
[b797d97] | 261 | s2 ^= s0; |
---|
| 262 | s3 ^= s1; |
---|
| 263 | s1 ^= s2; |
---|
| 264 | s0 ^= s3; |
---|
| 265 | s2 ^= t; |
---|
| 266 | s3 = rotl( s3, 11 ); |
---|
[4020f09] | 267 | return result; |
---|
| 268 | } // xoshiro128pp |
---|
| 269 | |
---|
| 270 | static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) { |
---|
[4c6ba5a] | 271 | // these are done explicitly in this order to attain repeatable seeding. |
---|
| 272 | // do not call splitmix32 directly in the state init since order of argument evaluation |
---|
| 273 | // may not be consistent leading to irreproducible seeding |
---|
| 274 | uint32_t seed1 = splitmix32( seed ); |
---|
| 275 | uint32_t seed2 = splitmix32( seed ); |
---|
| 276 | uint32_t seed3 = splitmix32( seed ); |
---|
| 277 | uint32_t seed4 = splitmix32( seed ); |
---|
| 278 | state = (xoshiro128pp_t){ seed1, seed2, seed3, seed4 }; |
---|
[261e107] | 279 | xoshiro128pp( state ); // prime |
---|
[4020f09] | 280 | } // xoshiro128pp_set_seed |
---|
| 281 | |
---|
[13c5e19] | 282 | //-------------------------------------------------- |
---|
[611f29d] | 283 | static inline uint64_t xorshift_13_7_17( uint64_t & state ) { |
---|
| 284 | uint64_t ret = state; |
---|
| 285 | state ^= state << 13; |
---|
| 286 | state ^= state >> 7; |
---|
| 287 | state ^= state << 17; |
---|
| 288 | return ret; |
---|
[4020f09] | 289 | } // xorshift_13_7_17 |
---|
[13c5e19] | 290 | |
---|
[261e107] | 291 | static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint64_t seed ) { |
---|
[dd46fd3] | 292 | state = seed; |
---|
[261e107] | 293 | xorshift_13_7_17( state ); // prime |
---|
[4020f09] | 294 | } // xorshift_13_7_17_set_seed |
---|
[dd46fd3] | 295 | |
---|
[611f29d] | 296 | //-------------------------------------------------- |
---|
[4020f09] | 297 | // Marsaglia shift-XOR PRNG with thread-local state |
---|
| 298 | // Period is 4G-1 |
---|
| 299 | // 0 is absorbing and must be avoided |
---|
| 300 | // Low-order bits are not particularly random |
---|
[611f29d] | 301 | static inline uint32_t xorshift_6_21_7( uint32_t & state ) { |
---|
| 302 | uint32_t ret = state; |
---|
| 303 | state ^= state << 6; |
---|
| 304 | state ^= state >> 21; |
---|
| 305 | state ^= state << 7; |
---|
| 306 | return ret; |
---|
| 307 | } // xorshift_6_21_7 |
---|
| 308 | |
---|
[dd46fd3] | 309 | static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) { |
---|
| 310 | state = seed; |
---|
[261e107] | 311 | xorshift_6_21_7( state ); // prime |
---|
[4020f09] | 312 | } // xorshift_6_21_7_set_seed |
---|
[dd46fd3] | 313 | |
---|
[261e107] | 314 | //-------------------------------------------------- |
---|
| 315 | // The state must be seeded with a nonzero value. |
---|
| 316 | static inline uint64_t xorshift_12_25_27( uint64_t & state ) { |
---|
| 317 | uint64_t ret = state; |
---|
| 318 | state ^= state >> 12; |
---|
| 319 | state ^= state << 25; |
---|
| 320 | state ^= state >> 27; |
---|
| 321 | return ret * 0x_2545_F491_4F6C_DD1D; |
---|
| 322 | } // xorshift_12_25_27 |
---|
| 323 | |
---|
| 324 | static inline void xorshift_12_25_27_set_seed( uint64_t & state, uint64_t seed ) { |
---|
| 325 | state = seed; |
---|
| 326 | xorshift_12_25_27( state ); // prime |
---|
| 327 | } // xorshift_12_25_27_set_seed |
---|
| 328 | |
---|
| 329 | //-------------------------------------------------- |
---|
| 330 | // The state must be seeded with a nonzero value. |
---|
| 331 | #ifndef KISS_64 |
---|
[a6bb5fc] | 332 | typedef struct { uint64_t z, w, jsr, jcong; } kiss_64_t; |
---|
[261e107] | 333 | #endif // ! KISS_64 |
---|
| 334 | |
---|
[b797d97] | 335 | static inline uint64_t kiss_64( kiss_64_t & rs ) with(rs) { |
---|
| 336 | kiss_64_t ret = rs; |
---|
[261e107] | 337 | z = 36969 * (z & 65535) + (z >> 16); |
---|
| 338 | w = 18000 * (w & 65535) + (w >> 16); |
---|
| 339 | jsr ^= (jsr << 13); |
---|
[b797d97] | 340 | jsr ^= (jsr >> 17); |
---|
[261e107] | 341 | jsr ^= (jsr << 5); |
---|
| 342 | jcong = 69069 * jcong + 1234567; |
---|
[3ff64cb] | 343 | return (((ret.z << 16) + ret.w) ^ ret.jcong) + ret.jsr; |
---|
[261e107] | 344 | } // kiss_64 |
---|
| 345 | |
---|
[b797d97] | 346 | static inline void kiss_64_set_seed( kiss_64_t & rs, uint64_t seed ) with(rs) { |
---|
[261e107] | 347 | z = 1; w = 1; jsr = 4; jcong = seed; |
---|
[b797d97] | 348 | kiss_64( rs ); // prime |
---|
[261e107] | 349 | } // kiss_64_set_seed |
---|
| 350 | |
---|
[13c5e19] | 351 | //-------------------------------------------------- |
---|
[4020f09] | 352 | // The state array must be initialized to non-zero in the first four words. |
---|
| 353 | #ifndef XORWOW |
---|
[a6bb5fc] | 354 | typedef struct { uint32_t a, b, c, d, counter; } xorwow_t; |
---|
[4020f09] | 355 | #endif // ! XORWOW |
---|
[13c5e19] | 356 | |
---|
[b797d97] | 357 | static inline uint32_t xorwow( xorwow_t & rs ) with(rs) { |
---|
[e57de69] | 358 | // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs". |
---|
[261e107] | 359 | uint32_t ret = a + counter; |
---|
| 360 | uint32_t t = d; |
---|
[13c5e19] | 361 | |
---|
[261e107] | 362 | uint32_t const s = a; |
---|
| 363 | d = c; |
---|
| 364 | c = b; |
---|
| 365 | b = s; |
---|
[13c5e19] | 366 | |
---|
| 367 | t ^= t >> 2; |
---|
| 368 | t ^= t << 1; |
---|
| 369 | t ^= s ^ (s << 4); |
---|
[261e107] | 370 | a = t; |
---|
| 371 | counter += 362437; |
---|
[611f29d] | 372 | return ret; |
---|
[4020f09] | 373 | } // xorwow |
---|
| 374 | |
---|
[b797d97] | 375 | static inline void xorwow_set_seed( xorwow_t & rs, uint32_t seed ) { |
---|
[4c6ba5a] | 376 | // these are done explicitly in this order to attain repeatable seeding. |
---|
| 377 | // do not call splitmix32 directly in the state init since order of argument evaluation |
---|
| 378 | // may not be consistent leading to irreproducible seeding |
---|
| 379 | uint32_t seed1 = splitmix32( seed ); |
---|
| 380 | uint32_t seed2 = splitmix32( seed ); |
---|
| 381 | uint32_t seed3 = splitmix32( seed ); |
---|
| 382 | uint32_t seed4 = splitmix32( seed ); |
---|
| 383 | rs = (xorwow_t){ seed1, seed2, seed3, seed4, 0 }; |
---|
[b797d97] | 384 | xorwow( rs ); // prime |
---|
[4020f09] | 385 | } // xorwow_set_seed |
---|
[611f29d] | 386 | |
---|
| 387 | //-------------------------------------------------- |
---|
[4020f09] | 388 | // Used in __tls_rand_fwd |
---|
[611f29d] | 389 | #define M (1_l64u << 48_l64u) |
---|
| 390 | #define A (25214903917_l64u) |
---|
| 391 | #define AI (18446708753438544741_l64u) |
---|
| 392 | #define C (11_l64u) |
---|
| 393 | #define D (16_l64u) |
---|
| 394 | |
---|
[e57de69] | 395 | // Bi-directional LCG random-number generator |
---|
[b797d97] | 396 | static inline uint32_t LCGBI_fwd( uint64_t & rs ) { |
---|
| 397 | rs = (A * rs + C) & (M - 1); |
---|
| 398 | return rs >> D; |
---|
[4020f09] | 399 | } // LCGBI_fwd |
---|
[611f29d] | 400 | |
---|
[b797d97] | 401 | static inline uint32_t LCGBI_bck( uint64_t & rs ) { |
---|
| 402 | unsigned int r = rs >> D; |
---|
| 403 | rs = AI * (rs - C) & (M - 1); |
---|
[611f29d] | 404 | return r; |
---|
[4020f09] | 405 | } // LCGBI_bck |
---|
[611f29d] | 406 | |
---|
| 407 | #undef M |
---|
| 408 | #undef A |
---|
| 409 | #undef AI |
---|
| 410 | #undef C |
---|
| 411 | #undef D |
---|
[9fce2572] | 412 | |
---|
| 413 | #endif // __cforall |
---|