-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorshift.h
140 lines (106 loc) · 2.28 KB
/
xorshift.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// xorshift.h
// ddouble
//
// Created by Juan Lucas Rey on 15/05/2016.
// Copyright © 2016 Juan Lucas Rey. All rights reserved.
//
#ifndef xorshift_h
#define xorshift_h
#include <stdint.h>
#include <vector>
#include <iostream>
using namespace std;
template< int i, typename Type, typename Enable = void >
class UnXorSh{
public:
static inline Type Right( Type x)
{
x ^= x >> i;
return UnXorSh< (i << 1), Type >::Right(x);
}
static inline Type Left( Type x)
{
x ^= x << i;
return UnXorSh< (i << 1), Type >::Left(x);
}
};
template< int i, typename Type >
class UnXorSh< i, Type, typename std::enable_if< (i >= sizeof(Type)*CHAR_BIT) >::type >{
public:
static inline Type Right( Type x)
{
return x;
}
static inline Type Left( Type x)
{
return x;
}
};
uint32_t UnXorShl32(uint32_t x, uint32_t shift);
uint32_t UnXorShr32(uint32_t x, uint32_t shift);
uint64_t UnXorShl64(uint64_t x, uint32_t shift);
uint64_t UnXorShr64(uint64_t x, uint32_t shift);
class PRNG32
{
public:
virtual uint32_t curr() = 0;
virtual uint32_t draw() = 0;
virtual uint32_t prev() = 0;
};
class PRNG64
{
public:
virtual uint64_t curr() = 0;
virtual uint64_t draw() = 0;
virtual uint64_t prev() = 0;
};
class xorshift128 : public PRNG32
{
public:
xorshift128(uint32_t x,uint32_t y,uint32_t z,uint32_t w):
mX(x),mY(y),mZ(z),mW(w){}
uint32_t curr(){return mW;}
uint32_t draw();
uint32_t prev();
private:
uint32_t mX,mY,mZ,mW;
};
class xorshift64star : public PRNG64
{
public:
xorshift64star(uint32_t x):
mX(x){}
uint64_t curr();
uint64_t draw();
uint64_t prev();
private:
uint64_t mX;
};
class xorshift1024star : public PRNG64
{
public:
xorshift1024star(vector<uint64_t> s, int p) : mS(s),mP(p)
{}
uint64_t curr();
uint64_t draw();
uint64_t prev();
private:
vector<uint64_t> mS;
int mP;
};
class xorshift128plus : public PRNG64
{
public:
xorshift128plus(uint64_t s1, uint64_t s2)
{
mS[0] = s1;
mS[1] = s2;
}
uint64_t curr();
uint64_t draw();
uint64_t prev();
private:
uint64_t mS[2];
};
#endif /* xorshift_h */