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 104           void xo_srand(pUCXT, Pid_t pid) {
19 104           U64 n, *xo_s = UCXT.xo_s;
20              
21             (void)pid;
22              
23 104           xo_s[0] = sm_rand(aUCXT);
24 104           xo_s[1] = sm_rand(aUCXT);
25 104           xo_s[2] = sm_rand(aUCXT);
26 104           xo_s[3] = sm_rand(aUCXT);
27              
28             /* stir 16 - 31 times */
29 104           n = 16 + (sm_rand(aUCXT) >> 60);
30 2570 100         while (n-- > 0)
31 2466           (void)xo_rand(aUCXT);
32 104           }
33              
34 18218           U64 xo_rand(pUCXT) {
35 18218           U64 *xo_s = UCXT.xo_s;
36              
37 18218           const U64 result = xo_rotl(xo_s[0] + xo_s[3], 23) + xo_s[0];
38              
39 18218           const U64 t = xo_s[1] << 17;
40              
41 18218           xo_s[2] ^= xo_s[0];
42 18218           xo_s[3] ^= xo_s[1];
43 18218           xo_s[1] ^= xo_s[2];
44 18218           xo_s[0] ^= xo_s[3];
45              
46 18218           xo_s[2] ^= t;
47              
48 18218           xo_s[3] = xo_rotl(xo_s[3], 45);
49              
50 18218           return result;
51             }
52              
53             /* ex:set ts=2 sw=2 itab=spaces: */