From 4bd9ec982b58aba0202794c0baea727da55a9df3 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 17 Aug 2020 13:55:54 +1000 Subject: [PATCH] rand: add the splitmix64 generator This is the suggested initialiser for xoshiro256plusplus state. --- CMakeLists.txt | 1 + rand/splitmix64.hpp | 44 +++++++++++++++++++++++++++++++++++++++++++ test/rand/buckets.cpp | 2 ++ 3 files changed, 47 insertions(+) create mode 100644 rand/splitmix64.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cb48acf..cccdd947 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -487,6 +487,7 @@ list ( rand/pcg.hpp rand/rdrand.cpp rand/rdrand.hpp + rand/splitmix64.hpp rand/system.hpp random.cpp random.hpp diff --git a/rand/splitmix64.hpp b/rand/splitmix64.hpp new file mode 100644 index 00000000..6b3676ff --- /dev/null +++ b/rand/splitmix64.hpp @@ -0,0 +1,44 @@ +/* Written in 2015 by Sebastiano Vigna (vigna@acm.org) + * + * To the extent possible under law, the author has dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + */ + +#include "../std.hpp" + +#include + + +namespace cruft::rand { + class splitmix64 { + public: + using result_type = u64; + + splitmix64 (u64 seed): + m_state (seed) + { ; } + + result_type operator() (void) + { + u64 z = (m_state += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); + } + + static constexpr result_type min (void) + { + return std::numeric_limits::min (); + } + + static constexpr result_type max (void) + { + return std::numeric_limits::max (); + } + + + private: + u64 m_state; + }; +} diff --git a/test/rand/buckets.cpp b/test/rand/buckets.cpp index 9dac1996..7e50d751 100644 --- a/test/rand/buckets.cpp +++ b/test/rand/buckets.cpp @@ -4,6 +4,7 @@ #include "rand/pcg.hpp" #include "rand/rdrand.hpp" #include "rand/system.hpp" +#include "rand/splitmix64.hpp" #include "tap.hpp" #include "maths.hpp" @@ -49,6 +50,7 @@ main (int,char**) { cruft::TAP::logger tap; + test_buckets (tap, 0x1234u); test_buckets> (tap, 0x1234u); test_buckets> (tap, 0x1234u); test_buckets (tap, 0x1234u);