Changes in / [70cd431:74227c6]


Ignore:
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/bits/random.hfa

    r70cd431 r74227c6  
    1010// Created On       : Fri Jan 14 07:18:11 2022
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec  9 17:08:30 2022
    13 // Update Count     : 169
     12// Last Modified On : Mon Dec  5 13:13:14 2022
     13// Update Count     : 128
    1414//
    1515
     
    2323// Set default PRNG for architecture size.
    2424#ifdef __x86_64__                                                                               // 64-bit architecture
    25         // 64-bit generators
    26         #define LEHMER64
    27         //#define XORSHIFT_12_25_27
    28         //#define XOSHIRO256PP
    29         //#define KISS_64
    30 
    31         // 32-bit generators
    32         #define XORSHIFT_6_21_7
    33         //#define XOSHIRO128PP
     25#define LEHMER64
     26#define XORSHIFT_6_21_7
     27//#define XOSHIRO256PP
     28//#define XOSHIRO128PP
    3429#else                                                                                                   // 32-bit architecture
    35         // 64-bit generators
    36         #define XORSHIFT_13_7_17
    37 
    38         // 32-bit generators
    39         #define XORSHIFT_6_21_7
     30#define XORSHIFT_13_7_17
     31#define XORSHIFT_6_21_7
    4032#endif // __x86_64__
    4133
     
    4335
    4436// SKULLDUGGERY: typedefs name struct and typedef with the same name to deal with CFA typedef numbering problem.
     37
     38#ifdef LEHMER64
     39#define PRNG_NAME_64 lehmer64
     40#define PRNG_STATE_64_T __uint128_t
     41#endif // LEHMER64
     42
     43#ifdef XORSHIFT_13_7_17
     44#define PRNG_NAME_64 xorshift_13_7_17
     45#define PRNG_STATE_64_T uint64_t
     46#endif // XORSHIFT_13_7_17
     47
     48#ifdef XORSHIFT_6_21_7
     49#define PRNG_NAME_32 xorshift_6_21_7
     50#define PRNG_STATE_32_T uint32_t
     51#endif // XORSHIFT_6_21_7
    4552
    4653#ifdef XOSHIRO256PP
     
    5562typedef struct PRNG_STATE_32_T { uint32_t s[4]; } PRNG_STATE_32_T;
    5663#endif // XOSHIRO128PP
    57 
    58 #ifdef LEHMER64
    59 #define PRNG_NAME_64 lehmer64
    60 #define PRNG_STATE_64_T __uint128_t
    61 #endif // LEHMER64
    62 
    63 #ifdef WYHASH64
    64 #define PRNG_NAME_64 wyhash64
    65 #define PRNG_STATE_64_T uint64_t
    66 #endif // LEHMER64
    67 
    68 #ifdef XORSHIFT_13_7_17
    69 #define PRNG_NAME_64 xorshift_13_7_17
    70 #define PRNG_STATE_64_T uint64_t
    71 #endif // XORSHIFT_13_7_17
    72 
    73 #ifdef XORSHIFT_6_21_7
    74 #define PRNG_NAME_32 xorshift_6_21_7
    75 #define PRNG_STATE_32_T uint32_t
    76 #endif // XORSHIFT_6_21_7
    77 
    78 #ifdef XORSHIFT_12_25_27
    79 #define PRNG_NAME_64 xorshift_12_25_27
    80 #define PRNG_STATE_64_T uint64_t
    81 #endif // XORSHIFT_12_25_27
    82 
    83 #ifdef KISS_64
    84 #define PRNG_NAME_64 kiss_64
    85 #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t)
    86 typedef struct PRNG_STATE_64_T { uint64_t z, w, jsr, jcong; } PRNG_STATE_64_T;
    87 #endif // KISS_^64
    8864
    8965#ifdef XORWOW
     
    10985
    11086
    111 // ALL PRNG ALGORITHMS ARE OPTIMIZED SO THAT THE PRNG LOGIC CAN HAPPEN IN PARALLEL WITH THE USE OF THE RESULT.
    112 // Therefore, the set_seed routine primes the state by calling the PRNG with the state so the seed is not return as the
    113 // first random value.
    114 
    11587#ifdef __cforall                                                                                // don't include in C code (invoke.h)
    11688
     
    148120static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state,  uint64_t seed ) {
    149121        state = (xoshiro256pp_t){ {seed, seed, seed, seed} };
    150         xoshiro256pp( state );
    151122} // xoshiro256pp_set_seed
    152123
     
    183154static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) {
    184155        state = (xoshiro128pp_t){ {seed, seed, seed, seed} };
    185         xoshiro128pp( state );                                                          // prime
    186156} // xoshiro128pp_set_seed
    187157
     
    198168        static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) {
    199169                state = seed;
    200                 lehmer64( state );
    201170        } // lehmer64_set_seed
    202171
    203172        //--------------------------------------------------
    204173        static inline uint64_t wyhash64( uint64_t & state ) {
    205                 uint64_t ret = state;
    206                 state += 0x_60be_e2be_e120_fc15;
     174                state += 0x60bee2bee120fc15;
    207175                __uint128_t tmp;
    208                 tmp = (__uint128_t) ret * 0x_a3b1_9535_4a39_b70d;
     176                tmp = (__uint128_t) state * 0xa3b195354a39b70d;
    209177                uint64_t m1 = (tmp >> 64) ^ tmp;
    210                 tmp = (__uint128_t)m1 * 0x_1b03_7387_12fa_d5c9;
     178                tmp = (__uint128_t)m1 * 0x1b03738712fad5c9;
    211179                uint64_t m2 = (tmp >> 64) ^ tmp;
    212180                return m2;
    213         } // wyhash64
    214 
    215         static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) {
     181        }
     182
     183        static inline void wyhash64_set_seed( __uint128_t & state, uint64_t seed ) {
    216184                state = seed;
    217                 wyhash64( state );                                                              // prime
    218         } // wyhash64_set_seed
     185        } // lehmer64_set_seed
    219186#endif // __SIZEOF_INT128__
    220187
     
    228195} // xorshift_13_7_17
    229196
    230 static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint64_t seed ) {
     197static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint32_t seed ) {
    231198        state = seed;
    232         xorshift_13_7_17( state );                                                      // prime
    233199} // xorshift_13_7_17_set_seed
    234200
     
    248214static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) {
    249215        state = seed;
    250         xorshift_6_21_7( state );                                                       // prime
    251216} // xorshift_6_21_7_set_seed
    252 
    253 //--------------------------------------------------
    254 // The state must be seeded with a nonzero value.
    255 static inline uint64_t xorshift_12_25_27( uint64_t & state ) {
    256         uint64_t ret = state;
    257         state ^= state >> 12;
    258         state ^= state << 25;
    259         state ^= state >> 27;
    260         return ret * 0x_2545_F491_4F6C_DD1D;
    261 } // xorshift_12_25_27
    262 
    263 static inline void xorshift_12_25_27_set_seed( uint64_t & state, uint64_t seed ) {
    264         state = seed;
    265         xorshift_12_25_27( state );                                                     // prime
    266 } // xorshift_12_25_27_set_seed
    267 
    268 //--------------------------------------------------
    269 // The state must be seeded with a nonzero value.
    270 #ifndef KISS_64
    271 typedef struct kiss_64_t { uint64_t z, w, jsr, jcong; } kiss_64_t;
    272 #endif // ! KISS_64
    273 
    274 static inline uint64_t kiss_64( kiss_64_t & state ) with(state) {
    275         kiss_64_t ret = state;
    276         z = 36969 * (z & 65535) + (z >> 16);
    277         w = 18000 * (w & 65535) + (w >> 16);
    278         jsr ^= (jsr << 17);
    279         jsr ^= (jsr << 13);
    280         jsr ^= (jsr << 5);
    281         jcong = 69069 * jcong + 1234567;
    282         return ((ret.z << 16 + ret.w) ^ ret.jcong) + ret.jsr;
    283 } // kiss_64
    284 
    285 static inline void kiss_64_set_seed( kiss_64_t & state, uint64_t seed ) with(state) {
    286         z = 1; w = 1; jsr = 4; jcong = seed;
    287         kiss_64( state );                                                                       // prime
    288 } // kiss_64_set_seed
    289217
    290218//--------------------------------------------------
     
    294222#endif // ! XORWOW
    295223
    296 static inline uint32_t xorwow( xorwow_t & state ) with(state) {
     224static inline uint32_t xorwow( xorwow_t & state ) {
    297225        // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs".
    298         uint32_t ret = a + counter;
    299         uint32_t t = d;
    300 
    301         uint32_t const s = a;
    302         d = c;
    303         c = b;
    304         b = s;
     226        uint32_t ret = state.a + state.counter;
     227        uint32_t t = state.d;
     228
     229        uint32_t const s = state.a;
     230        state.d = state.c;
     231        state.c = state.b;
     232        state.b = s;
    305233
    306234        t ^= t >> 2;
    307235        t ^= t << 1;
    308236        t ^= s ^ (s << 4);
    309         a = t;
    310         counter += 362437;
     237        state.a = t;
     238
     239        state.counter += 362437;
    311240        return ret;
    312241} // xorwow
     
    314243static inline void xorwow_set_seed( xorwow_t & state, uint32_t seed ) {
    315244        state = (xorwow_t){ seed, seed, seed, seed, 0 };
    316         xorwow( state );                                                                        // prime
    317245} // xorwow_set_seed
    318246
  • libcfa/src/concurrency/thread.cfa

    r70cd431 r74227c6  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec  9 15:13:04 2022
    13 // Update Count     : 98
     12// Last Modified On : Wed Nov 30 18:14:07 2022
     13// Update Count     : 95
    1414//
    1515
     
    230230void set_seed( size_t seed ) {
    231231        PRNG_STATE_T & state = active_thread()->random_state;
     232        __global_random_seed = seed;
    232233        PRNG_SET_SEED( state, seed );
    233         __global_random_seed = seed;
     234        (void)PRNG_NAME( state );                                                       // prime PRNG
    234235        __global_random_prime = seed;
    235236        __global_random_mask = true;
  • libcfa/src/stdlib.cfa

    r70cd431 r74227c6  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec  9 15:11:30 2022
    13 // Update Count     : 631
     12// Last Modified On : Mon Dec  5 11:59:03 2022
     13// Update Count     : 628
    1414//
    1515
     
    232232        __global_random_seed = seed;
    233233        PRNG_SET_SEED( __global_random_state, seed );
     234        PRNG_NAME( __global_random_state );
    234235} // set_seed
    235236
  • libcfa/src/stdlib.hfa

    r70cd431 r74227c6  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec  9 15:11:30 2022
    13 // Update Count     : 763
     12// Last Modified On : Wed Nov 30 18:18:26 2022
     13// Update Count     : 760
    1414//
    1515
     
    426426
    427427static inline {
    428         void set_seed( PRNG32 & prng, uint32_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_32( state, seed ); }
    429         uint32_t get_seed( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; }
     428        void set_seed( PRNG32 & prng, uint32_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_32( state, seed ); PRNG_NAME_32( state ); } // set seed
     429        uint32_t get_seed( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
    430430        uint32_t prng( PRNG32 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_32( state ); } // [0,UINT_MAX]
    431431        uint32_t prng( PRNG32 & prng, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)
     
    443443
    444444static inline {
    445         void set_seed( PRNG64 & prng, uint64_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_64( state, seed ); }
    446         uint64_t get_seed( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; }
     445        void set_seed( PRNG64 & prng, uint64_t seed_ ) with( prng ) { seed = seed_; PRNG_SET_SEED_64( state, seed ); PRNG_NAME_64( state ); } // set seed
     446        uint64_t get_seed( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed
    447447        uint64_t prng( PRNG64 & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return PRNG_NAME_64( state ); } // [0,UINT_MAX]
    448448        uint64_t prng( PRNG64 & prng, size_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u)
  • tests/.expect/nested_function.x64.txt

    r70cd431 r74227c6  
    1 total 80
     1total 75
Note: See TracChangeset for help on using the changeset viewer.