Changeset 90fb672
- Timestamp:
- Mar 21, 2023, 7:44:45 AM (18 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 1205b3e
- Parents:
- 12b006c
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/random.hfa
r12b006c r90fb672 10 10 // Created On : Fri Jan 14 07:18:11 2022 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Mar 20 10:01:40202313 // Update Count : 18 012 // Last Modified On : Mon Mar 20 21:45:24 2023 13 // Update Count : 186 14 14 // 15 15 … … 131 131 #ifdef __cforall // don't include in C code (invoke.h) 132 132 133 // Splitmix64134 133 // 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. 134 // 135 // Splitmix64 is not recommended for demanding random number requirements, but is often used to calculate initial states 136 // for other more complex pseudo-random number generators (see https://prng.di.unimi.it). 137 // Also https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64. 138 138 static inline uint64_t splitmix64( uint64_t & state ) { 139 139 state += 0x9e3779b97f4a7c15; … … 149 149 } // splitmix64_set_seed 150 150 151 // Splitmix32152 151 // 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 complex155 // pseudo-random number generators.156 // SplitMix32 is a 32 bit variant of Splitmix64 152 // 153 // Splitmix32 is not recommended for demanding random number requirements, but is often used to calculate initial states 154 // for other more complex pseudo-random number generators (see https://prng.di.unimi.it). 155 157 156 static inline uint32_t splitmix32( uint32_t & state ) { 158 157 state += 0x9e3779b9; … … 169 168 170 169 #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 170 //-------------------------------------------------- 171 static inline uint64_t lehmer64( __uint128_t & state ) { 172 __uint128_t ret = state; 173 state *= 0x_da94_2042_e4dd_58b5; 174 return ret >> 64; 175 } // lehmer64 176 177 static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) { 178 // The seed needs to be coprime with the 2^64 modulus to get the largest period, so no factors of 2 in the seed. 179 state = splitmix64( seed ); // prime 180 } // lehmer64_set_seed 181 182 //-------------------------------------------------- 183 static inline uint64_t wyhash64( uint64_t & state ) { 184 uint64_t ret = state; 185 state += 0x_60be_e2be_e120_fc15; 186 __uint128_t tmp; 187 tmp = (__uint128_t) ret * 0x_a3b1_9535_4a39_b70d; 188 uint64_t m1 = (tmp >> 64) ^ tmp; 189 tmp = (__uint128_t)m1 * 0x_1b03_7387_12fa_d5c9; 190 uint64_t m2 = (tmp >> 64) ^ tmp; 191 return m2; 192 } // wyhash64 193 194 static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) { 195 state = splitmix64( seed ); // prime 196 } // wyhash64_set_seed 195 197 #endif // __SIZEOF_INT128__ 196 198 … … 227 229 228 230 static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) { 229 // these are done explicitly in this order to attain repeatable seeding. 230 // do not call splitmix64 directly in the state init since order of argument evaluation 231 // may not be consistent leading to irreproducible seeding 232 uint64_t seed1 = splitmix64( seed ); 231 // To attain repeatable seeding, compute seeds separately because the order of argument evaluation is undefined. 232 uint64_t seed1 = splitmix64( seed ); // prime 233 233 uint64_t seed2 = splitmix64( seed ); 234 234 uint64_t seed3 = splitmix64( seed ); 235 235 uint64_t seed4 = splitmix64( seed ); 236 236 state = (xoshiro256pp_t){ seed1, seed2, seed3, seed4 }; 237 xoshiro256pp( state ); // prime238 237 } // xoshiro256pp_set_seed 239 238 … … 269 268 270 269 static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) { 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 ); 270 // To attain repeatable seeding, compute seeds separately because the order of argument evaluation is undefined. 271 uint32_t seed1 = splitmix32( seed ); // prime 275 272 uint32_t seed2 = splitmix32( seed ); 276 273 uint32_t seed3 = splitmix32( seed ); 277 274 uint32_t seed4 = splitmix32( seed ); 278 275 state = (xoshiro128pp_t){ seed1, seed2, seed3, seed4 }; 279 xoshiro128pp( state ); // prime280 276 } // xoshiro128pp_set_seed 281 277 … … 290 286 291 287 static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint64_t seed ) { 292 state = seed; 293 xorshift_13_7_17( state ); // prime 288 state = splitmix64( seed ); // prime 294 289 } // xorshift_13_7_17_set_seed 295 290 … … 308 303 309 304 static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) { 310 state = seed; 311 xorshift_6_21_7( state ); // prime 305 state = splitmix32( seed ); // prime 312 306 } // xorshift_6_21_7_set_seed 313 307 … … 323 317 324 318 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 319 state = splitmix64( seed ); // prime 327 320 } // xorshift_12_25_27_set_seed 328 321 … … 345 338 346 339 static inline void kiss_64_set_seed( kiss_64_t & rs, uint64_t seed ) with(rs) { 347 z = 1; w = 1; jsr = 4; jcong = seed; 348 kiss_64( rs ); // prime 340 z = 1; w = 1; jsr = 4; jcong = splitmix64( seed ); // prime 349 341 } // kiss_64_set_seed 350 342 … … 374 366 375 367 static inline void xorwow_set_seed( xorwow_t & rs, uint32_t seed ) { 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 ); 368 // To attain repeatable seeding, compute seeds separately because the order of argument evaluation is undefined. 369 uint32_t seed1 = splitmix32( seed ); // prime 380 370 uint32_t seed2 = splitmix32( seed ); 381 371 uint32_t seed3 = splitmix32( seed ); 382 372 uint32_t seed4 = splitmix32( seed ); 383 373 rs = (xorwow_t){ seed1, seed2, seed3, seed4, 0 }; 384 xorwow( rs ); // prime385 374 } // xorwow_set_seed 386 375 … … 388 377 // Used in __tls_rand_fwd 389 378 #define M (1_l64u << 48_l64u) 390 #define A (25 214903917_l64u)391 #define AI (18 446708753438544741_l64u)379 #define A (25_214_903_917_l64u) 380 #define AI (18_446_708_753_438_544_741_l64u) 392 381 #define C (11_l64u) 393 382 #define D (16_l64u) -
tests/.expect/PRNG.x64.txt
r12b006c r90fb672 1 1 2 2 PRNG() PRNG(5) PRNG(0,5) 3 2629641414891406278 3 04 11972157801652581900 3 55 94706820939349784372 26 316134424938305673 2 27 5572275127081588144 0 38 1 2394954141290188855 2 29 15386440704589550620 2 110 5760167266331356361 2 511 8021670258373873290 2 512 8813161879342109574 1 413 10525294799876107814 2014 14801827301969351008 3 015 1 7016612914230924215 0 016 5485205801221744751 3 217 6143666691223938511 4 018 1 5086131934315954459 4 519 4547668615176940328 4 520 1 7718351571399359777 0521 2636252641646208341 4 022 12820158953704882599 0 43 13944458589275087071 3 2 4 129977468648444256 0 4 5 2357727400298891021 2 2 6 8855179187835660146 3 3 7 9957620185645882382 4 1 8 13396406983727409795 0 5 9 3342782395220265920 0 5 10 1707651271867677937 1 0 11 16402561450140881681 0 1 12 17838519215740313729 4 2 13 7425936020594490136 4 0 14 4174865704721714670 3 5 15 16055269689200152092 0 2 16 15091270195803594018 1 5 17 11807315541476180798 1 1 18 10697186588988060306 4 1 19 14665526411527044929 3 2 20 11289342279096164771 2 5 21 16126980828050300615 1 4 22 7821578301767524260 4 1 23 23 seed 1009 24 24 … … 33 33 34 34 prng() prng(5) prng(0,5) 35 2629641414891406278 3 036 11972157801652581900 3 537 94706820939349784372 238 316134424938305673 2 239 5572275127081588144 0 340 1 2394954141290188855 2 241 15386440704589550620 2 142 5760167266331356361 2 543 8021670258373873290 2 544 8813161879342109574 1 445 10525294799876107814 2046 14801827301969351008 3 047 1 7016612914230924215 0 048 5485205801221744751 3 249 6143666691223938511 4 050 1 5086131934315954459 4 551 4547668615176940328 4 552 1 7718351571399359777 0553 2636252641646208341 4 054 12820158953704882599 0 435 13944458589275087071 3 2 36 129977468648444256 0 4 37 2357727400298891021 2 2 38 8855179187835660146 3 3 39 9957620185645882382 4 1 40 13396406983727409795 0 5 41 3342782395220265920 0 5 42 1707651271867677937 1 0 43 16402561450140881681 0 1 44 17838519215740313729 4 2 45 7425936020594490136 4 0 46 4174865704721714670 3 5 47 16055269689200152092 0 2 48 15091270195803594018 1 5 49 11807315541476180798 1 1 50 10697186588988060306 4 1 51 14665526411527044929 3 2 52 11289342279096164771 2 5 53 16126980828050300615 1 4 54 7821578301767524260 4 1 55 55 seed 1009 56 56 … … 65 65 66 66 prng(t) prng(t,5) prng(t,0,5) 67 2629641414891406278 3 068 11972157801652581900 3 569 94706820939349784372 270 316134424938305673 2 271 5572275127081588144 0 372 1 2394954141290188855 2 273 15386440704589550620 2 174 5760167266331356361 2 575 8021670258373873290 2 576 8813161879342109574 1 477 10525294799876107814 2078 14801827301969351008 3 079 1 7016612914230924215 0 080 5485205801221744751 3 281 6143666691223938511 4 082 1 5086131934315954459 4 583 4547668615176940328 4 584 1 7718351571399359777 0585 2636252641646208341 4 086 12820158953704882599 0 467 13944458589275087071 3 2 68 129977468648444256 0 4 69 2357727400298891021 2 2 70 8855179187835660146 3 3 71 9957620185645882382 4 1 72 13396406983727409795 0 5 73 3342782395220265920 0 5 74 1707651271867677937 1 0 75 16402561450140881681 0 1 76 17838519215740313729 4 2 77 7425936020594490136 4 0 78 4174865704721714670 3 5 79 16055269689200152092 0 2 80 15091270195803594018 1 5 81 11807315541476180798 1 1 82 10697186588988060306 4 1 83 14665526411527044929 3 2 84 11289342279096164771 2 5 85 16126980828050300615 1 4 86 7821578301767524260 4 1 87 87 seed 1009 88 88 -
tests/concurrent/pthread/.expect/bounded_buffer.x64.txt
r12b006c r90fb672 1 producer total value is 3 81602 consumer total value is 3 81601 producer total value is 39780 2 consumer total value is 39780
Note: See TracChangeset
for help on using the changeset viewer.