Ignore:
Timestamp:
Jan 13, 2022, 9:30:38 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
919a6b2
Parents:
f520c4c
Message:

consolidate random-number generators

File:
1 edited

Legend:

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

    rf520c4c r611f29d  
    33#include <stdint.h>
    44
     5// Pipelined to allow out-of-order overlap with reduced dependencies. Critically, return the current value, and compute
     6// and store the next value.
     7
    58//--------------------------------------------------
    69#if defined(__SIZEOF_INT128__)
    7         typedef __uint128_t __lehmer64_state_t;
    8         static inline uint64_t __lehmer64( __lehmer64_state_t & state ) {
     10        static inline uint64_t lehmer64( __uint128_t & state ) {
     11                __uint128_t ret = state;
    912                state *= 0xda942042e4dd58b5;
    10                 return state >> 64;
     13                return ret >> 64;
    1114        }
    1215
    1316//--------------------------------------------------
    14         typedef uint64_t __wyhash64_state_t;
    15         static inline uint64_t __wyhash64( __wyhash64_state_t & state ) {
     17        static inline uint64_t wyhash64( uint64_t & state ) {
    1618                state += 0x60bee2bee120fc15;
    1719                __uint128_t tmp;
     
    2527
    2628//--------------------------------------------------
    27 typedef uint64_t __xorshift64_state_t;
    28 static inline uint64_t __xorshift64( __xorshift64_state_t & state ) {
    29         uint64_t x = state;
    30         x ^= x << 13;
    31         x ^= x >> 7;
    32         x ^= x << 17;
    33         return state = x;
     29static inline uint64_t xorshift_13_7_17( uint64_t & state ) {
     30        uint64_t ret = state;
     31        state ^= state << 13;
     32        state ^= state >> 7;
     33        state ^= state << 17;
     34        return ret;
    3435}
     36
     37//--------------------------------------------------
     38static inline uint32_t xorshift_6_21_7( uint32_t & state ) {
     39        uint32_t ret = state;
     40        state ^= state << 6;
     41        state ^= state >> 21;
     42        state ^= state << 7;
     43        return ret;
     44} // xorshift_6_21_7
    3545
    3646//--------------------------------------------------
     
    3848  uint32_t a, b, c, d;
    3949  uint32_t counter;
    40 } __xorwow__state_t;
     50} xorwow__state_t;
    4151
    4252/* The state array must be initialized to not be all zero in the first four words */
    43 static inline uint32_t __xorwow( __xorwow__state_t & state ) {
     53static inline uint32_t xorwow( xorwow__state_t & state ) {
    4454        /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */
     55        uint32_t ret = state.a + state.counter;
    4556        uint32_t t = state.d;
    4657
     
    5667
    5768        state.counter += 362437;
    58         return t + state.counter;
     69        return ret;
    5970}
     71
     72//--------------------------------------------------
     73static inline uint32_t LCG( uint32_t & state ) {                // linear congruential generator
     74        uint32_t ret = state;
     75        state = 36969 * (state & 65535) + (state >> 16);        // 36969 is NOT prime! No not change it!
     76        return ret;
     77} // LCG
     78
     79//--------------------------------------------------
     80#define M  (1_l64u << 48_l64u)
     81#define A  (25214903917_l64u)
     82#define AI (18446708753438544741_l64u)
     83#define C  (11_l64u)
     84#define D  (16_l64u)
     85
     86static inline uint32_t LCGBI_fwd( uint64_t & state ) {
     87        state = (A * state + C) & (M - 1);
     88        return state >> D;
     89}
     90
     91static inline uint32_t LCGBI_bck( uint64_t & state ) {
     92        unsigned int r = state >> D;
     93        state = AI * (state - C) & (M - 1);
     94        return r;
     95}
     96
     97#undef M
     98#undef A
     99#undef AI
     100#undef C
     101#undef D
Note: See TracChangeset for help on using the changeset viewer.