ATLAS Offline Software
Loading...
Searching...
No Matches
FastReseededPRNG.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 CERN for the benefit of the ATLAS collaboration
3
4----------------------------------------------------------------------
5 Xoroshiro code copyright notice:
6Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
7
8To the extent possible under law, the author has dedicated all copyright
9and related and neighboring rights to this software to the public domain
10worldwide. This software is distributed without any warranty.
11
12See <http://creativecommons.org/publicdomain/zero/1.0/>.
13----------------------------------------------------------------------
14
15This provides a uniform random bit generator using xoroshiro128**
16seeded using 128 bit XXH3 to hash the provided seeds.
17
18Compared to other URBGs this is very cheap to construct, which is useful
19when the PRNG is reseeded every event and only used to generate a few random
20numbers.
21*/
22
24
25#define XXH_INLINE_ALL
26#include "xxhash.h"
27
31
32// Set seed
33void FastReseededPRNG::set_seed(const std::uint64_t* start, std::size_t len) {
34 const auto hash = XXH3_128bits(start, sizeof(std::uint64_t) * len);
35 m_seed_arr[0] = hash.high64;
36 m_seed_arr[1] = hash.low64;
37}
38
39// Reference xoroshiro128** implementation, with modifications to move to C++:
40namespace {
41std::uint64_t rotl(const std::uint64_t x, int k) {
42 return (x << k) | (x >> (64 - k));
43}
44} // namespace
45
46std::uint64_t FastReseededPRNG::next() {
47 const std::uint64_t s0 = m_seed_arr[0];
48 std::uint64_t s1 = m_seed_arr[1];
49 const std::uint64_t result = rotl(s0 * 5, 7) * 9;
50
51 s1 ^= s0;
52 m_seed_arr[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
53 m_seed_arr[1] = rotl(s1, 37); // c
54
55 return result;
56}
static Double_t s0
#define x
void set_seed(const std::uint64_t *start, std::size_t len)
std::uint64_t next()
result_type operator()()
std::array< std::uint64_t, 2 > m_seed_arr
std::uint64_t result_type
XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void *data, size_t len)
Unseeded 128-bit variant of XXH3.
xxHash prototypes and implementation