File Coverage

lib/PDL/Primitive-xoshiro256plus.c
Criterion Covered Total %
statement 29 65 44.6
branch 5 18 27.7
condition n/a
subroutine n/a
pod n/a
total 34 83 40.9


line stmt bran cond sub pod time code
1             #include
2             #include
3              
4             /* https://prng.di.unimi.it/xoshiro256plus.c, made re-entrant for PDL */
5             /* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
6              
7             To the extent possible under law, the author has dedicated all copyright
8             and related and neighboring rights to this software to the public domain
9             worldwide. This software is distributed without any warranty.
10              
11             See . */
12              
13             /* This is xoshiro256+ 1.0, our best and fastest generator for floating-point
14             numbers. We suggest to use its upper bits for floating-point
15             generation, as it is slightly faster than xoshiro256++/xoshiro256**. It
16             passes all tests we are aware of except for the lowest three bits,
17             which might fail linearity tests (and just those), so if low linear
18             complexity is not considered an issue (as it is usually the case) it
19             can be used to generate 64-bit outputs, too.
20              
21             We suggest to use a sign test to extract a random Boolean value, and
22             right shifts to extract subsets of bits.
23              
24             The state must be seeded so that it is not everywhere zero. If you have
25             a 64-bit seed, we suggest to seed a splitmix64 generator and use its
26             output to fill s. */
27              
28 10048           static inline uint64_t rotl(const uint64_t x, int k) {
29 10048           return (x << k) | (x >> (64 - k));
30             }
31              
32             /* needs to point at a suitably-initialised 4-long array */
33 10048           uint64_t xoshiro256plus_next(uint64_t *s) {
34 10048           const uint64_t result = s[0] + s[3];
35 10048           const uint64_t t = s[1] << 17;
36 10048           s[2] ^= s[0];
37 10048           s[3] ^= s[1];
38 10048           s[1] ^= s[2];
39 10048           s[0] ^= s[3];
40 10048           s[2] ^= t;
41 10048           s[3] = rotl(s[3], 45);
42 10048           return result;
43             }
44              
45             /* This is the jump function for the generator. It is equivalent
46             to 2^128 calls to next(); it can be used to generate 2^128
47             non-overlapping subsequences for parallel computations. */
48              
49 0           void xoshiro256plus_jump(uint64_t *s) {
50             static const uint64_t JUMP[] = { 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
51 0           uint64_t s0 = 0;
52 0           uint64_t s1 = 0;
53 0           uint64_t s2 = 0;
54 0           uint64_t s3 = 0;
55             int i, b;
56 0 0         for(i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
57 0 0         for(b = 0; b < 64; b++) {
58 0 0         if (JUMP[i] & UINT64_C(1) << b) {
59 0           s0 ^= s[0];
60 0           s1 ^= s[1];
61 0           s2 ^= s[2];
62 0           s3 ^= s[3];
63             }
64 0           xoshiro256plus_next(s);
65             }
66 0           s[0] = s0;
67 0           s[1] = s1;
68 0           s[2] = s2;
69 0           s[3] = s3;
70 0           }
71              
72              
73             /* This is the long-jump function for the generator. It is equivalent to
74             2^192 calls to next(); it can be used to generate 2^64 starting points,
75             from each of which jump() will generate 2^64 non-overlapping
76             subsequences for parallel distributed computations. */
77              
78 0           void xoshiro256plus_long_jump(uint64_t *s) {
79             static const uint64_t LONG_JUMP[] = { 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
80 0           uint64_t s0 = 0;
81 0           uint64_t s1 = 0;
82 0           uint64_t s2 = 0;
83 0           uint64_t s3 = 0;
84             int i, b;
85 0 0         for(i = 0; i < sizeof LONG_JUMP / sizeof *LONG_JUMP; i++)
86 0 0         for(b = 0; b < 64; b++) {
87 0 0         if (LONG_JUMP[i] & UINT64_C(1) << b) {
88 0           s0 ^= s[0];
89 0           s1 ^= s[1];
90 0           s2 ^= s[2];
91 0           s3 ^= s[3];
92             }
93 0           xoshiro256plus_next(s);
94             }
95 0           s[0] = s0;
96 0           s[1] = s1;
97 0           s[2] = s2;
98 0           s[3] = s3;
99 0           }
100              
101             /* https://prng.di.unimi.it/splitmix64.c, deleted licence same as above */
102             /* Written in 2015 by Sebastiano Vigna (vigna@acm.org) */
103              
104             /* This is a fixed-increment version of Java 8's SplittableRandom generator
105             See http://dx.doi.org/10.1145/2714064.2660195 and
106             http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html
107              
108             It is a very fast generator passing BigCrush, and it can be useful if
109             for some reason you absolutely want 64 bits of state. */
110              
111 468           uint64_t splitmix64_next(uint64_t *x) {
112 468           uint64_t z = (*x += 0x9e3779b97f4a7c15);
113 468           z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
114 468           z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
115 468           return z ^ (z >> 31);
116             }
117              
118             int pdl_srand_threads = -1; /* how many threads initialised for */
119             uint64_t *pdl_rand_state;
120              
121             /* suitably-initialises n 4-long arrays */
122 9           void pdl_srand(uint64_t **sptr, uint64_t seed, int n) {
123 9           uint64_t x = seed, *s = *sptr;
124 9 100         if (pdl_srand_threads < n) {
125 6 50         if (*sptr) free(*sptr);
126 6           *sptr = s = malloc(n * 4 * sizeof(*s));
127 6           pdl_srand_threads = n;
128             }
129 9           n *= 4;
130             int i;
131 477 100         for (i = 0; i < n; i++)
132 468           s[i] = splitmix64_next(&x);
133 9           }
134              
135 10048           double pdl_drand(uint64_t *s) {
136             /* code from https://prng.di.unimi.it/ */
137 10048           return (xoshiro256plus_next(s) >> 11) * 0x1.0p-53;
138             }