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