1 /* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
3 To the extent possible under law, the author has dedicated all copyright
4 and related and neighboring rights to this software to the public domain
5 worldwide. This software is distributed without any warranty.
7 See <http://creativecommons.org/publicdomain/zero/1.0/>. */
13 /* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
14 generators. It has excellent (sub-ns) speed, a state (256 bits) that is
15 large enough for any parallel application, and it passes all tests we
18 For generating just floating-point numbers, xoshiro256+ is even faster.
20 The state must be seeded so that it is not everywhere zero. If you have
21 a 64-bit seed, we suggest to seed a splitmix64 generator and use its
24 static inline uint64_t rotl(const uint64_t x, int k) {
25 return (x << k) | (x >> (64 - k));
30 uint64_t xoshiro(void) {
31 const uint64_t result = rotl(s[1] * 5, 7) * 9;
33 const uint64_t t = s[1] << 17;
42 s[3] = rotl(s[3], 45);
47 void prng_init(void) {
49 randomize(s, sizeof(s));
50 } while(!s[0] && !s[1] && !s[2] && !s[3]);
53 void prng_randomize(void *buf, size_t buflen) {
57 while(buflen > sizeof(value)) {
59 memcpy(p, &value, sizeof(value));
61 buflen -= sizeof(value);
69 memcpy(p, &value, buflen);