-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_random.cpp
201 lines (166 loc) · 5.55 KB
/
test_random.cpp
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
#include "crandom.hpp"
#include "chacha.hpp"
#include "aes.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "intrinsics.h"
static inline double
now() {
struct timeval tp;
gettimeofday(&tp, 0);
return tp.tv_sec + double(tp.tv_usec)/1000000;
}
void print_features(unsigned int features) {
printf(" w/%s%s%s%s%s\n",
(features) ? "" : "??",
(features & SSE2) ? " SSE2" : "",
(features & SSSE3) ? " SSSE3" : "",
(features & AESNI) ? " AESNI" : "",
(features & XOP) ? " XOP" : "");
}
using namespace crandom;
template<class gen, class t, bool slow>
void test(int n, unsigned int new_features, unsigned int of_features, gen *generator) {
unsigned int old_features = crandom_features,
good = HAVE(new_features) && ((new_features & MUST_MASK) == (MUST_MASK & of_features));
int i;
crandom_features = (crandom_features & ~of_features) | new_features | 1;
print_features(crandom_features);
t out = 0;
if (good) {
double start=now();
for (i=0; i<n; i++) {
if (slow) {
t x;
generator->randomize_slow_case((unsigned char *) &x, sizeof(x));
out += x;
} else {
out += generator->template random<t>();
}
}
start = now() - start;
printf("%0.1f ME (%0.1f MB) / %0.3f sec = %0.1f MB/sec",
i / 1000000.0,
i * sizeof(t) / 1000000.0,
start,
i * sizeof(t) / 1000000.0 / start);
printf("\n\n");
} else {
printf("(unsupported)\n\n");
}
crandom_features = old_features;
opacify(out);
}
void test_rand(int n) {
int i;
double start=now();
for (i=0; i<n; i++) {
rand();
}
start = now() - start;
printf("%0.1f ME (%0.1f MB) / %0.3f sec = %0.1f MB/sec",
i / 1000000.0,
i * sizeof(int) / 1000000.0,
start,
i * sizeof(int) / 1000000.0 / start);
printf("\n\n");
}
void test_random(int n) {
int i;
double start=now();
for (i=0; i<n; i++) {
random();
}
start = now() - start;
printf("%0.1f ME (%0.1f MB) / %0.3f sec = %0.1f MB/sec",
i / 1000000.0,
i * sizeof(int) / 1000000.0,
start,
i * sizeof(int) / 1000000.0 / start);
printf("\n\n");
}
#ifdef __MAC_10_0
void test_arc4random(int n) {
int i;
double start=now();
for (i=0; i<n; i++) {
arc4random();
}
start = now() - start;
printf("%0.1f ME (%0.1f MB) / %0.3f sec = %0.1f MB/sec",
i / 1000000.0,
i * sizeof(int) / 1000000.0,
start,
i * sizeof(int) / 1000000.0 / start);
printf("\n\n");
}
#endif
void test_drand48(int n) {
int i;
double start=now();
for (i=0; i<n; i++) {
drand48();
}
start = now() - start;
printf("%0.1f ME (%0.1f MB) / %0.3f sec = %0.1f MB/sec",
i / 1000000.0,
i * 6 / 1000000.0,
start,
i * 6 / 1000000.0 / start);
printf("\n\n");
}
int main(int argc, char **argv) {
(void) argc; (void) argv;
crandom_features = crandom_detect_features();
unsigned int chacha_features = SSE2 | SSSE3 | XOP, aes_features = AESNI;
prg_generator<chacha> *ch = new prg_generator<chacha>(true);
prg_generator<aes> *ae = new prg_generator<aes>(true);
printf("****** Generators and processor features ******\n\n");
printf("chacha, direct, u_int32_t");
test<prg_generator<chacha>, u_int32_t, false>(100000000, SSE2 | SSSE3 | XOP, chacha_features, ch);
printf("chacha, direct, u_int32_t");
test<prg_generator<chacha>, u_int32_t, false>(100000000, SSE2 | SSSE3, chacha_features, ch);
printf("chacha, direct, u_int32_t");
test<prg_generator<chacha>, u_int32_t, false>(100000000, SSE2, chacha_features, ch);
printf("chacha, direct, u_int32_t");
test<prg_generator<chacha>, u_int32_t, false>(10000000, 0, chacha_features, ch);
printf("aes, direct, u_int32_t");
test<prg_generator<aes>, u_int32_t, false>(100000000, AESNI, aes_features, ae);
printf("aes, direct, u_int32_t");
test<prg_generator<aes>, u_int32_t, false>(10000000, 0, aes_features, ae);
printf("rand, int\n");
test_rand(100000000);
printf("random, int\n");
test_random(100000000);
#ifdef __MAC_10_0
printf("arc4random, int\n");
test_arc4random(10000000);
#endif
printf("drand48, int\n");
test_drand48(10000000);
printf("****** Data sizes ******\n\n");
printf("chacha, direct, u_int128_t");
test<prg_generator<chacha>, u_int128_t, false>(100000000, 0, 0, ch);
printf("chacha, direct, u_int8_t");
test<prg_generator<chacha>, u_int8_t, false>(100000000, 0, 0, ch);
printf("chacha, direct, float");
test<prg_generator<chacha>, float, false>(100000000, 0, 0, ch);
printf("chacha, direct, double");
test<prg_generator<chacha>, double, false>(100000000, 0, 0, ch);
printf("aes, direct, u_int128_t");
test<prg_generator<aes>, u_int128_t, false>(100000000, AESNI, aes_features, ae);
printf("aes, direct, u_int8_t");
test<prg_generator<aes>, u_int8_t, false>(100000000, 0, 0, ae);
printf("****** Indirection ******\n\n");
printf("aes, indirect, u_int32_t");
test<generator_base, u_int32_t, false>(100000000, 0, 0,
opacify(1) ? static_cast<generator_base *>(ae) : static_cast<generator_base *>(ch));
printf("chacha, indirect, u_int32_t");
test<generator_base, u_int32_t, false>(100000000, 0, 0,
opacify(1) ? static_cast<generator_base *>(ch) : static_cast<generator_base *>(ae));
printf("chacha, indirect, u_int32_t, slow case");
test<generator_base, u_int32_t, true>(100000000, 0, 0,
opacify(1) ? static_cast<generator_base *>(ch) : static_cast<generator_base *>(ae));
return 0;
}