forked from johnsalmon/cpp-counter-based-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_based_engine.hpp
306 lines (274 loc) · 11.2 KB
/
counter_based_engine.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#pragma once
#include "detail.hpp"
#include <limits>
#include <array>
#include <random>
#include <iosfwd>
#include <algorithm>
#include <cstring>
#include <vector>
#include "threefry_prf.hpp"
#include "philox_prf.hpp"
namespace std{
template<typename prf, size_t c>
class counter_based_engine{
static_assert(numeric_limits<typename prf::output_value_type>::max() >= prf::output_count);
static_assert(c > 0);
// FIXME! static_assert(c * prf::input_word_size <= numeric_limits<uintmax_t>::digits); // could be relaxed with more code
// assertions that should be part of a prf concept:
static_assert(prf::input_word_size > 0);
// Question: should it be possible for the input and output input_word_size to be different?
static_assert(prf::output_word_size > 0);
static_assert(prf::output_count > 0);
// prf::in_type is std::array-like (subscriptable, has an integral value_type, specializes tuple_size)
public:
using result_type = prf::output_value_type;
using seed_value_type = prf::input_value_type;
static constexpr size_t word_size = prf::output_word_size;
static constexpr size_t counter_count = c;
static constexpr size_t counter_word_size = prf::input_word_size;
static constexpr size_t seed_count = prf::input_count - counter_count;
static constexpr size_t seed_word_size = prf::input_word_size;
private:
using prf_result_type = array<result_type, prf::output_count>;
static constexpr size_t result_count = prf::output_count;
static constexpr size_t input_count = prf::input_count;
static constexpr size_t input_word_size = prf::input_word_size;
using input_value_type = prf::input_value_type;
using in_type = array<input_value_type, input_count>;
static_assert(integral<input_value_type>);
static constexpr auto in_mask = detail::fffmask<input_value_type, prf::input_word_size>;
static constexpr auto result_mask = detail::fffmask<result_type,
std::min<size_t>(numeric_limits<result_type>::digits, word_size)>;
in_type in;
prf_result_type results;
// To save space, store the index of the next result to be returned in the 0th results slot.
const auto& ridxref() const {
return results[0];
}
auto& ridxref() {
return results[0];
}
// Some methods to manipulate a (possibly) multi-word counter in
// the first few elements of in[]. An integral counter_type isn't
// strictly necessary. We could do all the counter arithmetic by
// carefully carrying bits between the first few counter_count
// elements of in[]. But this is a lot easier.
// WARNING: the code is poorly tested.
using counter_type = detail::uint_fast<c*prf::input_word_size>;
counter_type get_counter() const{
uint64_t ret = 0;
for(size_t i=0; i<counter_count; ++i)
ret |= uint64_t(in[i])<<(input_word_size * i);
return ret;
}
static void set_counter(in_type& inn, counter_type newctr){
static_assert(input_word_size * counter_count <= numeric_limits<counter_type>::digits);
for(size_t i=0; i<counter_count; ++i)
inn[i] = (newctr >> (input_word_size*i)) & in_mask;
}
void incr_counter(){
in[0] = (in[0] + 1) & in_mask;
for(size_t i=1; i<counter_count; ++i){
if(in[i-1])
[[likely]]return;
in[i] = (in[i] + 1) & in_mask;
}
}
public:
// First, satisfy the requirements for a uniform_random_bit_generator
// result_type - defined above
// min, max
static constexpr result_type min(){ return 0; }
static constexpr result_type max(){ return result_mask; };
static constexpr result_type default_seed = 20111115u;
// operator()
result_type operator()(){
#if 0
// N.B. Writing it out doesn't seem to be an faster than
// letting the compiler optimize away all the loops
// in the bulk-generation overload.
auto ri = ridxref();
if(ri == 0){
// call prf and increment ctr
prf{}(begin(in), begin(results));
incr_counter();
}
result_type ret = results[ri++];
if(ri == result_count)
ri = 0;
ridxref() = ri;
return ret;
#else
result_type ret;
(*this)(&ret, &ret+1);
return ret;
#endif
}
// constraints borrowed from ranges::fill ??
// FIXME - overflow checking. If we do it,
// then the overflow behavior should be as if
// we had done:
// while(n--) *out++ = (*this)();
// Worth the trouble?
template <output_iterator<const result_type&> O, sized_sentinel_for<O> S>
O operator()(O out, S sen){
auto n = sen - out;
// Deliver any saved results
auto ri = ridxref();
if(ri && n){
while(ri < result_count && n){
*out++ = results[ri++];
--n;
}
if(ri == result_count)
ri = 0;
}
// Call the bulk generator
auto nprf = n/result_count;
// lazily construct the input range. No need
// to allocate and fill a big chunk of memory
using namespace std::ranges;
auto c0 = get_counter();
in_type inn;
out = prf{}.generate(views::iota(c0, c0+nprf) |
views::transform([&](auto ctr){
inn = in;
set_counter(inn, ctr);
return ranges::begin(inn);
}),
out);
n -= nprf*result_count;
set_counter(in, c0 + nprf);
// Restock the results array
if(ri == 0 && n){
prf{}(std::begin(in), std::begin(results));
incr_counter();
}
// Finish off any stragglers.
while(n--)
*out++ = results[ri++];
ridxref() = ri;
return out;
}
// And now, the requirements for a random number engine:
// constructors, seed and assignment methods:
counter_based_engine() : counter_based_engine(default_seed){}
explicit counter_based_engine(result_type s){ seed(s); }
void seed(result_type value = default_seed){
array<seed_value_type, seed_count> K = { input_value_type(value) & in_mask };
seed(K);
}
template <typename SeedSeq> // FIXME - disambiguate result_type
explicit counter_based_engine(SeedSeq& q){ seed(q); }
template <typename SeedSeq> // FIXME - disambiguate result_type
void seed(SeedSeq& s){
// Generate 32-bits at a time with the SeedSeq.
// Generate enough to fill prf::in
constexpr size_t N32_per_in = (input_word_size-1)/32 + 1;
array<uint_fast32_t, N32_per_in * seed_count> k32;
s.generate(k32.begin(), k32.end());
auto k32p = begin(k32);
array<seed_value_type, seed_count> iv;
for(auto& v : iv){
v = 0;
for(size_t j=0; j<N32_per_in; ++j)
v |= seed_value_type(*k32p++) << (32*j);
v &= in_mask;
}
seed(iv);
}
// (in)equality operators
bool operator==(const counter_based_engine& rhs) const { return in == rhs.in && ridxref() == rhs.ridxref(); }
bool operator!=(const counter_based_engine& rhs) const { return !operator==(rhs); }
// discard - N.B. There are a lot of tricky corner cases here
// that have NOT been tested. E.g., really large jumps and/or
// an input_value_type that's wider than w.
void discard(unsigned long long jump) {
auto oldridx = ridxref();
unsigned newridx = (jump + oldridx) % result_count;
unsigned long long jumpll = jump + oldridx - (!oldridx && newridx);
jumpll /= result_count;
jumpll += !oldridx;
input_value_type jumpctr = jumpll & in_mask;
input_value_type oldctr = get_counter();
input_value_type newctr = (jumpctr-1 + oldctr) & in_mask;
set_counter(in, newctr);
if(newridx){
if(jumpctr)
prf{}(begin(in), begin(results));
incr_counter();
}else if(newctr == 0){
newridx = result_count;
}
ridxref() = newridx;
}
// stream inserter and extractor
template <typename CharT, typename Traits>
friend basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& os, const counter_based_engine& p){
// FIXME - save/restore os state
ostream_iterator<input_value_type> osin(os, " ");
ranges::copy(p.in, osin);
return os << p.ridxref();
}
template<typename CharT, typename Traits>
friend basic_istream<CharT, Traits>& operator>>(basic_istream<CharT, Traits>& is, counter_based_engine& p){
// FIXME - save/restore is state
istream_iterator<input_value_type> isiin(is);
copy_n(isiin, input_count, begin(p.in));
result_type ridx;
is >> ridx;
if(ridx)
prf{}(begin(p.in), begin(p.results));
p.ridxref() = ridx;
return is;
}
// Extensions:
// counter_based_engine has public methods and types that are not
// required for a Uniform Random Number Engine:
// - the type of the underlying prf and a reference to it.
using prf_type = prf;
// - how many values are consumed in the seed(InRange) member
// and corresponding constructor?
// Constructors and seed members from from a 'seed-range'
template <integral T>
explicit counter_based_engine(initializer_list<T> il){
seed(il);
}
template <integral T>
void seed(initializer_list<T> il){
seed(ranges::subrange(il));
}
template <detail::integral_input_range InRange>
explicit counter_based_engine(InRange iv){
seed(iv);
}
template <detail::integral_input_range InRange>
void seed(InRange _in){
// copy _in to in:
auto inp = ranges::begin(_in);
auto ine = ranges::end(_in);
for(size_t i=counter_count; i<input_count; ++i)
in[i] = (inp == ine) ? 0 : seed_value_type(*inp++) & in_mask; // ?? throw if *inp > in_mask??
set_counter(in, 0);
ridxref() = 0;
}
// Additional possible extensions:
//
// - a method to get the iv.
// - methods that return the sequence length and how many calls are left. N.B. these
// would need a way to return a value larger than numeric_limits<uintmax_t>::max().
// - a rewind() method (the opposite of discard).
// - a const operator[](ull N) that returns the Nth value,
// leaving the state alone.
// - a seek(ull) method to set the internal counter.
};
using philox2x32 = counter_based_engine<philox2x32_prf, 2>;
using philox4x32 = counter_based_engine<philox4x32_prf, 2>;
using philox2x64 = counter_based_engine<philox2x64_prf, 1>;
using philox4x64 = counter_based_engine<philox4x64_prf, 1>;
using threefry2x32 = counter_based_engine<threefry2x32_prf, 2>;
using threefry4x32 = counter_based_engine<threefry4x32_prf, 2>;
using threefry2x64 = counter_based_engine<threefry2x64_prf, 1>;
using threefry4x64 = counter_based_engine<threefry4x64_prf, 1>;
} // namespace std