xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_draid_rand.c (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 /*
2  * Xorshift Pseudo Random Number Generator based on work by David Blackman
3  * and Sebastiano Vigna (vigna@acm.org).
4  *
5  *   "Further scramblings of Marsaglia's xorshift generators"
6  *   http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf
7  *   http://prng.di.unimi.it/xoroshiro128plusplus.c
8  *
9  * To the extent possible under law, the author has dedicated all copyright
10  * and related and neighboring rights to this software to the public domain
11  * worldwide. This software is distributed without any warranty.
12  *
13  * See <http://creativecommons.org/publicdomain/zero/1.0/>.
14  *
15  * This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid,
16  * small-state generators. It is extremely (sub-ns) fast and it passes all
17  * tests we are aware of, but its state space is large enough only for
18  * mild parallelism.
19  */
20 
21 #include <sys/vdev_draid.h>
22 
23 static inline uint64_t rotl(const uint64_t x, int k)
24 {
25 	return (x << k) | (x >> (64 - k));
26 }
27 
28 uint64_t
29 vdev_draid_rand(uint64_t *s)
30 {
31 	const uint64_t s0 = s[0];
32 	uint64_t s1 = s[1];
33 	const uint64_t result = rotl(s0 + s1, 17) + s0;
34 
35 	s1 ^= s0;
36 	s[0] = rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b
37 	s[1] = rotl(s1, 28); // c
38 
39 	return (result);
40 }
41