Changeset 09965e5
- Timestamp:
- Mar 20, 2023, 4:58:21 PM (20 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 12b006c
- Parents:
- 814a4da
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/random.hfa
r814a4da r09965e5 28 28 #define XOSHIRO256PP 29 29 //#define KISS_64 30 // #define SPLITMIX_64 30 31 31 32 // 32-bit generators … … 37 38 //#define XORSHIFT_13_7_17 38 39 #define XOSHIRO256PP 40 // #define SPLITMIX_64 39 41 40 42 // 32-bit generators … … 78 80 #endif // XORSHIFT_6_21_7 79 81 80 #ifdef SPLITMIX_3281 #define PRNG_NAME_32 splitmix82 #define PRNG_STATE_32_T uint32_t83 #endif // SPLITMIX3284 85 82 #ifdef XORSHIFT_12_25_27 86 83 #define PRNG_NAME_64 xorshift_12_25_27 87 84 #define PRNG_STATE_64_T uint64_t 88 85 #endif // XORSHIFT_12_25_27 86 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 89 96 90 97 #ifdef KISS_64 … … 124 131 #ifdef __cforall // don't include in C code (invoke.h) 125 132 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 ) { 139 state += 0x9e3779b97f4a7c15; /* increment the state variable */ 140 uint64_t z = state; /* copy the state to a working variable */ 141 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; /* xor the variable with the variable right bit shifted 30 then multiply by a constant */ 142 z = (z ^ (z >> 27)) * 0x94d049bb133111eb; /* xor the variable with the variable right bit shifted 27 then multiply by a constant */ 143 return z ^ (z >> 31); /* return the variable xored with itself right bit shifted 31 */ 144 } 145 146 static inline void splitmix64_set_seed( uint64_t & state , uint64_t seed ) { 147 state = seed; 148 splitmix64( state ); // prime 149 } // splitmix32_set_seed 150 151 126 152 // Splitmix32 127 153 // https://github.com/bryc/code/blob/master/jshash/PRNGs.md#splitmix32 … … 130 156 // pseudo-random number generators. 131 157 // SplitMix32 is a 32 bit variant of Splitmix64 132 133 158 static inline uint32_t splitmix32( uint32_t & state ) { 134 159 state += 0x9e3779b9; … … 169 194 return m2; 170 195 } // wyhash64 171 172 static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) {173 state = seed;174 wyhash64( state ); // prime175 } // wyhash64_set_seed176 196 #endif // __SIZEOF_INT128__ 177 197 … … 208 228 209 229 static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) { 210 uint64_t state;211 wyhash64_set_seed( state, seed );212 230 // these are done explicitly in this order to attain repeatable seeding. 213 231 // do not call splitmix32 directly in the state init since order of argument evaluation 214 232 // may not be consistent leading to irreproducible seeding 215 uint64_t seed1 = wyhash64( state);216 uint64_t seed2 = wyhash64( state);217 uint64_t seed3 = wyhash64( state);218 uint64_t seed4 = wyhash64( state);233 uint64_t seed1 = splitmix64( seed ); 234 uint64_t seed2 = splitmix64( seed ); 235 uint64_t seed3 = splitmix64( seed ); 236 uint64_t seed4 = splitmix64( seed ); 219 237 state = (xoshiro256pp_t){ seed1, seed2, seed3, seed4 }; 220 238 xoshiro256pp( state ); // prime
Note: See TracChangeset
for help on using the changeset viewer.