-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWRng_Isaac.hh
241 lines (205 loc) · 6.34 KB
/
WRng_Isaac.hh
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef WRNG_ISAAC_HH__
#define WRNG_ISAAC_HH__
#include <cstdint>
#define WRNG_ISAAC_VERSION 0x010000
#define WRNG_ISAAC_VERSION_STRING "1.0.0"
#define WRNG_ISAAC_COPYRIGHT_STRING "WRng Isaac v" WRNG_ISAAC_VERSION_STRING " (C)2020 Juha Nieminen"
namespace WRng
{
template<std::uint32_t> class Isaac;
using Isaac_s = Isaac<4>;
using Isaac_c = Isaac<8>;
}
template<unsigned kRandSizeL>
class WRng::Isaac
{
public:
static const std::uint32_t kRandSize = (1<<kRandSizeL);
Isaac(std::uint32_t seed = 0);
Isaac(std::uint32_t seed1, std::uint32_t seed2);
Isaac(const std::uint32_t* seeds, std::uint32_t seedsAmount);
void setSeed(std::uint32_t seed);
void setSeed(std::uint32_t seed1, std::uint32_t seed2);
void setSeed(const std::uint32_t* seeds, std::uint32_t seedsAmount);
std::uint32_t getNext();
typedef std::uint32_t result_type;
static constexpr std::uint32_t min() { return 0U; }
static constexpr std::uint32_t max() { return ~0U; }
std::uint32_t operator()() { return getNext(); }
const std::uint32_t* randValuesArray() const;
void refill();
//------------------------------------------------------------------------
protected:
struct randctx
{
std::uint32_t randrsl[kRandSize];
std::uint32_t randmem[kRandSize];
std::uint32_t randcnt;
std::uint32_t randa, randb, randc;
};
randctx data;
static void randinit(randctx* ctx, bool flag);
static void isaac(randctx* ctx);
};
template<std::uint32_t kRandSizeL>
inline const std::uint32_t* WRng::Isaac<kRandSizeL>::randValuesArray() const
{
return data.randrsl;
}
template<std::uint32_t kRandSizeL>
inline unsigned WRng::Isaac<kRandSizeL>::getNext()
{
if(!data.randcnt)
{
isaac(&data);
data.randcnt = kRandSize-1;
}
else --data.randcnt;
return data.randrsl[data.randcnt];
}
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::refill()
{
isaac(&data);
data.randcnt = kRandSize-1;
}
#define WRNG_ISAAC_ind(mm,x) (*(std::uint32_t *)((std::uint8_t*)(mm) + ((x) & ((kRandSize-1)<<2))))
#define WRNG_ISAAC_rngstep(mix,a,b,mm,m,m2,r,x) { \
x = *m; \
a = (a^(mix)) + *(m2++); \
*(m++) = y = WRNG_ISAAC_ind(mm,x) + a + b; \
*(r++) = b = WRNG_ISAAC_ind(mm,y>>kRandSizeL) + x; }
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::isaac(randctx* ctx)
{
std::uint32_t a,b,x,y,*m,*mm,*m2,*r,*mend;
mm=ctx->randmem; r=ctx->randrsl;
a = ctx->randa; b = ctx->randb + (++ctx->randc);
for (m = mm, mend = m2 = m+(kRandSize/2); m<mend; )
{
WRNG_ISAAC_rngstep( a<<13, a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a>>6 , a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a<<2 , a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a>>16, a, b, mm, m, m2, r, x);
}
for (m2 = mm; m2<mend; )
{
WRNG_ISAAC_rngstep( a<<13, a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a>>6 , a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a<<2 , a, b, mm, m, m2, r, x);
WRNG_ISAAC_rngstep( a>>16, a, b, mm, m, m2, r, x);
}
ctx->randb = b; ctx->randa = a;
}
#undef WRNG_ISAAC_ind
#undef WRNG_ISAAC_rngstep
#define WRNG_ISAAC_mix(a,b,c,d,e,f,g,h) { \
a^=b<<11; d+=a; b+=c; \
b^=c>>2; e+=b; c+=d; \
c^=d<<8; f+=c; d+=e; \
d^=e>>16; g+=d; e+=f; \
e^=f<<10; h+=e; f+=g; \
f^=g>>4; a+=f; g+=h; \
g^=h<<8; b+=g; h+=a; \
h^=a>>9; c+=h; a+=b; }
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::randinit(randctx* ctx, bool flag)
{
std::uint32_t a,b,c,d,e,f,g,h;
std::uint32_t *m,*r;
ctx->randa = ctx->randb = ctx->randc = 0;
m=ctx->randmem;
r=ctx->randrsl;
a=b=c=d=e=f=g=h=0x9e3779b9;
for (unsigned i=0; i<4; ++i)
{
WRNG_ISAAC_mix(a,b,c,d,e,f,g,h);
}
if (flag)
{
for (unsigned i=0; i<kRandSize; i+=8)
{
a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
WRNG_ISAAC_mix(a,b,c,d,e,f,g,h);
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
}
for (unsigned i=0; i<kRandSize; i+=8)
{
a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
WRNG_ISAAC_mix(a,b,c,d,e,f,g,h);
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
}
}
else
{
for (unsigned i=0; i<kRandSize; i+=8)
{
WRNG_ISAAC_mix(a,b,c,d,e,f,g,h);
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
}
}
isaac(ctx);
ctx->randcnt=kRandSize;
}
#undef WRNG_ISAAC_mix
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::setSeed(unsigned seed)
{
for(std::uint32_t i = 0; i < kRandSize; ++i)
{
seed = seed * 2147001325U + 715136305U;
data.randrsl[i] = seed ^ (seed>>14);
}
randinit(&data, true);
}
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::setSeed(unsigned seed1, unsigned seed2)
{
for(std::uint32_t i = 0; i < kRandSize; i += 2)
{
seed1 = seed1 * 2147001325U + 715136305U;
seed2 = seed2 * 1812433253U + 12345U;
data.randrsl[i] = seed1 ^ (seed1>>13);
data.randrsl[i+1] = seed2 ^ (seed2>>15);
}
randinit(&data, true);
}
template<std::uint32_t kRandSizeL>
inline void WRng::Isaac<kRandSizeL>::setSeed(const unsigned* seeds, unsigned seedsAmount)
{
if(seedsAmount == 0) setSeed(0);
else
{
if(seedsAmount > kRandSize) seedsAmount = kRandSize;
for(unsigned i = 0; i < seedsAmount; ++i)
data.randrsl[i] = seeds[i];
unsigned seed = *seeds;
for(unsigned i = seedsAmount; i < kRandSize; ++i)
{
seed = seed * 2147001325U + 715136305U;
data.randrsl[i] = seed ^ (seed>>15);
}
randinit(&data, true);
}
}
template<std::uint32_t kRandSizeL>
inline WRng::Isaac<kRandSizeL>::Isaac(unsigned seed)
{
setSeed(seed);
}
template<std::uint32_t kRandSizeL>
inline WRng::Isaac<kRandSizeL>::Isaac(unsigned seed1, unsigned seed2)
{
setSeed(seed1, seed2);
}
template<std::uint32_t kRandSizeL>
inline WRng::Isaac<kRandSizeL>::Isaac(const unsigned* seeds, unsigned seedsAmount)
{
setSeed(seeds, seedsAmount);
}
#endif