Changeset 4020f09 for libcfa/src
- Timestamp:
- Dec 5, 2022, 1:23:03 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- d4c8b59
- Parents:
- aadb0c8
- Location:
- libcfa/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/random.hfa
raadb0c8 r4020f09 10 10 // Created On : Fri Jan 14 07:18:11 2022 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Dec 1 11:52:08202213 // Update Count : 1 1512 // Last Modified On : Mon Dec 5 13:13:14 2022 13 // Update Count : 128 14 14 // 15 15 … … 30 30 #define XORSHIFT_13_7_17 31 31 #define XORSHIFT_6_21_7 32 //#define XOSHIRO256PP33 //#define XOSHIRO128PP34 32 #endif // __x86_64__ 35 33 36 // C/CFA PRNG name and random-state. 34 // Define C/CFA PRNG name and random-state. 35 36 // SKULLDUGGERY: typedefs name struct and typedef with the same name to deal with CFA typedef numbering problem. 37 37 38 38 #ifdef LEHMER64 … … 53 53 #ifdef XOSHIRO256PP 54 54 #define PRNG_NAME_64 xoshiro256pp 55 #define PRNG_STATE_64_T structGLUE(PRNG_NAME_64,_t)56 PRNG_STATE_64_T { uint64_t s[4]; };55 #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t) 56 typedef struct PRNG_STATE_64_T { uint64_t s[4]; } PRNG_STATE_64_T; 57 57 #endif // XOSHIRO256PP 58 58 59 59 #ifdef XOSHIRO128PP 60 60 #define PRNG_NAME_32 xoshiro128pp 61 #define PRNG_STATE_32_T struct GLUE(PRNG_NAME_32,_t) 62 PRNG_STATE_32_T { uint32_t s[4]; }; 61 #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t) 62 typedef struct PRNG_STATE_32_T { uint32_t s[4]; } PRNG_STATE_32_T; 63 #endif // XOSHIRO128PP 64 65 #ifdef XORWOW 66 #define PRNG_NAME_32 xorwow 67 #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t) 68 typedef struct PRNG_STATE_32_T { uint32_t a, b, c, d, counter; } PRNG_STATE_32_T; 63 69 #endif // XOSHIRO128PP 64 70 … … 81 87 #ifdef __cforall // don't include in C code (invoke.h) 82 88 89 // https://prng.di.unimi.it/xoshiro256starstar.c 90 // 91 // This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state 92 // (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of. 93 // 94 // For generating just floating-point numbers, xoshiro256+ is even faster. 95 // 96 // The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a 97 // splitmix64 generator and use its output to fill s. 98 99 #ifndef XOSHIRO256PP 100 typedef struct xoshiro256pp_t { uint64_t s[4]; } xoshiro256pp_t; 101 #endif // ! XOSHIRO256PP 102 103 static inline uint64_t xoshiro256pp( xoshiro256pp_t & rs ) with(rs) { 104 inline uint64_t rotl(const uint64_t x, int k) { 105 return (x << k) | (x >> (64 - k)); 106 } // rotl 107 108 const uint64_t result = rotl( s[0] + s[3], 23 ) + s[0]; 109 const uint64_t t = s[1] << 17; 110 111 s[2] ^= s[0]; 112 s[3] ^= s[1]; 113 s[1] ^= s[2]; 114 s[0] ^= s[3]; 115 s[2] ^= t; 116 s[3] = rotl( s[3], 45 ); 117 return result; 118 } // xoshiro256pp 119 120 static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) { 121 state = (xoshiro256pp_t){ {seed, seed, seed, seed} }; 122 } // xoshiro256pp_set_seed 123 83 124 // https://prng.di.unimi.it/xoshiro128plusplus.c 84 125 // … … 91 132 92 133 #ifndef XOSHIRO128PP 93 struct xoshiro128pp_t { uint32_t s[4]; };134 typedef struct xoshiro128pp_t { uint32_t s[4]; } xoshiro128pp_t; 94 135 #endif // ! XOSHIRO128PP 95 136 … … 97 138 inline uint32_t rotl( const uint32_t x, int k ) { 98 139 return (x << k) | (x >> (32 - k)); 99 } 140 } // rotl 100 141 101 142 const uint32_t result = rotl( s[0] + s[3], 7 ) + s[0]; … … 109 150 s[3] = rotl( s[3], 11 ); 110 151 return result; 111 } 152 } // xoshiro128pp 112 153 113 154 static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) { 114 155 state = (xoshiro128pp_t){ {seed, seed, seed, seed} }; 115 156 } // xoshiro128pp_set_seed 116 117 // This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state118 // (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of.119 //120 // For generating just floating-point numbers, xoshiro256+ is even faster.121 //122 // The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a123 // splitmix64 generator and use its output to fill s.124 125 #ifndef XOSHIRO256PP126 struct xoshiro256pp_t { uint64_t s[4]; };127 #endif // ! XOSHIRO256PP128 129 static inline uint64_t xoshiro256pp( xoshiro256pp_t & rs ) with(rs) {130 inline uint64_t rotl(const uint64_t x, int k) {131 return (x << k) | (x >> (64 - k));132 }133 134 const uint64_t result = rotl( s[0] + s[3], 23 ) + s[0];135 const uint64_t t = s[1] << 17;136 137 s[2] ^= s[0];138 s[3] ^= s[1];139 s[1] ^= s[2];140 s[0] ^= s[3];141 s[2] ^= t;142 s[3] = rotl( s[3], 45 );143 return result;144 }145 146 static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) {147 state = (xoshiro256pp_t){ {seed, seed, seed, seed} };148 } // xoshiro256pp_set_seed149 157 150 158 #ifdef __SIZEOF_INT128__ … … 185 193 state ^= state << 17; 186 194 return ret; 187 } 195 } // xorshift_13_7_17 188 196 189 197 static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint32_t seed ) { 190 198 state = seed; 191 } 192 193 //-------------------------------------------------- 199 } // xorshift_13_7_17_set_seed 200 201 //-------------------------------------------------- 202 // Marsaglia shift-XOR PRNG with thread-local state 203 // Period is 4G-1 204 // 0 is absorbing and must be avoided 205 // Low-order bits are not particularly random 194 206 static inline uint32_t xorshift_6_21_7( uint32_t & state ) { 195 207 uint32_t ret = state; … … 202 214 static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) { 203 215 state = seed; 204 } 205 206 //-------------------------------------------------- 207 typedef struct { 208 uint32_t a, b, c, d; 209 uint32_t counter; 210 } xorwow__state_t; 211 212 // The state array must be initialized to not be all zero in the first four words. 213 static inline uint32_t xorwow( xorwow__state_t & state ) { 216 } // xorshift_6_21_7_set_seed 217 218 //-------------------------------------------------- 219 // The state array must be initialized to non-zero in the first four words. 220 #ifndef XORWOW 221 typedef struct xorwow_t { uint32_t a, b, c, d, counter; } xorwow_t; 222 #endif // ! XORWOW 223 224 static inline uint32_t xorwow( xorwow_t & state ) { 214 225 // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs". 215 226 uint32_t ret = state.a + state.counter; … … 228 239 state.counter += 362437; 229 240 return ret; 230 } 231 241 } // xorwow 242 243 static inline void xorwow_set_seed( xorwow_t & state, uint32_t seed ) { 244 state = (xorwow_t){ seed, seed, seed, seed, 0 }; 245 } // xorwow_set_seed 246 247 //-------------------------------------------------- 232 248 // Used in __tls_rand_fwd 233 //--------------------------------------------------234 249 #define M (1_l64u << 48_l64u) 235 250 #define A (25214903917_l64u) … … 242 257 state = (A * state + C) & (M - 1); 243 258 return state >> D; 244 } 259 } // LCGBI_fwd 245 260 246 261 static inline uint32_t LCGBI_bck( uint64_t & state ) { … … 248 263 state = AI * (state - C) & (M - 1); 249 264 return r; 250 } 265 } // LCGBI_bck 251 266 252 267 #undef M -
libcfa/src/startup.cfa
raadb0c8 r4020f09 10 10 // Created On : Tue Jul 24 16:21:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Nov 30 18:14:06202213 // Update Count : 6812 // Last Modified On : Mon Dec 5 11:41:58 2022 13 // Update Count : 73 14 14 // 15 15 … … 71 71 __global_random_seed = rdtscl(); 72 72 PRNG_SET_SEED( __global_random_state, __global_random_seed ); 73 73 74 __cfaabi_interpose_startup(); 74 75 __cfaabi_device_startup(); -
libcfa/src/stdlib.cfa
raadb0c8 r4020f09 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Dec 3 17:14:13 202213 // Update Count : 62 312 // Last Modified On : Mon Dec 5 11:59:03 2022 13 // Update Count : 628 14 14 // 15 15 … … 233 233 PRNG_SET_SEED( __global_random_state, seed ); 234 234 PRNG_NAME( __global_random_state ); 235 } 235 } // set_seed 236 236 237 size_t get_seed() { return __global_random_seed; } 237 238 size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
Note: See TracChangeset
for help on using the changeset viewer.