| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| |
| 5 |
|
| #include "ulib/xoshiro.h" |
| 6 |
|
| #include "ulib/splitmix.h" |
| 7 |
|
| |
| 8 |
|
| #ifdef __cplusplus |
| 9 |
|
| } |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| /* based on xoshiro256++ |
| 13 |
|
| * https://prng.di.unimi.it/xoshiro256plusplus.c |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| #define xo_rotl(x,k) (((x) << (k)) | ((x) >> (64 - (k)))) |
| 17 |
|
| |
| 18 |
204 |
| void uu_xoshiro_srand(pUCXT) { |
| 19 |
204 |
| U64 n, *xo_s = SMEM->xo_s; |
| 20 |
|
| |
| 21 |
204 |
| xo_s[0] = uu_splitmix_rand(aUCXT); |
| 22 |
204 |
| xo_s[1] = uu_splitmix_rand(aUCXT); |
| 23 |
204 |
| xo_s[2] = uu_splitmix_rand(aUCXT); |
| 24 |
204 |
| xo_s[3] = uu_splitmix_rand(aUCXT); |
| 25 |
|
| |
| 26 |
|
| /* stir 16 - 31 times */ |
| 27 |
204 |
| n = 16 + (uu_splitmix_rand(aUCXT) >> 60); |
| 28 |
4941 |
▶ | while (n-- > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (n-- > 0) |
4737 |
204 |
4941 |
|
| 29 |
4737 |
| (void)uu_xoshiro_rand(aUCXT); |
| 30 |
204 |
| } |
| 31 |
|
| |
| 32 |
116221 |
| U64 uu_xoshiro_rand(pUCXT) { |
| 33 |
116221 |
| U64 *xo_s = SMEM->xo_s; |
| 34 |
|
| |
| 35 |
116221 |
| const U64 result = xo_rotl(xo_s[0] + xo_s[3], 23) + xo_s[0]; |
| 36 |
|
| |
| 37 |
116221 |
| const U64 t = xo_s[1] << 17; |
| 38 |
|
| |
| 39 |
116221 |
| xo_s[2] ^= xo_s[0]; |
| 40 |
116221 |
| xo_s[3] ^= xo_s[1]; |
| 41 |
116221 |
| xo_s[1] ^= xo_s[2]; |
| 42 |
116221 |
| xo_s[0] ^= xo_s[3]; |
| 43 |
|
| |
| 44 |
116221 |
| xo_s[2] ^= t; |
| 45 |
|
| |
| 46 |
116221 |
| xo_s[3] = xo_rotl(xo_s[3], 45); |
| 47 |
|
| |
| 48 |
116221 |
| return result; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| /* ex:set ts=2 sw=2 itab=spaces: */ |