2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2014-01-05 09:59:17 +00:00
|
|
|
#include <nall/serializer.hpp>
|
|
|
|
#include <nall/stdint.hpp>
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
namespace nall {
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2014-01-05 09:59:17 +00:00
|
|
|
struct RandomNumberGenerator {
|
2015-07-14 09:32:43 +00:00
|
|
|
virtual auto seed(uint64_t) -> void = 0;
|
|
|
|
virtual auto operator()() -> uint64_t = 0;
|
|
|
|
virtual auto serialize(serializer&) -> void = 0;
|
2014-01-05 09:59:17 +00:00
|
|
|
};
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2014-01-05 09:59:17 +00:00
|
|
|
//Galois LFSR using CRC64 polynomials
|
|
|
|
struct LinearFeedbackShiftRegisterGenerator : RandomNumberGenerator {
|
2015-07-14 09:32:43 +00:00
|
|
|
auto seed(uint64_t seed) -> void {
|
2014-01-05 09:59:17 +00:00
|
|
|
lfsr = seed;
|
|
|
|
for(unsigned n = 0; n < 8; n++) operator()();
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto operator()() -> uint64_t {
|
2014-01-05 09:59:17 +00:00
|
|
|
return lfsr = (lfsr >> 1) ^ (-(lfsr & 1) & crc64jones);
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
2011-03-20 13:57:55 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto serialize(serializer& s) -> void {
|
2014-01-05 09:59:17 +00:00
|
|
|
s.integer(lfsr);
|
2013-05-02 11:25:45 +00:00
|
|
|
}
|
2011-03-20 13:57:55 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
private:
|
2014-01-05 09:59:17 +00:00
|
|
|
static const uint64_t crc64ecma = 0x42f0e1eba9ea3693;
|
|
|
|
static const uint64_t crc64jones = 0xad93d23594c935a9;
|
|
|
|
uint64_t lfsr = crc64ecma;
|
2013-05-02 11:25:45 +00:00
|
|
|
};
|
2011-03-20 13:57:55 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
inline auto random() -> uint64_t {
|
2014-01-05 09:59:17 +00:00
|
|
|
static LinearFeedbackShiftRegisterGenerator lfsr;
|
|
|
|
return lfsr();
|
|
|
|
}
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|