mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 14:30:36 +00:00
added XorShiftMask
This commit is contained in:
parent
51a56dcbf6
commit
d4bab41d15
1 changed files with 31 additions and 0 deletions
31
xorshift.hh
31
xorshift.hh
|
|
@ -8,6 +8,37 @@ Copyright 2018 Ahmet Inan <inan@aicodix.de>
|
||||||
|
|
||||||
namespace CODE {
|
namespace CODE {
|
||||||
|
|
||||||
|
template <typename TYPE, int BITS, int FIRST, int SECOND, int THIRD, int SEED = 1>
|
||||||
|
class XorShiftMask
|
||||||
|
{
|
||||||
|
static const TYPE mask = (1 << BITS) - 1;
|
||||||
|
TYPE y_;
|
||||||
|
public:
|
||||||
|
typedef TYPE result_type;
|
||||||
|
static constexpr result_type min()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
static constexpr result_type max()
|
||||||
|
{
|
||||||
|
return mask;
|
||||||
|
}
|
||||||
|
XorShiftMask(TYPE y = SEED) : y_(y) {}
|
||||||
|
void reset(TYPE y = SEED)
|
||||||
|
{
|
||||||
|
y_ = y;
|
||||||
|
}
|
||||||
|
TYPE operator()()
|
||||||
|
{
|
||||||
|
y_ ^= y_ << FIRST;
|
||||||
|
y_ &= mask;
|
||||||
|
y_ ^= y_ >> SECOND;
|
||||||
|
y_ ^= y_ << THIRD;
|
||||||
|
y_ &= mask;
|
||||||
|
return y_;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class Xorshift32
|
class Xorshift32
|
||||||
{
|
{
|
||||||
static const uint32_t Y = 2463534242;
|
static const uint32_t Y = 2463534242;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue