Changeset 611f29d for libcfa/src/bits
- Timestamp:
- Jan 13, 2022, 9:30:38 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- 919a6b2
- Parents:
- f520c4c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified libcfa/src/bits/random.hfa ¶
rf520c4c r611f29d 3 3 #include <stdint.h> 4 4 5 // Pipelined to allow out-of-order overlap with reduced dependencies. Critically, return the current value, and compute 6 // and store the next value. 7 5 8 //-------------------------------------------------- 6 9 #if defined(__SIZEOF_INT128__) 7 typedef __uint128_t __lehmer64_state_t;8 static inline uint64_t __lehmer64( __lehmer64_state_t & state ) {10 static inline uint64_t lehmer64( __uint128_t & state ) { 11 __uint128_t ret = state; 9 12 state *= 0xda942042e4dd58b5; 10 return state>> 64;13 return ret >> 64; 11 14 } 12 15 13 16 //-------------------------------------------------- 14 typedef uint64_t __wyhash64_state_t; 15 static inline uint64_t __wyhash64( __wyhash64_state_t & state ) { 17 static inline uint64_t wyhash64( uint64_t & state ) { 16 18 state += 0x60bee2bee120fc15; 17 19 __uint128_t tmp; … … 25 27 26 28 //-------------------------------------------------- 27 typedef uint64_t __xorshift64_state_t; 28 static inline uint64_t __xorshift64( __xorshift64_state_t & state ) { 29 uint64_t x = state; 30 x ^= x << 13; 31 x ^= x >> 7; 32 x ^= x << 17; 33 return state = x; 29 static inline uint64_t xorshift_13_7_17( uint64_t & state ) { 30 uint64_t ret = state; 31 state ^= state << 13; 32 state ^= state >> 7; 33 state ^= state << 17; 34 return ret; 34 35 } 36 37 //-------------------------------------------------- 38 static inline uint32_t xorshift_6_21_7( uint32_t & state ) { 39 uint32_t ret = state; 40 state ^= state << 6; 41 state ^= state >> 21; 42 state ^= state << 7; 43 return ret; 44 } // xorshift_6_21_7 35 45 36 46 //-------------------------------------------------- … … 38 48 uint32_t a, b, c, d; 39 49 uint32_t counter; 40 } __xorwow__state_t;50 } xorwow__state_t; 41 51 42 52 /* The state array must be initialized to not be all zero in the first four words */ 43 static inline uint32_t __xorwow( __xorwow__state_t & state ) {53 static inline uint32_t xorwow( xorwow__state_t & state ) { 44 54 /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */ 55 uint32_t ret = state.a + state.counter; 45 56 uint32_t t = state.d; 46 57 … … 56 67 57 68 state.counter += 362437; 58 return t + state.counter;69 return ret; 59 70 } 71 72 //-------------------------------------------------- 73 static inline uint32_t LCG( uint32_t & state ) { // linear congruential generator 74 uint32_t ret = state; 75 state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime! No not change it! 76 return ret; 77 } // LCG 78 79 //-------------------------------------------------- 80 #define M (1_l64u << 48_l64u) 81 #define A (25214903917_l64u) 82 #define AI (18446708753438544741_l64u) 83 #define C (11_l64u) 84 #define D (16_l64u) 85 86 static inline uint32_t LCGBI_fwd( uint64_t & state ) { 87 state = (A * state + C) & (M - 1); 88 return state >> D; 89 } 90 91 static inline uint32_t LCGBI_bck( uint64_t & state ) { 92 unsigned int r = state >> D; 93 state = AI * (state - C) & (M - 1); 94 return r; 95 } 96 97 #undef M 98 #undef A 99 #undef AI 100 #undef C 101 #undef D
Note: See TracChangeset
for help on using the changeset viewer.