Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r5d1ebb9 raa8e24c3  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jan 13 21:38:30 2022
    13 // Update Count     : 593
     12// Last Modified On : Wed Dec 29 15:32:44 2021
     13// Update Count     : 512
    1414//
    1515
    1616#include "stdlib.hfa"
    17 #include "bits/random.hfa"
    18 #include "concurrency/invoke.h"                                                 // random_state
    1917
    2018//---------------------------------------
     
    223221//---------------------------------------
    224222
     223static uint32_t seed = 0;                                                               // current seed
     224static thread_local uint32_t state;                                             // random state
     225
     226void set_seed( uint32_t seed_ ) { state = seed = seed_; }
     227uint32_t get_seed() { return seed; }
     228
    225229#define GENERATOR LCG
    226230
    227 uint32_t __global_random_seed;                                                  // sequential/concurrent
    228 uint32_t __global_random_state;                                         // sequential only
    229 
    230 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; GENERATOR( state ); } // set seed
     231inline 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
     240inline 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
    231246uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); }
    232247
    233 void set_seed( uint32_t seed ) { __global_random_seed = seed; GENERATOR( __global_random_state ); }
    234 uint32_t get_seed() { return __global_random_seed; }
    235 uint32_t prng( void ) { return GENERATOR( __global_random_state ); } // [0,UINT_MAX]
     248uint32_t prng( void ) { return GENERATOR( state ); }
    236249
    237250//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.