-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitmix.hpp
213 lines (179 loc) · 5.85 KB
/
splitmix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef SPLITMIX_HPP_INCLUDED
#define SPLITMIX_HPP_INCLUDED 1
/*
* A C++ implementation of SplitMix
* Original design by Guy L. Steele, Jr., Doug Lea and Christine H. Flood
* Described in _Fast splittable pseudorandom number generators_
* http://dx.doi.org/10.1145/2714064.2660195 and implemented in
* Java 8 as SplittableRandom
* Based on code from the original paper, with revisions based on changes
* made to the the Java 8 source, at
* http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/
* src/share/classes/java/util/SplittableRandom.java
* and other publicly available implementations.
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Melissa E. O'Neill
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <cstdint>
namespace splitmix_detail {
template <typename IntRep>
IntRep fast_exp(IntRep x, IntRep power)
{
IntRep result = IntRep(1);
IntRep multiplier = x;
while (power != IntRep(0)) {
IntRep thismult = power & IntRep(1) ? multiplier : IntRep(1);
result *= thismult;
power >>= 1;
multiplier *= multiplier;
}
return result;
}
template <typename IntRep>
inline IntRep modular_inverse(IntRep x)
{
return fast_exp(x, IntRep(-1));
}
#ifndef __GNUC__
// Fallback code based on code at https://en.wikipedia.org/wiki/Hamming_weight
// use a popcount intrinsic if you can since most modern CPUs have one.
template <typename IntRep>
inline unsigned int pop_count(IntRep x) {
unsigned int count = 0;
while (x) {
++count;
x &= x - IntRep(1);
}
return count;
}
#else
template <typename IntRep>
inline IntRep pop_count(IntRep x) {
if (sizeof(int) <= sizeof(IntRep))
return __builtin_popcount(x);
else if (sizeof(long) <= sizeof(IntRep))
return __builtin_popcountl(x);
else
return __builtin_popcountll(x);
}
#endif
template <uint64_t m1, uint64_t m2,
unsigned int p, unsigned int q, unsigned int r,
uint64_t m3, uint64_t m4,
unsigned int s, unsigned int t, unsigned int u>
class splitmix64_base {
public:
using result_type = uint64_t;
static constexpr result_type min() { return 0; };
static constexpr result_type max() { return ~result_type(0); };
uint64_t seed_;
uint64_t gamma_;
protected:
static uint64_t mix_gamma(uint64_t x) {
x ^= x >> p;
x *= m1;
x ^= x >> q;
x *= m2;
x ^= x >> r;
x |= 1ul;
int n = pop_count(x ^ (x >> 1));
return (n < 24) ? x ^ 0xaaaaaaaaaaaaaaaa : x;
}
static uint64_t mix64(uint64_t x) {
x ^= x >> s;
x *= m3;
x ^= x >> t;
x *= m4;
x ^= x >> u;
return x;
}
void advance() {
seed_ += gamma_;
}
uint64_t next_seed() {
uint64_t result = seed_;
advance();
return result;
}
public:
splitmix64_base(uint64_t seed = 0xbad0ff1ced15ea5e,
uint64_t gamma = 0x9e3779b97f4a7c15)
: seed_(seed), gamma_(gamma | 1)
{
// Nothing (else) to do.
}
uint64_t operator()() {
return mix64(next_seed());
}
void advance(uint64_t delta) {
seed_ += delta * gamma_;
}
void backstep(uint64_t delta) {
advance(-delta);
}
bool wrapped() {
return seed_ == 0;
}
uint64_t operator-(const splitmix64_base& other) {
return (seed_ - other.seed_) * modular_inverse(other.gamma_);
}
splitmix64_base split() {
uint64_t new_seed = operator()();
uint64_t new_gamma = mix_gamma(next_seed());
return { new_seed, new_gamma };
}
bool operator==(const splitmix64_base& rhs) {
return (seed_ == rhs.seed_) && (gamma_ == rhs.gamma_);
}
};
template <uint64_t m5, uint64_t m6, unsigned int v, unsigned int w,
typename splitmix>
class splitmix32_base : public splitmix {
public:
using result_type = uint32_t;
static constexpr result_type min() { return 0; };
static constexpr result_type max() { return ~result_type(0); };
using splitmix::splitmix;
result_type operator()() {
uint64_t seed = splitmix::next_seed();
seed ^= seed >> v;
seed *= m5;
seed ^= seed >> w;
seed *= m6;
return result_type(seed >> 32);
}
splitmix32_base split() {
splitmix s = splitmix::split();
return {s.seed_, s.gamma_};
}
};
}
using splitmix64 = splitmix_detail::splitmix64_base<
0xff51afd7ed558ccdul, 0xc4ceb9fe1a85ec53ul,
33, 33, 33,
0xbf58476d1ce4e5b9ul, 0x94d049bb133111ebul,
30, 27, 31>;
using splitmix32 = splitmix_detail::splitmix32_base<
0x62a9d9ed799705f5ul, 0xcb24d0a5c88c35b3ul,
33, 28, splitmix64>;
#endif // SPLITMIX_HPP_INCLUDED