| 1 |
/* |
| 2 |
* mptRandom.h |
| 3 |
* ----------- |
| 4 |
* Purpose: PRNG |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
#include "mpt/base/bit.hpp" |
| 16 |
#include "mpt/mutex/mutex.hpp" |
| 17 |
#ifdef MODPLUG_TRACKER |
| 18 |
#include "mpt/random/crand.hpp" |
| 19 |
#endif // MODPLUG_TRACKER |
| 20 |
#include "mpt/random/default_engines.hpp" |
| 21 |
#include "mpt/random/device.hpp" |
| 22 |
#include "mpt/random/engine.hpp" |
| 23 |
#include "mpt/random/engine_lcg.hpp" |
| 24 |
#include "mpt/random/seed.hpp" |
| 25 |
|
| 26 |
#include <limits> |
| 27 |
#include <random> |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
OPENMPT_NAMESPACE_BEGIN |
| 32 |
|
| 33 |
|
| 34 |
// NOTE: |
| 35 |
// We implement our own PRNG and distribution functions as the implementations |
| 36 |
// of std::uniform_int_distribution is either wrong (not uniform in MSVC2010) or |
| 37 |
// not guaranteed to be livelock-free for bad PRNGs (in GCC, Clang, boost). |
| 38 |
// We resort to a simpler implementation with only power-of-2 result ranges for |
| 39 |
// both the underlying PRNG and our interface function. This saves us from |
| 40 |
// complicated code having to deal with partial bits of entropy. |
| 41 |
// Our interface still somewhat follows the mindset of C++11 <random> (with the |
| 42 |
// addition of a simple wrapper function mpt::random which saves the caller from |
| 43 |
// instantiating distribution objects for the common uniform distribution case. |
| 44 |
// We are still using std::random_device for initial seeding when avalable and |
| 45 |
// after working around its set of problems. |
| 46 |
|
| 47 |
|
| 48 |
namespace mpt |
| 49 |
{ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
template <typename Trng> |
| 54 |
class thread_safe_prng |
| 55 |
: private Trng |
| 56 |
{ |
| 57 |
private: |
| 58 |
mpt::mutex m; |
| 59 |
public: |
| 60 |
using result_type = typename Trng::result_type; |
| 61 |
public: |
| 62 |
template <typename Trd> |
| 63 |
explicit thread_safe_prng(Trd & rd) |
| 64 |
: Trng(mpt::make_prng<Trng>(rd)) |
| 65 |
{ |
| 66 |
return; |
| 67 |
} |
| 68 |
thread_safe_prng(Trng rng) |
| 69 |
: Trng(rng) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
public: |
| 74 |
static MPT_CONSTEXPRINLINE typename engine_traits<Trng>::result_type min() |
| 75 |
{ |
| 76 |
return Trng::min(); |
| 77 |
} |
| 78 |
static MPT_CONSTEXPRINLINE typename engine_traits<Trng>::result_type max() |
| 79 |
{ |
| 80 |
return Trng::max(); |
| 81 |
} |
| 82 |
static MPT_CONSTEXPRINLINE int result_bits() |
| 83 |
{ |
| 84 |
return engine_traits<Trng>::result_bits(); |
| 85 |
} |
| 86 |
public: |
| 87 |
typename engine_traits<Trng>::result_type operator()() |
| 88 |
{ |
| 89 |
mpt::lock_guard<mpt::mutex> l(m); |
| 90 |
return Trng::operator()(); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
#ifdef MPT_BUILD_FUZZER |
| 96 |
|
| 97 |
// Use deterministic seeding |
| 98 |
using random_device = deterministic_random_device; |
| 99 |
|
| 100 |
#else // !MPT_BUILD_FUZZER |
| 101 |
|
| 102 |
// mpt::random_device always generates 32 bits of entropy |
| 103 |
using random_device = mpt::sane_random_device; |
| 104 |
|
| 105 |
#endif // MPT_BUILD_FUZZER |
| 106 |
|
| 107 |
|
| 108 |
#ifdef MPT_BUILD_FUZZER |
| 109 |
|
| 110 |
// Use fast PRNGs in order to not waste time fuzzing more complex PRNG |
| 111 |
// implementations. |
| 112 |
using fast_prng = deterministic_fast_engine; |
| 113 |
using good_prng = deterministic_good_engine; |
| 114 |
|
| 115 |
#else // !MPT_BUILD_FUZZER |
| 116 |
|
| 117 |
// We cannot use std::minstd_rand here because it has not a power-of-2 sized |
| 118 |
// output domain which we rely upon. |
| 119 |
using fast_prng = fast_engine; // about 3 ALU operations, ~32bit of state, suited for inner loops |
| 120 |
using good_prng = good_engine; |
| 121 |
|
| 122 |
#endif // MPT_BUILD_FUZZER |
| 123 |
|
| 124 |
|
| 125 |
using default_prng = mpt::good_prng; |
| 126 |
|
| 127 |
|
| 128 |
mpt::random_device & global_random_device(); |
| 129 |
mpt::thread_safe_prng<mpt::default_prng> & global_prng(); |
| 130 |
|
| 131 |
#ifdef MPT_BUILD_FUZZER |
| 132 |
void reinit_global_random(); |
| 133 |
#endif // MPT_BUILD_FUZZER |
| 134 |
|
| 135 |
#if defined(MODPLUG_TRACKER) && !defined(MPT_BUILD_WINESUPPORT) |
| 136 |
void set_global_random_device(mpt::random_device *rd); |
| 137 |
void set_global_prng(mpt::thread_safe_prng<mpt::default_prng> *rng); |
| 138 |
#endif // MODPLUG_TRACKER && !MPT_BUILD_WINESUPPORT |
| 139 |
|
| 140 |
|
| 141 |
} // namespace mpt |
| 142 |
|
| 143 |
|
| 144 |
OPENMPT_NAMESPACE_END |