forked from sysprog21/simrupt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoroshiro.c
52 lines (42 loc) · 1.04 KB
/
xoroshiro.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
#include "xoroshiro.h"
static inline u64 rotl(const u64 x, int k)
{
return (x << k) | (x >> (64 - k));
}
static inline void seed(struct state_array *obj, u64 s0, u64 s1)
{
obj->array[0] = s0;
obj->array[1] = s1;
}
u64 xoro_next(struct state_array *obj)
{
const u64 s0 = obj->array[0];
u64 s1 = obj->array[1];
const u64 result = rotl(s0 + s1, 24) + s0;
s1 ^= s0;
obj->array[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16);
obj->array[1] = rotl(s1, 37);
return result;
}
void xoro_jump(struct state_array *obj)
{
static const u64 JUMP[] = {0xdf900294d8f554a5, 0x170865df4b3201fc};
u64 s0 = 0;
u64 s1 = 0;
int i, b;
for (i = 0; i < sizeof(JUMP) / sizeof(*JUMP); i++) {
for (b = 0; b < 64; b++) {
if (JUMP[i] & (u64) (1) << b) {
s0 ^= obj->array[0];
s1 ^= obj->array[1];
}
xoro_next(obj);
}
}
obj->array[0] = s0;
obj->array[1] = s1;
}
void xoro_init(struct state_array *obj)
{
seed(obj, 314159265, 1618033989);
}