Changeset 09965e5 for libcfa/src


Ignore:
Timestamp:
Mar 20, 2023, 4:58:21 PM (21 months ago)
Author:
caparsons <caparson@…>
Branches:
ADT, ast-experimental, master
Children:
12b006c
Parents:
814a4da
Message:

added splitmix64 since wyhash has 128 bit dependency and xoshiro doesnt

File:
1 edited

Legend:

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

    r814a4da r09965e5  
    2828        #define XOSHIRO256PP
    2929        //#define KISS_64
     30    // #define SPLITMIX_64
    3031
    3132        // 32-bit generators
     
    3738        //#define XORSHIFT_13_7_17
    3839        #define XOSHIRO256PP
     40    // #define SPLITMIX_64
    3941
    4042        // 32-bit generators
     
    7880#endif // XORSHIFT_6_21_7
    7981
    80 #ifdef SPLITMIX_32
    81 #define PRNG_NAME_32 splitmix
    82 #define PRNG_STATE_32_T uint32_t
    83 #endif // SPLITMIX32
    84 
    8582#ifdef XORSHIFT_12_25_27
    8683#define PRNG_NAME_64 xorshift_12_25_27
    8784#define PRNG_STATE_64_T uint64_t
    8885#endif // XORSHIFT_12_25_27
     86
     87#ifdef SPLITMIX_64
     88#define PRNG_NAME_64 splitmix64
     89#define PRNG_STATE_64_T uint64_t
     90#endif // SPLITMIX32
     91
     92#ifdef SPLITMIX_32
     93#define PRNG_NAME_32 splitmix32
     94#define PRNG_STATE_32_T uint32_t
     95#endif // SPLITMIX32
    8996
    9097#ifdef KISS_64
     
    124131#ifdef __cforall                                                                                // don't include in C code (invoke.h)
    125132
     133// Splitmix64
     134// https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64
     135// Splitmix64 is not recommended for demanding random number requirements,
     136// but is often used to calculate initial states for other more complex
     137// pseudo-random number generators.                             
     138static inline uint64_t splitmix64( uint64_t & state ) {
     139    state += 0x9e3779b97f4a7c15;             /* increment the state variable */
     140    uint64_t z = state;                      /* copy the state to a working variable */
     141    z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;  /* xor the variable with the variable right bit shifted 30 then multiply by a constant */
     142    z = (z ^ (z >> 27)) * 0x94d049bb133111eb;  /* xor the variable with the variable right bit shifted 27 then multiply by a constant */
     143    return z ^ (z >> 31);                      /* return the variable xored with itself right bit shifted 31 */
     144}
     145
     146static inline void splitmix64_set_seed( uint64_t & state , uint64_t seed ) {
     147    state = seed;
     148    splitmix64( state );                                                                // prime
     149} // splitmix32_set_seed
     150
     151
    126152// Splitmix32
    127153// https://github.com/bryc/code/blob/master/jshash/PRNGs.md#splitmix32
     
    130156// pseudo-random number generators.
    131157// SplitMix32 is a 32 bit variant of Splitmix64
    132 
    133158static inline uint32_t splitmix32( uint32_t & state ) {
    134159    state += 0x9e3779b9;
     
    169194                return m2;
    170195        } // wyhash64
    171 
    172         static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) {
    173                 state = seed;
    174                 wyhash64( state );                                                              // prime
    175         } // wyhash64_set_seed
    176196#endif // __SIZEOF_INT128__
    177197
     
    208228
    209229static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) {
    210     uint64_t state;
    211     wyhash64_set_seed( state, seed );
    212230    // these are done explicitly in this order to attain repeatable seeding.
    213231    // do not call splitmix32 directly in the state init since order of argument evaluation
    214232    // may not be consistent leading to irreproducible seeding
    215     uint64_t seed1 = wyhash64( state );
    216     uint64_t seed2 = wyhash64( state );
    217     uint64_t seed3 = wyhash64( state );
    218     uint64_t seed4 = wyhash64( state );
     233    uint64_t seed1 = splitmix64( seed );
     234    uint64_t seed2 = splitmix64( seed );
     235    uint64_t seed3 = splitmix64( seed );
     236    uint64_t seed4 = splitmix64( seed );
    219237        state = (xoshiro256pp_t){ seed1, seed2, seed3, seed4 };
    220238        xoshiro256pp( state );                                                          // prime
Note: See TracChangeset for help on using the changeset viewer.