-
Notifications
You must be signed in to change notification settings - Fork 4
/
mwc.c
57 lines (47 loc) · 1.05 KB
/
mwc.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
#include <stdio.h>
#include <stdint.h>
#include "rdrand_stdint.h"
#define R 4096
#define A 17872
#define B 0xffffffff
#define ITERATIONS 100
void init_mwc(uint32_t *state,uint32_t *c,uint32_t *n) {
uint32_t x;
int i;
printf("A\n");
fflush(stdout);
for (i=0; i<R; i++) {
do {
rdrand_get_uint32_retry(10,&x);
} while (x > (B-1));
state[i] = x;
}
printf("A\n");
fflush(stdout);
do {
*c = rdrand_get_uint32_retry(10,&x);
} while (x > (B-1));
*n = 0;
}
uint32_t update_mwc(uint32_t *state,uint32_t *c,uint32_t *n) {
uint64_t t;
int ptr;
*n = (*n + 1) % R;
ptr = (*n + 1) % R;
t = ((uint64_t)state[ptr] * (uint64_t)A) + (uint64_t)*c;
*c = t >> 32;
state[*n] = t & 0xffffffff;
return state[*n];
}
int main() {
uint32_t state[R];
uint32_t c;
uint32_t n;
uint32_t result;
int i;
init_mwc(state, &c, &n);
for (i=0;i<ITERATIONS;i++) {
result = update_mwc(state, &c, &n);
printf("%08x\n",result);
}
}