Changeset aa8e24c3
- Timestamp:
- Dec 29, 2021, 5:06:07 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- aa0a1ad, db1ebed
- Parents:
- 7a6ae53
- git-author:
- Peter A. Buhr <pabuhr@…> (12/29/21 17:00:15)
- git-committer:
- Peter A. Buhr <pabuhr@…> (12/29/21 17:06:07)
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
r7a6ae53 raa8e24c3 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 12 07:46:09 202013 // Update Count : 5 0312 // Last Modified On : Wed Dec 29 15:32:44 2021 13 // Update Count : 512 14 14 // 15 15 … … 221 221 //--------------------------------------- 222 222 223 bool threading_enabled(void) __attribute__((weak)) { 224 return false; 225 } 223 static uint32_t seed = 0; // current seed 224 static thread_local uint32_t state; // random state 225 226 void set_seed( uint32_t seed_ ) { state = seed = seed_; } 227 uint32_t get_seed() { return seed; } 228 229 #define GENERATOR LCG 230 231 inline uint32_t MarsagliaXor( uint32_t & state ) { 232 if ( unlikely( seed == 0 ) ) set_seed( rdtscl() ); 233 else if ( unlikely( state == 0 ) ) state = seed; 234 state ^= state << 6; 235 state ^= state >> 21; 236 state ^= state << 7; 237 return state; 238 } // MarsagliaXor 239 240 inline uint32_t LCG( uint32_t & state ) { // linear congruential generator 241 if ( unlikely( seed == 0 ) ) set_seed( rdtscl() ); 242 else if ( unlikely( state == 0 ) ) state = seed; 243 return state = 36973 * (state & 65535) + (state >> 16); 244 } // LCG 245 246 uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); } 247 248 uint32_t prng( void ) { return GENERATOR( state ); } 249 250 //--------------------------------------- 251 252 bool threading_enabled( void ) __attribute__(( weak )) { return false; } 226 253 227 254 // Local Variables: // -
libcfa/src/stdlib.hfa
r7a6ae53 raa8e24c3 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Apr 20 21:20:03202113 // Update Count : 5 7512 // Last Modified On : Wed Dec 29 15:30:58 2021 13 // Update Count : 591 14 14 // 15 15 … … 363 363 364 364 static inline { 365 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l ) + l; } // [l,u)366 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u); } // [0,u)365 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] 366 long int random( long int u ) { return random( 0, u - 1 ); } // [0,u) 367 367 unsigned long int random( void ) { return lrand48(); } 368 368 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) 369 unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l ) + l; } // [l,u)369 unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] 370 370 371 371 char random( void ) { return (unsigned long int)random(); } … … 388 388 //--------------------------------------- 389 389 390 struct PRNG { 391 uint32_t callcnt; // call count 392 uint32_t seed; // current seed 393 uint32_t state; // random state 394 }; // PRNG 395 396 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 397 static inline { 398 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed 399 void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed 400 void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed 401 uint32_t get_seed( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed 402 uint32_t prng( PRNG & prng, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u) 403 uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u] 404 uint32_t calls( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; } 405 } // distribution 406 407 extern void set_seed( uint32_t seed ); // set per thread seed 408 extern uint32_t get_seed(); // get seed 409 extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 410 static inline { 411 uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )); 412 uint32_t prng( uint32_t u ) { return prng() % u; } // [0,u) 413 uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )); 414 uint32_t prng( uint32_t l, uint32_t u ) { return prng( u - l + 1 ) + l; } // [l,u] 415 } // distribution 416 417 //--------------------------------------- 418 390 419 extern bool threading_enabled( void ) OPTIONAL_THREAD; 391 420 -
tests/.expect/random.x64.txt
r7a6ae53 raa8e24c3 1 1 õ 2 2 = 3 V 3 K 4 4 -911259971 5 5 6 6 -4 6 11 7 7 1232105397 8 8 0 9 1 89 11 10 10 -914096085 11 11 1 12 15 12 20 13 13 2077092859 14 14 1 15 1 115 12 16 16 0.677254 17 17 0.678106775246139
Note: See TracChangeset
for help on using the changeset viewer.