| [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
|
|---|
| [c2dfa56a] | 12 | // Last Modified On : Mon Mar 20 10:01:40 2023
|
|---|
| 13 | // Update Count : 180
|
|---|
| [e57de69] | 14 | //
|
|---|
| 15 |
|
|---|
| [13c5e19] | 16 | #pragma once
|
|---|
| 17 |
|
|---|
| [d9585291] | 18 | #include <stdint.h> // uintXX_t
|
|---|
| [13c5e19] | 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
|
|---|
| [261e107] | 25 | // 64-bit generators
|
|---|
| [b797d978] | 26 | //#define LEHMER64
|
|---|
| [261e107] | 27 | //#define XORSHIFT_12_25_27
|
|---|
| [b797d978] | 28 | #define XOSHIRO256PP
|
|---|
| [261e107] | 29 | //#define KISS_64
|
|---|
| 30 |
|
|---|
| 31 | // 32-bit generators
|
|---|
| [b797d978] | 32 | //#define XORSHIFT_6_21_7
|
|---|
| 33 | #define XOSHIRO128PP
|
|---|
| [d2ad151] | 34 | #else // 32-bit architecture
|
|---|
| [261e107] | 35 | // 64-bit generators
|
|---|
| [b797d978] | 36 | //#define XORSHIFT_13_7_17
|
|---|
| 37 | #define XOSHIRO256PP
|
|---|
| [261e107] | 38 |
|
|---|
| 39 | // 32-bit generators
|
|---|
| [b797d978] | 40 | //#define XORSHIFT_6_21_7
|
|---|
| 41 | #define XOSHIRO128PP
|
|---|
| [d2ad151] | 42 | #endif // __x86_64__
|
|---|
| 43 |
|
|---|
| [4020f09] | 44 | // Define C/CFA PRNG name and random-state.
|
|---|
| 45 |
|
|---|
| [261e107] | 46 | #ifdef XOSHIRO256PP
|
|---|
| 47 | #define PRNG_NAME_64 xoshiro256pp
|
|---|
| 48 | #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t)
|
|---|
| [c2dfa56a] | 49 | typedef struct { uint64_t s0, s1, s2, s3; } PRNG_STATE_64_T;
|
|---|
| [261e107] | 50 | #endif // XOSHIRO256PP
|
|---|
| 51 |
|
|---|
| 52 | #ifdef XOSHIRO128PP
|
|---|
| 53 | #define PRNG_NAME_32 xoshiro128pp
|
|---|
| 54 | #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t)
|
|---|
| [c2dfa56a] | 55 | typedef struct { uint32_t s0, s1, s2, s3; } PRNG_STATE_32_T;
|
|---|
| [261e107] | 56 | #endif // XOSHIRO128PP
|
|---|
| 57 |
|
|---|
| [9fce2572] | 58 | #ifdef LEHMER64
|
|---|
| [dd46fd3] | 59 | #define PRNG_NAME_64 lehmer64
|
|---|
| 60 | #define PRNG_STATE_64_T __uint128_t
|
|---|
| [9fce2572] | 61 | #endif // LEHMER64
|
|---|
| 62 |
|
|---|
| [261e107] | 63 | #ifdef WYHASH64
|
|---|
| 64 | #define PRNG_NAME_64 wyhash64
|
|---|
| 65 | #define PRNG_STATE_64_T uint64_t
|
|---|
| 66 | #endif // LEHMER64
|
|---|
| 67 |
|
|---|
| [c8238c0] | 68 | #ifdef XORSHIFT_13_7_17
|
|---|
| 69 | #define PRNG_NAME_64 xorshift_13_7_17
|
|---|
| 70 | #define PRNG_STATE_64_T uint64_t
|
|---|
| 71 | #endif // XORSHIFT_13_7_17
|
|---|
| 72 |
|
|---|
| [9fce2572] | 73 | #ifdef XORSHIFT_6_21_7
|
|---|
| [dd46fd3] | 74 | #define PRNG_NAME_32 xorshift_6_21_7
|
|---|
| 75 | #define PRNG_STATE_32_T uint32_t
|
|---|
| [9fce2572] | 76 | #endif // XORSHIFT_6_21_7
|
|---|
| 77 |
|
|---|
| [261e107] | 78 | #ifdef XORSHIFT_12_25_27
|
|---|
| 79 | #define PRNG_NAME_64 xorshift_12_25_27
|
|---|
| 80 | #define PRNG_STATE_64_T uint64_t
|
|---|
| 81 | #endif // XORSHIFT_12_25_27
|
|---|
| [dd46fd3] | 82 |
|
|---|
| [261e107] | 83 | #ifdef KISS_64
|
|---|
| 84 | #define PRNG_NAME_64 kiss_64
|
|---|
| 85 | #define PRNG_STATE_64_T GLUE(PRNG_NAME_64,_t)
|
|---|
| [c2dfa56a] | 86 | typedef struct { uint64_t z, w, jsr, jcong; } PRNG_STATE_64_T;
|
|---|
| [261e107] | 87 | #endif // KISS_^64
|
|---|
| [4020f09] | 88 |
|
|---|
| 89 | #ifdef XORWOW
|
|---|
| 90 | #define PRNG_NAME_32 xorwow
|
|---|
| 91 | #define PRNG_STATE_32_T GLUE(PRNG_NAME_32,_t)
|
|---|
| [c2dfa56a] | 92 | typedef struct { uint32_t a, b, c, d, counter; } PRNG_STATE_32_T;
|
|---|
| [dd46fd3] | 93 | #endif // XOSHIRO128PP
|
|---|
| 94 |
|
|---|
| 95 | #define PRNG_SET_SEED_64 GLUE(PRNG_NAME_64,_set_seed)
|
|---|
| 96 | #define PRNG_SET_SEED_32 GLUE(PRNG_NAME_32,_set_seed)
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | // Default PRNG used by runtime.
|
|---|
| 100 | #ifdef __x86_64__ // 64-bit architecture
|
|---|
| 101 | #define PRNG_NAME PRNG_NAME_64
|
|---|
| 102 | #define PRNG_STATE_T PRNG_STATE_64_T
|
|---|
| 103 | #else // 32-bit architecture
|
|---|
| 104 | #define PRNG_NAME PRNG_NAME_32
|
|---|
| 105 | #define PRNG_STATE_T PRNG_STATE_32_T
|
|---|
| 106 | #endif // __x86_64__
|
|---|
| 107 |
|
|---|
| 108 | #define PRNG_SET_SEED GLUE(PRNG_NAME,_set_seed)
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| [261e107] | 111 | // ALL PRNG ALGORITHMS ARE OPTIMIZED SO THAT THE PRNG LOGIC CAN HAPPEN IN PARALLEL WITH THE USE OF THE RESULT.
|
|---|
| [b797d978] | 112 | // Specifically, the current random state is copied for returning, before computing the next value. As a consequence,
|
|---|
| 113 | // the set_seed routine primes the PRNG by calling it with the state so the seed is not return as the first random
|
|---|
| 114 | // value.
|
|---|
| 115 |
|
|---|
| [261e107] | 116 |
|
|---|
| [9fce2572] | 117 | #ifdef __cforall // don't include in C code (invoke.h)
|
|---|
| 118 |
|
|---|
| [4020f09] | 119 | // https://prng.di.unimi.it/xoshiro256starstar.c
|
|---|
| [dd46fd3] | 120 | //
|
|---|
| 121 | // This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state
|
|---|
| 122 | // (256 bits) that is large enough for any parallel application, and it passes all tests we are aware of.
|
|---|
| 123 | //
|
|---|
| 124 | // For generating just floating-point numbers, xoshiro256+ is even faster.
|
|---|
| 125 | //
|
|---|
| 126 | // The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a
|
|---|
| 127 | // splitmix64 generator and use its output to fill s.
|
|---|
| 128 |
|
|---|
| 129 | #ifndef XOSHIRO256PP
|
|---|
| [c2dfa56a] | 130 | typedef struct { uint64_t s0, s1, s2, s3; } xoshiro256pp_t;
|
|---|
| [dd46fd3] | 131 | #endif // ! XOSHIRO256PP
|
|---|
| 132 |
|
|---|
| 133 | static inline uint64_t xoshiro256pp( xoshiro256pp_t & rs ) with(rs) {
|
|---|
| [b797d978] | 134 | inline uint64_t rotl( const uint64_t x, int k ) {
|
|---|
| [dd46fd3] | 135 | return (x << k) | (x >> (64 - k));
|
|---|
| [4020f09] | 136 | } // rotl
|
|---|
| [dd46fd3] | 137 |
|
|---|
| [b797d978] | 138 | const uint64_t result = rotl( s0 + s3, 23 ) + s0;
|
|---|
| 139 | const uint64_t t = s1 << 17;
|
|---|
| [dd46fd3] | 140 |
|
|---|
| [b797d978] | 141 | s2 ^= s0;
|
|---|
| 142 | s3 ^= s1;
|
|---|
| 143 | s1 ^= s2;
|
|---|
| 144 | s0 ^= s3;
|
|---|
| 145 | s2 ^= t;
|
|---|
| 146 | s3 = rotl( s3, 45 );
|
|---|
| [dd46fd3] | 147 | return result;
|
|---|
| [4020f09] | 148 | } // xoshiro256pp
|
|---|
| [dd46fd3] | 149 |
|
|---|
| [b797d978] | 150 | static inline void xoshiro256pp_set_seed( xoshiro256pp_t & state, uint64_t seed ) {
|
|---|
| 151 | state = (xoshiro256pp_t){ seed, seed, seed, seed };
|
|---|
| [c2dfa56a] | 152 | xoshiro256pp( state ); // prime
|
|---|
| [dd46fd3] | 153 | } // xoshiro256pp_set_seed
|
|---|
| 154 |
|
|---|
| [4020f09] | 155 | // https://prng.di.unimi.it/xoshiro128plusplus.c
|
|---|
| 156 | //
|
|---|
| 157 | // This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid generators. It has excellent speed, a state size
|
|---|
| 158 | // (128 bits) that is large enough for mild parallelism, and it passes all tests we are aware of.
|
|---|
| 159 | //
|
|---|
| 160 | // For generating just single-precision (i.e., 32-bit) floating-point numbers, xoshiro128+ is even faster.
|
|---|
| 161 | //
|
|---|
| 162 | // The state must be seeded so that it is not everywhere zero.
|
|---|
| 163 |
|
|---|
| 164 | #ifndef XOSHIRO128PP
|
|---|
| [c2dfa56a] | 165 | typedef struct { uint32_t s0, s1, s2, s3; } xoshiro128pp_t;
|
|---|
| [4020f09] | 166 | #endif // ! XOSHIRO128PP
|
|---|
| 167 |
|
|---|
| 168 | static inline uint32_t xoshiro128pp( xoshiro128pp_t & rs ) with(rs) {
|
|---|
| 169 | inline uint32_t rotl( const uint32_t x, int k ) {
|
|---|
| 170 | return (x << k) | (x >> (32 - k));
|
|---|
| 171 | } // rotl
|
|---|
| 172 |
|
|---|
| [b797d978] | 173 | const uint32_t result = rotl( s0 + s3, 7 ) + s0;
|
|---|
| 174 | const uint32_t t = s1 << 9;
|
|---|
| [4020f09] | 175 |
|
|---|
| [b797d978] | 176 | s2 ^= s0;
|
|---|
| 177 | s3 ^= s1;
|
|---|
| 178 | s1 ^= s2;
|
|---|
| 179 | s0 ^= s3;
|
|---|
| 180 | s2 ^= t;
|
|---|
| 181 | s3 = rotl( s3, 11 );
|
|---|
| [4020f09] | 182 | return result;
|
|---|
| 183 | } // xoshiro128pp
|
|---|
| 184 |
|
|---|
| 185 | static inline void xoshiro128pp_set_seed( xoshiro128pp_t & state, uint32_t seed ) {
|
|---|
| [b797d978] | 186 | state = (xoshiro128pp_t){ seed, seed, seed, seed };
|
|---|
| [261e107] | 187 | xoshiro128pp( state ); // prime
|
|---|
| [4020f09] | 188 | } // xoshiro128pp_set_seed
|
|---|
| 189 |
|
|---|
| [dd46fd3] | 190 | #ifdef __SIZEOF_INT128__
|
|---|
| 191 | //--------------------------------------------------
|
|---|
| [611f29d] | 192 | static inline uint64_t lehmer64( __uint128_t & state ) {
|
|---|
| 193 | __uint128_t ret = state;
|
|---|
| [d9585291] | 194 | state *= 0x_da94_2042_e4dd_58b5;
|
|---|
| [611f29d] | 195 | return ret >> 64;
|
|---|
| [dd46fd3] | 196 | } // lehmer64
|
|---|
| [13c5e19] | 197 |
|
|---|
| [dd46fd3] | 198 | static inline void lehmer64_set_seed( __uint128_t & state, uint64_t seed ) {
|
|---|
| [d9585291] | 199 | // The seed needs to be coprime with the 2^64 modulus to get the largest period, so no factors of 2 in the seed.
|
|---|
| [dd46fd3] | 200 | state = seed;
|
|---|
| [b797d978] | 201 | lehmer64( state ); // prime
|
|---|
| [dd46fd3] | 202 | } // lehmer64_set_seed
|
|---|
| 203 |
|
|---|
| 204 | //--------------------------------------------------
|
|---|
| [611f29d] | 205 | static inline uint64_t wyhash64( uint64_t & state ) {
|
|---|
| [261e107] | 206 | uint64_t ret = state;
|
|---|
| 207 | state += 0x_60be_e2be_e120_fc15;
|
|---|
| [7812a7b5] | 208 | __uint128_t tmp;
|
|---|
| [261e107] | 209 | tmp = (__uint128_t) ret * 0x_a3b1_9535_4a39_b70d;
|
|---|
| [7812a7b5] | 210 | uint64_t m1 = (tmp >> 64) ^ tmp;
|
|---|
| [261e107] | 211 | tmp = (__uint128_t)m1 * 0x_1b03_7387_12fa_d5c9;
|
|---|
| [7812a7b5] | 212 | uint64_t m2 = (tmp >> 64) ^ tmp;
|
|---|
| 213 | return m2;
|
|---|
| [261e107] | 214 | } // wyhash64
|
|---|
| [dd46fd3] | 215 |
|
|---|
| [261e107] | 216 | static inline void wyhash64_set_seed( uint64_t & state, uint64_t seed ) {
|
|---|
| [dd46fd3] | 217 | state = seed;
|
|---|
| [261e107] | 218 | wyhash64( state ); // prime
|
|---|
| 219 | } // wyhash64_set_seed
|
|---|
| [dd46fd3] | 220 | #endif // __SIZEOF_INT128__
|
|---|
| [13c5e19] | 221 |
|
|---|
| 222 | //--------------------------------------------------
|
|---|
| [611f29d] | 223 | static inline uint64_t xorshift_13_7_17( uint64_t & state ) {
|
|---|
| 224 | uint64_t ret = state;
|
|---|
| 225 | state ^= state << 13;
|
|---|
| 226 | state ^= state >> 7;
|
|---|
| 227 | state ^= state << 17;
|
|---|
| 228 | return ret;
|
|---|
| [4020f09] | 229 | } // xorshift_13_7_17
|
|---|
| [13c5e19] | 230 |
|
|---|
| [261e107] | 231 | static inline void xorshift_13_7_17_set_seed( uint64_t & state, uint64_t seed ) {
|
|---|
| [dd46fd3] | 232 | state = seed;
|
|---|
| [261e107] | 233 | xorshift_13_7_17( state ); // prime
|
|---|
| [4020f09] | 234 | } // xorshift_13_7_17_set_seed
|
|---|
| [dd46fd3] | 235 |
|
|---|
| [611f29d] | 236 | //--------------------------------------------------
|
|---|
| [4020f09] | 237 | // Marsaglia shift-XOR PRNG with thread-local state
|
|---|
| 238 | // Period is 4G-1
|
|---|
| 239 | // 0 is absorbing and must be avoided
|
|---|
| 240 | // Low-order bits are not particularly random
|
|---|
| [611f29d] | 241 | static inline uint32_t xorshift_6_21_7( uint32_t & state ) {
|
|---|
| 242 | uint32_t ret = state;
|
|---|
| 243 | state ^= state << 6;
|
|---|
| 244 | state ^= state >> 21;
|
|---|
| 245 | state ^= state << 7;
|
|---|
| 246 | return ret;
|
|---|
| 247 | } // xorshift_6_21_7
|
|---|
| 248 |
|
|---|
| [dd46fd3] | 249 | static inline void xorshift_6_21_7_set_seed( uint32_t & state, uint32_t seed ) {
|
|---|
| 250 | state = seed;
|
|---|
| [261e107] | 251 | xorshift_6_21_7( state ); // prime
|
|---|
| [4020f09] | 252 | } // xorshift_6_21_7_set_seed
|
|---|
| [dd46fd3] | 253 |
|
|---|
| [261e107] | 254 | //--------------------------------------------------
|
|---|
| 255 | // The state must be seeded with a nonzero value.
|
|---|
| 256 | static inline uint64_t xorshift_12_25_27( uint64_t & state ) {
|
|---|
| 257 | uint64_t ret = state;
|
|---|
| 258 | state ^= state >> 12;
|
|---|
| 259 | state ^= state << 25;
|
|---|
| 260 | state ^= state >> 27;
|
|---|
| 261 | return ret * 0x_2545_F491_4F6C_DD1D;
|
|---|
| 262 | } // xorshift_12_25_27
|
|---|
| 263 |
|
|---|
| 264 | static inline void xorshift_12_25_27_set_seed( uint64_t & state, uint64_t seed ) {
|
|---|
| 265 | state = seed;
|
|---|
| 266 | xorshift_12_25_27( state ); // prime
|
|---|
| 267 | } // xorshift_12_25_27_set_seed
|
|---|
| 268 |
|
|---|
| 269 | //--------------------------------------------------
|
|---|
| 270 | // The state must be seeded with a nonzero value.
|
|---|
| 271 | #ifndef KISS_64
|
|---|
| [c2dfa56a] | 272 | typedef struct { uint64_t z, w, jsr, jcong; } kiss_64_t;
|
|---|
| [261e107] | 273 | #endif // ! KISS_64
|
|---|
| 274 |
|
|---|
| [b797d978] | 275 | static inline uint64_t kiss_64( kiss_64_t & rs ) with(rs) {
|
|---|
| 276 | kiss_64_t ret = rs;
|
|---|
| [261e107] | 277 | z = 36969 * (z & 65535) + (z >> 16);
|
|---|
| 278 | w = 18000 * (w & 65535) + (w >> 16);
|
|---|
| 279 | jsr ^= (jsr << 13);
|
|---|
| [b797d978] | 280 | jsr ^= (jsr >> 17);
|
|---|
| [261e107] | 281 | jsr ^= (jsr << 5);
|
|---|
| 282 | jcong = 69069 * jcong + 1234567;
|
|---|
| [3ff64cb] | 283 | return (((ret.z << 16) + ret.w) ^ ret.jcong) + ret.jsr;
|
|---|
| [261e107] | 284 | } // kiss_64
|
|---|
| 285 |
|
|---|
| [b797d978] | 286 | static inline void kiss_64_set_seed( kiss_64_t & rs, uint64_t seed ) with(rs) {
|
|---|
| [261e107] | 287 | z = 1; w = 1; jsr = 4; jcong = seed;
|
|---|
| [b797d978] | 288 | kiss_64( rs ); // prime
|
|---|
| [261e107] | 289 | } // kiss_64_set_seed
|
|---|
| 290 |
|
|---|
| [13c5e19] | 291 | //--------------------------------------------------
|
|---|
| [4020f09] | 292 | // The state array must be initialized to non-zero in the first four words.
|
|---|
| 293 | #ifndef XORWOW
|
|---|
| [c2dfa56a] | 294 | typedef struct { uint32_t a, b, c, d, counter; } xorwow_t;
|
|---|
| [4020f09] | 295 | #endif // ! XORWOW
|
|---|
| [13c5e19] | 296 |
|
|---|
| [b797d978] | 297 | static inline uint32_t xorwow( xorwow_t & rs ) with(rs) {
|
|---|
| [e57de69] | 298 | // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs".
|
|---|
| [261e107] | 299 | uint32_t ret = a + counter;
|
|---|
| 300 | uint32_t t = d;
|
|---|
| [13c5e19] | 301 |
|
|---|
| [261e107] | 302 | uint32_t const s = a;
|
|---|
| 303 | d = c;
|
|---|
| 304 | c = b;
|
|---|
| 305 | b = s;
|
|---|
| [13c5e19] | 306 |
|
|---|
| 307 | t ^= t >> 2;
|
|---|
| 308 | t ^= t << 1;
|
|---|
| 309 | t ^= s ^ (s << 4);
|
|---|
| [261e107] | 310 | a = t;
|
|---|
| 311 | counter += 362437;
|
|---|
| [611f29d] | 312 | return ret;
|
|---|
| [4020f09] | 313 | } // xorwow
|
|---|
| 314 |
|
|---|
| [b797d978] | 315 | static inline void xorwow_set_seed( xorwow_t & rs, uint32_t seed ) {
|
|---|
| 316 | rs = (xorwow_t){ seed, seed, seed, seed, 0 };
|
|---|
| 317 | xorwow( rs ); // prime
|
|---|
| [4020f09] | 318 | } // xorwow_set_seed
|
|---|
| [611f29d] | 319 |
|
|---|
| 320 | //--------------------------------------------------
|
|---|
| [4020f09] | 321 | // Used in __tls_rand_fwd
|
|---|
| [611f29d] | 322 | #define M (1_l64u << 48_l64u)
|
|---|
| 323 | #define A (25214903917_l64u)
|
|---|
| 324 | #define AI (18446708753438544741_l64u)
|
|---|
| 325 | #define C (11_l64u)
|
|---|
| 326 | #define D (16_l64u)
|
|---|
| 327 |
|
|---|
| [e57de69] | 328 | // Bi-directional LCG random-number generator
|
|---|
| [b797d978] | 329 | static inline uint32_t LCGBI_fwd( uint64_t & rs ) {
|
|---|
| 330 | rs = (A * rs + C) & (M - 1);
|
|---|
| 331 | return rs >> D;
|
|---|
| [4020f09] | 332 | } // LCGBI_fwd
|
|---|
| [611f29d] | 333 |
|
|---|
| [b797d978] | 334 | static inline uint32_t LCGBI_bck( uint64_t & rs ) {
|
|---|
| 335 | unsigned int r = rs >> D;
|
|---|
| 336 | rs = AI * (rs - C) & (M - 1);
|
|---|
| [611f29d] | 337 | return r;
|
|---|
| [4020f09] | 338 | } // LCGBI_bck
|
|---|
| [611f29d] | 339 |
|
|---|
| 340 | #undef M
|
|---|
| 341 | #undef A
|
|---|
| 342 | #undef AI
|
|---|
| 343 | #undef C
|
|---|
| 344 | #undef D
|
|---|
| [9fce2572] | 345 |
|
|---|
| 346 | #endif // __cforall
|
|---|