Ignore:
File:
1 edited

Legend:

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

    re57de69 r7812a7b5  
    1 //
    2 // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
    3 //
    4 // The contents of this file are covered under the licence agreement in the
    5 // file "LICENCE" distributed with Cforall.
    6 //
    7 // random.hfa --
    8 //
    9 // Author           : Peter A. Buhr
    10 // Created On       : Fri Jan 14 07:18:11 2022
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jan 14 07:18:58 2022
    13 // Update Count     : 1
    14 //
    15 
    161#pragma once
    172
    183#include <stdint.h>
    194
    20 // Pipelined to allow out-of-order overlap with reduced dependencies. Critically, the current random state is returned
    21 // (copied), and then compute and store the next random value.
    22 
     5//--------------------------------------------------
    236#if defined(__SIZEOF_INT128__)
    24 //--------------------------------------------------
    25         static inline uint64_t lehmer64( __uint128_t & state ) {
    26                 __uint128_t ret = state;
     7        typedef __uint128_t __lehmer64_state_t;
     8        static inline uint64_t __lehmer64( __lehmer64_state_t & state ) {
    279                state *= 0xda942042e4dd58b5;
    28                 return ret >> 64;
     10                return state >> 64;
    2911        }
    3012
    3113//--------------------------------------------------
    32         static inline uint64_t wyhash64( uint64_t & state ) {
     14        typedef uint64_t __wyhash64_state_t;
     15        static inline uint64_t __wyhash64( __wyhash64_state_t & state ) {
    3316                state += 0x60bee2bee120fc15;
    3417                __uint128_t tmp;
     
    4225
    4326//--------------------------------------------------
    44 static inline uint64_t xorshift_13_7_17( uint64_t & state ) {
    45         uint64_t ret = state;
    46         state ^= state << 13;
    47         state ^= state >> 7;
    48         state ^= state << 17;
    49         return ret;
     27typedef uint64_t __xorshift64_state_t;
     28static 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;
    5034}
    51 
    52 //--------------------------------------------------
    53 static inline uint32_t xorshift_6_21_7( uint32_t & state ) {
    54         uint32_t ret = state;
    55         state ^= state << 6;
    56         state ^= state >> 21;
    57         state ^= state << 7;
    58         return ret;
    59 } // xorshift_6_21_7
    6035
    6136//--------------------------------------------------
     
    6338  uint32_t a, b, c, d;
    6439  uint32_t counter;
    65 } xorwow__state_t;
     40} __xorwow__state_t;
    6641
    67 // The state array must be initialized to not be all zero in the first four words.
    68 static inline uint32_t xorwow( xorwow__state_t & state ) {
    69         // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs".
    70         uint32_t ret = state.a + state.counter;
     42/* The state array must be initialized to not be all zero in the first four words */
     43static inline uint32_t __xorwow( __xorwow__state_t & state ) {
     44        /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */
    7145        uint32_t t = state.d;
    7246
     
    8256
    8357        state.counter += 362437;
    84         return ret;
     58        return t + state.counter;
    8559}
    86 
    87 //--------------------------------------------------
    88 static inline uint32_t LCG( uint32_t & state ) {                // linear congruential generator
    89         uint32_t ret = state;
    90         state = 36969 * (state & 65535) + (state >> 16);        // 36969 is NOT prime! No not change it!
    91         return ret;
    92 } // LCG
    93 
    94 //--------------------------------------------------
    95 #define M  (1_l64u << 48_l64u)
    96 #define A  (25214903917_l64u)
    97 #define AI (18446708753438544741_l64u)
    98 #define C  (11_l64u)
    99 #define D  (16_l64u)
    100 
    101 // Bi-directional LCG random-number generator
    102 static inline uint32_t LCGBI_fwd( uint64_t & state ) {
    103         state = (A * state + C) & (M - 1);
    104         return state >> D;
    105 }
    106 
    107 static inline uint32_t LCGBI_bck( uint64_t & state ) {
    108         unsigned int r = state >> D;
    109         state = AI * (state - C) & (M - 1);
    110         return r;
    111 }
    112 
    113 #undef M
    114 #undef A
    115 #undef AI
    116 #undef C
    117 #undef D
Note: See TracChangeset for help on using the changeset viewer.