Changeset 12c1eef for libcfa/src/bits/random.hfa
- Timestamp:
- Jan 19, 2022, 2:36:56 PM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- 97c215f
- Parents:
- 5235d49 (diff), 6a33e40 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
libcfa/src/bits/random.hfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/bits/random.hfa
r5235d49 r12c1eef 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 1 16 #pragma once 2 17 3 18 #include <stdint.h> 4 19 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 23 #if defined(__SIZEOF_INT128__) 5 24 //-------------------------------------------------- 6 #if defined(__SIZEOF_INT128__) 7 typedef __uint128_t __lehmer64_state_t; 8 static inline uint64_t __lehmer64( __lehmer64_state_t & state ) { 25 static inline uint64_t lehmer64( __uint128_t & state ) { 26 __uint128_t ret = state; 9 27 state *= 0xda942042e4dd58b5; 10 return state>> 64;28 return ret >> 64; 11 29 } 12 30 13 31 //-------------------------------------------------- 14 typedef uint64_t __wyhash64_state_t; 15 static inline uint64_t __wyhash64( __wyhash64_state_t & state ) { 32 static inline uint64_t wyhash64( uint64_t & state ) { 16 33 state += 0x60bee2bee120fc15; 17 34 __uint128_t tmp; … … 25 42 26 43 //-------------------------------------------------- 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; 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; 34 50 } 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 35 60 36 61 //-------------------------------------------------- … … 38 63 uint32_t a, b, c, d; 39 64 uint32_t counter; 40 } __xorwow__state_t;65 } xorwow__state_t; 41 66 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" */ 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; 45 71 uint32_t t = state.d; 46 72 … … 56 82 57 83 state.counter += 362437; 58 return t + state.counter;84 return ret; 59 85 } 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.