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