ATLAS Offline Software
Loading...
Searching...
No Matches
Control
CxxUtils
Root
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:
6
Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
7
8
To the extent possible under law, the author has dedicated all copyright
9
and related and neighboring rights to this software to the public domain
10
worldwide. This software is distributed without any warranty.
11
12
See <http://creativecommons.org/publicdomain/zero/1.0/>.
13
----------------------------------------------------------------------
14
15
This provides a uniform random bit generator using xoroshiro128**
16
seeded using 128 bit XXH3 to hash the provided seeds.
17
18
Compared to other URBGs this is very cheap to construct, which is useful
19
when the PRNG is reseeded every event and only used to generate a few random
20
numbers.
21
*/
22
23
#include "
CxxUtils/FastReseededPRNG.h
"
24
25
#define XXH_INLINE_ALL
26
#include "
xxhash.h
"
27
28
FastReseededPRNG::result_type
FastReseededPRNG::operator()
() {
29
return
next
();
30
}
31
32
// Set seed
33
void
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++:
40
namespace
{
41
std::uint64_t rotl(
const
std::uint64_t
x
,
int
k) {
42
return
(
x
<< k) | (
x
>> (64 - k));
43
}
44
}
// namespace
45
46
std::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
}
FastReseededPRNG.h
s0
static Double_t s0
Definition
LArPhysWaveHECTool.cxx:37
x
#define x
FastReseededPRNG::set_seed
void set_seed(const std::uint64_t *start, std::size_t len)
Definition
FastReseededPRNG.cxx:33
FastReseededPRNG::next
std::uint64_t next()
Definition
FastReseededPRNG.cxx:46
FastReseededPRNG::operator()
result_type operator()()
Definition
FastReseededPRNG.cxx:28
FastReseededPRNG::m_seed_arr
std::array< std::uint64_t, 2 > m_seed_arr
Definition
FastReseededPRNG.h:47
FastReseededPRNG::result_type
std::uint64_t result_type
Definition
FastReseededPRNG.h:30
XXH3_128bits
XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void *data, size_t len)
Unseeded 128-bit variant of XXH3.
result
xxhash.h
xxHash prototypes and implementation
Generated on
for ATLAS Offline Software by
1.14.0