Ignore:
Timestamp:
Jan 12, 2022, 6:34:58 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
00f5fde
Parents:
a10f6b4
Message:

third attempt at specialized PRNG

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    ra10f6b4 r1959528  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 10 17:07:21 2022
    13 // Update Count     : 572
     12// Last Modified On : Wed Jan 12 16:25:36 2022
     13// Update Count     : 578
    1414//
    1515
     
    223223//---------------------------------------
    224224
     225// Pipelined to allow OoO overlap with reduced dependencies. Critically, return the current value, and compute and store
     226// the next value.
     227
    225228#define GENERATOR LCG
    226229
    227 inline uint32_t MarsagliaXor( uint32_t & state ) {
     230static inline uint32_t MarsagliaXor( uint32_t & state ) {
     231        uint32_t ret = state;
    228232        state ^= state << 6;
    229233        state ^= state >> 21;
    230234        state ^= state << 7;
    231         return state;
     235        return ret;
    232236} // MarsagliaXor
    233237
    234 inline uint32_t LCG( uint32_t & state ) {                               // linear congruential generator
    235         return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime!
     238static inline uint32_t LCG( uint32_t & state ) {                // linear congruential generator
     239        uint32_t ret = state;
     240        state = 36969 * (state & 65535) + (state >> 16);        // 36969 is NOT prime! No not change it!
     241        return ret;
    236242} // LCG
    237243
    238 uint32_t __thread_seed = rdtscl();                                              // global thread seed
    239 
    240 void set_seed( uint32_t seed ) { __thread_seed = seed; }
    241 uint32_t get_seed() { return  __thread_seed; }
     244uint32_t __global_random_seed;
     245
     246void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; GENERATOR( state ); } // set seed
    242247uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); }
    243248
    244 uint32_t prng( void ) { return GENERATOR( __thread_seed ); } // [0,UINT_MAX]
     249void set_seed( uint32_t seed ) {
     250        active_thread()->random_state = __global_random_seed = seed;
     251        GENERATOR( active_thread()->random_state );
     252} // set_seed
     253uint32_t get_seed() { return __global_random_seed; }
     254uint32_t prng( void ) { return GENERATOR( active_thread()->random_state ); } // [0,UINT_MAX]
    245255
    246256//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.