[e57de69] | 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
|
---|
[c8238c0] | 12 | // Last Modified On : Thu Dec 1 11:52:08 2022
|
---|
| 13 | // Update Count : 115
|
---|
[e57de69] | 14 | //
|
---|
| 15 |
|
---|
[13c5e19] | 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <stdint.h>
|
---|
| 19 |
|
---|
[dd46fd3] | 20 | #define GLUE2( x, y ) x##y
|
---|
| 21 | #define GLUE( x, y ) GLUE2( x, y )
|
---|
| 22 |
|
---|
[9fce2572] | 23 | // Set default PRNG for architecture size.
|
---|
[d2ad151] | 24 | #ifdef __x86_64__ // 64-bit architecture
|
---|
| 25 | #define LEHMER64
|
---|
[dd46fd3] | 26 | #define XORSHIFT_6_21_7
|
---|
| 27 | //#define XOSHIRO256PP
|
---|
| 28 | //#define XOSHIRO128PP
|
---|
[d2ad151] | 29 | #else // 32-bit architecture
|
---|
[c8238c0] | 30 | #define XORSHIFT_13_7_17
|
---|
[d2ad151] | 31 | #define XORSHIFT_6_21_7
|
---|
[c8238c0] | 32 | //#define XOSHIRO256PP
|
---|
| 33 | //#define XOSHIRO128PP
|
---|
[d2ad151] | 34 | #endif // __x86_64__
|
---|
| 35 |
|
---|
[9fce2572] | 36 | // C/CFA PRNG name and random-state.
|
---|
[dd46fd3] | 37 |
|
---|
[9fce2572] | 38 | #ifdef LEHMER64
|
---|
[dd46fd3] | 39 | #define PRNG_NAME_64 lehmer64
|
---|
| 40 | #define PRNG_STATE_64_T __uint128_t
|
---|
[9fce2572] | 41 | #endif // LEHMER64
|
---|
| 42 |
|
---|
[c8238c0] | 43 | #ifdef XORSHIFT_13_7_17
|
---|
| 44 | #define PRNG_NAME_64 xorshift_13_7_17
|
---|
| 45 | #define PRNG_STATE_64_T uint64_t
|
---|
| 46 | #endif // XORSHIFT_13_7_17
|
---|
| 47 |
|
---|
[9fce2572] | 48 | #ifdef XORSHIFT_6_21_7
|
---|
[dd46fd3] | 49 | #define PRNG_NAME_32 xorshift_6_21_7
|
---|
| 50 | #define PRNG_STATE_32_T uint32_t
|
---|
[9fce2572] | 51 | #endif // XORSHIFT_6_21_7
|
---|
| 52 |
|
---|
[dd46fd3] | 53 | #ifdef XOSHIRO256PP
|
---|
| 54 | #define PRNG_NAME_64 xoshiro256pp
|
---|
| 55 | #define PRNG_STATE_64_T struct GLUE(PRNG_NAME_64,_t)
|
---|
| 56 | PRNG_STATE_64_T { uint64_t s[4]; };
|
---|
| 57 | #endif // XOSHIRO256PP
|
---|
| 58 |
|
---|
| 59 | #ifdef XOSHIRO128PP
|
---|
| 60 | #define PRNG_NAME_32 xoshiro128pp
|
---|
| 61 | #define PRNG_STATE_32_T struct GLUE(PRNG_NAME_32,_t)
|
---|
| 62 | PRNG_STATE_32_T { uint32_t s[4]; };
|
---|
| 63 | #endif // XOSHIRO128PP
|
---|
| 64 |
|
---|
| 65 | #define PRNG_SET_SEED_64 GLUE(PRNG_NAME_64,_set_seed)
|
---|
| 66 | #define PRNG_SET_SEED_32 GLUE(PRNG_NAME_32,_set_seed)
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | // Default PRNG used by runtime.
|
---|
| 70 | #ifdef __x86_64__ // 64-bit architecture
|
---|
| 71 | #define PRNG_NAME PRNG_NAME_64
|
---|
| 72 | #define PRNG_STATE_T PRNG_STATE_64_T
|
---|
| 73 | #else // 32-bit architecture
|
---|
| 74 | #define PRNG_NAME PRNG_NAME_32
|
---|
| 75 | #define PRNG_STATE_T PRNG_STATE_32_T
|
---|
| 76 | #endif // __x86_64__
|
---|
| 77 |
|
---|
| 78 | #define PRNG_SET_SEED GLUE(PRNG_NAME,_set_seed)
|
---|
| 79 |
|
---|
| 80 |
|
---|
[9fce2572] | 81 | #ifdef __cforall // don't include in C code (invoke.h)
|
---|
| 82 |
|
---|
[dd46fd3] | 83 | // https://prng.di.unimi.it/xoshiro128plusplus.c
|
---|
| 84 | //
|
---|
| 85 | // This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid generators. It has excellent speed, a state size
|
---|
| 86 | // (128 bits) that is large enough for mild parallelism, and it passes all tests we are aware of.
|
---|
| 87 | //
|
---|
| 88 | // For generating just single-precision (i.e., 32-bit) floating-point numbers, xoshiro128+ is even faster.
|
---|
| 89 | //
|
---|
| 90 | // The state must be seeded so that it is not everywhere zero.
|
---|
[611f29d] | 91 |
|
---|
[dd46fd3] | 92 | #ifndef XOSHIRO128PP
|
---|
| 93 | struct xoshiro128pp_t { uint32_t s[4]; };
|
---|
| 94 | #endif // ! XOSHIRO128PP
|
---|
| 95 |
|
---|
| 96 | static inline uint32_t xoshiro128pp( xoshiro128pp_t & rs ) with(rs) {
|
---|
| 97 | inline uint32_t rotl( const uint32_t x, int k ) {
|
---|
| 98 | return (x << k) | (x >> (32 - k));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | const uint32_t result = rotl( s[0] + s[3], 7 ) + s[0];
|
---|
| 102 | const uint32_t t = s[1] << 9;
|
---|
| 103 |
|
---|
| 104 | s[2] ^= s[0];
|
---|
| 105 | s[3] ^= s[1];
|
---|
| 106 | s[1] ^= s[2];
|
---|
| 107 | s[0] ^= s[3];
|
---|
| 108 | s[2] ^= t;
|
---|
| 109 | s[3] = rotl( s[3], 11 );
|
---|
| 110 | return result;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) {
|
---|
| 114 | state = (xoshiro128pp_t){ {seed, seed, seed, seed} };
|
---|
| 115 | } // xoshiro128pp_set_seed
|
---|
| 116 |
|
---|
| 117 | // This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state
|
---|
| 118 | // (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of.
|
---|
| 119 | //
|
---|
| 120 | // For generating just floating-point numbers, xoshiro256+ is even faster.
|
---|
| 121 | //
|
---|
| 122 | // The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a
|
---|
| 123 | // splitmix64 generator and use its output to fill s.
|
---|
| 124 |
|
---|
| 125 | #ifndef XOSHIRO256PP
|
---|
| 126 | struct xoshiro256pp_t { uint64_t s[4]; };
|
---|
| 127 | #endif // ! XOSHIRO256PP
|
---|
| 128 |
|
---|
| 129 | static inline uint64_t xoshiro256pp( xoshiro256pp_t & rs ) with(rs) {
|
---|
| 130 | inline uint64_t rotl(const uint64_t x, int k) {
|
---|
| 131 | return (x << k) | (x >> (64 - k));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | const uint64_t result = rotl( s[0] + s[3], 23 ) + s[0];
|
---|
| 135 | const uint64_t t = s[1] << 17;
|
---|
| 136 |
|
---|
| 137 | s[2] ^= s[0];
|
---|
| 138 | s[3] ^= s[1];
|
---|
| 139 | s[1] ^= s[2];
|
---|
| 140 | s[0] ^= s[3];
|
---|
| 141 | s[2] ^= t;
|
---|
| 142 | s[3] = rotl( s[3], 45 );
|
---|
| 143 | return result;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) {
|
---|
| 147 | state = (xoshiro256pp_t){ {seed, seed, seed, seed} };
|
---|
| 148 | } // xoshiro256pp_set_seed
|
---|
| 149 |
|
---|
| 150 | #ifdef __SIZEOF_INT128__
|
---|
| 151 | // Pipelined to allow out-of-order overlap with reduced dependencies. Critically, the current random state is
|
---|
| 152 | // returned (copied), and then compute and store the next random value.
|
---|
| 153 | //--------------------------------------------------
|
---|
[611f29d] | 154 | static inline uint64_t lehmer64( __uint128_t & state ) {
|
---|
| 155 | __uint128_t ret = state;
|
---|
[7812a7b5] | 156 | state *= 0xda942042e4dd58b5;
|
---|
[611f29d] | 157 | return ret >> 64;
|
---|
[dd46fd3] | 158 | } // lehmer64
|
---|
[13c5e19] | 159 |
|
---|
[dd46fd3] | 160 | static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) {
|
---|
| 161 | state = seed;
|
---|
| 162 | } // lehmer64_set_seed
|
---|
| 163 |
|
---|
| 164 | //--------------------------------------------------
|
---|
[611f29d] | 165 | static inline uint64_t wyhash64( uint64_t & state ) {
|
---|
[7812a7b5] | 166 | state += 0x60bee2bee120fc15;
|
---|
| 167 | __uint128_t tmp;
|
---|
| 168 | tmp = (__uint128_t) state * 0xa3b195354a39b70d;
|
---|
| 169 | uint64_t m1 = (tmp >> 64) ^ tmp;
|
---|
| 170 | tmp = (__uint128_t)m1 * 0x1b03738712fad5c9;
|
---|
| 171 | uint64_t m2 = (tmp >> 64) ^ tmp;
|
---|
| 172 | return m2;
|
---|
| 173 | }
|
---|
[dd46fd3] | 174 |
|
---|
| 175 | static inline void wyhash64_set_seed( __uint128_t & state, uint64_t seed ) {
|
---|
| 176 | state = seed;
|
---|
| 177 | } // lehmer64_set_seed
|
---|
| 178 | #endif // __SIZEOF_INT128__
|
---|
[13c5e19] | 179 |
|
---|
| 180 | //--------------------------------------------------
|
---|
[611f29d] | 181 | static inline uint64_t xorshift_13_7_17( uint64_t & state ) {
|
---|
| 182 | uint64_t ret = state;
|
---|
| 183 | state ^= state << 13;
|
---|
| 184 | state ^= state >> 7;
|
---|
| 185 | state ^= state << 17;
|
---|
| 186 | return ret;
|
---|
[13c5e19] | 187 | }
|
---|
| 188 |
|
---|
[dd46fd3] | 189 | static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint32_t seed ) {
|
---|
| 190 | state = seed;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[611f29d] | 193 | //--------------------------------------------------
|
---|
| 194 | static inline uint32_t xorshift_6_21_7( uint32_t & state ) {
|
---|
| 195 | uint32_t ret = state;
|
---|
| 196 | state ^= state << 6;
|
---|
| 197 | state ^= state >> 21;
|
---|
| 198 | state ^= state << 7;
|
---|
| 199 | return ret;
|
---|
| 200 | } // xorshift_6_21_7
|
---|
| 201 |
|
---|
[dd46fd3] | 202 | static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) {
|
---|
| 203 | state = seed;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[13c5e19] | 206 | //--------------------------------------------------
|
---|
| 207 | typedef struct {
|
---|
[9fce2572] | 208 | uint32_t a, b, c, d;
|
---|
| 209 | uint32_t counter;
|
---|
[611f29d] | 210 | } xorwow__state_t;
|
---|
[13c5e19] | 211 |
|
---|
[e57de69] | 212 | // The state array must be initialized to not be all zero in the first four words.
|
---|
[611f29d] | 213 | static inline uint32_t xorwow( xorwow__state_t & state ) {
|
---|
[e57de69] | 214 | // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs".
|
---|
[611f29d] | 215 | uint32_t ret = state.a + state.counter;
|
---|
[13c5e19] | 216 | uint32_t t = state.d;
|
---|
| 217 |
|
---|
| 218 | uint32_t const s = state.a;
|
---|
| 219 | state.d = state.c;
|
---|
| 220 | state.c = state.b;
|
---|
| 221 | state.b = s;
|
---|
| 222 |
|
---|
| 223 | t ^= t >> 2;
|
---|
| 224 | t ^= t << 1;
|
---|
| 225 | t ^= s ^ (s << 4);
|
---|
| 226 | state.a = t;
|
---|
| 227 |
|
---|
| 228 | state.counter += 362437;
|
---|
[611f29d] | 229 | return ret;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[dd46fd3] | 232 | // Used in __tls_rand_fwd
|
---|
[611f29d] | 233 | //--------------------------------------------------
|
---|
| 234 | #define M (1_l64u << 48_l64u)
|
---|
| 235 | #define A (25214903917_l64u)
|
---|
| 236 | #define AI (18446708753438544741_l64u)
|
---|
| 237 | #define C (11_l64u)
|
---|
| 238 | #define D (16_l64u)
|
---|
| 239 |
|
---|
[e57de69] | 240 | // Bi-directional LCG random-number generator
|
---|
[611f29d] | 241 | static inline uint32_t LCGBI_fwd( uint64_t & state ) {
|
---|
| 242 | state = (A * state + C) & (M - 1);
|
---|
| 243 | return state >> D;
|
---|
[4177592f] | 244 | }
|
---|
[611f29d] | 245 |
|
---|
| 246 | static inline uint32_t LCGBI_bck( uint64_t & state ) {
|
---|
| 247 | unsigned int r = state >> D;
|
---|
| 248 | state = AI * (state - C) & (M - 1);
|
---|
| 249 | return r;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | #undef M
|
---|
| 253 | #undef A
|
---|
| 254 | #undef AI
|
---|
| 255 | #undef C
|
---|
| 256 | #undef D
|
---|
[9fce2572] | 257 |
|
---|
| 258 | #endif // __cforall
|
---|