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
|
||||
#define PCG_EXTRAS_HPP_INCLUDED 1
|
||||
|
||||
#include <cassert>
|
||||
#include <cinttypes>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <locale>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <locale>
|
||||
#include <iterator>
|
||||
|
||||
#ifdef __GNUC__
|
||||
#include <cxxabi.h>
|
||||
|
@ -78,19 +78,16 @@
|
|||
namespace pcg_extras {
|
||||
typedef __uint128_t pcg128_t;
|
||||
}
|
||||
#define PCG_128BIT_CONSTANT(high,low) \
|
||||
((pcg_extras::pcg128_t(high) << 64) + low)
|
||||
#define PCG_128BIT_CONSTANT(high, low) ((pcg_extras::pcg128_t(high) << 64) + low)
|
||||
#else
|
||||
#include "pcg_uint128.hpp"
|
||||
namespace pcg_extras {
|
||||
typedef pcg_extras::uint_x4<uint32_t, uint64_t> pcg128_t;
|
||||
}
|
||||
#define PCG_128BIT_CONSTANT(high,low) \
|
||||
pcg_extras::pcg128_t(high,low)
|
||||
#define PCG_128BIT_CONSTANT(high, low) pcg_extras::pcg128_t(high, low)
|
||||
#define PCG_EMULATED_128BIT_MATH 1
|
||||
#endif
|
||||
|
||||
|
||||
namespace pcg_extras {
|
||||
|
||||
/*
|
||||
|
@ -117,9 +114,7 @@ namespace pcg_extras {
|
|||
*/
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_ostream<CharT,Traits>&
|
||||
operator<<(std::basic_ostream<CharT,Traits>& out, pcg128_t value)
|
||||
{
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, pcg128_t value) {
|
||||
auto desired_base = out.flags() & out.basefield;
|
||||
bool want_hex = desired_base == out.hex;
|
||||
|
||||
|
@ -161,9 +156,7 @@ namespace pcg_extras {
|
|||
}
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_istream<CharT,Traits>&
|
||||
operator>>(std::basic_istream<CharT,Traits>& in, pcg128_t& value)
|
||||
{
|
||||
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& in, pcg128_t& value) {
|
||||
typename std::basic_istream<CharT, Traits>::sentry s(in);
|
||||
|
||||
if (!s)
|
||||
|
@ -209,16 +202,12 @@ namespace pcg_extras {
|
|||
*/
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_ostream<CharT,Traits>&
|
||||
operator<<(std::basic_ostream<CharT,Traits>&out, uint8_t value)
|
||||
{
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, uint8_t value) {
|
||||
return out << uint32_t(value);
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits>
|
||||
std::basic_istream<CharT,Traits>&
|
||||
operator>>(std::basic_istream<CharT,Traits>& in, uint8_t& target)
|
||||
{
|
||||
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& in, uint8_t& target) {
|
||||
uint32_t value = 0xdecea5edU;
|
||||
in >> value;
|
||||
if (!in && value == 0xdecea5edU)
|
||||
|
@ -236,18 +225,14 @@ namespace pcg_extras {
|
|||
* 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Useful bitwise operations.
|
||||
*/
|
||||
|
@ -258,9 +243,7 @@ namespace pcg_extras {
|
|||
* generator defined later.
|
||||
*/
|
||||
|
||||
template <typename itype>
|
||||
inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift)
|
||||
{
|
||||
template <typename itype> inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift) {
|
||||
if (2 * shift >= bits) {
|
||||
return x ^ (x >> shift);
|
||||
}
|
||||
|
@ -287,9 +270,7 @@ namespace pcg_extras {
|
|||
* (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
|
||||
*/
|
||||
|
||||
template <typename itype>
|
||||
inline itype rotl(itype value, bitcount_t rot)
|
||||
{
|
||||
template <typename itype> inline itype rotl(itype value, bitcount_t rot) {
|
||||
constexpr bitcount_t bits = sizeof(itype) * 8;
|
||||
constexpr bitcount_t mask = bits - 1;
|
||||
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
||||
|
@ -299,9 +280,7 @@ namespace pcg_extras {
|
|||
#endif
|
||||
}
|
||||
|
||||
template <typename itype>
|
||||
inline itype rotr(itype value, bitcount_t rot)
|
||||
{
|
||||
template <typename itype> inline itype rotr(itype value, bitcount_t rot) {
|
||||
constexpr bitcount_t bits = sizeof(itype) * 8;
|
||||
constexpr bitcount_t mask = bits - 1;
|
||||
#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
|
||||
|
@ -320,27 +299,23 @@ namespace pcg_extras {
|
|||
*/
|
||||
#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));
|
||||
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));
|
||||
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));
|
||||
return value;
|
||||
}
|
||||
|
||||
#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));
|
||||
return value;
|
||||
}
|
||||
|
@ -351,29 +326,16 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
|
||||
#pragma intrinsic(_rotr, _rotr64, _rotr8, _rotr16)
|
||||
|
||||
inline uint8_t rotr(uint8_t value, bitcount_t rot)
|
||||
{
|
||||
return _rotr8(value, rot);
|
||||
}
|
||||
inline uint8_t rotr(uint8_t value, bitcount_t rot) { return _rotr8(value, rot); }
|
||||
|
||||
inline uint16_t rotr(uint16_t value, bitcount_t rot)
|
||||
{
|
||||
return _rotr16(value, rot);
|
||||
}
|
||||
inline uint16_t rotr(uint16_t value, bitcount_t rot) { return _rotr16(value, rot); }
|
||||
|
||||
inline uint32_t rotr(uint32_t value, bitcount_t rot)
|
||||
{
|
||||
return _rotr(value, rot);
|
||||
}
|
||||
inline uint32_t rotr(uint32_t value, bitcount_t rot) { return _rotr(value, rot); }
|
||||
|
||||
inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
||||
{
|
||||
return _rotr64(value, rot);
|
||||
}
|
||||
inline uint64_t rotr(uint64_t value, bitcount_t rot) { return _rotr64(value, rot); }
|
||||
|
||||
#endif // PCG_USE_INLINE_ASM
|
||||
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
@ -402,10 +364,7 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
/* uneven_copy helper, case where destination ints are less than 32 bit. */
|
||||
|
||||
template <class SrcIter, class DestIter>
|
||||
SrcIter uneven_copy_impl(
|
||||
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
||||
std::true_type)
|
||||
{
|
||||
SrcIter uneven_copy_impl(SrcIter src_first, DestIter dest_first, DestIter dest_last, std::true_type) {
|
||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||
|
||||
|
@ -431,10 +390,7 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
/* uneven_copy helper, case where destination ints are more than 32 bit. */
|
||||
|
||||
template <class SrcIter, class DestIter>
|
||||
SrcIter uneven_copy_impl(
|
||||
SrcIter src_first, DestIter dest_first, DestIter dest_last,
|
||||
std::false_type)
|
||||
{
|
||||
SrcIter uneven_copy_impl(SrcIter src_first, DestIter dest_first, DestIter dest_last, std::false_type) {
|
||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||
|
||||
|
@ -460,16 +416,13 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
/* uneven_copy, call the right code for larger vs. smaller */
|
||||
|
||||
template <class SrcIter, class DestIter>
|
||||
inline SrcIter uneven_copy(SrcIter src_first,
|
||||
DestIter dest_first, DestIter dest_last)
|
||||
{
|
||||
inline SrcIter uneven_copy(SrcIter src_first, DestIter dest_first, DestIter dest_last) {
|
||||
typedef typename std::iterator_traits<SrcIter>::value_type src_t;
|
||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||
|
||||
constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
|
||||
|
||||
return uneven_copy_impl(src_first, dest_first, dest_last,
|
||||
std::integral_constant<bool, DEST_IS_SMALLER>{});
|
||||
return uneven_copy_impl(src_first, dest_first, dest_last, std::integral_constant<bool, DEST_IS_SMALLER>{});
|
||||
}
|
||||
|
||||
/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
|
||||
|
@ -477,26 +430,20 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
*/
|
||||
|
||||
template <size_t size, typename SeedSeq, typename DestIter>
|
||||
inline void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
||||
std::true_type)
|
||||
{
|
||||
inline void generate_to_impl(SeedSeq&& generator, DestIter dest, std::true_type) {
|
||||
generator.generate(dest, dest + size);
|
||||
}
|
||||
|
||||
template <size_t size, typename SeedSeq, typename DestIter>
|
||||
void generate_to_impl(SeedSeq&& generator, DestIter dest,
|
||||
std::false_type)
|
||||
{
|
||||
void generate_to_impl(SeedSeq&& generator, DestIter dest, std::false_type) {
|
||||
typedef typename std::iterator_traits<DestIter>::value_type dest_t;
|
||||
constexpr auto DEST_SIZE = sizeof(dest_t);
|
||||
constexpr auto GEN_SIZE = sizeof(uint32_t);
|
||||
|
||||
constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
|
||||
constexpr size_t FROM_ELEMS =
|
||||
GEN_IS_SMALLER
|
||||
? size * ((DEST_SIZE+GEN_SIZE-1) / GEN_SIZE)
|
||||
: (size + (GEN_SIZE / DEST_SIZE) - 1)
|
||||
/ ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
|
||||
GEN_IS_SMALLER ? size * ((DEST_SIZE + GEN_SIZE - 1) / GEN_SIZE)
|
||||
: (size + (GEN_SIZE / DEST_SIZE) - 1) / ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
|
||||
// this odd code ^^^^^^^^^^^^^^^^^ is work-around for
|
||||
// a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
|
||||
|
||||
|
@ -513,13 +460,11 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
}
|
||||
|
||||
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;
|
||||
constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
|
||||
|
||||
generate_to_impl<size>(std::forward<SeedSeq>(generator), dest,
|
||||
std::integral_constant<bool, IS_32BIT>{});
|
||||
generate_to_impl<size>(std::forward<SeedSeq>(generator), dest, std::integral_constant<bool, IS_32BIT>{});
|
||||
}
|
||||
|
||||
/* generate_one, produce a value of integral type using a SeedSeq
|
||||
|
@ -528,20 +473,16 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
*/
|
||||
|
||||
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];
|
||||
generate_to<N>(std::forward<SeedSeq>(generator), result);
|
||||
return result[i];
|
||||
}
|
||||
|
||||
template <typename RngType>
|
||||
auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
|
||||
-> typename RngType::result_type
|
||||
{
|
||||
auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound) -> typename RngType::result_type {
|
||||
typedef typename RngType::result_type rtype;
|
||||
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
|
||||
% upper_bound;
|
||||
rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound) % upper_bound;
|
||||
for (;;) {
|
||||
rtype r = rng() - RngType::min();
|
||||
if (r >= threshold)
|
||||
|
@ -549,9 +490,7 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
}
|
||||
}
|
||||
|
||||
template <typename Iter, typename RandType>
|
||||
void shuffle(Iter from, Iter to, RandType&& rng)
|
||||
{
|
||||
template <typename Iter, typename RandType> void shuffle(Iter from, Iter to, RandType&& rng) {
|
||||
typedef typename std::iterator_traits<Iter>::difference_type delta_t;
|
||||
typedef typename std::remove_reference<RandType>::type::result_type result_t;
|
||||
auto count = to - from;
|
||||
|
@ -576,32 +515,24 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
* a problem in practice.
|
||||
*/
|
||||
|
||||
template <typename RngType>
|
||||
class seed_seq_from {
|
||||
template <typename RngType> class seed_seq_from {
|
||||
private:
|
||||
RngType rng_;
|
||||
|
||||
typedef uint_least32_t result_type;
|
||||
|
||||
public:
|
||||
template<typename... Args>
|
||||
seed_seq_from(Args&&... args) :
|
||||
rng_(std::forward<Args>(args)...)
|
||||
{
|
||||
template <typename... Args> seed_seq_from(Args&&... args) : rng_(std::forward<Args>(args)...) {
|
||||
// Nothing (else) to do...
|
||||
}
|
||||
|
||||
template<typename Iter>
|
||||
void generate(Iter start, Iter finish)
|
||||
{
|
||||
template <typename Iter> void generate(Iter start, Iter finish) {
|
||||
for (auto i = start; i != finish; ++i)
|
||||
*i = result_type(rng_());
|
||||
}
|
||||
|
||||
constexpr size_t size() const
|
||||
{
|
||||
return (sizeof(typename RngType::result_type) > sizeof(result_type)
|
||||
&& RngType::max() > ~size_t(0UL))
|
||||
constexpr size_t size() const {
|
||||
return (sizeof(typename RngType::result_type) > sizeof(result_type) && RngType::max() > ~size_t(0UL))
|
||||
? ~size_t(0UL)
|
||||
: size_t(RngType::max());
|
||||
}
|
||||
|
@ -614,18 +545,14 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
* value.
|
||||
*/
|
||||
|
||||
template <typename IntType>
|
||||
struct static_arbitrary_seed {
|
||||
template <typename IntType> struct static_arbitrary_seed {
|
||||
private:
|
||||
static constexpr IntType fnv(IntType hash, const char* pos) {
|
||||
return *pos == '\0'
|
||||
? hash
|
||||
: fnv((hash * IntType(16777619U)) ^ *pos, (pos+1));
|
||||
return *pos == '\0' ? hash : fnv((hash * IntType(16777619U)) ^ *pos, (pos + 1));
|
||||
}
|
||||
|
||||
public:
|
||||
static constexpr IntType value = fnv(IntType(2166136261U ^ sizeof(IntType)),
|
||||
__DATE__ __TIME__ __FILE__);
|
||||
static constexpr IntType value = fnv(IntType(2166136261U ^ sizeof(IntType)), __DATE__ __TIME__ __FILE__);
|
||||
};
|
||||
|
||||
// Sometimes, when debugging or testing, it's handy to be able print the name
|
||||
|
@ -637,16 +564,13 @@ inline uint64_t rotr(uint64_t value, bitcount_t rot)
|
|||
|
||||
#if __cpp_rtti || __GXX_RTTI
|
||||
|
||||
template <typename T>
|
||||
struct printable_typename {};
|
||||
template <typename T> struct printable_typename {};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
|
||||
template <typename T> std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
|
||||
const char* implementation_typename = typeid(T).name();
|
||||
#ifdef __GNUC__
|
||||
int status;
|
||||
char* pretty_name =
|
||||
abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
|
||||
char* pretty_name = abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
|
||||
if (status == 0)
|
||||
out << pretty_name;
|
||||
free(static_cast<void*>(pretty_name));
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue