File Coverage

ulib/xoshiro.c
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine n/a
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
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 215           void uu_xoshiro_srand(pUCXT) {
19 215           U64 n, *xo_s = SMEM->xo_s;
20              
21 215           xo_s[0] = uu_splitmix_rand(aUCXT);
22 215           xo_s[1] = uu_splitmix_rand(aUCXT);
23 215           xo_s[2] = uu_splitmix_rand(aUCXT);
24 215           xo_s[3] = uu_splitmix_rand(aUCXT);
25              
26             /* stir 16 - 31 times */
27 215           n = 16 + (uu_splitmix_rand(aUCXT) >> 60);
28 5169 100         while (n-- > 0)
29 4954           (void)uu_xoshiro_rand(aUCXT);
30 215           }
31              
32 105869           U64 uu_xoshiro_rand(pUCXT) {
33 105869           U64 *xo_s = SMEM->xo_s;
34              
35 105869           const U64 result = xo_rotl(xo_s[0] + xo_s[3], 23) + xo_s[0];
36              
37 105869           const U64 t = xo_s[1] << 17;
38              
39 105869           xo_s[2] ^= xo_s[0];
40 105869           xo_s[3] ^= xo_s[1];
41 105869           xo_s[1] ^= xo_s[2];
42 105869           xo_s[0] ^= xo_s[3];
43              
44 105869           xo_s[2] ^= t;
45              
46 105869           xo_s[3] = xo_rotl(xo_s[3], 45);
47              
48 105869           return result;
49             }
50              
51             /* ex:set ts=2 sw=2 itab=spaces: */