-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathprng.c
208 lines (164 loc) · 5.11 KB
/
prng.c
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
// PRNG for SSRC, written by Naoki Shibata ( shibatch@users.sourceforge.net )
// This PRNG passes Big Crush TestU01. Period length = 2^128
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <float.h>
#include <assert.h>
//
typedef struct u128 {
uint64_t l, u;
} u128;
static unsigned int add64c(uint64_t *ret, uint64_t *u, uint64_t *v) {
uint64_t w;
int c;
w = *u + *v;
c = w < *u;
*ret = w;
return c;
}
static void add128(u128 *r, u128 *u, u128 *v) {
uint64_t rl, ru;
ru = u->u + v->u + add64c(&rl, &u->l, &v->l);
r->l = rl;
r->u = ru;
}
static u128 nextState(u128 state) {
u128 ret, u;
u.u = 0xE7866D8CFFF116AAULL;
u.l = 0xA933E07E1CB7963DULL;
add128(&ret, &state, &u);
return ret;
}
//
#define MAGIC 0xFEEDBABE
typedef struct SleefRNG {
uint32_t magic;
u128 state;
uint64_t remain;
int remainSize;
} SleefRNG;
SleefRNG *SleefRNG_init(uint64_t seed) {
SleefRNG *thiz = (SleefRNG *)calloc(sizeof(SleefRNG), 1);
thiz->magic = MAGIC;
thiz->state.l = 0xAD62418D14EA8247ULL * seed + 0x01C4B4886CC66F59ULL;
thiz->state.u = 0xFFD1390A0ADC2FB8ULL * seed + 0xDABBB8174D95C99BULL;
return thiz;
}
void SleefRNG_dispose(SleefRNG *thiz) {
assert(thiz->magic == MAGIC);
thiz->magic = 0;
free(thiz);
}
#define C0 0xf3dd0fb7820fde37ULL
#define C1 0xe6c6ac2c59e52811ULL
#define C2 0x2fc7871fff7c5b45ULL
#define C3 0x47c7e1f70aa4f7c5ULL
#define C4 0x094f02b7fb9ba895ULL
#define C5 0x89afda817e744570ULL
#define C6 0xc7277d052c7bf14bULL
#define C7 0x474f4433b10b081dULL
#define C8 0xa0a543d9f16196a5ULL
#define C9 0x06dce455629a8955ULL
static uint64_t xrandom64(SleefRNG *thiz) {
assert(thiz->magic == MAGIC);
int n;
thiz->state = nextState(thiz->state);
uint64_t s = thiz->state.u ^ thiz->state.l, t = s;
n = (s >> 24) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C0;
n = (s >> 6) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C1;
n = (s >> 18) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C2;
n = (s >> 48) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C3;
n = (s >> 0) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C4;
n = (s >> 12) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C5;
n = (s >> 36) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C6;
n = (s >> 54) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C7;
n = (s >> 28) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C8;
n = (s >> 10) & 63; t = ((t << n) | (t >> (64 - n))); t ^= C9;
return t;
}
//
uint64_t SleefRNG_next(SleefRNG *thiz, int nbits) {
assert(thiz->magic == MAGIC);
if (nbits > thiz->remainSize) {
thiz->remain = xrandom64(thiz);
thiz->remainSize = 64;
}
uint64_t ret = thiz->remain & ~(-1LL << nbits);
thiz->remain >>= nbits;
thiz->remainSize -= nbits;
return ret;
}
uint64_t SleefRNG_next64(SleefRNG *thiz) {
assert(thiz->magic == MAGIC);
return xrandom64(thiz);
}
void SleefRNG_nextBytes(SleefRNG *thiz, uint8_t *ptr, size_t z) {
assert(thiz->magic == MAGIC);
size_t i;
for(i=0;i<(z & ~7);i+=8) {
*((uint64_t *)&(ptr[i])) = xrandom64(thiz);
}
for(;i<z;i++) {
ptr[i] = SleefRNG_next(thiz, 8);
}
}
double SleefRNG_nextDouble(SleefRNG *thiz) {
assert(thiz->magic == MAGIC);
return (double)xrandom64(thiz) * (1.0 / (1ULL << 32) / (1ULL << 32));
}
double SleefRNG_nextRectangularDouble(SleefRNG *thiz, double min, double max) {
assert(thiz->magic == MAGIC && max >= min);
return min + (double)xrandom64(thiz) * (1.0 / (1ULL << 32) / (1ULL << 32)) * (max-min);
}
void SleefRNG_fillRectangularDouble(SleefRNG *thiz, double *ptr, size_t z, double min, double max) {
assert(thiz->magic == MAGIC && max >= min);
size_t i;
for(i=0;i<z;i++) {
ptr[i] = min + (double)xrandom64(thiz) * (1.0 / (1ULL << 32) / (1ULL << 32)) * (max-min);
}
}
double SleefRNG_nextTriangularDouble(SleefRNG *thiz, double peak) {
assert(thiz->magic == MAGIC);
return ((double)xrandom64(thiz) - (double)xrandom64(thiz)) * (1.0 / (1ULL << 32) / (1ULL << 32)) * peak;
}
void SleefRNG_fillTriangularDouble(SleefRNG *thiz, double *ptr, size_t z, double peak) {
assert(thiz->magic == MAGIC);
size_t i;
for(i=0;i<z;i++) {
ptr[i] = ((double)xrandom64(thiz) - (double)xrandom64(thiz)) * (1.0 / (1ULL << 32) / (1ULL << 32)) * peak;
}
}
double SleefRNG_nextTwoLevelDouble(SleefRNG *thiz, double peak) {
assert(thiz->magic == MAGIC);
return (SleefRNG_next(thiz, 1) != 0) ? -peak : peak;
}
void SleefRNG_fillTwoLevelDouble(SleefRNG *thiz, double *ptr, size_t z, double peak) {
assert(thiz->magic == MAGIC);
size_t i;
for(i=0;i<z;i++) {
ptr[i] = (SleefRNG_next(thiz, 1) != 0) ? -peak : peak;
}
}
//
#if 0
#include "bbattery.h"
struct SleefRNG *rng;
unsigned long getbits (void *param, void *state) { return SleefRNG_next(rng, 32); }
double getu01(void *param, void *state) { return SleefRNG_nextDouble(rng); }
void writestate(void *state) {}
int main (int argc, char **argv)
{
unif01_Gen *gen = calloc(1, sizeof(unif01_Gen));
gen->state = NULL;
gen->param = NULL;
gen->name = "xorrotate";
gen->GetU01 = getu01;
gen->GetBits = getbits;
gen->Write = writestate;
rng = SleefRNG_init(atoi(argv[1]));
for(;;) bbattery_BigCrush(gen);
return 0;
}
#endif