]> git.meshlink.io Git - meshlink/blob - src/xoshiro.c
Avoid allocating packet buffers unnecessarily.
[meshlink] / src / xoshiro.c
1 /*  Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
2
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.
6
7 See <http://creativecommons.org/publicdomain/zero/1.0/>. */
8
9 #include <stdint.h>
10 #include "xoshiro.h"
11
12 /* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
13    generators. It has excellent (sub-ns) speed, a state (256 bits) that is
14    large enough for any parallel application, and it passes all tests we
15    are aware of.
16
17    For generating just floating-point numbers, xoshiro256+ is even faster.
18
19    The state must be seeded so that it is not everywhere zero. If you have
20    a 64-bit seed, we suggest to seed a splitmix64 generator and use its
21    output to fill s. */
22
23 static inline uint64_t rotl(const uint64_t x, int k) {
24         return (x << k) | (x >> (64 - k));
25 }
26
27 uint64_t xoshiro(uint64_t s[4]) {
28         const uint64_t result = rotl(s[1] * 5, 7) * 9;
29
30         const uint64_t t = s[1] << 17;
31
32         s[2] ^= s[0];
33         s[3] ^= s[1];
34         s[1] ^= s[2];
35         s[0] ^= s[3];
36
37         s[2] ^= t;
38
39         s[3] = rotl(s[3], 45);
40
41         return result;
42 }