Changes in libcfa/src/bits/random.hfa [4020f09:261e107]
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/bits/random.hfa
r4020f09 r261e107 10 10 // Created On : Fri Jan 14 07:18:11 2022 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Dec 5 13:13:14202213 // Update Count : 1 2812 // Last Modified On : Fri Dec 9 17:08:30 2022 13 // Update Count : 169 14 14 // 15 15 … … 23 23 // Set default PRNG for architecture size. 24 24 #ifdef __x86_64__ // 64bit architecture 25 #define LEHMER64 26 #define XORSHIFT_6_21_7 27 //#define XOSHIRO256PP 28 //#define XOSHIRO128PP 25 // 64bit generators 26 #define LEHMER64 27 //#define XORSHIFT_12_25_27 28 //#define XOSHIRO256PP 29 //#define KISS_64 30 31 // 32bit generators 32 #define XORSHIFT_6_21_7 33 //#define XOSHIRO128PP 29 34 #else // 32bit architecture 30 #define XORSHIFT_13_7_17 31 #define XORSHIFT_6_21_7 35 // 64bit generators 36 #define XORSHIFT_13_7_17 37 38 // 32bit generators 39 #define XORSHIFT_6_21_7 32 40 #endif // __x86_64__ 33 41 … … 35 43 36 44 // SKULLDUGGERY: typedefs name struct and typedef with the same name to deal with CFA typedef numbering problem. 37 38 #ifdef LEHMER6439 #define PRNG_NAME_64 lehmer6440 #define PRNG_STATE_64_T __uint128_t41 #endif // LEHMER6442 43 #ifdef XORSHIFT_13_7_1744 #define PRNG_NAME_64 xorshift_13_7_1745 #define PRNG_STATE_64_T uint64_t46 #endif // XORSHIFT_13_7_1747 48 #ifdef XORSHIFT_6_21_749 #define PRNG_NAME_32 xorshift_6_21_750 #define PRNG_STATE_32_T uint32_t51 #endif // XORSHIFT_6_21_752 45 53 46 #ifdef XOSHIRO256PP … … 62 55 typedef struct PRNG_STATE_32_T { uint32_t s[4]; } PRNG_STATE_32_T; 63 56 #endif // XOSHIRO128PP 57 58 #ifdef LEHMER64 59 #define PRNG_NAME_64 lehmer64 60 #define PRNG_STATE_64_T __uint128_t 61 #endif // LEHMER64 62 63 #ifdef WYHASH64 64 #define PRNG_NAME_64 wyhash64 65 #define PRNG_STATE_64_T uint64_t 66 #endif // LEHMER64 67 68 #ifdef XORSHIFT_13_7_17 69 #define PRNG_NAME_64 xorshift_13_7_17 70 #define PRNG_STATE_64_T uint64_t 71 #endif // XORSHIFT_13_7_17 72 73 #ifdef XORSHIFT_6_21_7 74 #define PRNG_NAME_32 xorshift_6_21_7 75 #define PRNG_STATE_32_T uint32_t 76 #endif // XORSHIFT_6_21_7 77 78 #ifdef XORSHIFT_12_25_27 79 #define PRNG_NAME_64 xorshift_12_25_27 80 #define PRNG_STATE_64_T uint64_t 81 #endif // XORSHIFT_12_25_27 82 83 #ifdef KISS_64 84 #define PRNG_NAME_64 kiss_64 85 #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t) 86 typedef struct PRNG_STATE_64_T { uint64_t z, w, jsr, jcong; } PRNG_STATE_64_T; 87 #endif // KISS_^64 64 88 65 89 #ifdef XORWOW … … 85 109 86 110 111 // ALL PRNG ALGORITHMS ARE OPTIMIZED SO THAT THE PRNG LOGIC CAN HAPPEN IN PARALLEL WITH THE USE OF THE RESULT. 112 // Therefore, the set_seed routine primes the state by calling the PRNG with the state so the seed is not return as the 113 // first random value. 114 87 115 #ifdef __cforall // don't include in C code (invoke.h) 88 116 … … 120 148 static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) { 121 149 state = (xoshiro256pp_t){ {seed, seed, seed, seed} }; 150 xoshiro256pp( state ); 122 151 } // xoshiro256pp_set_seed 123 152 … … 154 183 static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) { 155 184 state = (xoshiro128pp_t){ {seed, seed, seed, seed} }; 185 xoshiro128pp( state ); // prime 156 186 } // xoshiro128pp_set_seed 157 187 … … 168 198 static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) { 169 199 state = seed; 200 lehmer64( state ); 170 201 } // lehmer64_set_seed 171 202 172 203 // 173 204 static inline uint64_t wyhash64( uint64_t & state ) { 174 state += 0x60bee2bee120fc15; 205 uint64_t ret = state; 206 state += 0x_60be_e2be_e120_fc15; 175 207 __uint128_t tmp; 176 tmp = (__uint128_t) state * 0xa3b195354a39b70d;208 tmp = (__uint128_t) ret * 0x_a3b1_9535_4a39_b70d; 177 209 uint64_t m1 = (tmp >> 64) ^ tmp; 178 tmp = (__uint128_t)m1 * 0x 1b03738712fad5c9;210 tmp = (__uint128_t)m1 * 0x_1b03_7387_12fa_d5c9; 179 211 uint64_t m2 = (tmp >> 64) ^ tmp; 180 212 return m2; 181 } 182 183 static inline void wyhash64_set_seed( __uint128_t & state, uint64_t seed ) {213 } // wyhash64 214 215 static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) { 184 216 state = seed; 185 } // lehmer64_set_seed 217 wyhash64( state ); // prime 218 } // wyhash64_set_seed 186 219 #endif // __SIZEOF_INT128__ 187 220 … … 195 228 } // xorshift_13_7_17 196 229 197 static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint 32_t seed ) {230 static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint64_t seed ) { 198 231 state = seed; 232 xorshift_13_7_17( state ); // prime 199 233 } // xorshift_13_7_17_set_seed 200 234 … … 214 248 static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) { 215 249 state = seed; 250 xorshift_6_21_7( state ); // prime 216 251 } // xorshift_6_21_7_set_seed 252 253 // 254 // The state must be seeded with a nonzero value. 255 static inline uint64_t xorshift_12_25_27( uint64_t & state ) { 256 uint64_t ret = state; 257 state ^= state >> 12; 258 state ^= state << 25; 259 state ^= state >> 27; 260 return ret * 0x_2545_F491_4F6C_DD1D; 261 } // xorshift_12_25_27 262 263 static inline void xorshift_12_25_27_set_seed( uint64_t & state, uint64_t seed ) { 264 state = seed; 265 xorshift_12_25_27( state ); // prime 266 } // xorshift_12_25_27_set_seed 267 268 // 269 // The state must be seeded with a nonzero value. 270 #ifndef KISS_64 271 typedef struct kiss_64_t { uint64_t z, w, jsr, jcong; } kiss_64_t; 272 #endif // ! KISS_64 273 274 static inline uint64_t kiss_64( kiss_64_t & state ) with(state) { 275 kiss_64_t ret = state; 276 z = 36969 * (z & 65535) + (z >> 16); 277 w = 18000 * (w & 65535) + (w >> 16); 278 jsr ^= (jsr << 17); 279 jsr ^= (jsr << 13); 280 jsr ^= (jsr << 5); 281 jcong = 69069 * jcong + 1234567; 282 return ((ret.z << 16 + ret.w) ^ ret.jcong) + ret.jsr; 283 } // kiss_64 284 285 static inline void kiss_64_set_seed( kiss_64_t & state, uint64_t seed ) with(state) { 286 z = 1; w = 1; jsr = 4; jcong = seed; 287 kiss_64( state ); // prime 288 } // kiss_64_set_seed 217 289 218 290 // … … 222 294 #endif // ! XORWOW 223 295 224 static inline uint32_t xorwow( xorwow_t & state ) {296 static inline uint32_t xorwow( xorwow_t & state ) with(state) { 225 297 // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs". 226 uint32_t ret = state.a + state.counter;227 uint32_t t = state.d;228 229 uint32_t const s = state.a;230 state.d = state.c;231 state.c = state.b;232 state.b = s;298 uint32_t ret = a + counter; 299 uint32_t t = d; 300 301 uint32_t const s = a; 302 d = c; 303 c = b; 304 b = s; 233 305 234 306 t ^= t >> 2; 235 307 t ^= t << 1; 236 308 t ^= s ^ (s << 4); 237 state.a = t; 238 239 state.counter += 362437; 309 a = t; 310 counter += 362437; 240 311 return ret; 241 312 } // xorwow … … 243 314 static inline void xorwow_set_seed( xorwow_t & state, uint32_t seed ) { 244 315 state = (xorwow_t){ seed, seed, seed, seed, 0 }; 316 xorwow( state ); // prime 245 317 } // xorwow_set_seed 246 318
Note: See TracChangeset
for help on using the changeset viewer.