| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: Olivier Lapicque */ |
| 3 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "openmpt/all/BuildSettings.hpp" |
| 8 |
|
| 9 |
#include "mpt/base/bit.hpp" |
| 10 |
#include "mpt/random/random.hpp" |
| 11 |
#include "openmpt/base/Types.hpp" |
| 12 |
|
| 13 |
#include <limits> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
|
| 17 |
OPENMPT_NAMESPACE_BEGIN |
| 18 |
|
| 19 |
|
| 20 |
namespace mpt |
| 21 |
{ |
| 22 |
|
| 23 |
|
| 24 |
namespace rng |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
template <typename Tstate, typename Tvalue, Tstate x1, Tstate x2, Tstate x3, Tstate x4, int rol1, int rol2> |
| 29 |
class modplug |
| 30 |
{ |
| 31 |
public: |
| 32 |
typedef Tstate state_type; |
| 33 |
typedef Tvalue result_type; |
| 34 |
|
| 35 |
private: |
| 36 |
state_type state1; |
| 37 |
state_type state2; |
| 38 |
|
| 39 |
public: |
| 40 |
template <typename Trng> |
| 41 |
explicit inline modplug(Trng &rd) |
| 42 |
: state1(mpt::random<state_type>(rd)) |
| 43 |
, state2(mpt::random<state_type>(rd)) |
| 44 |
{ |
| 45 |
} |
| 46 |
explicit inline modplug(state_type seed1, state_type seed2) |
| 47 |
: state1(seed1) |
| 48 |
, state2(seed2) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
public: |
| 53 |
static MPT_CONSTEXPRINLINE result_type min() |
| 54 |
{ |
| 55 |
return static_cast<result_type>(0); |
| 56 |
} |
| 57 |
static MPT_CONSTEXPRINLINE result_type max() |
| 58 |
{ |
| 59 |
return std::numeric_limits<result_type>::max(); |
| 60 |
} |
| 61 |
static MPT_CONSTEXPRINLINE int result_bits() |
| 62 |
{ |
| 63 |
static_assert(std::is_integral<result_type>::value); |
| 64 |
static_assert(std::is_unsigned<result_type>::value); |
| 65 |
return std::numeric_limits<result_type>::digits; |
| 66 |
} |
| 67 |
inline result_type operator()() |
| 68 |
{ |
| 69 |
state_type a = state1; |
| 70 |
state_type b = state2; |
| 71 |
a = mpt::rotl(a, rol1); |
| 72 |
a ^= x1; |
| 73 |
a += x2 + (b * x3); |
| 74 |
b += mpt::rotl(a, rol2) * x4; |
| 75 |
state1 = a; |
| 76 |
state2 = b; |
| 77 |
result_type result = static_cast<result_type>(b); |
| 78 |
return result; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
typedef modplug<uint32, uint32, 0x10204080u, 0x78649E7Du, 4, 5, 1, 16> modplug_dither; |
| 83 |
|
| 84 |
|
| 85 |
} // namespace rng |
| 86 |
|
| 87 |
|
| 88 |
} // namespace mpt |
| 89 |
|
| 90 |
|
| 91 |
OPENMPT_NAMESPACE_END |