-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrng_stub.c
107 lines (91 loc) · 2.25 KB
/
testrng_stub.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "bbattery.h"
#include "unif01.h"
#include "util.h"
#include "caml/mlvalues.h"
#include "caml/callback.h"
#define BUFSIZE 1000000
static int fidx = BUFSIZE;
static int bidx = BUFSIZE;
static double *fbuf;
static unsigned long *bbuf;
static double OCamlGen_U01 (void *state, void *param)
{
if (fidx + 1> BUFSIZE){
value f = *caml_named_value ("get_random_floats");
value buf = caml_callback (f, Val_long (BUFSIZE));
assert (Tag_val (buf) == Double_array_tag);
fbuf = &Double_field (buf, 0);
fidx = 0;
}
return fbuf[fidx++];
}
static unsigned long OCamlGen_Bits (void *state, void *param)
{
unsigned long res;
if (bidx + 2 > BUFSIZE){
value b = *caml_named_value ("get_random_bits");
value buf = caml_callback (b, Val_long (BUFSIZE));
assert (Tag_val (buf) == 0);
bbuf = (unsigned long *) &Field (buf, 0);
bidx = 0;
}
res = (unsigned long) Long_val (bbuf[bidx]);
++ bidx;
return res;
}
char const *generator_name = "OCaml stdlib generator";
static void WrOCamlGen (void *junk)
{
printf (" %s\n", generator_name);
}
unif01_Gen * unif01_CreateOCamlGen (void)
{
unif01_Gen *gen;
size_t len;
gen = util_Malloc (sizeof (unif01_Gen));
len = strlen (generator_name);
gen->name = util_Calloc (len + 1, sizeof (char));
strncpy (gen->name, generator_name, len);
gen->param = NULL;
gen->state = NULL;
gen->Write = &WrOCamlGen;
gen->GetBits = &OCamlGen_Bits;
gen->GetU01 = &OCamlGen_U01;
return gen;
}
void unif01_DeleteOCamlGen (unif01_Gen *gen)
{
if (NULL == gen) return;
gen->name = util_Free (gen->name);
util_Free (gen);
}
CAMLprim value smallcrush (value unit)
{
unif01_Gen *gen;
gen = unif01_CreateOCamlGen ();
bbattery_SmallCrush (gen);
fflush (stdout);
unif01_DeleteOCamlGen (gen);
return Val_unit;
}
CAMLprim value crush (value unit)
{
unif01_Gen *gen;
gen = unif01_CreateOCamlGen ();
bbattery_Crush (gen);
fflush (stdout);
unif01_DeleteOCamlGen (gen);
return Val_unit;
}
CAMLprim value bigcrush (value unit)
{
unif01_Gen *gen;
gen = unif01_CreateOCamlGen ();
bbattery_BigCrush (gen);
fflush (stdout);
unif01_DeleteOCamlGen (gen);
return Val_unit;
}