Style changes for pcg_random to keep my CI from yelling at me.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
779ddd49e3
commit
e599bc730f
|
@ -33,17 +33,17 @@
|
||||||
#ifndef PCG_EXTRAS_HPP_INCLUDED
|
#ifndef PCG_EXTRAS_HPP_INCLUDED
|
||||||
#define PCG_EXTRAS_HPP_INCLUDED 1
|
#define PCG_EXTRAS_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cassert>
|
|
||||||
#include <limits>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <limits>
|
||||||
|
#include <locale>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <locale>
|
|
||||||
#include <iterator>
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
|
@ -78,28 +78,25 @@
|
||||||
namespace pcg_extras {
|
namespace pcg_extras {
|
||||||
typedef __uint128_t pcg128_t;
|
typedef __uint128_t pcg128_t;
|
||||||
}
|
}
|
||||||
#define PCG_128BIT_CONSTANT(high,low) \
|
#define PCG_128BIT_CONSTANT(high, low) ((pcg_extras::pcg128_t(high) << 64) + low)
|
||||||
((pcg_extras::pcg128_t(high) << 64) + low)
|
|
||||||
#else
|
#else
|
||||||
#include "pcg_uint128.hpp"
|
#include "pcg_uint128.hpp"
|
||||||
namespace pcg_extras {
|
namespace pcg_extras {
|
||||||
typedef pcg_extras::uint_x4<uint32_t,uint64_t> pcg128_t;
|
typedef pcg_extras::uint_x4<uint32_t, uint64_t> pcg128_t;
|
||||||
}
|
}
|
||||||
#define PCG_128BIT_CONSTANT(high,low) \
|
#define PCG_128BIT_CONSTANT(high, low) pcg_extras::pcg128_t(high, low)
|
||||||
pcg_extras::pcg128_t(high,low)
|
#define PCG_EMULATED_128BIT_MATH 1
|
||||||
#define PCG_EMULATED_128BIT_MATH 1
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace pcg_extras {
|
namespace pcg_extras {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We often need to represent a "number of bits". When used normally, these
|
* We often need to represent a "number of bits". When used normally, these
|
||||||
* numbers are never greater than 128, so an unsigned char is plenty.
|
* numbers are never greater than 128, so an unsigned char is plenty.
|
||||||
* If you're using a nonstandard generator of a larger size, you can set
|
* If you're using a nonstandard generator of a larger size, you can set
|
||||||
* PCG_BITCOUNT_T to have it define it as a larger size. (Some compilers
|
* PCG_BITCOUNT_T to have it define it as a larger size. (Some compilers
|
||||||
* might produce faster code if you set it to an unsigned int.)
|
* might produce faster code if you set it to an unsigned int.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef PCG_BITCOUNT_T
|
#ifndef PCG_BITCOUNT_T
|
||||||
typedef uint8_t bitcount_t;
|
typedef uint8_t bitcount_t;
|
||||||
|
@ -107,25 +104,23 @@ namespace pcg_extras {
|
||||||
typedef PCG_BITCOUNT_T bitcount_t;
|
typedef PCG_BITCOUNT_T bitcount_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* C++ requires us to be able to serialize RNG state by printing or reading
|
* C++ requires us to be able to serialize RNG state by printing or reading
|
||||||
* it from a stream. Because we use 128-bit ints, we also need to be able
|
* it from a stream. Because we use 128-bit ints, we also need to be able
|
||||||
* ot print them, so here is code to do so.
|
* ot print them, so here is code to do so.
|
||||||
*
|
*
|
||||||
* This code provides enough functionality to print 128-bit ints in decimal
|
* This code provides enough functionality to print 128-bit ints in decimal
|
||||||
* and zero-padded in hex. It's not a full-featured implementation.
|
* and zero-padded in hex. It's not a full-featured implementation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename CharT, typename Traits>
|
template <typename CharT, typename Traits>
|
||||||
std::basic_ostream<CharT,Traits>&
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, pcg128_t value) {
|
||||||
operator<<(std::basic_ostream<CharT,Traits>& out, pcg128_t value)
|
|
||||||
{
|
|
||||||
auto desired_base = out.flags() & out.basefield;
|
auto desired_base = out.flags() & out.basefield;
|
||||||
bool want_hex = desired_base == out.hex;
|
bool want_hex = desired_base == out.hex;
|
||||||
|
|
||||||
if (want_hex) {
|
if (want_hex) {
|
||||||
uint64_t highpart = uint64_t(value >> 64);
|
uint64_t highpart = uint64_t(value >> 64);
|
||||||
uint64_t lowpart = uint64_t(value);
|
uint64_t lowpart = uint64_t(value);
|
||||||
auto desired_width = out.width();
|
auto desired_width = out.width();
|
||||||
if (desired_width > 16) {
|
if (desired_width > 16) {
|
||||||
out.width(desired_width - 16);
|
out.width(desired_width - 16);
|
||||||
|
@ -148,7 +143,7 @@ namespace pcg_extras {
|
||||||
constexpr size_t MAX_CHARS_128BIT = 40;
|
constexpr size_t MAX_CHARS_128BIT = 40;
|
||||||
|
|
||||||
char buffer[MAX_CHARS_128BIT];
|
char buffer[MAX_CHARS_128BIT];
|
||||||
char* pos = buffer+sizeof(buffer);
|
char* pos = buffer + sizeof(buffer);
|
||||||
*(--pos) = '\0';
|
*(--pos) = '\0';
|
||||||
constexpr auto BASE = pcg128_t(10ULL);
|
constexpr auto BASE = pcg128_t(10ULL);
|
||||||
do {
|
do {
|
||||||
|
@ -156,15 +151,13 @@ namespace pcg_extras {
|
||||||
auto mod = uint32_t(value - (div * BASE));
|
auto mod = uint32_t(value - (div * BASE));
|
||||||
*(--pos) = '0' + char(mod);
|
*(--pos) = '0' + char(mod);
|
||||||
value = div;
|
value = div;
|
||||||
} while(value != pcg128_t(0ULL));
|
} while (value != pcg128_t(0ULL));
|
||||||
return out << pos;
|
return out << pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename CharT, typename Traits>
|
template <typename CharT, typename Traits>
|
||||||
std::basic_istream<CharT,Traits>&
|
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& in, pcg128_t& value) {
|
||||||
operator>>(std::basic_istream<CharT,Traits>& in, pcg128_t& value)
|
typename std::basic_istream<CharT, Traits>::sentry s(in);
|
||||||
{
|
|
||||||
typename std::basic_istream<CharT,Traits>::sentry s(in);
|
|
||||||
|
|
||||||
if (!s)
|
if (!s)
|
||||||
return in;
|
return in;
|
||||||
|
@ -173,7 +166,7 @@ namespace pcg_extras {
|
||||||
pcg128_t current(0ULL);
|
pcg128_t current(0ULL);
|
||||||
bool did_nothing = true;
|
bool did_nothing = true;
|
||||||
bool overflow = false;
|
bool overflow = false;
|
||||||
for(;;) {
|
for (;;) {
|
||||||
CharT wide_ch = in.get();
|
CharT wide_ch = in.get();
|
||||||
if (!in.good())
|
if (!in.good())
|
||||||
break;
|
break;
|
||||||
|
@ -184,7 +177,7 @@ namespace pcg_extras {
|
||||||
}
|
}
|
||||||
did_nothing = false;
|
did_nothing = false;
|
||||||
pcg128_t digit(uint32_t(ch - '0'));
|
pcg128_t digit(uint32_t(ch - '0'));
|
||||||
pcg128_t timesbase = current*BASE;
|
pcg128_t timesbase = current * BASE;
|
||||||
overflow = overflow || timesbase < current;
|
overflow = overflow || timesbase < current;
|
||||||
current = timesbase + digit;
|
current = timesbase + digit;
|
||||||
overflow = overflow || current < digit;
|
overflow = overflow || current < digit;
|
||||||
|
@ -201,24 +194,20 @@ namespace pcg_extras {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Likewise, if people use tiny rngs, we'll be serializing uint8_t.
|
* Likewise, if people use tiny rngs, we'll be serializing uint8_t.
|
||||||
* If we just used the provided IO operators, they'd read/write chars,
|
* If we just used the provided IO operators, they'd read/write chars,
|
||||||
* not ints, so we need to define our own. We *can* redefine this operator
|
* not ints, so we need to define our own. We *can* redefine this operator
|
||||||
* here because we're in our own namespace.
|
* here because we're in our own namespace.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename CharT, typename Traits>
|
template <typename CharT, typename Traits>
|
||||||
std::basic_ostream<CharT,Traits>&
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, uint8_t value) {
|
||||||
operator<<(std::basic_ostream<CharT,Traits>&out, uint8_t value)
|
|
||||||
{
|
|
||||||
return out << uint32_t(value);
|
return out << uint32_t(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename CharT, typename Traits>
|
template <typename CharT, typename Traits>
|
||||||
std::basic_istream<CharT,Traits>&
|
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& in, uint8_t& target) {
|
||||||
operator>>(std::basic_istream<CharT,Traits>& in, uint8_t& target)
|
|
||||||
{
|
|
||||||
uint32_t value = 0xdecea5edU;
|
uint32_t value = 0xdecea5edU;
|
||||||
in >> value;
|
in >> value;
|
||||||
if (!in && value == 0xdecea5edU)
|
if (!in && value == 0xdecea5edU)
|
||||||
|
@ -231,40 +220,34 @@ namespace pcg_extras {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unfortunately, the above functions don't get found in preference to the
|
/* Unfortunately, the above functions don't get found in preference to the
|
||||||
* built in ones, so we create some more specific overloads that will.
|
* built in ones, so we create some more specific overloads that will.
|
||||||
* Ugh.
|
* Ugh.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& out, uint8_t value)
|
inline std::ostream& operator<<(std::ostream& out, uint8_t value) {
|
||||||
{
|
return pcg_extras::operator<<<char>(out, value);
|
||||||
return pcg_extras::operator<< <char>(out, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::istream& operator>>(std::istream& in, uint8_t& value)
|
inline std::istream& operator>>(std::istream& in, uint8_t& value) {
|
||||||
{
|
return pcg_extras::operator>><char>(in, value);
|
||||||
return pcg_extras::operator>> <char>(in, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Useful bitwise operations.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XorShifts are invertable, but they are someting of a pain to invert.
|
||||||
|
* This function backs them out. It's used by the whacky "inside out"
|
||||||
|
* generator defined later.
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
template <typename itype> inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift) {
|
||||||
* Useful bitwise operations.
|
if (2 * shift >= bits) {
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* XorShifts are invertable, but they are someting of a pain to invert.
|
|
||||||
* This function backs them out. It's used by the whacky "inside out"
|
|
||||||
* generator defined later.
|
|
||||||
*/
|
|
||||||
|
|
||||||
template <typename itype>
|
|
||||||
inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift)
|
|
||||||
{
|
|
||||||
if (2*shift >= bits) {
|
|
||||||
return x ^ (x >> shift);
|
return x ^ (x >> shift);
|
||||||
}
|
}
|
||||||
itype lowmask1 = (itype(1U) << (bits - shift*2)) - 1;
|
itype lowmask1 = (itype(1U) << (bits - shift * 2)) - 1;
|
||||||
itype highmask1 = ~lowmask1;
|
itype highmask1 = ~lowmask1;
|
||||||
itype top1 = x;
|
itype top1 = x;
|
||||||
itype bottom1 = x & lowmask1;
|
itype bottom1 = x & lowmask1;
|
||||||
|
@ -278,36 +261,32 @@ namespace pcg_extras {
|
||||||
return top1 | bottom2;
|
return top1 | bottom2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Rotate left and right.
|
* Rotate left and right.
|
||||||
*
|
*
|
||||||
* In ideal world, compilers would spot idiomatic rotate code and convert it
|
* In ideal world, compilers would spot idiomatic rotate code and convert it
|
||||||
* to a rotate instruction. Of course, opinions vary on what the correct
|
* to a rotate instruction. Of course, opinions vary on what the correct
|
||||||
* idiom is and how to spot it. For clang, sometimes it generates better
|
* idiom is and how to spot it. For clang, sometimes it generates better
|
||||||
* (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
|
* (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename itype>
|
template <typename itype> inline itype rotl(itype value, bitcount_t rot) {
|
||||||
inline itype rotl(itype value, bitcount_t rot)
|
|
||||||
{
|
|
||||||
constexpr bitcount_t bits = sizeof(itype) * 8;
|
constexpr bitcount_t bits = sizeof(itype) * 8;
|
||||||
constexpr bitcount_t mask = bits - 1;
|
constexpr bitcount_t mask = bits - 1;
|
||||||
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
||||||
return rot ? (value << rot) | (value >> (bits - rot)) : value;
|
return rot ? (value << rot) | (value >> (bits - rot)) : value;
|
||||||
#else
|
#else
|
||||||
return (value << rot) | (value >> ((- rot) & mask));
|
return (value << rot) | (value >> ((-rot) & mask));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename itype>
|
template <typename itype> inline itype rotr(itype value, bitcount_t rot) {
|
||||||
inline itype rotr(itype value, bitcount_t rot)
|
|
||||||
{
|
|
||||||
constexpr bitcount_t bits = sizeof(itype) * 8;
|
constexpr bitcount_t bits = sizeof(itype) * 8;
|
||||||
constexpr bitcount_t mask = bits - 1;
|
constexpr bitcount_t mask = bits - 1;
|
||||||
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
||||||
return rot ? (value >> rot) | (value << (bits - rot)) : value;
|
return rot ? (value >> rot) | (value << (bits - rot)) : value;
|
||||||
#else
|
#else
|
||||||
return (value >> rot) | (value << ((- rot) & mask));
|
return (value >> rot) | (value << ((-rot) & mask));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,32 +297,28 @@ namespace pcg_extras {
|
||||||
*
|
*
|
||||||
* These overloads will be preferred over the general template code above.
|
* These overloads will be preferred over the general template code above.
|
||||||
*/
|
*/
|
||||||
#if PCG_USE_INLINE_ASM && __GNUC__ && (__x86_64__ || __i386__)
|
#if PCG_USE_INLINE_ASM && __GNUC__ && (__x86_64__ || __i386__)
|
||||||
|
|
||||||
inline uint8_t rotr(uint8_t value, bitcount_t rot)
|
inline uint8_t rotr(uint8_t value, bitcount_t rot) {
|
||||||
{
|
asm("rorb %%cl, %0" : "=r"(value) : "0"(value), "c"(rot));
|
||||||
asm ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
return value;
|
||||||
return value;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
inline uint16_t rotr(uint16_t value, bitcount_t rot)
|
inline uint16_t rotr(uint16_t value, bitcount_t rot) {
|
||||||
{
|
asm("rorw %%cl, %0" : "=r"(value) : "0"(value), "c"(rot));
|
||||||
asm ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
return value;
|
||||||
return value;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
inline uint32_t rotr(uint32_t value, bitcount_t rot)
|
inline uint32_t rotr(uint32_t value, bitcount_t rot) {
|
||||||
{
|
asm("rorl %%cl, %0" : "=r"(value) : "0"(value), "c"(rot));
|
||||||
asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
return value;
|
||||||
return value;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#if __x86_64__
|
#if __x86_64__
|
||||||
inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
inline uint64_t rotr(uint64_t value, bitcount_t rot) {
|
||||||
{
|
asm("rorq %%cl, %0" : "=r"(value) : "0"(value), "c"(rot));
|
||||||
asm ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
|
return value;
|
||||||
return value;
|
}
|
||||||
}
|
|
||||||
#endif // __x86_64__
|
#endif // __x86_64__
|
||||||
|
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
|
@ -351,97 +326,78 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||||
|
|
||||||
#pragma intrinsic(_rotr, _rotr64, _rotr8, _rotr16)
|
#pragma intrinsic(_rotr, _rotr64, _rotr8, _rotr16)
|
||||||
|
|
||||||
inline uint8_t rotr(uint8_t value, bitcount_t rot)
|
inline uint8_t rotr(uint8_t value, bitcount_t rot) { return _rotr8(value, rot); }
|
||||||
{
|
|
||||||
return _rotr8(value, rot);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline uint16_t rotr(uint16_t value, bitcount_t rot)
|
inline uint16_t rotr(uint16_t value, bitcount_t rot) { return _rotr16(value, rot); }
|
||||||
{
|
|
||||||
return _rotr16(value, rot);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline uint32_t rotr(uint32_t value, bitcount_t rot)
|
inline uint32_t rotr(uint32_t value, bitcount_t rot) { return _rotr(value, rot); }
|
||||||
{
|
|
||||||
return _rotr(value, rot);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
inline uint64_t rotr(uint64_t value, bitcount_t rot) { return _rotr64(value, rot); }
|
||||||
{
|
|
||||||
return _rotr64(value, rot);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // PCG_USE_INLINE_ASM
|
#endif // PCG_USE_INLINE_ASM
|
||||||
|
|
||||||
|
/*
|
||||||
/*
|
* The C++ SeedSeq concept (modelled by seed_seq) can fill an array of
|
||||||
* The C++ SeedSeq concept (modelled by seed_seq) can fill an array of
|
* 32-bit integers with seed data, but sometimes we want to produce
|
||||||
* 32-bit integers with seed data, but sometimes we want to produce
|
* larger or smaller integers.
|
||||||
* larger or smaller integers.
|
*
|
||||||
*
|
* The following code handles this annoyance.
|
||||||
* The following code handles this annoyance.
|
*
|
||||||
*
|
* uneven_copy will copy an array of 32-bit ints to an array of larger or
|
||||||
* uneven_copy will copy an array of 32-bit ints to an array of larger or
|
* smaller ints (actually, the code is general it only needing forward
|
||||||
* smaller ints (actually, the code is general it only needing forward
|
* iterators). The copy is identical to the one that would be performed if
|
||||||
* iterators). The copy is identical to the one that would be performed if
|
* we just did memcpy on a standard little-endian machine, but works
|
||||||
* we just did memcpy on a standard little-endian machine, but works
|
* regardless of the endian of the machine (or the weirdness of the ints
|
||||||
* regardless of the endian of the machine (or the weirdness of the ints
|
* involved).
|
||||||
* involved).
|
*
|
||||||
*
|
* generate_to initializes an array of integers using a SeedSeq
|
||||||
* generate_to initializes an array of integers using a SeedSeq
|
* object. It is given the size as a static constant at compile time and
|
||||||
* object. It is given the size as a static constant at compile time and
|
* tries to avoid memory allocation. If we're filling in 32-bit constants
|
||||||
* tries to avoid memory allocation. If we're filling in 32-bit constants
|
* we just do it directly. If we need a separate buffer and it's small,
|
||||||
* we just do it directly. If we need a separate buffer and it's small,
|
* we allocate it on the stack. Otherwise, we fall back to heap allocation.
|
||||||
* we allocate it on the stack. Otherwise, we fall back to heap allocation.
|
* Ugh.
|
||||||
* Ugh.
|
*
|
||||||
*
|
* generate_one produces a single value of some integral type using a
|
||||||
* generate_one produces a single value of some integral type using a
|
* SeedSeq object.
|
||||||
* SeedSeq object.
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
/* uneven_copy helper, case where destination ints are less than 32 bit. */
|
/* uneven_copy helper, case where destination ints are less than 32 bit. */
|
||||||
|
|
||||||
template<class SrcIter, class DestIter>
|
template <class SrcIter, class DestIter>
|
||||||
SrcIter uneven_copy_impl(
|
SrcIter uneven_copy_impl(SrcIter src_first, DestIter dest_first, DestIter dest_last, std::true_type) {
|
||||||
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||||
std::true_type)
|
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
|
||||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||||
|
|
||||||
constexpr bitcount_t SRC_SIZE = sizeof(src_t);
|
constexpr bitcount_t SRC_SIZE = sizeof(src_t);
|
||||||
constexpr bitcount_t DEST_SIZE = sizeof(dest_t);
|
constexpr bitcount_t DEST_SIZE = sizeof(dest_t);
|
||||||
constexpr bitcount_t DEST_BITS = DEST_SIZE * 8;
|
constexpr bitcount_t DEST_BITS = DEST_SIZE * 8;
|
||||||
constexpr bitcount_t SCALE = SRC_SIZE / DEST_SIZE;
|
constexpr bitcount_t SCALE = SRC_SIZE / DEST_SIZE;
|
||||||
|
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
src_t value = 0;
|
src_t value = 0;
|
||||||
|
|
||||||
while (dest_first != dest_last) {
|
while (dest_first != dest_last) {
|
||||||
if ((count++ % SCALE) == 0)
|
if ((count++ % SCALE) == 0)
|
||||||
value = *src_first++; // Get more bits
|
value = *src_first++; // Get more bits
|
||||||
else
|
else
|
||||||
value >>= DEST_BITS; // Move down bits
|
value >>= DEST_BITS; // Move down bits
|
||||||
|
|
||||||
*dest_first++ = dest_t(value); // Truncates, ignores high bits.
|
*dest_first++ = dest_t(value); // Truncates, ignores high bits.
|
||||||
}
|
}
|
||||||
return src_first;
|
return src_first;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* uneven_copy helper, case where destination ints are more than 32 bit. */
|
/* uneven_copy helper, case where destination ints are more than 32 bit. */
|
||||||
|
|
||||||
template<class SrcIter, class DestIter>
|
template <class SrcIter, class DestIter>
|
||||||
SrcIter uneven_copy_impl(
|
SrcIter uneven_copy_impl(SrcIter src_first, DestIter dest_first, DestIter dest_last, std::false_type) {
|
||||||
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||||
std::false_type)
|
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
|
||||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||||
|
|
||||||
constexpr auto SRC_SIZE = sizeof(src_t);
|
constexpr auto SRC_SIZE = sizeof(src_t);
|
||||||
constexpr auto SRC_BITS = SRC_SIZE * 8;
|
constexpr auto SRC_BITS = SRC_SIZE * 8;
|
||||||
constexpr auto DEST_SIZE = sizeof(dest_t);
|
constexpr auto DEST_SIZE = sizeof(dest_t);
|
||||||
constexpr auto SCALE = (DEST_SIZE+SRC_SIZE-1) / SRC_SIZE;
|
constexpr auto SCALE = (DEST_SIZE + SRC_SIZE - 1) / SRC_SIZE;
|
||||||
|
|
||||||
while (dest_first != dest_last) {
|
while (dest_first != dest_last) {
|
||||||
dest_t value(0UL);
|
dest_t value(0UL);
|
||||||
|
@ -457,91 +413,76 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||||
return src_first;
|
return src_first;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* uneven_copy, call the right code for larger vs. smaller */
|
/* uneven_copy, call the right code for larger vs. smaller */
|
||||||
|
|
||||||
template<class SrcIter, class DestIter>
|
template <class SrcIter, class DestIter>
|
||||||
inline SrcIter uneven_copy(SrcIter src_first,
|
inline SrcIter uneven_copy(SrcIter src_first, DestIter dest_first, DestIter dest_last) {
|
||||||
DestIter dest_first, DestIter dest_last)
|
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
|
||||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||||
|
|
||||||
constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
|
constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
|
||||||
|
|
||||||
return uneven_copy_impl(src_first, dest_first, dest_last,
|
return uneven_copy_impl(src_first, dest_first, dest_last, std::integral_constant<bool, DEST_IS_SMALLER>{});
|
||||||
std::integral_constant<bool, DEST_IS_SMALLER>{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
|
/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
|
||||||
* (actually works for any random-access iterator)
|
* (actually works for any random-access iterator)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <size_t size, typename SeedSeq, typename DestIter>
|
template <size_t size, typename SeedSeq, typename DestIter>
|
||||||
inline void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
inline void generate_to_impl(SeedSeq&& generator, DestIter dest, std::true_type) {
|
||||||
std::true_type)
|
generator.generate(dest, dest + size);
|
||||||
{
|
|
||||||
generator.generate(dest, dest+size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t size, typename SeedSeq, typename DestIter>
|
template <size_t size, typename SeedSeq, typename DestIter>
|
||||||
void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
void generate_to_impl(SeedSeq&& generator, DestIter dest, std::false_type) {
|
||||||
std::false_type)
|
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||||
constexpr auto DEST_SIZE = sizeof(dest_t);
|
constexpr auto DEST_SIZE = sizeof(dest_t);
|
||||||
constexpr auto GEN_SIZE = sizeof(uint32_t);
|
constexpr auto GEN_SIZE = sizeof(uint32_t);
|
||||||
|
|
||||||
constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
|
constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
|
||||||
constexpr size_t FROM_ELEMS =
|
constexpr size_t FROM_ELEMS =
|
||||||
GEN_IS_SMALLER
|
GEN_IS_SMALLER ? size * ((DEST_SIZE + GEN_SIZE - 1) / GEN_SIZE)
|
||||||
? size * ((DEST_SIZE+GEN_SIZE-1) / GEN_SIZE)
|
: (size + (GEN_SIZE / DEST_SIZE) - 1) / ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
|
||||||
: (size + (GEN_SIZE / DEST_SIZE) - 1)
|
|
||||||
/ ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
|
|
||||||
// this odd code ^^^^^^^^^^^^^^^^^ is work-around for
|
// this odd code ^^^^^^^^^^^^^^^^^ is work-around for
|
||||||
// a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
|
// a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
|
||||||
|
|
||||||
if (FROM_ELEMS <= 1024) {
|
if (FROM_ELEMS <= 1024) {
|
||||||
uint32_t buffer[FROM_ELEMS];
|
uint32_t buffer[FROM_ELEMS];
|
||||||
generator.generate(buffer, buffer+FROM_ELEMS);
|
generator.generate(buffer, buffer + FROM_ELEMS);
|
||||||
uneven_copy(buffer, dest, dest+size);
|
uneven_copy(buffer, dest, dest + size);
|
||||||
} else {
|
} else {
|
||||||
uint32_t* buffer = static_cast<uint32_t*>(malloc(GEN_SIZE * FROM_ELEMS));
|
uint32_t* buffer = static_cast<uint32_t*>(malloc(GEN_SIZE * FROM_ELEMS));
|
||||||
generator.generate(buffer, buffer+FROM_ELEMS);
|
generator.generate(buffer, buffer + FROM_ELEMS);
|
||||||
uneven_copy(buffer, dest, dest+size);
|
uneven_copy(buffer, dest, dest + size);
|
||||||
free(static_cast<void*>(buffer));
|
free(static_cast<void*>(buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t size, typename SeedSeq, typename DestIter>
|
template <size_t size, typename SeedSeq, typename DestIter>
|
||||||
inline void generate_to(SeedSeq&& generator, DestIter dest)
|
inline void generate_to(SeedSeq&& generator, DestIter dest) {
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||||
constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
|
constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
|
||||||
|
|
||||||
generate_to_impl<size>(std::forward<SeedSeq>(generator), dest,
|
generate_to_impl<size>(std::forward<SeedSeq>(generator), dest, std::integral_constant<bool, IS_32BIT>{});
|
||||||
std::integral_constant<bool, IS_32BIT>{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* generate_one, produce a value of integral type using a SeedSeq
|
/* generate_one, produce a value of integral type using a SeedSeq
|
||||||
* (optionally, we can have it produce more than one and pick which one
|
* (optionally, we can have it produce more than one and pick which one
|
||||||
* we want)
|
* we want)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename UInt, size_t i = 0UL, size_t N = i+1UL, typename SeedSeq>
|
template <typename UInt, size_t i = 0UL, size_t N = i + 1UL, typename SeedSeq>
|
||||||
inline UInt generate_one(SeedSeq&& generator)
|
inline UInt generate_one(SeedSeq&& generator) {
|
||||||
{
|
|
||||||
UInt result[N];
|
UInt result[N];
|
||||||
generate_to<N>(std::forward<SeedSeq>(generator), result);
|
generate_to<N>(std::forward<SeedSeq>(generator), result);
|
||||||
return result[i];
|
return result[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename RngType>
|
template <typename RngType>
|
||||||
auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
|
auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound) -> typename RngType::result_type {
|
||||||
-> typename RngType::result_type
|
|
||||||
{
|
|
||||||
typedef typename RngType::result_type rtype;
|
typedef typename RngType::result_type rtype;
|
||||||
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
|
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound) % upper_bound;
|
||||||
% upper_bound;
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
rtype r = rng() - RngType::min();
|
rtype r = rng() - RngType::min();
|
||||||
if (r >= threshold)
|
if (r >= threshold)
|
||||||
|
@ -549,9 +490,7 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Iter, typename RandType>
|
template <typename Iter, typename RandType> void shuffle(Iter from, Iter to, RandType&& rng) {
|
||||||
void shuffle(Iter from, Iter to, RandType&& rng)
|
|
||||||
{
|
|
||||||
typedef typename std::iterator_traits<Iter>::difference_type delta_t;
|
typedef typename std::iterator_traits<Iter>::difference_type delta_t;
|
||||||
typedef typename std::remove_reference<RandType>::type::result_type result_t;
|
typedef typename std::remove_reference<RandType>::type::result_type result_t;
|
||||||
auto count = to - from;
|
auto count = to - from;
|
||||||
|
@ -564,89 +503,74 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Although std::seed_seq is useful, it isn't everything. Often we want to
|
* Although std::seed_seq is useful, it isn't everything. Often we want to
|
||||||
* initialize a random-number generator some other way, such as from a random
|
* initialize a random-number generator some other way, such as from a random
|
||||||
* device.
|
* device.
|
||||||
*
|
*
|
||||||
* Technically, it does not meet the requirements of a SeedSequence because
|
* Technically, it does not meet the requirements of a SeedSequence because
|
||||||
* it lacks some of the rarely-used member functions (some of which would
|
* it lacks some of the rarely-used member functions (some of which would
|
||||||
* be impossible to provide). However the C++ standard is quite specific
|
* be impossible to provide). However the C++ standard is quite specific
|
||||||
* that actual engines only called the generate method, so it ought not to be
|
* that actual engines only called the generate method, so it ought not to be
|
||||||
* a problem in practice.
|
* a problem in practice.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename RngType>
|
template <typename RngType> class seed_seq_from {
|
||||||
class seed_seq_from {
|
|
||||||
private:
|
private:
|
||||||
RngType rng_;
|
RngType rng_;
|
||||||
|
|
||||||
typedef uint_least32_t result_type;
|
typedef uint_least32_t result_type;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename... Args>
|
template <typename... Args> seed_seq_from(Args&&... args) : rng_(std::forward<Args>(args)...) {
|
||||||
seed_seq_from(Args&&... args) :
|
|
||||||
rng_(std::forward<Args>(args)...)
|
|
||||||
{
|
|
||||||
// Nothing (else) to do...
|
// Nothing (else) to do...
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Iter>
|
template <typename Iter> void generate(Iter start, Iter finish) {
|
||||||
void generate(Iter start, Iter finish)
|
|
||||||
{
|
|
||||||
for (auto i = start; i != finish; ++i)
|
for (auto i = start; i != finish; ++i)
|
||||||
*i = result_type(rng_());
|
*i = result_type(rng_());
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t size() const
|
constexpr size_t size() const {
|
||||||
{
|
return (sizeof(typename RngType::result_type) > sizeof(result_type) && RngType::max() > ~size_t(0UL))
|
||||||
return (sizeof(typename RngType::result_type) > sizeof(result_type)
|
? ~size_t(0UL)
|
||||||
&& RngType::max() > ~size_t(0UL))
|
: size_t(RngType::max());
|
||||||
? ~size_t(0UL)
|
|
||||||
: size_t(RngType::max());
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Sometimes you might want a distinct seed based on when the program
|
* Sometimes you might want a distinct seed based on when the program
|
||||||
* was compiled. That way, a particular instance of the program will
|
* was compiled. That way, a particular instance of the program will
|
||||||
* behave the same way, but when recompiled it'll produce a different
|
* behave the same way, but when recompiled it'll produce a different
|
||||||
* value.
|
* value.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <typename IntType>
|
template <typename IntType> struct static_arbitrary_seed {
|
||||||
struct static_arbitrary_seed {
|
|
||||||
private:
|
private:
|
||||||
static constexpr IntType fnv(IntType hash, const char* pos) {
|
static constexpr IntType fnv(IntType hash, const char* pos) {
|
||||||
return *pos == '\0'
|
return *pos == '\0' ? hash : fnv((hash * IntType(16777619U)) ^ *pos, (pos + 1));
|
||||||
? hash
|
|
||||||
: fnv((hash * IntType(16777619U)) ^ *pos, (pos+1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr IntType value = fnv(IntType(2166136261U ^ sizeof(IntType)),
|
static constexpr IntType value = fnv(IntType(2166136261U ^ sizeof(IntType)), __DATE__ __TIME__ __FILE__);
|
||||||
__DATE__ __TIME__ __FILE__);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sometimes, when debugging or testing, it's handy to be able print the name
|
// Sometimes, when debugging or testing, it's handy to be able print the name
|
||||||
// of a (in human-readable form). This code allows the idiom:
|
// of a (in human-readable form). This code allows the idiom:
|
||||||
//
|
//
|
||||||
// cout << printable_typename<my_foo_type_t>()
|
// cout << printable_typename<my_foo_type_t>()
|
||||||
//
|
//
|
||||||
// to print out my_foo_type_t (or its concrete type if it is a synonym)
|
// to print out my_foo_type_t (or its concrete type if it is a synonym)
|
||||||
|
|
||||||
#if __cpp_rtti || __GXX_RTTI
|
#if __cpp_rtti || __GXX_RTTI
|
||||||
|
|
||||||
template <typename T>
|
template <typename T> struct printable_typename {};
|
||||||
struct printable_typename {};
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T> std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
|
||||||
std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
|
const char* implementation_typename = typeid(T).name();
|
||||||
const char *implementation_typename = typeid(T).name();
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
int status;
|
int status;
|
||||||
char* pretty_name =
|
char* pretty_name = abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
|
||||||
abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
|
|
||||||
if (status == 0)
|
if (status == 0)
|
||||||
out << pretty_name;
|
out << pretty_name;
|
||||||
free(static_cast<void*>(pretty_name));
|
free(static_cast<void*>(pretty_name));
|
||||||
|
@ -657,7 +581,7 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // __cpp_rtti || __GXX_RTTI
|
#endif // __cpp_rtti || __GXX_RTTI
|
||||||
|
|
||||||
} // namespace pcg_extras
|
} // namespace pcg_extras
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue