Changes in libcfa/src/bits/random.hfa [e57de69:7812a7b5]
- File:
-
- 1 edited
-
libcfa/src/bits/random.hfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/random.hfa
re57de69 r7812a7b5 1 //2 // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo3 //4 // The contents of this file are covered under the licence agreement in the5 // file "LICENCE" distributed with Cforall.6 //7 // random.hfa --8 //9 // Author : Peter A. Buhr10 // Created On : Fri Jan 14 07:18:11 202211 // Last Modified By : Peter A. Buhr12 // Last Modified On : Fri Jan 14 07:18:58 202213 // Update Count : 114 //15 16 1 #pragma once 17 2 18 3 #include <stdint.h> 19 4 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 //-------------------------------------------------- 23 6 #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 ) { 27 9 state *= 0xda942042e4dd58b5; 28 return ret>> 64;10 return state >> 64; 29 11 } 30 12 31 13 //-------------------------------------------------- 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 ) { 33 16 state += 0x60bee2bee120fc15; 34 17 __uint128_t tmp; … … 42 25 43 26 //-------------------------------------------------- 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; 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; 50 34 } 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_760 35 61 36 //-------------------------------------------------- … … 63 38 uint32_t a, b, c, d; 64 39 uint32_t counter; 65 } xorwow__state_t;40 } __xorwow__state_t; 66 41 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 */ 43 static inline uint32_t __xorwow( __xorwow__state_t & state ) { 44 /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */ 71 45 uint32_t t = state.d; 72 46 … … 82 56 83 57 state.counter += 362437; 84 return ret;58 return t + state.counter; 85 59 } 86 87 //--------------------------------------------------88 static inline uint32_t LCG( uint32_t & state ) { // linear congruential generator89 uint32_t ret = state;90 state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime! No not change it!91 return ret;92 } // LCG93 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 generator102 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 M114 #undef A115 #undef AI116 #undef C117 #undef D
Note:
See TracChangeset
for help on using the changeset viewer.