8 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_JSF
9 #define INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_JSF
16 namespace Molassembler {
59 typename UnsignedType,
84 constexpr
explicit JSF() =
default;
87 constexpr
explicit JSF(
const std::array<UnsignedType, 4>& input) {
89 std::is_unsigned<UnsignedType>::value,
90 "The underlying type of the JSF generator must be unsigned!"
97 explicit JSF(std::seed_seq& seedSeq) {
102 explicit JSF(
int seedInt) {
110 constexpr
void seed(
const std::array<UnsignedType, 4>& input) {
115 void seed(std::seed_seq& seedSeq) {
121 std::seed_seq seedSeq {{seed}};
153 return !(*
this == other);
160 UnsignedType a_, b_, c_, d_;
165 static constexpr
unsigned bits = 8 *
sizeof(UnsignedType);
170 static constexpr UnsignedType rotate_(UnsignedType x,
unsigned k) {
171 return (x << k) | (x >> (bits - k));
174 constexpr
void advance_() {
175 UnsignedType e = a_ - rotate_(b_, p);
176 a_ = b_ ^ rotate_(c_, q);
177 b_ = c_ + ((r > 0) ? rotate_(d_, r) : d_);
182 constexpr
void advance_(
unsigned N) {
183 for(
unsigned i = 0; i < N; ++i) {
188 constexpr
void seed_(
const std::array<UnsignedType, 4>& state) {
195 void seed_(std::seed_seq& seedSeq) {
196 std::array<UnsignedType, 4> stateArray;
199 std::begin(stateArray),
207 if constexpr(std::is_same<UnsignedType, std::uint64_t>::value) {
211 std::array<UnsignedType, 4> topBits;
219 for(
unsigned i = 0; i < 4; ++i) {
220 stateArray[i] ^= (topBits[i] << 32);
239 using JSF32na = JSF<uint32_t, 27, 17, 0>;
240 using JSF32nb = JSF<uint32_t, 9, 16, 0>;
241 using JSF32nc = JSF<uint32_t, 9, 24, 0>;
242 using JSF32nd = JSF<uint32_t, 10, 16, 0>;
243 using JSF32ne = JSF<uint32_t, 10, 24, 0>;
244 using JSF32nf = JSF<uint32_t, 11, 16, 0>;
245 using JSF32ng = JSF<uint32_t, 11, 24, 0>;
246 using JSF32nh = JSF<uint32_t, 25, 8, 0>;
247 using JSF32ni = JSF<uint32_t, 25, 16, 0>;
248 using JSF32nj = JSF<uint32_t, 26, 8, 0>;
249 using JSF32nk = JSF<uint32_t, 26, 16, 0>;
250 using JSF32nl = JSF<uint32_t, 26, 17, 0>;
251 using JSF32nm = JSF<uint32_t, 27, 16, 0>;
253 using JSF32ra = JSF<uint32_t, 3, 14, 24>;
254 using JSF32rb = JSF<uint32_t, 3, 25, 15>;
255 using JSF32rc = JSF<uint32_t, 4, 15, 24>;
256 using JSF32rd = JSF<uint32_t, 6, 16, 28>;
257 using JSF32re = JSF<uint32_t, 7, 16, 27>;
258 using JSF32rf = JSF<uint32_t, 8, 14, 3>;
259 using JSF32rg = JSF<uint32_t, 11, 16, 23>;
260 using JSF32rh = JSF<uint32_t, 12, 16, 22>;
261 using JSF32ri = JSF<uint32_t, 12, 17, 23>;
262 using JSF32rj = JSF<uint32_t, 13, 16, 22>;
263 using JSF32rk = JSF<uint32_t, 15, 25, 3>;
264 using JSF32rl = JSF<uint32_t, 16, 9, 3>;
265 using JSF32rm = JSF<uint32_t, 17, 9, 3>;
266 using JSF32rn = JSF<uint32_t, 17, 27, 7>;
267 using JSF32ro = JSF<uint32_t, 19, 7, 3>;
268 using JSF32rp = JSF<uint32_t, 23, 15, 11>;
269 using JSF32rq = JSF<uint32_t, 23, 16, 11>;
270 using JSF32rr = JSF<uint32_t, 23, 17, 11>;
271 using JSF32rs = JSF<uint32_t, 24, 3, 16>;
272 using JSF32rt = JSF<uint32_t, 24, 4, 16>;
273 using JSF32ru = JSF<uint32_t, 25, 14, 3>;
274 using JSF32rv = JSF<uint32_t, 27, 16, 6>;
275 using JSF32rw = JSF<uint32_t, 27, 16, 7>;
277 using JSF32n = JSF32na;
278 using JSF32r = JSF32rq;
279 using JSF32 = JSF32n;
283 using JSF64na = JSF<uint64_t, 39, 11, 0>;
284 using JSF64ra = JSF<uint64_t, 7, 13, 37>;
286 using JSF64n = JSF64na;
287 using JSF64r = JSF64ra;
288 using JSF64 = JSF64r;
291 template<
class Engine = JSF32>
297 engine.seed(272181374);
299 std::random_device randomDevice;
302 std::array<typename Engine::result_type, 4> {
JSF(std::seed_seq &seedSeq)
Construct from a seed sequence.
Definition: Jsf.h:97
static constexpr result_type max()
Maximum value of result_type.
Definition: Jsf.h:76
constexpr UnsignedType operator()()
Advance the state and return the current value.
Definition: Jsf.h:132
constexpr bool operator==(const JSF &other) const
Compares the underlying state of two instances.
Definition: Jsf.h:142
constexpr JSF()=default
Default constructor.
JSF(int seedInt)
Construct from a single integer seed value.
Definition: Jsf.h:102
Provides a seeded engine instance.
Definition: Jsf.h:292
void seed(int seed)
Seed the underlying state with a single integer value.
Definition: Jsf.h:120
constexpr JSF(const std::array< UnsignedType, 4 > &input)
Construct from four seed values.
Definition: Jsf.h:87
constexpr void seed(const std::array< UnsignedType, 4 > &input)
Seed the underlying state with four values.
Definition: Jsf.h:110
static constexpr result_type min()
Minimum value of result_type.
Definition: Jsf.h:71
UnsignedType result_type
When used as a functor, this is the return type of operator ()
Definition: Jsf.h:66
General class enabling the construction of a pattern of PRNGs by Bob Jenkins.
Definition: Jsf.h:63
void seed(std::seed_seq &seedSeq)
Seed the underlying state with a seed sequence.
Definition: Jsf.h:115
constexpr bool operator!=(const JSF &other) const
Compares the underlying state of two instances.
Definition: Jsf.h:152