X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Frand%2FGaloisLFSR.hpp;h=395ff8f0546cc7b75cf111a4b996a7705052f29b;hb=41652fb3d73f12e6ae4ce7380244a75a4f5c6797;hp=88a1d37e899f1b26e369a727a804d7755ba7d33e;hpb=2ea26d9ca5eaeae65daa0edbbaeada8c1f23670e;p=blank.git diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp index 88a1d37..395ff8f 100644 --- a/src/rand/GaloisLFSR.hpp +++ b/src/rand/GaloisLFSR.hpp @@ -11,10 +11,21 @@ class GaloisLFSR { public: // seed should be non-zero - explicit GaloisLFSR(std::uint64_t seed) noexcept; + explicit GaloisLFSR(std::uint64_t seed) noexcept + : state(seed) { } // get the next bit - bool operator ()() noexcept; + bool operator ()() noexcept { + bool result = state & 1; + state >>= 1; + if (result) { + state |= 0x8000000000000000; + state ^= mask; + } else { + state &= 0x7FFFFFFFFFFFFFFF; + } + return result; + } template T operator ()(T &out) noexcept { @@ -27,12 +38,19 @@ public: return out = static_cast(state); } + template + T Next() noexcept { + T next; + return (*this)(next); + } + private: std::uint64_t state; // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi) static constexpr std::uint64_t mask = 0xD800000000000000; }; + } #endif