1/*
2 * PCG Random Number Generation for C++
3 *
4 * Copyright 2014-2017 Melissa O'Neill <oneill@pcg-random.org>,
5 * and the PCG Project contributors.
6 *
7 * SPDX-License-Identifier: (Apache-2.0 OR MIT)
8 *
9 * Licensed under the Apache License, Version 2.0 (provided in
10 * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
11 * or under the MIT license (provided in LICENSE-MIT.txt and at
12 * http://opensource.org/licenses/MIT), at your option. This file may not
13 * be copied, modified, or distributed except according to those terms.
14 *
15 * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
16 * express or implied. See your chosen license for details.
17 *
18 * For additional information about the PCG random number generation scheme,
19 * visit http://www.pcg-random.org/.
20 */
21
22/*
23 * This file provides support code that is useful for random-number generation
24 * but not specific to the PCG generation scheme, including:
25 * - 128-bit int support for platforms where it isn't available natively
26 * - bit twiddling operations
27 * - I/O of 128-bit and 8-bit integers
28 * - Handling the evilness of SeedSeq
29 * - Support for efficiently producing random numbers less than a given
30 * bound
31 */
32
33#ifndef PCG_EXTRAS_HPP_INCLUDED
34#define PCG_EXTRAS_HPP_INCLUDED 1
35
36#include <cinttypes>
37#include <cstddef>
38#include <cstdlib>
39#include <cstring>
40#include <cassert>
41#include <limits>
42#include <iostream>
43#include <type_traits>
44#include <utility>
45#include <locale>
46#include <iterator>
47
48#ifdef __GNUC__
49 #include <cxxabi.h>
50#endif
51
52/*
53 * Abstractions for compiler-specific directives
54 */
55
56#ifdef __GNUC__
57 #define PCG_NOINLINE __attribute__((noinline))
58#else
59 #define PCG_NOINLINE
60#endif
61
62/*
63 * Some members of the PCG library use 128-bit math. When compiling on 64-bit
64 * platforms, both GCC and Clang provide 128-bit integer types that are ideal
65 * for the job.
66 *
67 * On 32-bit platforms (or with other compilers), we fall back to a C++
68 * class that provides 128-bit unsigned integers instead. It may seem
69 * like we're reinventing the wheel here, because libraries already exist
70 * that support large integers, but most existing libraries provide a very
71 * generic multiprecision code, but here we're operating at a fixed size.
72 * Also, most other libraries are fairly heavyweight. So we use a direct
73 * implementation. Sadly, it's much slower than hand-coded assembly or
74 * direct CPU support.
75 *
76 */
77#if __SIZEOF_INT128__
78 namespace pcg_extras {
79 typedef __uint128_t pcg128_t;
80 }
81 #define PCG_128BIT_CONSTANT(high,low) \
82 ((pcg128_t(high) << 64) + low)
83#else
84 #include "pcg_uint128.hpp"
85 namespace pcg_extras {
86 typedef pcg_extras::uint_x4<uint32_t,uint64_t> pcg128_t;
87 }
88 #define PCG_128BIT_CONSTANT(high,low) \
89 pcg128_t(high,low)
90 #define PCG_EMULATED_128BIT_MATH 1
91#endif
92
93
94namespace pcg_extras {
95
96/*
97 * We often need to represent a "number of bits". When used normally, these
98 * numbers are never greater than 128, so an unsigned char is plenty.
99 * If you're using a nonstandard generator of a larger size, you can set
100 * PCG_BITCOUNT_T to have it define it as a larger size. (Some compilers
101 * might produce faster code if you set it to an unsigned int.)
102 */
103
104#ifndef PCG_BITCOUNT_T
105 typedef uint8_t bitcount_t;
106#else
107 typedef PCG_BITCOUNT_T bitcount_t;
108#endif
109
110/*
111 * C++ requires us to be able to serialize RNG state by printing or reading
112 * it from a stream. Because we use 128-bit ints, we also need to be able
113 * ot print them, so here is code to do so.
114 *
115 * This code provides enough functionality to print 128-bit ints in decimal
116 * and zero-padded in hex. It's not a full-featured implementation.
117 */
118
119template <typename CharT, typename Traits>
120std::basic_ostream<CharT,Traits>&
121operator<<(std::basic_ostream<CharT,Traits>& out, pcg128_t value)
122{
123 auto desired_base = out.flags() & out.basefield;
124 bool want_hex = desired_base == out.hex;
125
126 if (want_hex) {
127 uint64_t highpart = uint64_t(value >> 64);
128 uint64_t lowpart = uint64_t(value);
129 auto desired_width = out.width();
130 if (desired_width > 16) {
131 out.width(desired_width - 16);
132 }
133 if (highpart != 0 || desired_width > 16)
134 out << highpart;
135 CharT oldfill = '\0';
136 if (highpart != 0) {
137 out.width(16);
138 oldfill = out.fill('0');
139 }
140 auto oldflags = out.setf(decltype(desired_base){}, out.showbase);
141 out << lowpart;
142 out.setf(oldflags);
143 if (highpart != 0) {
144 out.fill(oldfill);
145 }
146 return out;
147 }
148 constexpr size_t MAX_CHARS_128BIT = 40;
149
150 char buffer[MAX_CHARS_128BIT];
151 char* pos = buffer+sizeof(buffer);
152 *(--pos) = '\0';
153 constexpr auto BASE = pcg128_t(10ULL);
154 do {
155 auto div = value / BASE;
156 auto mod = uint32_t(value - (div * BASE));
157 *(--pos) = '0' + char(mod);
158 value = div;
159 } while(value != pcg128_t(0ULL));
160 return out << pos;
161}
162
163template <typename CharT, typename Traits>
164std::basic_istream<CharT,Traits>&
165operator>>(std::basic_istream<CharT,Traits>& in, pcg128_t& value)
166{
167 typename std::basic_istream<CharT,Traits>::sentry s(in);
168
169 if (!s)
170 return in;
171
172 constexpr auto BASE = pcg128_t(10ULL);
173 pcg128_t current(0ULL);
174 bool did_nothing = true;
175 bool overflow = false;
176 for(;;) {
177 CharT wide_ch = in.get();
178 if (!in.good())
179 break;
180 auto ch = in.narrow(wide_ch, '\0');
181 if (ch < '0' || ch > '9') {
182 in.unget();
183 break;
184 }
185 did_nothing = false;
186 pcg128_t digit(uint32_t(ch - '0'));
187 pcg128_t timesbase = current*BASE;
188 overflow = overflow || timesbase < current;
189 current = timesbase + digit;
190 overflow = overflow || current < digit;
191 }
192
193 if (did_nothing || overflow) {
194 in.setstate(std::ios::failbit);
195 if (overflow)
196 current = ~pcg128_t(0ULL);
197 }
198
199 value = current;
200
201 return in;
202}
203
204/*
205 * Likewise, if people use tiny rngs, we'll be serializing uint8_t.
206 * If we just used the provided IO operators, they'd read/write chars,
207 * not ints, so we need to define our own. We *can* redefine this operator
208 * here because we're in our own namespace.
209 */
210
211template <typename CharT, typename Traits>
212std::basic_ostream<CharT,Traits>&
213operator<<(std::basic_ostream<CharT,Traits>&out, uint8_t value)
214{
215 return out << uint32_t(value);
216}
217
218template <typename CharT, typename Traits>
219std::basic_istream<CharT,Traits>&
220operator>>(std::basic_istream<CharT,Traits>& in, uint8_t& target)
221{
222 uint32_t value = 0xdecea5edU;
223 in >> value;
224 if (!in && value == 0xdecea5edU)
225 return in;
226 if (value > uint8_t(~0)) {
227 in.setstate(std::ios::failbit);
228 value = ~0U;
229 }
230 target = uint8_t(value);
231 return in;
232}
233
234/* Unfortunately, the above functions don't get found in preference to the
235 * built in ones, so we create some more specific overloads that will.
236 * Ugh.
237 */
238
239inline std::ostream& operator<<(std::ostream& out, uint8_t value)
240{
241 return pcg_extras::operator<< <char>(out, value);
242}
243
244inline std::istream& operator>>(std::istream& in, uint8_t& value)
245{
246 return pcg_extras::operator>> <char>(in, value);
247}
248
249
250
251/*
252 * Useful bitwise operations.
253 */
254
255/*
256 * XorShifts are invertable, but they are someting of a pain to invert.
257 * This function backs them out. It's used by the whacky "inside out"
258 * generator defined later.
259 */
260
261template <typename itype>
262inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift)
263{
264 if (2*shift >= bits) {
265 return x ^ (x >> shift);
266 }
267 itype lowmask1 = (itype(1U) << (bits - shift*2)) - 1;
268 itype highmask1 = ~lowmask1;
269 itype top1 = x;
270 itype bottom1 = x & lowmask1;
271 top1 ^= top1 >> shift;
272 top1 &= highmask1;
273 x = top1 | bottom1;
274 itype lowmask2 = (itype(1U) << (bits - shift)) - 1;
275 itype bottom2 = x & lowmask2;
276 bottom2 = unxorshift(bottom2, bits - shift, shift);
277 bottom2 &= lowmask1;
278 return top1 | bottom2;
279}
280
281/*
282 * Rotate left and right.
283 *
284 * In ideal world, compilers would spot idiomatic rotate code and convert it
285 * to a rotate instruction. Of course, opinions vary on what the correct
286 * idiom is and how to spot it. For clang, sometimes it generates better
287 * (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
288 */
289
290template <typename itype>
291inline itype rotl(itype value, bitcount_t rot)
292{
293 constexpr bitcount_t bits = sizeof(itype) * 8;
294 constexpr bitcount_t mask = bits - 1;
295#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
296 return rot ? (value << rot) | (value >> (bits - rot)) : value;
297#else
298 return (value << rot) | (value >> ((- rot) & mask));
299#endif
300}
301
302template <typename itype>
303inline itype rotr(itype value, bitcount_t rot)
304{
305 constexpr bitcount_t bits = sizeof(itype) * 8;
306 constexpr bitcount_t mask = bits - 1;
307#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
308 return rot ? (value >> rot) | (value << (bits - rot)) : value;
309#else
310 return (value >> rot) | (value << ((- rot) & mask));
311#endif
312}
313
314/* Unfortunately, both Clang and GCC sometimes perform poorly when it comes
315 * to properly recognizing idiomatic rotate code, so for we also provide
316 * assembler directives (enabled with PCG_USE_INLINE_ASM). Boo, hiss.
317 * (I hope that these compilers get better so that this code can die.)
318 *
319 * These overloads will be preferred over the general template code above.
320 */
321#if PCG_USE_INLINE_ASM && __GNUC__ && (__x86_64__ || __i386__)
322
323inline uint8_t rotr(uint8_t value, bitcount_t rot)
324{
325 asm ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
326 return value;
327}
328
329inline uint16_t rotr(uint16_t value, bitcount_t rot)
330{
331 asm ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
332 return value;
333}
334
335inline uint32_t rotr(uint32_t value, bitcount_t rot)
336{
337 asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
338 return value;
339}
340
341#if __x86_64__
342inline uint64_t rotr(uint64_t value, bitcount_t rot)
343{
344 asm ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
345 return value;
346}
347#endif // __x86_64__
348
349#endif // PCG_USE_INLINE_ASM
350
351
352/*
353 * The C++ SeedSeq concept (modelled by seed_seq) can fill an array of
354 * 32-bit integers with seed data, but sometimes we want to produce
355 * larger or smaller integers.
356 *
357 * The following code handles this annoyance.
358 *
359 * uneven_copy will copy an array of 32-bit ints to an array of larger or
360 * smaller ints (actually, the code is general it only needing forward
361 * iterators). The copy is identical to the one that would be performed if
362 * we just did memcpy on a standard little-endian machine, but works
363 * regardless of the endian of the machine (or the weirdness of the ints
364 * involved).
365 *
366 * generate_to initializes an array of integers using a SeedSeq
367 * object. It is given the size as a static constant at compile time and
368 * tries to avoid memory allocation. If we're filling in 32-bit constants
369 * we just do it directly. If we need a separate buffer and it's small,
370 * we allocate it on the stack. Otherwise, we fall back to heap allocation.
371 * Ugh.
372 *
373 * generate_one produces a single value of some integral type using a
374 * SeedSeq object.
375 */
376
377 /* uneven_copy helper, case where destination ints are less than 32 bit. */
378
379template<class SrcIter, class DestIter>
380SrcIter uneven_copy_impl(
381 SrcIter src_first, DestIter dest_first, DestIter dest_last,
382 std::true_type)
383{
384 typedef typename std::iterator_traits<SrcIter>::value_type src_t;
385 typedef typename std::iterator_traits<DestIter>::value_type dest_t;
386
387 constexpr bitcount_t SRC_SIZE = sizeof(src_t);
388 constexpr bitcount_t DEST_SIZE = sizeof(dest_t);
389 constexpr bitcount_t DEST_BITS = DEST_SIZE * 8;
390 constexpr bitcount_t SCALE = SRC_SIZE / DEST_SIZE;
391
392 size_t count = 0;
393 src_t value = 0;
394
395 while (dest_first != dest_last) {
396 if ((count++ % SCALE) == 0)
397 value = *src_first++; // Get more bits
398 else
399 value >>= DEST_BITS; // Move down bits
400
401 *dest_first++ = dest_t(value); // Truncates, ignores high bits.
402 }
403 return src_first;
404}
405
406 /* uneven_copy helper, case where destination ints are more than 32 bit. */
407
408template<class SrcIter, class DestIter>
409SrcIter uneven_copy_impl(
410 SrcIter src_first, DestIter dest_first, DestIter dest_last,
411 std::false_type)
412{
413 typedef typename std::iterator_traits<SrcIter>::value_type src_t;
414 typedef typename std::iterator_traits<DestIter>::value_type dest_t;
415
416 constexpr auto SRC_SIZE = sizeof(src_t);
417 constexpr auto SRC_BITS = SRC_SIZE * 8;
418 constexpr auto DEST_SIZE = sizeof(dest_t);
419 constexpr auto SCALE = (DEST_SIZE+SRC_SIZE-1) / SRC_SIZE;
420
421 while (dest_first != dest_last) {
422 dest_t value(0UL);
423 unsigned int shift = 0;
424
425 for (size_t i = 0; i < SCALE; ++i) {
426 value |= dest_t(*src_first++) << shift;
427 shift += SRC_BITS;
428 }
429
430 *dest_first++ = value;
431 }
432 return src_first;
433}
434
435/* uneven_copy, call the right code for larger vs. smaller */
436
437template<class SrcIter, class DestIter>
438inline SrcIter uneven_copy(SrcIter src_first,
439 DestIter dest_first, DestIter dest_last)
440{
441 typedef typename std::iterator_traits<SrcIter>::value_type src_t;
442 typedef typename std::iterator_traits<DestIter>::value_type dest_t;
443
444 constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
445
446 return uneven_copy_impl(src_first, dest_first, dest_last,
447 std::integral_constant<bool, DEST_IS_SMALLER>{});
448}
449
450/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
451 * (actually works for any random-access iterator)
452 */
453
454template <size_t size, typename SeedSeq, typename DestIter>
455inline void generate_to_impl(SeedSeq&& generator, DestIter dest,
456 std::true_type)
457{
458 generator.generate(dest, dest+size);
459}
460
461template <size_t size, typename SeedSeq, typename DestIter>
462void generate_to_impl(SeedSeq&& generator, DestIter dest,
463 std::false_type)
464{
465 typedef typename std::iterator_traits<DestIter>::value_type dest_t;
466 constexpr auto DEST_SIZE = sizeof(dest_t);
467 constexpr auto GEN_SIZE = sizeof(uint32_t);
468
469 constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
470 constexpr size_t FROM_ELEMS =
471 GEN_IS_SMALLER
472 ? size * ((DEST_SIZE+GEN_SIZE-1) / GEN_SIZE)
473 : (size + (GEN_SIZE / DEST_SIZE) - 1)
474 / ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
475 // this odd code ^^^^^^^^^^^^^^^^^ is work-around for
476 // a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
477
478 if (FROM_ELEMS <= 1024) {
479 uint32_t buffer[FROM_ELEMS];
480 generator.generate(buffer, buffer+FROM_ELEMS);
481 uneven_copy(buffer, dest, dest+size);
482 } else {
483 uint32_t* buffer = static_cast<uint32_t*>(malloc(GEN_SIZE * FROM_ELEMS));
484 generator.generate(buffer, buffer+FROM_ELEMS);
485 uneven_copy(buffer, dest, dest+size);
486 free(static_cast<void*>(buffer));
487 }
488}
489
490template <size_t size, typename SeedSeq, typename DestIter>
491inline void generate_to(SeedSeq&& generator, DestIter dest)
492{
493 typedef typename std::iterator_traits<DestIter>::value_type dest_t;
494 constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
495
496 generate_to_impl<size>(std::forward<SeedSeq>(generator), dest,
497 std::integral_constant<bool, IS_32BIT>{});
498}
499
500/* generate_one, produce a value of integral type using a SeedSeq
501 * (optionally, we can have it produce more than one and pick which one
502 * we want)
503 */
504
505template <typename UInt, size_t i = 0UL, size_t N = i+1UL, typename SeedSeq>
506inline UInt generate_one(SeedSeq&& generator)
507{
508 UInt result[N];
509 generate_to<N>(std::forward<SeedSeq>(generator), result);
510 return result[i];
511}
512
513template <typename RngType>
514auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
515 -> typename RngType::result_type
516{
517 typedef typename RngType::result_type rtype;
518 rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
519 % upper_bound;
520 for (;;) {
521 rtype r = rng() - RngType::min();
522 if (r >= threshold)
523 return r % upper_bound;
524 }
525}
526
527template <typename Iter, typename RandType>
528void shuffle(Iter from, Iter to, RandType&& rng)
529{
530 typedef typename std::iterator_traits<Iter>::difference_type delta_t;
531 typedef typename std::remove_reference<RandType>::type::result_type result_t;
532 auto count = to - from;
533 while (count > 1) {
534 delta_t chosen = delta_t(bounded_rand(rng, result_t(count)));
535 --count;
536 --to;
537 using std::swap;
538 swap(*(from + chosen), *to);
539 }
540}
541
542/*
543 * Although std::seed_seq is useful, it isn't everything. Often we want to
544 * initialize a random-number generator some other way, such as from a random
545 * device.
546 *
547 * Technically, it does not meet the requirements of a SeedSequence because
548 * it lacks some of the rarely-used member functions (some of which would
549 * be impossible to provide). However the C++ standard is quite specific
550 * that actual engines only called the generate method, so it ought not to be
551 * a problem in practice.
552 */
553
554template <typename RngType>
555class seed_seq_from {
556private:
557 RngType rng_;
558
559 typedef uint_least32_t result_type;
560
561public:
562 template<typename... Args>
563 seed_seq_from(Args&&... args) :
564 rng_(std::forward<Args>(args)...)
565 {
566 // Nothing (else) to do...
567 }
568
569 template<typename Iter>
570 void generate(Iter start, Iter finish)
571 {
572 for (auto i = start; i != finish; ++i)
573 *i = result_type(rng_());
574 }
575
576 constexpr size_t size() const
577 {
578 return (sizeof(typename RngType::result_type) > sizeof(result_type)
579 && RngType::max() > ~size_t(0UL))
580 ? ~size_t(0UL)
581 : size_t(RngType::max());
582 }
583};
584
585// Sometimes, when debugging or testing, it's handy to be able print the name
586// of a (in human-readable form). This code allows the idiom:
587//
588// cout << printable_typename<my_foo_type_t>()
589//
590// to print out my_foo_type_t (or its concrete type if it is a synonym)
591
592#if __cpp_rtti || __GXX_RTTI
593
594template <typename T>
595struct printable_typename {};
596
597template <typename T>
598std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
599 const char *implementation_typename = typeid(T).name();
600#ifdef __GNUC__
601 int status;
602 char* pretty_name =
603 abi::__cxa_demangle(implementation_typename, NULL, NULL, &status);
604 if (status == 0)
605 out << pretty_name;
606 free(static_cast<void*>(pretty_name));
607 if (status == 0)
608 return out;
609#endif
610 out << implementation_typename;
611 return out;
612}
613
614#endif // __cpp_rtti || __GXX_RTTI
615
616} // namespace pcg_extras
617
618#endif // PCG_EXTRAS_HPP_INCLUDED
619