1 // This is a modified version of xoroshiro128++ from:
2 //
3 //   https://prng.di.unimi.it/xoroshiro128plusplus.c
4 //
5 // The code is modified to accept a pointer to a state object rather than
6 // having a single global state. The algorithm itself is unmodified. Original
7 // licence header follows:
8 
9 /*  Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org)
10 
11 To the extent possible under law, the author has dedicated all copyright
12 and related and neighboring rights to this software to the public domain
13 worldwide.
14 
15 Permission to use, copy, modify, and/or distribute this software for any
16 purpose with or without fee is hereby granted.
17 
18 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24 IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
25 
26 #include <stdint.h>
27 
28 /* This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid,
29    small-state generators. It is extremely (sub-ns) fast and it passes all
30    tests we are aware of, but its state space is large enough only for
31    mild parallelism.
32 
33    For generating just floating-point numbers, xoroshiro128+ is even
34    faster (but it has a very mild bias, see notes in the comments).
35 
36    The state must be seeded so that it is not everywhere zero. If you have
37    a 64-bit seed, we suggest to seed a splitmix64 generator and use its
38    output to fill s. */
39 
40 
xrand_rotl(const uint64_t x,int k)41 static inline uint64_t xrand_rotl(const uint64_t x, int k) {
42   return (x << k) | (x >> (64 - k));
43 }
44 
45 typedef struct xrand_state {
46   uint64_t s0;
47   uint64_t s1;
48 } xrand_state_t;
49 
xrand_next(xrand_state_t * s)50 static inline uint64_t xrand_next(xrand_state_t *s) {
51   const uint64_t s0 = s->s0;
52   uint64_t s1 = s->s1;
53   const uint64_t result = xrand_rotl(s0 + s1, 17) + s0;
54 
55   s1 ^= s0;
56   s->s0 = xrand_rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b
57   s->s1 = xrand_rotl(s1, 28); // c
58 
59   return result;
60 }
61 
62 
63 /* This is the jump function for the generator. It is equivalent
64    to 2^64 calls to next(); it can be used to generate 2^64
65    non-overlapping subsequences for parallel computations. */
66 
xrand_jump(xrand_state_t * s)67 static inline void xrand_jump(xrand_state_t *s) {
68   static const uint64_t JUMP[] = { 0x2bd7a6a6e99c2ddc, 0x0992ccaf6a6fca05 };
69 
70   uint64_t s0 = 0;
71   uint64_t s1 = 0;
72   for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
73     for(int b = 0; b < 64; b++) {
74       if (JUMP[i] & UINT64_C(1) << b) {
75         s0 ^= s->s0;
76         s1 ^= s->s1;
77       }
78       xrand_next(s);
79     }
80 
81   s->s0 = s0;
82   s->s1 = s1;
83 }
84 
85 // Default seed for reproducible test runs: just 128 bits of /dev/urandom
86 #define XRAND_DEFAULT_INIT (xrand_state_t){0xfb11ab871044f128ull,  0x365396cb1df2665dull}
87 
88