1 /*
2 * PCG Random Number Generation for C++
3 *
4 * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * For additional information about the PCG random number generation scheme,
19 * including its license and other licensing options, visit
20 *
21 * http://www.pcg-random.org
22 */
23
24 /*
25 * Modified by The OpenClonk.org Project to improve compatibility with
26 * Microsoft Visual C++.
27 */
28
29 /*
30 * This code provides the reference implementation of the PCG family of
31 * random number generators. The code is complex because it implements
32 *
33 * - several members of the PCG family, specifically members corresponding
34 * to the output functions:
35 * - XSH RR (good for 64-bit state, 32-bit output)
36 * - XSH RS (good for 64-bit state, 32-bit output)
37 * - XSL RR (good for 128-bit state, 64-bit output)
38 * - RXS M XS (statistically most powerful generator)
39 * - XSL RR RR (good for 128-bit state, 128-bit output)
40 * - and RXS, RXS M, XSH, XSL (mostly for testing)
41 * - at potentially *arbitrary* bit sizes
42 * - with four different techniques for random streams (MCG, one-stream
43 * LCG, settable-stream LCG, unique-stream LCG)
44 * - and the extended generation schemes allowing arbitrary periods
45 * - with all features of C++11 random number generation (and more),
46 * some of which are somewhat painful, including
47 * - initializing with a SeedSequence which writes 32-bit values
48 * to memory, even though the state of the generator may not
49 * use 32-bit values (it might use smaller or larger integers)
50 * - I/O for RNGs and a prescribed format, which needs to handle
51 * the issue that 8-bit and 128-bit integers don't have working
52 * I/O routines (e.g., normally 8-bit = char, not integer)
53 * - equality and inequality for RNGs
54 * - and a number of convenience typedefs to mask all the complexity
55 *
56 * The code employes a fairly heavy level of abstraction, and has to deal
57 * with various C++ minutia. If you're looking to learn about how the PCG
58 * scheme works, you're probably best of starting with one of the other
59 * codebases (see www.pcg-random.org). But if you're curious about the
60 * constants for the various output functions used in those other, simpler,
61 * codebases, this code shows how they are calculated.
62 *
63 * On the positive side, at least there are convenience typedefs so that you
64 * can say
65 *
66 * pcg32 myRNG;
67 *
68 * rather than:
69 *
70 * pcg_detail::engine<
71 * uint32_t, // Output Type
72 * uint64_t, // State Type
73 * pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func
74 * pcg_detail::specific_stream<uint64_t>, // Stream Kind
75 * pcg_detail::default_multiplier<uint64_t> // LCG Mult
76 * > myRNG;
77 *
78 */
79
80 #ifndef PCG_RAND_HPP_INCLUDED
81 #define PCG_RAND_HPP_INCLUDED 1
82
83 #include <cinttypes>
84 #include <cstddef>
85 #include <cstdlib>
86 #include <cstring>
87 #include <cassert>
88 #include <limits>
89 #include <iostream>
90 #include <type_traits>
91 #include <utility>
92 #include <locale>
93 #include <new>
94 #include <stdexcept>
95
96 /*
97 * The pcg_extras namespace contains some support code that is likley to
98 * be useful for a variety of RNGs, including:
99 * - 128-bit int support for platforms where it isn't available natively
100 * - bit twiddling operations
101 * - I/O of 128-bit and 8-bit integers
102 * - Handling the evilness of SeedSeq
103 * - Support for efficiently producing random numbers less than a given
104 * bound
105 */
106
107 #include "pcg_extras.hpp"
108
109 namespace pcg_detail {
110
111 using namespace pcg_extras;
112
113 /*
114 * The LCG generators need some constants to function. This code lets you
115 * look up the constant by *type*. For example
116 *
117 * default_multiplier<uint32_t>::multiplier()
118 *
119 * gives you the default multipler for 32-bit integers. We use the name
120 * of the constant and not a generic word like value to allow these classes
121 * to be used as mixins.
122 */
123
124 template <typename T>
125 struct default_multiplier {
126 // Not defined for an arbitrary type
127 };
128
129 template <typename T>
130 struct default_increment {
131 // Not defined for an arbitrary type
132 };
133
134 #define PCG_DEFINE_CONSTANT(type, what, kind, constant) \
135 template <> \
136 struct what ## _ ## kind<type> { \
137 static constexpr type kind() { \
138 return constant; \
139 } \
140 };
141
142 PCG_DEFINE_CONSTANT(uint8_t, default, multiplier, 141U)
143 PCG_DEFINE_CONSTANT(uint8_t, default, increment, 77U)
144
145 PCG_DEFINE_CONSTANT(uint16_t, default, multiplier, 12829U)
146 PCG_DEFINE_CONSTANT(uint16_t, default, increment, 47989U)
147
148 PCG_DEFINE_CONSTANT(uint32_t, default, multiplier, 747796405U)
149 PCG_DEFINE_CONSTANT(uint32_t, default, increment, 2891336453U)
150
151 PCG_DEFINE_CONSTANT(uint64_t, default, multiplier, 6364136223846793005ULL)
152 PCG_DEFINE_CONSTANT(uint64_t, default, increment, 1442695040888963407ULL)
153
154 PCG_DEFINE_CONSTANT(pcg128_t, default, multiplier,
155 PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL))
156 PCG_DEFINE_CONSTANT(pcg128_t, default, increment,
157 PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL))
158
159
160 /*
161 * Each PCG generator is available in four variants, based on how it applies
162 * the additive constant for its underlying LCG; the variations are:
163 *
164 * single stream - all instances use the same fixed constant, thus
165 * the RNG always somewhere in same sequence
166 * mcg - adds zero, resulting in a single stream and reduced
167 * period
168 * specific stream - the constant can be changed at any time, selecting
169 * a different random sequence
170 * unique stream - the constant is based on the memory addresss of the
171 * object, thus every RNG has its own unique sequence
172 *
173 * This variation is provided though mixin classes which define a function
174 * value called increment() that returns the nesessary additive constant.
175 */
176
177
178
179 /*
180 * unique stream
181 */
182
183
184 template <typename itype>
185 class unique_stream {
186 protected:
187 static constexpr bool is_mcg = false;
188
189 // Is never called, but is provided for symmetry with specific_stream
set_stream(...)190 void set_stream(...)
191 {
192 abort();
193 }
194
195 public:
196 typedef itype state_type;
197
increment() const198 constexpr itype increment() const {
199 return itype(reinterpret_cast<unsigned long>(this) | 1);
200 }
201
stream() const202 constexpr itype stream() const
203 {
204 return increment() >> 1;
205 }
206
207 static constexpr bool can_specify_stream = false;
208
streams_pow2()209 static constexpr size_t streams_pow2()
210 {
211 return (sizeof(itype) < sizeof(size_t) ? sizeof(itype)
212 : sizeof(size_t))*8 - 1u;
213 }
214
215 protected:
216 constexpr unique_stream() = default;
217 };
218
219
220 /*
221 * no stream (mcg)
222 */
223
224 template <typename itype>
225 class no_stream {
226 protected:
227 static constexpr bool is_mcg = true;
228
229 // Is never called, but is provided for symmetry with specific_stream
set_stream(...)230 void set_stream(...)
231 {
232 abort();
233 }
234
235 public:
236 typedef itype state_type;
237
increment()238 static constexpr itype increment() {
239 return 0;
240 }
241
242 static constexpr bool can_specify_stream = false;
243
streams_pow2()244 static constexpr size_t streams_pow2()
245 {
246 return 0u;
247 }
248
249 protected:
250 constexpr no_stream() = default;
251 };
252
253
254 /*
255 * single stream/sequence (oneseq)
256 */
257
258 template <typename itype>
259 class oneseq_stream : public default_increment<itype> {
260 protected:
261 static constexpr bool is_mcg = false;
262
263 // Is never called, but is provided for symmetry with specific_stream
set_stream(...)264 void set_stream(...)
265 {
266 abort();
267 }
268
269 public:
270 typedef itype state_type;
271
stream()272 static constexpr itype stream()
273 {
274 return default_increment<itype>::increment() >> 1;
275 }
276
277 static constexpr bool can_specify_stream = false;
278
streams_pow2()279 static constexpr size_t streams_pow2()
280 {
281 return 0u;
282 }
283
284 protected:
285 constexpr oneseq_stream() = default;
286 };
287
288
289 /*
290 * specific stream
291 */
292
293 template <typename itype>
294 class specific_stream {
295 protected:
296 static constexpr bool is_mcg = false;
297
298 itype inc_ = default_increment<itype>::increment();
299
300 public:
301 typedef itype state_type;
302 typedef itype stream_state;
303
increment() const304 constexpr itype increment() const {
305 return inc_;
306 }
307
stream()308 itype stream()
309 {
310 return inc_ >> 1;
311 }
312
set_stream(itype specific_seq)313 void set_stream(itype specific_seq)
314 {
315 inc_ = (specific_seq << 1) | 1;
316 }
317
318 static constexpr bool can_specify_stream = true;
319
streams_pow2()320 static constexpr size_t streams_pow2()
321 {
322 return (sizeof(itype)*8) - 1u;
323 }
324
325 protected:
326 specific_stream() = default;
327
specific_stream(itype specific_seq)328 specific_stream(itype specific_seq)
329 : inc_((specific_seq << 1) | itype(1U))
330 {
331 // Nothing (else) to do.
332 }
333 };
334
335
336 /*
337 * This is where it all comes together. This function joins together three
338 * mixin classes which define
339 * - the LCG additive constant (the stream)
340 * - the LCG multiplier
341 * - the output function
342 * in addition, we specify the type of the LCG state, and the result type,
343 * and whether to use the pre-advance version of the state for the output
344 * (increasing instruction-level parallelism) or the post-advance version
345 * (reducing register pressure).
346 *
347 * Given the high level of parameterization, the code has to use some
348 * template-metaprogramming tricks to handle some of the suble variations
349 * involved.
350 */
351
352 template <typename xtype, typename itype,
353 typename output_mixin,
354 bool output_previous = true,
355 typename stream_mixin = oneseq_stream<itype>,
356 typename multiplier_mixin = default_multiplier<itype> >
357 class engine : protected output_mixin,
358 public stream_mixin,
359 protected multiplier_mixin {
360 protected:
361 itype state_;
362
363 struct can_specify_stream_tag {};
364 struct no_specifiable_stream_tag {};
365
366 using stream_mixin::increment;
367 using multiplier_mixin::multiplier;
368
369 public:
370 typedef xtype result_type;
371 typedef itype state_type;
372
period_pow2()373 static constexpr size_t period_pow2()
374 {
375 return sizeof(state_type)*8 - 2*stream_mixin::is_mcg;
376 }
377
378 // It would be nice to use std::numeric_limits for these, but
379 // we can't be sure that it'd be defined for the 128-bit types.
380
min()381 static constexpr result_type min()
382 {
383 return result_type(0UL);
384 }
385
max()386 static constexpr result_type max()
387 {
388 return ~result_type(0UL);
389 }
390
391 protected:
bump(itype state)392 itype bump(itype state)
393 {
394 return state * multiplier() + increment();
395 }
396
base_generate()397 itype base_generate()
398 {
399 return state_ = bump(state_);
400 }
401
base_generate0()402 itype base_generate0()
403 {
404 itype old_state = state_;
405 state_ = bump(state_);
406 return old_state;
407 }
408
409 public:
operator ()()410 result_type operator()()
411 {
412 if (output_previous)
413 return this->output(base_generate0());
414 else
415 return this->output(base_generate());
416 }
417
operator ()(result_type upper_bound)418 result_type operator()(result_type upper_bound)
419 {
420 return bounded_rand(*this, upper_bound);
421 }
422
423 protected:
424 static itype advance(itype state, itype delta,
425 itype cur_mult, itype cur_plus);
426
427 static itype distance(itype cur_state, itype newstate, itype cur_mult,
428 itype cur_plus, itype mask = ~itype(0U));
429
distance(itype newstate,itype mask=~itype (0U)) const430 itype distance(itype newstate, itype mask = ~itype(0U)) const
431 {
432 return distance(state_, newstate, multiplier(), increment(), mask);
433 }
434
435 public:
advance(itype delta)436 void advance(itype delta)
437 {
438 state_ = advance(state_, delta, this->multiplier(), this->increment());
439 }
440
backstep(itype delta)441 void backstep(itype delta)
442 {
443 advance(-delta);
444 }
445
discard(itype delta)446 void discard(itype delta)
447 {
448 advance(delta);
449 }
450
wrapped()451 bool wrapped()
452 {
453 if (stream_mixin::is_mcg) {
454 // For MCGs, the low order two bits never change. In this
455 // implementation, we keep them fixed at 3 to make this test
456 // easier.
457 return state_ == 3;
458 } else {
459 return state_ == 0;
460 }
461 }
462
engine(itype state=itype (0xcafef00dd15ea5e5ULL))463 engine(itype state = itype(0xcafef00dd15ea5e5ULL))
464 : state_(this->is_mcg ? state|state_type(3U)
465 : bump(state + this->increment()))
466 {
467 // Nothing else to do.
468 }
469
470 // This function may or may not exist. It thus has to be a template
471 // to use SFINAE; users don't have to worry about its template-ness.
472
473 template <typename sm = stream_mixin>
engine(itype state,typename sm::stream_state stream_seed)474 engine(itype state, typename sm::stream_state stream_seed)
475 : stream_mixin(stream_seed),
476 state_(this->is_mcg ? state|state_type(3U)
477 : bump(state + this->increment()))
478 {
479 // Nothing else to do.
480 }
481
482 template<typename SeedSeq>
engine(SeedSeq && seedSeq,typename std::enable_if<!stream_mixin::can_specify_stream &&!std::is_convertible<SeedSeq,itype>::value &&!std::is_convertible<SeedSeq,engine>::value,no_specifiable_stream_tag>::type={})483 engine(SeedSeq&& seedSeq, typename std::enable_if<
484 !stream_mixin::can_specify_stream
485 && !std::is_convertible<SeedSeq, itype>::value
486 && !std::is_convertible<SeedSeq, engine>::value,
487 no_specifiable_stream_tag>::type = {})
488 : engine(generate_one<itype>(std::forward<SeedSeq>(seedSeq)))
489 {
490 // Nothing else to do.
491 }
492
493 template<typename SeedSeq>
engine(SeedSeq && seedSeq,typename std::enable_if<stream_mixin::can_specify_stream &&!std::is_convertible<SeedSeq,itype>::value &&!std::is_convertible<SeedSeq,engine>::value,can_specify_stream_tag>::type={})494 engine(SeedSeq&& seedSeq, typename std::enable_if<
495 stream_mixin::can_specify_stream
496 && !std::is_convertible<SeedSeq, itype>::value
497 && !std::is_convertible<SeedSeq, engine>::value,
498 can_specify_stream_tag>::type = {})
499 : engine(generate_one<itype,1,2>(seedSeq),
500 generate_one<itype,0,2>(seedSeq))
501 {
502 // Nothing else to do.
503 }
504
505
506 template<typename... Args>
seed(Args &&...args)507 void seed(Args&&... args)
508 {
509 new (this) engine(std::forward<Args>(args)...);
510 }
511
512 template <typename xtype1, typename itype1,
513 typename output_mixin1, bool output_previous1,
514 typename stream_mixin_lhs, typename multiplier_mixin_lhs,
515 typename stream_mixin_rhs, typename multiplier_mixin_rhs>
516 friend bool operator==(const engine<xtype1,itype1,
517 output_mixin1,output_previous1,
518 stream_mixin_lhs, multiplier_mixin_lhs>&,
519 const engine<xtype1,itype1,
520 output_mixin1,output_previous1,
521 stream_mixin_rhs, multiplier_mixin_rhs>&);
522
523 template <typename xtype1, typename itype1,
524 typename output_mixin1, bool output_previous1,
525 typename stream_mixin_lhs, typename multiplier_mixin_lhs,
526 typename stream_mixin_rhs, typename multiplier_mixin_rhs>
527 friend itype1 operator-(const engine<xtype1,itype1,
528 output_mixin1,output_previous1,
529 stream_mixin_lhs, multiplier_mixin_lhs>&,
530 const engine<xtype1,itype1,
531 output_mixin1,output_previous1,
532 stream_mixin_rhs, multiplier_mixin_rhs>&);
533
534 template <typename CharT, typename Traits,
535 typename xtype1, typename itype1,
536 typename output_mixin1, bool output_previous1,
537 typename stream_mixin1, typename multiplier_mixin1>
538 friend std::basic_ostream<CharT,Traits>&
539 operator<<(std::basic_ostream<CharT,Traits>& out,
540 const engine<xtype1,itype1,
541 output_mixin1,output_previous1,
542 stream_mixin1, multiplier_mixin1>&);
543
544 template <typename CharT, typename Traits,
545 typename xtype1, typename itype1,
546 typename output_mixin1, bool output_previous1,
547 typename stream_mixin1, typename multiplier_mixin1>
548 friend std::basic_istream<CharT,Traits>&
549 operator>>(std::basic_istream<CharT,Traits>& in,
550 engine<xtype1, itype1,
551 output_mixin1, output_previous1,
552 stream_mixin1, multiplier_mixin1>& rng);
553 };
554
555 template <typename CharT, typename Traits,
556 typename xtype, typename itype,
557 typename output_mixin, bool output_previous,
558 typename stream_mixin, typename multiplier_mixin>
559 std::basic_ostream<CharT,Traits>&
operator <<(std::basic_ostream<CharT,Traits> & out,const engine<xtype,itype,output_mixin,output_previous,stream_mixin,multiplier_mixin> & rng)560 operator<<(std::basic_ostream<CharT,Traits>& out,
561 const engine<xtype,itype,
562 output_mixin,output_previous,
563 stream_mixin, multiplier_mixin>& rng)
564 {
565 auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
566 auto space = out.widen(' ');
567 auto orig_fill = out.fill();
568
569 out << rng.multiplier() << space
570 << rng.increment() << space
571 << rng.state_;
572
573 out.flags(orig_flags);
574 out.fill(orig_fill);
575 return out;
576 }
577
578
579 template <typename CharT, typename Traits,
580 typename xtype, typename itype,
581 typename output_mixin, bool output_previous,
582 typename stream_mixin, typename multiplier_mixin>
583 std::basic_istream<CharT,Traits>&
operator >>(std::basic_istream<CharT,Traits> & in,engine<xtype,itype,output_mixin,output_previous,stream_mixin,multiplier_mixin> & rng)584 operator>>(std::basic_istream<CharT,Traits>& in,
585 engine<xtype,itype,
586 output_mixin,output_previous,
587 stream_mixin, multiplier_mixin>& rng)
588 {
589 auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
590
591 itype multiplier, increment, state;
592 in >> multiplier >> increment >> state;
593
594 if (!in.fail()) {
595 bool good = true;
596 if (multiplier != rng.multiplier()) {
597 good = false;
598 } else if (rng.can_specify_stream) {
599 rng.set_stream(increment >> 1);
600 } else if (increment != rng.increment()) {
601 good = false;
602 }
603 if (good) {
604 rng.state_ = state;
605 } else {
606 in.clear(std::ios::failbit);
607 }
608 }
609
610 in.flags(orig_flags);
611 return in;
612 }
613
614
615 template <typename xtype, typename itype,
616 typename output_mixin, bool output_previous,
617 typename stream_mixin, typename multiplier_mixin>
618 itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
619 multiplier_mixin>::advance(
620 itype state, itype delta, itype cur_mult, itype cur_plus)
621 {
622 // The method used here is based on Brown, "Random Number Generation
623 // with Arbitrary Stride,", Transactions of the American Nuclear
624 // Society (Nov. 1994). The algorithm is very similar to fast
625 // exponentiation.
626 //
627 // Even though delta is an unsigned integer, we can pass a
628 // signed integer to go backwards, it just goes "the long way round".
629
630 constexpr itype ZERO = 0u; // itype may be a non-trivial types, so
631 constexpr itype ONE = 1u; // we define some ugly constants.
632 itype acc_mult = 1;
633 itype acc_plus = 0;
634 while (delta > ZERO) {
635 if (delta & ONE) {
636 acc_mult *= cur_mult;
637 acc_plus = acc_plus*cur_mult + cur_plus;
638 }
639 cur_plus = (cur_mult+ONE)*cur_plus;
640 cur_mult *= cur_mult;
641 delta >>= 1;
642 }
643 return acc_mult * state + acc_plus;
644 }
645
646 template <typename xtype, typename itype,
647 typename output_mixin, bool output_previous,
648 typename stream_mixin, typename multiplier_mixin>
649 itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
650 multiplier_mixin>::distance(
651 itype cur_state, itype newstate, itype cur_mult, itype cur_plus, itype mask)
652 {
653 constexpr itype ONE = 1u; // itype could be weird, so use constant
654 itype the_bit = stream_mixin::is_mcg ? itype(4u) : itype(1u);
655 itype distance = 0u;
656 while ((cur_state & mask) != (newstate & mask)) {
657 if ((cur_state & the_bit) != (newstate & the_bit)) {
658 cur_state = cur_state * cur_mult + cur_plus;
659 distance |= the_bit;
660 }
661 assert((cur_state & the_bit) == (newstate & the_bit));
662 the_bit <<= 1;
663 cur_plus = (cur_mult+ONE)*cur_plus;
664 cur_mult *= cur_mult;
665 }
666 return stream_mixin::is_mcg ? distance >> 2 : distance;
667 }
668
669 template <typename xtype, typename itype,
670 typename output_mixin, bool output_previous,
671 typename stream_mixin_lhs, typename multiplier_mixin_lhs,
672 typename stream_mixin_rhs, typename multiplier_mixin_rhs>
673 itype operator-(const engine<xtype,itype,
674 output_mixin,output_previous,
675 stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
676 const engine<xtype,itype,
677 output_mixin,output_previous,
678 stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
679 {
680 if (lhs.multiplier() != rhs.multiplier()
681 || lhs.increment() != rhs.increment())
682 throw std::logic_error("incomparable generators");
683 return rhs.distance(lhs.state_);
684 }
685
686
687 template <typename xtype, typename itype,
688 typename output_mixin, bool output_previous,
689 typename stream_mixin_lhs, typename multiplier_mixin_lhs,
690 typename stream_mixin_rhs, typename multiplier_mixin_rhs>
operator ==(const engine<xtype,itype,output_mixin,output_previous,stream_mixin_lhs,multiplier_mixin_lhs> & lhs,const engine<xtype,itype,output_mixin,output_previous,stream_mixin_rhs,multiplier_mixin_rhs> & rhs)691 bool operator==(const engine<xtype,itype,
692 output_mixin,output_previous,
693 stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
694 const engine<xtype,itype,
695 output_mixin,output_previous,
696 stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
697 {
698 return (lhs.multiplier() == rhs.multiplier())
699 && (lhs.increment() == rhs.increment())
700 && (lhs.state_ == rhs.state_);
701 }
702
703 template <typename xtype, typename itype,
704 typename output_mixin, bool output_previous,
705 typename stream_mixin_lhs, typename multiplier_mixin_lhs,
706 typename stream_mixin_rhs, typename multiplier_mixin_rhs>
operator !=(const engine<xtype,itype,output_mixin,output_previous,stream_mixin_lhs,multiplier_mixin_lhs> & lhs,const engine<xtype,itype,output_mixin,output_previous,stream_mixin_rhs,multiplier_mixin_rhs> & rhs)707 inline bool operator!=(const engine<xtype,itype,
708 output_mixin,output_previous,
709 stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
710 const engine<xtype,itype,
711 output_mixin,output_previous,
712 stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
713 {
714 return !operator==(lhs,rhs);
715 }
716
717
718 template <typename xtype, typename itype,
719 template<typename XT,typename IT> class output_mixin,
720 bool output_previous = (sizeof(itype) <= 8)>
721 using oneseq_base = engine<xtype, itype,
722 output_mixin<xtype, itype>, output_previous,
723 oneseq_stream<itype> >;
724
725 template <typename xtype, typename itype,
726 template<typename XT,typename IT> class output_mixin,
727 bool output_previous = (sizeof(itype) <= 8)>
728 using unique_base = engine<xtype, itype,
729 output_mixin<xtype, itype>, output_previous,
730 unique_stream<itype> >;
731
732 template <typename xtype, typename itype,
733 template<typename XT,typename IT> class output_mixin,
734 bool output_previous = (sizeof(itype) <= 8)>
735 using setseq_base = engine<xtype, itype,
736 output_mixin<xtype, itype>, output_previous,
737 specific_stream<itype> >;
738
739 template <typename xtype, typename itype,
740 template<typename XT,typename IT> class output_mixin,
741 bool output_previous = (sizeof(itype) <= 8)>
742 using mcg_base = engine<xtype, itype,
743 output_mixin<xtype, itype>, output_previous,
744 no_stream<itype> >;
745
746 /*
747 * OUTPUT FUNCTIONS.
748 *
749 * These are the core of the PCG generation scheme. They specify how to
750 * turn the base LCG's internal state into the output value of the final
751 * generator.
752 *
753 * They're implemented as mixin classes.
754 *
755 * All of the classes have code that is written to allow it to be applied
756 * at *arbitrary* bit sizes, although in practice they'll only be used at
757 * standard sizes supported by C++.
758 */
759
760 /*
761 * XSH RS -- high xorshift, followed by a random shift
762 *
763 * Fast. A good performer.
764 */
765
766 template <typename xtype, typename itype>
767 struct xsh_rs_mixin {
outputpcg_detail::xsh_rs_mixin768 static xtype output(itype internal)
769 {
770 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
771 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
772 constexpr bitcount_t sparebits = bits - xtypebits;
773 constexpr bitcount_t opbits =
774 sparebits-5 >= 64 ? 5
775 : sparebits-4 >= 32 ? 4
776 : sparebits-3 >= 16 ? 3
777 : sparebits-2 >= 4 ? 2
778 : sparebits-1 >= 1 ? 1
779 : 0;
780 constexpr bitcount_t mask = (1 << opbits) - 1;
781 constexpr bitcount_t maxrandshift = mask;
782 constexpr bitcount_t topspare = opbits;
783 constexpr bitcount_t bottomspare = sparebits - topspare;
784 constexpr bitcount_t xshift = topspare + (xtypebits+maxrandshift)/2;
785 bitcount_t rshift =
786 opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
787 internal ^= internal >> xshift;
788 xtype result = xtype(internal >> (bottomspare - maxrandshift + rshift));
789 return result;
790 }
791 };
792
793 /*
794 * XSH RR -- high xorshift, followed by a random rotate
795 *
796 * Fast. A good performer. Slightly better statistically than XSH RS.
797 */
798
799 template <typename xtype, typename itype>
800 struct xsh_rr_mixin {
outputpcg_detail::xsh_rr_mixin801 static xtype output(itype internal)
802 {
803 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
804 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
805 constexpr bitcount_t sparebits = bits - xtypebits;
806 constexpr bitcount_t wantedopbits =
807 xtypebits >= 128 ? 7
808 : xtypebits >= 64 ? 6
809 : xtypebits >= 32 ? 5
810 : xtypebits >= 16 ? 4
811 : 3;
812 constexpr bitcount_t opbits =
813 sparebits >= wantedopbits ? wantedopbits
814 : sparebits;
815 constexpr bitcount_t amplifier = wantedopbits - opbits;
816 constexpr bitcount_t mask = (1 << opbits) - 1;
817 constexpr bitcount_t topspare = opbits;
818 constexpr bitcount_t bottomspare = sparebits - topspare;
819 constexpr bitcount_t xshift = (topspare + xtypebits)/2;
820 bitcount_t rot = opbits ? bitcount_t(internal >> (bits - opbits)) & mask
821 : 0;
822 bitcount_t amprot = (rot << amplifier) & mask;
823 internal ^= internal >> xshift;
824 xtype result = xtype(internal >> bottomspare);
825 result = rotr(result, amprot);
826 return result;
827 }
828 };
829
830 /*
831 * RXS -- random xorshift
832 */
833
834 template <typename xtype, typename itype>
835 struct rxs_mixin {
output_rxspcg_detail::rxs_mixin836 static xtype output_rxs(itype internal)
837 {
838 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
839 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
840 constexpr bitcount_t shift = bits - xtypebits;
841 constexpr bitcount_t extrashift = (xtypebits - shift)/2;
842 bitcount_t rshift = shift > 64+8 ? (internal >> (bits - 6)) & 63
843 : shift > 32+4 ? (internal >> (bits - 5)) & 31
844 : shift > 16+2 ? (internal >> (bits - 4)) & 15
845 : shift > 8+1 ? (internal >> (bits - 3)) & 7
846 : shift > 4+1 ? (internal >> (bits - 2)) & 3
847 : shift > 2+1 ? (internal >> (bits - 1)) & 1
848 : 0;
849 internal ^= internal >> (shift + extrashift - rshift);
850 xtype result = internal >> rshift;
851 return result;
852 }
853 };
854
855 /*
856 * RXS M XS -- random xorshift, mcg multiply, fixed xorshift
857 *
858 * The most statistically powerful generator, but all those steps
859 * make it slower than some of the others. We give it the rottenest jobs.
860 *
861 * Because it's usually used in contexts where the state type and the
862 * result type are the same, it is a permutation and is thus invertable.
863 * We thus provide a function to invert it. This function is used to
864 * for the "inside out" generator used by the extended generator.
865 */
866
867 /* Defined type-based concepts for the multiplication step. They're actually
868 * all derived by truncating the 128-bit, which was computed to be a good
869 * "universal" constant.
870 */
871
872 template <typename T>
873 struct mcg_multiplier {
874 // Not defined for an arbitrary type
875 };
876
877 template <typename T>
878 struct mcg_unmultiplier {
879 // Not defined for an arbitrary type
880 };
881
882 PCG_DEFINE_CONSTANT(uint8_t, mcg, multiplier, 217U)
883 PCG_DEFINE_CONSTANT(uint8_t, mcg, unmultiplier, 105U)
884
885 PCG_DEFINE_CONSTANT(uint16_t, mcg, multiplier, 62169U)
886 PCG_DEFINE_CONSTANT(uint16_t, mcg, unmultiplier, 28009U)
887
888 PCG_DEFINE_CONSTANT(uint32_t, mcg, multiplier, 277803737U)
889 PCG_DEFINE_CONSTANT(uint32_t, mcg, unmultiplier, 2897767785U)
890
891 PCG_DEFINE_CONSTANT(uint64_t, mcg, multiplier, 12605985483714917081ULL)
892 PCG_DEFINE_CONSTANT(uint64_t, mcg, unmultiplier, 15009553638781119849ULL)
893
894 PCG_DEFINE_CONSTANT(pcg128_t, mcg, multiplier,
895 PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL))
896 PCG_DEFINE_CONSTANT(pcg128_t, mcg, unmultiplier,
897 PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL))
898
899
900 template <typename xtype, typename itype>
901 struct rxs_m_xs_mixin {
outputpcg_detail::rxs_m_xs_mixin902 static xtype output(itype internal)
903 {
904 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
905 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
906 constexpr bitcount_t opbits = xtypebits >= 128 ? 6
907 : xtypebits >= 64 ? 5
908 : xtypebits >= 32 ? 4
909 : xtypebits >= 16 ? 3
910 : 2;
911 constexpr bitcount_t shift = bits - xtypebits;
912 constexpr bitcount_t mask = (1 << opbits) - 1;
913 bitcount_t rshift =
914 opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
915 internal ^= internal >> (opbits + rshift);
916 internal *= mcg_multiplier<itype>::multiplier();
917 xtype result = internal >> shift;
918 result ^= result >> ((2U*xtypebits+2U)/3U);
919 return result;
920 }
921
unoutputpcg_detail::rxs_m_xs_mixin922 static itype unoutput(itype internal)
923 {
924 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
925 constexpr bitcount_t opbits = bits >= 128 ? 6
926 : bits >= 64 ? 5
927 : bits >= 32 ? 4
928 : bits >= 16 ? 3
929 : 2;
930 constexpr bitcount_t mask = (1 << opbits) - 1;
931
932 internal = unxorshift(internal, bits, (2U*bits+2U)/3U);
933
934 internal *= mcg_unmultiplier<itype>::unmultiplier();
935
936 bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
937 internal = unxorshift(internal, bits, opbits + rshift);
938
939 return internal;
940 }
941 };
942
943
944 /*
945 * RXS M -- random xorshift, mcg multiply
946 */
947
948 template <typename xtype, typename itype>
949 struct rxs_m_mixin {
outputpcg_detail::rxs_m_mixin950 static xtype output(itype internal)
951 {
952 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
953 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
954 constexpr bitcount_t opbits = xtypebits >= 128 ? 6
955 : xtypebits >= 64 ? 5
956 : xtypebits >= 32 ? 4
957 : xtypebits >= 16 ? 3
958 : 2;
959 constexpr bitcount_t shift = bits - xtypebits;
960 constexpr bitcount_t mask = (1 << opbits) - 1;
961 bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
962 internal ^= internal >> (opbits + rshift);
963 internal *= mcg_multiplier<itype>::multiplier();
964 xtype result = internal >> shift;
965 return result;
966 }
967 };
968
969 /*
970 * XSL RR -- fixed xorshift (to low bits), random rotate
971 *
972 * Useful for 128-bit types that are split across two CPU registers.
973 */
974
975 template <typename xtype, typename itype>
976 struct xsl_rr_mixin {
outputpcg_detail::xsl_rr_mixin977 static xtype output(itype internal)
978 {
979 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
980 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
981 constexpr bitcount_t sparebits = bits - xtypebits;
982 constexpr bitcount_t wantedopbits = xtypebits >= 128 ? 7
983 : xtypebits >= 64 ? 6
984 : xtypebits >= 32 ? 5
985 : xtypebits >= 16 ? 4
986 : 3;
987 constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
988 : sparebits;
989 constexpr bitcount_t amplifier = wantedopbits - opbits;
990 constexpr bitcount_t mask = (1 << opbits) - 1;
991 constexpr bitcount_t topspare = sparebits;
992 constexpr bitcount_t bottomspare = sparebits - topspare;
993 constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
994
995 bitcount_t rot =
996 opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
997 bitcount_t amprot = (rot << amplifier) & mask;
998 internal ^= internal >> xshift;
999 xtype result = xtype(internal >> bottomspare);
1000 result = rotr(result, amprot);
1001 return result;
1002 }
1003 };
1004
1005
1006 /*
1007 * XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts)
1008 *
1009 * Useful for 128-bit types that are split across two CPU registers.
1010 * If you really want an invertable 128-bit RNG, I guess this is the one.
1011 */
1012
1013 template <typename T> struct halfsize_trait {};
1014 template <> struct halfsize_trait<pcg128_t> { typedef uint64_t type; };
1015 template <> struct halfsize_trait<uint64_t> { typedef uint32_t type; };
1016 template <> struct halfsize_trait<uint32_t> { typedef uint16_t type; };
1017 template <> struct halfsize_trait<uint16_t> { typedef uint8_t type; };
1018
1019 template <typename xtype, typename itype>
1020 struct xsl_rr_rr_mixin {
1021 typedef typename halfsize_trait<itype>::type htype;
1022
outputpcg_detail::xsl_rr_rr_mixin1023 static itype output(itype internal)
1024 {
1025 constexpr bitcount_t htypebits = bitcount_t(sizeof(htype) * 8);
1026 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
1027 constexpr bitcount_t sparebits = bits - htypebits;
1028 constexpr bitcount_t wantedopbits = htypebits >= 128 ? 7
1029 : htypebits >= 64 ? 6
1030 : htypebits >= 32 ? 5
1031 : htypebits >= 16 ? 4
1032 : 3;
1033 constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
1034 : sparebits;
1035 constexpr bitcount_t amplifier = wantedopbits - opbits;
1036 constexpr bitcount_t mask = (1 << opbits) - 1;
1037 constexpr bitcount_t topspare = sparebits;
1038 constexpr bitcount_t xshift = (topspare + htypebits) / 2;
1039
1040 bitcount_t rot =
1041 opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
1042 bitcount_t amprot = (rot << amplifier) & mask;
1043 internal ^= internal >> xshift;
1044 htype lowbits = htype(internal);
1045 lowbits = rotr(lowbits, amprot);
1046 htype highbits = htype(internal >> topspare);
1047 bitcount_t rot2 = lowbits & mask;
1048 bitcount_t amprot2 = (rot2 << amplifier) & mask;
1049 highbits = rotr(highbits, amprot2);
1050 return (itype(highbits) << topspare) ^ itype(lowbits);
1051 }
1052 };
1053
1054
1055 /*
1056 * XSH -- fixed xorshift (to high bits)
1057 *
1058 * You shouldn't use this at 64-bits or less.
1059 */
1060
1061 template <typename xtype, typename itype>
1062 struct xsh_mixin {
outputpcg_detail::xsh_mixin1063 static xtype output(itype internal)
1064 {
1065 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
1066 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
1067 constexpr bitcount_t sparebits = bits - xtypebits;
1068 constexpr bitcount_t topspare = 0;
1069 constexpr bitcount_t bottomspare = sparebits - topspare;
1070 constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
1071
1072 internal ^= internal >> xshift;
1073 xtype result = internal >> bottomspare;
1074 return result;
1075 }
1076 };
1077
1078 /*
1079 * XSL -- fixed xorshift (to low bits)
1080 *
1081 * You shouldn't use this at 64-bits or less.
1082 */
1083
1084 template <typename xtype, typename itype>
1085 struct xsl_mixin {
outputpcg_detail::xsl_mixin1086 inline xtype output(itype internal)
1087 {
1088 constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
1089 constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
1090 constexpr bitcount_t sparebits = bits - xtypebits;
1091 constexpr bitcount_t topspare = sparebits;
1092 constexpr bitcount_t bottomspare = sparebits - topspare;
1093 constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
1094
1095 internal ^= internal >> xshift;
1096 xtype result = internal >> bottomspare;
1097 return result;
1098 }
1099 };
1100
1101 /* ---- End of Output Functions ---- */
1102
1103
1104 template <typename baseclass>
1105 struct inside_out : private baseclass {
1106 inside_out() = delete;
1107
1108 typedef typename baseclass::result_type result_type;
1109 typedef typename baseclass::state_type state_type;
1110 static_assert(sizeof(result_type) == sizeof(state_type),
1111 "Require a RNG whose output function is a permutation");
1112
external_steppcg_detail::inside_out1113 static bool external_step(result_type& randval, size_t i)
1114 {
1115 state_type state = baseclass::unoutput(randval);
1116 state = state * baseclass::multiplier() + baseclass::increment()
1117 + state_type(i*2);
1118 result_type result = baseclass::output(state);
1119 randval = result;
1120 state_type zero =
1121 baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
1122 return result == zero;
1123 }
1124
external_advancepcg_detail::inside_out1125 static bool external_advance(result_type& randval, size_t i,
1126 result_type delta, bool forwards = true)
1127 {
1128 state_type state = baseclass::unoutput(randval);
1129 state_type mult = baseclass::multiplier();
1130 state_type inc = baseclass::increment() + state_type(i*2);
1131 state_type zero =
1132 baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
1133 state_type dist_to_zero = baseclass::distance(state, zero, mult, inc);
1134 bool crosses_zero =
1135 forwards ? dist_to_zero <= delta
1136 : (-dist_to_zero) <= delta;
1137 if (!forwards)
1138 delta = -delta;
1139 state = baseclass::advance(state, delta, mult, inc);
1140 randval = baseclass::output(state);
1141 return crosses_zero;
1142 }
1143 };
1144
1145
1146 template <bitcount_t table_pow2, bitcount_t advance_pow2, typename baseclass, typename extvalclass, bool kdd = true>
1147 class extended : public baseclass {
1148 public:
1149 typedef typename baseclass::state_type state_type;
1150 typedef typename baseclass::result_type result_type;
1151 typedef inside_out<extvalclass> insideout;
1152
1153 private:
1154 static constexpr bitcount_t rtypebits = sizeof(result_type)*8;
1155 static constexpr bitcount_t stypebits = sizeof(state_type)*8;
1156
1157 static constexpr bitcount_t tick_limit_pow2 = 64U;
1158
1159 static constexpr size_t table_size = 1UL << table_pow2;
1160 static constexpr size_t table_shift = stypebits - table_pow2;
1161 static constexpr state_type table_mask =
1162 (state_type(1U) << table_pow2) - state_type(1U);
1163
1164 static constexpr bool may_tick =
1165 (advance_pow2 < stypebits) && (advance_pow2 < tick_limit_pow2);
1166 static constexpr size_t tick_shift = stypebits - advance_pow2;
1167 static constexpr state_type tick_mask =
1168 may_tick ? state_type(
1169 (uint64_t(1) << (advance_pow2*may_tick)) - 1)
1170 // ^-- stupidity to appease GCC warnings
1171 : ~state_type(0U);
1172
1173 static constexpr bool may_tock = stypebits < tick_limit_pow2;
1174
1175 result_type data_[table_size];
1176
1177 PCG_NOINLINE void advance_table();
1178
1179 PCG_NOINLINE void advance_table(state_type delta, bool isForwards = true);
1180
get_extended_value()1181 result_type& get_extended_value()
1182 {
1183 state_type state = this->state_;
1184 if (kdd && baseclass::is_mcg) {
1185 // The low order bits of an MCG are constant, so drop them.
1186 state >>= 2;
1187 }
1188 size_t index = kdd ? state & table_mask
1189 : state >> table_shift;
1190
1191 if (may_tick) {
1192 bool tick = kdd ? (state & tick_mask) == state_type(0u)
1193 : (state >> tick_shift) == state_type(0u);
1194 if (tick)
1195 advance_table();
1196 }
1197 if (may_tock) {
1198 bool tock = state == state_type(0u);
1199 if (tock)
1200 advance_table();
1201 }
1202 return data_[index];
1203 }
1204
1205 public:
period_pow2()1206 static constexpr size_t period_pow2()
1207 {
1208 return baseclass::period_pow2() + table_size*extvalclass::period_pow2();
1209 }
1210
operator ()()1211 PCG_ALWAYS_INLINE result_type operator()()
1212 {
1213 result_type rhs = get_extended_value();
1214 result_type lhs = this->baseclass::operator()();
1215 return lhs ^ rhs;
1216 }
1217
operator ()(result_type upper_bound)1218 result_type operator()(result_type upper_bound)
1219 {
1220 return bounded_rand(*this, upper_bound);
1221 }
1222
set(result_type wanted)1223 void set(result_type wanted)
1224 {
1225 result_type& rhs = get_extended_value();
1226 result_type lhs = this->baseclass::operator()();
1227 rhs = lhs ^ wanted;
1228 }
1229
1230 void advance(state_type distance, bool forwards = true);
1231
backstep(state_type distance)1232 void backstep(state_type distance)
1233 {
1234 advance(distance, false);
1235 }
1236
extended(const result_type * data)1237 extended(const result_type* data)
1238 : baseclass()
1239 {
1240 datainit(data);
1241 }
1242
extended(const result_type * data,state_type seed)1243 extended(const result_type* data, state_type seed)
1244 : baseclass(seed)
1245 {
1246 datainit(data);
1247 }
1248
1249 // This function may or may not exist. It thus has to be a template
1250 // to use SFINAE; users don't have to worry about its template-ness.
1251
1252 template <typename bc = baseclass>
extended(const result_type * data,state_type seed,typename bc::stream_state stream_seed)1253 extended(const result_type* data, state_type seed,
1254 typename bc::stream_state stream_seed)
1255 : baseclass(seed, stream_seed)
1256 {
1257 datainit(data);
1258 }
1259
extended()1260 extended()
1261 : baseclass()
1262 {
1263 selfinit();
1264 }
1265
extended(state_type seed)1266 extended(state_type seed)
1267 : baseclass(seed)
1268 {
1269 selfinit();
1270 }
1271
1272 // This function may or may not exist. It thus has to be a template
1273 // to use SFINAE; users don't have to worry about its template-ness.
1274
1275 template <typename bc = baseclass>
extended(state_type seed,typename bc::stream_state stream_seed)1276 extended(state_type seed, typename bc::stream_state stream_seed)
1277 : baseclass(seed, stream_seed)
1278 {
1279 selfinit();
1280 }
1281
1282 private:
1283 void selfinit();
1284 void datainit(const result_type* data);
1285
1286 public:
1287
1288 template<typename SeedSeq, typename = typename std::enable_if<
1289 !std::is_convertible<SeedSeq, result_type>::value
1290 && !std::is_convertible<SeedSeq, extended>::value>::type>
extended(SeedSeq && seedSeq)1291 extended(SeedSeq&& seedSeq)
1292 : baseclass(seedSeq)
1293 {
1294 generate_to<table_size>(seedSeq, data_);
1295 }
1296
1297 template<typename... Args>
seed(Args &&...args)1298 void seed(Args&&... args)
1299 {
1300 new (this) extended(std::forward<Args>(args)...);
1301 }
1302
1303 template <bitcount_t table_pow2_, bitcount_t advance_pow2_,
1304 typename baseclass_, typename extvalclass_, bool kdd_>
1305 friend bool operator==(const extended<table_pow2_, advance_pow2_,
1306 baseclass_, extvalclass_, kdd_>&,
1307 const extended<table_pow2_, advance_pow2_,
1308 baseclass_, extvalclass_, kdd_>&);
1309
1310 template <typename CharT, typename Traits,
1311 bitcount_t table_pow2_, bitcount_t advance_pow2_,
1312 typename baseclass_, typename extvalclass_, bool kdd_>
1313 friend std::basic_ostream<CharT,Traits>&
1314 operator<<(std::basic_ostream<CharT,Traits>& out,
1315 const extended<table_pow2_, advance_pow2_,
1316 baseclass_, extvalclass_, kdd_>&);
1317
1318 template <typename CharT, typename Traits,
1319 bitcount_t table_pow2_, bitcount_t advance_pow2_,
1320 typename baseclass_, typename extvalclass_, bool kdd_>
1321 friend std::basic_istream<CharT,Traits>&
1322 operator>>(std::basic_istream<CharT,Traits>& in,
1323 extended<table_pow2_, advance_pow2_,
1324 baseclass_, extvalclass_, kdd_>&);
1325
1326 };
1327
1328
1329 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1330 typename baseclass, typename extvalclass, bool kdd>
datainit(const result_type * data)1331 void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::datainit(
1332 const result_type* data)
1333 {
1334 for (size_t i = 0; i < table_size; ++i)
1335 data_[i] = data[i];
1336 }
1337
1338 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1339 typename baseclass, typename extvalclass, bool kdd>
selfinit()1340 void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit()
1341 {
1342 // We need to fill the extended table with something, and we have
1343 // very little provided data, so we use the base generator to
1344 // produce values. Although not ideal (use a seed sequence, folks!),
1345 // unexpected correlations are mitigated by
1346 // - using XOR differences rather than the number directly
1347 // - the way the table is accessed, its values *won't* be accessed
1348 // in the same order the were written.
1349 // - any strange correlations would only be apparent if we
1350 // were to backstep the generator so that the base generator
1351 // was generating the same values again
1352 result_type xdiff = baseclass::operator()() - baseclass::operator()();
1353 for (size_t i = 0; i < table_size; ++i) {
1354 data_[i] = baseclass::operator()() ^ xdiff;
1355 }
1356 }
1357
1358 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1359 typename baseclass, typename extvalclass, bool kdd>
operator ==(const extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & lhs,const extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & rhs)1360 bool operator==(const extended<table_pow2, advance_pow2,
1361 baseclass, extvalclass, kdd>& lhs,
1362 const extended<table_pow2, advance_pow2,
1363 baseclass, extvalclass, kdd>& rhs)
1364 {
1365 auto& base_lhs = static_cast<const baseclass&>(lhs);
1366 auto& base_rhs = static_cast<const baseclass&>(rhs);
1367 return base_lhs == base_rhs
1368 && !memcmp((void*) lhs.data_, (void*) rhs.data_, sizeof(lhs.data_));
1369 }
1370
1371 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1372 typename baseclass, typename extvalclass, bool kdd>
operator !=(const extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & lhs,const extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & rhs)1373 inline bool operator!=(const extended<table_pow2, advance_pow2,
1374 baseclass, extvalclass, kdd>& lhs,
1375 const extended<table_pow2, advance_pow2,
1376 baseclass, extvalclass, kdd>& rhs)
1377 {
1378 return lhs != rhs;
1379 }
1380
1381 template <typename CharT, typename Traits,
1382 bitcount_t table_pow2, bitcount_t advance_pow2,
1383 typename baseclass, typename extvalclass, bool kdd>
1384 std::basic_ostream<CharT,Traits>&
operator <<(std::basic_ostream<CharT,Traits> & out,const extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & rng)1385 operator<<(std::basic_ostream<CharT,Traits>& out,
1386 const extended<table_pow2, advance_pow2,
1387 baseclass, extvalclass, kdd>& rng)
1388 {
1389 auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
1390 auto space = out.widen(' ');
1391 auto orig_fill = out.fill();
1392
1393 out << rng.multiplier() << space
1394 << rng.increment() << space
1395 << rng.state_;
1396
1397 for (const auto& datum : rng.data_)
1398 out << space << datum;
1399
1400 out.flags(orig_flags);
1401 out.fill(orig_fill);
1402 return out;
1403 }
1404
1405 template <typename CharT, typename Traits,
1406 bitcount_t table_pow2, bitcount_t advance_pow2,
1407 typename baseclass, typename extvalclass, bool kdd>
1408 std::basic_istream<CharT,Traits>&
operator >>(std::basic_istream<CharT,Traits> & in,extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd> & rng)1409 operator>>(std::basic_istream<CharT,Traits>& in,
1410 extended<table_pow2, advance_pow2,
1411 baseclass, extvalclass, kdd>& rng)
1412 {
1413 extended<table_pow2, advance_pow2, baseclass, extvalclass> new_rng;
1414 auto& base_rng = static_cast<baseclass&>(new_rng);
1415 in >> base_rng;
1416
1417 if (in.fail())
1418 return in;
1419
1420 auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
1421
1422 for (auto& datum : new_rng.data_) {
1423 in >> datum;
1424 if (in.fail())
1425 goto bail;
1426 }
1427
1428 rng = new_rng;
1429
1430 bail:
1431 in.flags(orig_flags);
1432 return in;
1433 }
1434
1435
1436
1437 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1438 typename baseclass, typename extvalclass, bool kdd>
1439 void
advance_table()1440 extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table()
1441 {
1442 bool carry = false;
1443 for (size_t i = 0; i < table_size; ++i) {
1444 if (carry) {
1445 carry = insideout::external_step(data_[i],i+1);
1446 }
1447 bool carry2 = insideout::external_step(data_[i],i+1);
1448 carry = carry || carry2;
1449 }
1450 }
1451
1452 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1453 typename baseclass, typename extvalclass, bool kdd>
1454 void
advance_table(state_type delta,bool isForwards)1455 extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table(
1456 state_type delta, bool isForwards)
1457 {
1458 typedef typename baseclass::state_type base_state_t;
1459 typedef typename extvalclass::state_type ext_state_t;
1460 constexpr bitcount_t basebits = sizeof(base_state_t)*8;
1461 constexpr bitcount_t extbits = sizeof(ext_state_t)*8;
1462 static_assert(basebits <= extbits || advance_pow2 > 0,
1463 "Current implementation might overflow its carry");
1464
1465 base_state_t carry = 0;
1466 for (size_t i = 0; i < table_size; ++i) {
1467 base_state_t total_delta = carry + delta;
1468 ext_state_t trunc_delta = ext_state_t(total_delta);
1469 if (basebits > extbits) {
1470 carry = total_delta >> extbits;
1471 } else {
1472 carry = 0;
1473 }
1474 carry +=
1475 insideout::external_advance(data_[i],i+1, trunc_delta, isForwards);
1476 }
1477 }
1478
1479 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1480 typename baseclass, typename extvalclass, bool kdd>
advance(state_type distance,bool forwards)1481 void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance(
1482 state_type distance, bool forwards)
1483 {
1484 static_assert(kdd,
1485 "Efficient advance is too hard for non-kdd extension. "
1486 "For a weak advance, cast to base class");
1487 state_type zero =
1488 baseclass::is_mcg ? this->state_ & state_type(3U) : state_type(0U);
1489 if (may_tick) {
1490 state_type ticks = distance >> (advance_pow2*may_tick);
1491 // ^-- stupidity to appease GCC
1492 // warnings
1493 state_type adv_mask =
1494 baseclass::is_mcg ? tick_mask << 2 : tick_mask;
1495 state_type next_advance_distance = this->distance(zero, adv_mask);
1496 if (!forwards)
1497 next_advance_distance = (-next_advance_distance) & tick_mask;
1498 if (next_advance_distance < (distance & tick_mask)) {
1499 ++ticks;
1500 }
1501 if (ticks)
1502 advance_table(ticks, forwards);
1503 }
1504 if (forwards) {
1505 if (may_tock && this->distance(zero) <= distance)
1506 advance_table();
1507 baseclass::advance(distance);
1508 } else {
1509 if (may_tock && -(this->distance(zero)) <= distance)
1510 advance_table(state_type(1U), false);
1511 baseclass::advance(-distance);
1512 }
1513 }
1514
1515 } // namespace pcg_detail
1516
1517 namespace pcg_engines {
1518
1519 using namespace pcg_detail;
1520
1521 /* Predefined types for XSH RS */
1522
1523 typedef oneseq_base<uint8_t, uint16_t, xsh_rs_mixin> oneseq_xsh_rs_16_8;
1524 typedef oneseq_base<uint16_t, uint32_t, xsh_rs_mixin> oneseq_xsh_rs_32_16;
1525 typedef oneseq_base<uint32_t, uint64_t, xsh_rs_mixin> oneseq_xsh_rs_64_32;
1526 typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin> oneseq_xsh_rs_128_64;
1527
1528 typedef unique_base<uint8_t, uint16_t, xsh_rs_mixin> unique_xsh_rs_16_8;
1529 typedef unique_base<uint16_t, uint32_t, xsh_rs_mixin> unique_xsh_rs_32_16;
1530 typedef unique_base<uint32_t, uint64_t, xsh_rs_mixin> unique_xsh_rs_64_32;
1531 typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin> unique_xsh_rs_128_64;
1532
1533 typedef setseq_base<uint8_t, uint16_t, xsh_rs_mixin> setseq_xsh_rs_16_8;
1534 typedef setseq_base<uint16_t, uint32_t, xsh_rs_mixin> setseq_xsh_rs_32_16;
1535 typedef setseq_base<uint32_t, uint64_t, xsh_rs_mixin> setseq_xsh_rs_64_32;
1536 typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin> setseq_xsh_rs_128_64;
1537
1538 typedef mcg_base<uint8_t, uint16_t, xsh_rs_mixin> mcg_xsh_rs_16_8;
1539 typedef mcg_base<uint16_t, uint32_t, xsh_rs_mixin> mcg_xsh_rs_32_16;
1540 typedef mcg_base<uint32_t, uint64_t, xsh_rs_mixin> mcg_xsh_rs_64_32;
1541 typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin> mcg_xsh_rs_128_64;
1542
1543 /* Predefined types for XSH RR */
1544
1545 typedef oneseq_base<uint8_t, uint16_t, xsh_rr_mixin> oneseq_xsh_rr_16_8;
1546 typedef oneseq_base<uint16_t, uint32_t, xsh_rr_mixin> oneseq_xsh_rr_32_16;
1547 typedef oneseq_base<uint32_t, uint64_t, xsh_rr_mixin> oneseq_xsh_rr_64_32;
1548 typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin> oneseq_xsh_rr_128_64;
1549
1550 typedef unique_base<uint8_t, uint16_t, xsh_rr_mixin> unique_xsh_rr_16_8;
1551 typedef unique_base<uint16_t, uint32_t, xsh_rr_mixin> unique_xsh_rr_32_16;
1552 typedef unique_base<uint32_t, uint64_t, xsh_rr_mixin> unique_xsh_rr_64_32;
1553 typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin> unique_xsh_rr_128_64;
1554
1555 typedef setseq_base<uint8_t, uint16_t, xsh_rr_mixin> setseq_xsh_rr_16_8;
1556 typedef setseq_base<uint16_t, uint32_t, xsh_rr_mixin> setseq_xsh_rr_32_16;
1557 typedef setseq_base<uint32_t, uint64_t, xsh_rr_mixin> setseq_xsh_rr_64_32;
1558 typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin> setseq_xsh_rr_128_64;
1559
1560 typedef mcg_base<uint8_t, uint16_t, xsh_rr_mixin> mcg_xsh_rr_16_8;
1561 typedef mcg_base<uint16_t, uint32_t, xsh_rr_mixin> mcg_xsh_rr_32_16;
1562 typedef mcg_base<uint32_t, uint64_t, xsh_rr_mixin> mcg_xsh_rr_64_32;
1563 typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin> mcg_xsh_rr_128_64;
1564
1565
1566 /* Predefined types for RXS M XS */
1567
1568 typedef oneseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_8_8;
1569 typedef oneseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_16_16;
1570 typedef oneseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_32_32;
1571 typedef oneseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_64_64;
1572 typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_128_128;
1573
1574 typedef unique_base<uint8_t, uint8_t, rxs_m_xs_mixin> unique_rxs_m_xs_8_8;
1575 typedef unique_base<uint16_t, uint16_t, rxs_m_xs_mixin> unique_rxs_m_xs_16_16;
1576 typedef unique_base<uint32_t, uint32_t, rxs_m_xs_mixin> unique_rxs_m_xs_32_32;
1577 typedef unique_base<uint64_t, uint64_t, rxs_m_xs_mixin> unique_rxs_m_xs_64_64;
1578 typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> unique_rxs_m_xs_128_128;
1579
1580 typedef setseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> setseq_rxs_m_xs_8_8;
1581 typedef setseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> setseq_rxs_m_xs_16_16;
1582 typedef setseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> setseq_rxs_m_xs_32_32;
1583 typedef setseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> setseq_rxs_m_xs_64_64;
1584 typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> setseq_rxs_m_xs_128_128;
1585
1586 // MCG versions don't make sense here, so aren't defined.
1587
1588 /* Predefined types for XSL RR (only defined for "large" types) */
1589
1590 typedef oneseq_base<uint32_t, uint64_t, xsl_rr_mixin> oneseq_xsl_rr_64_32;
1591 typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin> oneseq_xsl_rr_128_64;
1592
1593 typedef unique_base<uint32_t, uint64_t, xsl_rr_mixin> unique_xsl_rr_64_32;
1594 typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin> unique_xsl_rr_128_64;
1595
1596 typedef setseq_base<uint32_t, uint64_t, xsl_rr_mixin> setseq_xsl_rr_64_32;
1597 typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin> setseq_xsl_rr_128_64;
1598
1599 typedef mcg_base<uint32_t, uint64_t, xsl_rr_mixin> mcg_xsl_rr_64_32;
1600 typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin> mcg_xsl_rr_128_64;
1601
1602
1603 /* Predefined types for XSL RR RR (only defined for "large" types) */
1604
1605 typedef oneseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
1606 oneseq_xsl_rr_rr_64_64;
1607 typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
1608 oneseq_xsl_rr_rr_128_128;
1609
1610 typedef unique_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
1611 unique_xsl_rr_rr_64_64;
1612 typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
1613 unique_xsl_rr_rr_128_128;
1614
1615 typedef setseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
1616 setseq_xsl_rr_rr_64_64;
1617 typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
1618 setseq_xsl_rr_rr_128_128;
1619
1620 // MCG versions don't make sense here, so aren't defined.
1621
1622 /* Extended generators */
1623
1624 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1625 typename BaseRNG, bool kdd = true>
1626 using ext_std8 = extended<table_pow2, advance_pow2, BaseRNG,
1627 oneseq_rxs_m_xs_8_8, kdd>;
1628
1629 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1630 typename BaseRNG, bool kdd = true>
1631 using ext_std16 = extended<table_pow2, advance_pow2, BaseRNG,
1632 oneseq_rxs_m_xs_16_16, kdd>;
1633
1634 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1635 typename BaseRNG, bool kdd = true>
1636 using ext_std32 = extended<table_pow2, advance_pow2, BaseRNG,
1637 oneseq_rxs_m_xs_32_32, kdd>;
1638
1639 template <bitcount_t table_pow2, bitcount_t advance_pow2,
1640 typename BaseRNG, bool kdd = true>
1641 using ext_std64 = extended<table_pow2, advance_pow2, BaseRNG,
1642 oneseq_rxs_m_xs_64_64, kdd>;
1643
1644
1645 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1646 using ext_oneseq_rxs_m_xs_32_32 =
1647 ext_std32<table_pow2, advance_pow2, oneseq_rxs_m_xs_32_32, kdd>;
1648
1649 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1650 using ext_mcg_xsh_rs_64_32 =
1651 ext_std32<table_pow2, advance_pow2, mcg_xsh_rs_64_32, kdd>;
1652
1653 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1654 using ext_oneseq_xsh_rs_64_32 =
1655 ext_std32<table_pow2, advance_pow2, oneseq_xsh_rs_64_32, kdd>;
1656
1657 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1658 using ext_setseq_xsh_rr_64_32 =
1659 ext_std32<table_pow2, advance_pow2, setseq_xsh_rr_64_32, kdd>;
1660
1661 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1662 using ext_mcg_xsl_rr_128_64 =
1663 ext_std64<table_pow2, advance_pow2, mcg_xsl_rr_128_64, kdd>;
1664
1665 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1666 using ext_oneseq_xsl_rr_128_64 =
1667 ext_std64<table_pow2, advance_pow2, oneseq_xsl_rr_128_64, kdd>;
1668
1669 template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
1670 using ext_setseq_xsl_rr_128_64 =
1671 ext_std64<table_pow2, advance_pow2, setseq_xsl_rr_128_64, kdd>;
1672
1673 } // namespace pcg_engines
1674
1675 typedef pcg_engines::setseq_xsh_rr_64_32 pcg32;
1676 typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq;
1677 typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique;
1678 typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast;
1679
1680 typedef pcg_engines::setseq_xsl_rr_128_64 pcg64;
1681 typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq;
1682 typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique;
1683 typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast;
1684
1685 typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure;
1686 typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure;
1687 typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure;
1688 typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure;
1689 typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure;
1690
1691 typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure;
1692 typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure;
1693 typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure;
1694 typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure;
1695 typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure;
1696
1697
1698 // These two extended RNGs provide two-dimensionally equidistributed
1699 // 32-bit generators. pcg32_k2_fast occupies the same space as pcg64,
1700 // and can be called twice to generate 64 bits, but does not required
1701 // 128-bit math; on 32-bit systems, it's faster than pcg64 as well.
1702
1703 typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k2;
1704 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k2_fast;
1705
1706 // These eight extended RNGs have about as much state as arc4random
1707 //
1708 // - the k variants are k-dimensionally equidistributed
1709 // - the c variants offer better crypographic security
1710 //
1711 // (just how good the cryptographic security is is an open question)
1712
1713 typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k64;
1714 typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,true> pcg32_k64_oneseq;
1715 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k64_fast;
1716
1717 typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,false> pcg32_c64;
1718 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,false> pcg32_c64_oneseq;
1719 typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,false> pcg32_c64_fast;
1720
1721 typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,true> pcg64_k32;
1722 typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,true> pcg64_k32_oneseq;
1723 typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,true> pcg64_k32_fast;
1724
1725 typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,false> pcg64_c32;
1726 typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,false> pcg64_c32_oneseq;
1727 typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,false> pcg64_c32_fast;
1728
1729 // These eight extended RNGs have more state than the Mersenne twister
1730 //
1731 // - the k variants are k-dimensionally equidistributed
1732 // - the c variants offer better crypographic security
1733 //
1734 // (just how good the cryptographic security is is an open question)
1735
1736 typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,true> pcg32_k1024;
1737 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,true> pcg32_k1024_fast;
1738
1739 typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,false> pcg32_c1024;
1740 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,false> pcg32_c1024_fast;
1741
1742 typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,true> pcg64_k1024;
1743 typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,true> pcg64_k1024_fast;
1744
1745 typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,false> pcg64_c1024;
1746 typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,false> pcg64_c1024_fast;
1747
1748 // These generators have an insanely huge period (2^524352), and is suitable
1749 // for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary
1750 // point in the future. [Actually, over the full period of the generator, it
1751 // will produce every 64 KB ZIP file 2^64 times!]
1752
1753 typedef pcg_engines::ext_setseq_xsh_rr_64_32<14,16,true> pcg32_k16384;
1754 typedef pcg_engines::ext_oneseq_xsh_rs_64_32<14,32,true> pcg32_k16384_fast;
1755
1756 #endif // PCG_RAND_HPP_INCLUDED
1757