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