Added splitmix64 seed generator. Added splitmix64 random generator. Fortune for rngxx's current commit: Future small blessing − 末小吉master
parent
d9e34e7e70
commit
86b855bda3
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <rngxx.hpp>
|
||||
|
||||
#include "internal/seed.h"
|
||||
|
||||
namespace rng
|
||||
{
|
||||
struct sm64 final : public Random
|
||||
{
|
||||
inline sm64() : m_generator(seed::splitmix64::random()){}
|
||||
inline sm64(const seed::splitmix64& g) : m_generator(g){}
|
||||
explicit inline sm64(seed_gen<u64>& generator) : m_generator(generator){}
|
||||
inline sm64(u64 seed) : Random(), m_generator(seed){}
|
||||
|
||||
RNG_OVERRIDE(inline u64, next_u64, ()) { return m_generator.generate_seed(); }
|
||||
protected:
|
||||
inline f64 _sample() override { return m_generator.generate_f64(); }
|
||||
private:
|
||||
seed::splitmix64 m_generator;
|
||||
};
|
||||
}
|
@ -1,13 +1,33 @@
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
|
||||
#include <seed.h>
|
||||
|
||||
extern "C" {
|
||||
// TODO: XXX: how to return a god damn array without typedef ffs????
|
||||
extern u64 (*sm64_compute[2])(u64, u64);
|
||||
#include <common.h>
|
||||
|
||||
#include <rngxx/sm64.h>
|
||||
|
||||
static u64 _sm64_next(u64& x)
|
||||
{
|
||||
u64 z = (x += 0x9e3779b97f4a7c15);
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
||||
return z ^ (z >> 31);
|
||||
}
|
||||
|
||||
namespace rng::seed
|
||||
{
|
||||
splitmix64::splitmix64(u64 u) : m_state(sm64_compute(u, 0)){}
|
||||
splitmix64::splitmix64(u64 u) : m_state(u){}
|
||||
|
||||
sm128 splitmix64::generate_seed() { return sm128 { m_state = sm64_compute(m_state[0], m_state[1]) }; }
|
||||
u64 splitmix64::generate_seed() { return _sm64_next(m_state); }
|
||||
f64 splitmix64::generate_f64() { return generate_seed() / pow(2.0, 64); }
|
||||
|
||||
u64 splitmix64::oneshot(u64& state) { return _sm64_next(state); }
|
||||
f64 splitmix64::oneshotf(u64& state) { return _sm64_next(state) / pow(2.0, 64); }
|
||||
|
||||
splitmix64 splitmix64::random()
|
||||
{
|
||||
splitmix64 r (oneshot(time(NULL)));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue