xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_draid_rand.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: LicenseRef-OpenZFS-ThirdParty-PublicDomain
27877fdebSMatt Macy /*
37877fdebSMatt Macy  * Xorshift Pseudo Random Number Generator based on work by David Blackman
47877fdebSMatt Macy  * and Sebastiano Vigna (vigna@acm.org).
57877fdebSMatt Macy  *
67877fdebSMatt Macy  *   "Further scramblings of Marsaglia's xorshift generators"
77877fdebSMatt Macy  *   http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf
87877fdebSMatt Macy  *   http://prng.di.unimi.it/xoroshiro128plusplus.c
97877fdebSMatt Macy  *
107877fdebSMatt Macy  * To the extent possible under law, the author has dedicated all copyright
117877fdebSMatt Macy  * and related and neighboring rights to this software to the public domain
127877fdebSMatt Macy  * worldwide. This software is distributed without any warranty.
137877fdebSMatt Macy  *
147877fdebSMatt Macy  * See <http://creativecommons.org/publicdomain/zero/1.0/>.
157877fdebSMatt Macy  *
167877fdebSMatt Macy  * This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid,
177877fdebSMatt Macy  * small-state generators. It is extremely (sub-ns) fast and it passes all
187877fdebSMatt Macy  * tests we are aware of, but its state space is large enough only for
197877fdebSMatt Macy  * mild parallelism.
207877fdebSMatt Macy  */
217877fdebSMatt Macy 
227877fdebSMatt Macy #include <sys/vdev_draid.h>
237877fdebSMatt Macy 
rotl(const uint64_t x,int k)247877fdebSMatt Macy static inline uint64_t rotl(const uint64_t x, int k)
257877fdebSMatt Macy {
267877fdebSMatt Macy 	return (x << k) | (x >> (64 - k));
277877fdebSMatt Macy }
287877fdebSMatt Macy 
297877fdebSMatt Macy uint64_t
vdev_draid_rand(uint64_t * s)307877fdebSMatt Macy vdev_draid_rand(uint64_t *s)
317877fdebSMatt Macy {
327877fdebSMatt Macy 	const uint64_t s0 = s[0];
337877fdebSMatt Macy 	uint64_t s1 = s[1];
347877fdebSMatt Macy 	const uint64_t result = rotl(s0 + s1, 17) + s0;
357877fdebSMatt Macy 
367877fdebSMatt Macy 	s1 ^= s0;
377877fdebSMatt Macy 	s[0] = rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b
387877fdebSMatt Macy 	s[1] = rotl(s1, 28); // c
397877fdebSMatt Macy 
407877fdebSMatt Macy 	return (result);
417877fdebSMatt Macy }
42