1 |
/* |
2 |
* randomtest.C: brief test program for 32-bit and 64-bit output of SFMT. |
3 |
* |
4 |
* @author Mutsuo Saito (Hiroshima-univ) |
5 |
* |
6 |
* Copyright © 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima |
7 |
* University. All rights reserved. |
8 |
* |
9 |
* The new BSD License is applied to this software, see doc/poddoc/license.pod |
10 |
*/ |
11 |
|
12 |
#include "unit/test.h" |
13 |
|
14 |
#ifndef TIMING |
15 |
#define TIMING 0 |
16 |
#endif |
17 |
#ifndef VERBOSE |
18 |
#define VERBOSE 0 |
19 |
#endif |
20 |
|
21 |
#define BLOCK_SIZE 100000 |
22 |
#define BLOCK_SIZE64 50000 |
23 |
#define COUNT 1000 |
24 |
|
25 |
#if defined(HAVE_ALTIVEC) |
26 |
static vector unsigned int array1[BLOCK_SIZE / 4]; |
27 |
static vector unsigned int array2[10000 / 4]; |
28 |
#elif defined(HAVE_SSE2) |
29 |
static __m128i array1[BLOCK_SIZE / 4]; |
30 |
static __m128i array2[10000 / 4]; |
31 |
#else |
32 |
static uint64_t array1[BLOCK_SIZE / 4][2]; |
33 |
static uint64_t array2[10000 / 4][2]; |
34 |
#endif |
35 |
|
36 |
#ifndef ONLY64 |
37 |
test<1> (void) |
38 |
{ |
39 |
int i; |
40 |
uint32_t *array32 = reinterpret_cast<uint32_t *> (array1); |
41 |
uint32_t *array32_2 = reinterpret_cast<uint32_t *> (array2); |
42 |
uint32_t ini[4] = { 0x1234, 0x5678, 0x9abc, 0xdef0 }; |
43 |
uint32_t r32; |
44 |
|
45 |
if (get_min_array_size32 () > 10000) |
46 |
{ |
47 |
#if VERBOSE |
48 |
printf ("array size too small!\n"); |
49 |
#endif |
50 |
exit (1); |
51 |
} |
52 |
#if VERBOSE |
53 |
printf ("%s\n32 bit generated randoms\n", get_idstring ()); |
54 |
printf ("init_gen_rand__________\n"); |
55 |
#endif |
56 |
/* 32 bit generation */ |
57 |
init_gen_rand (1234); |
58 |
fill_array32 (array32, 10000); |
59 |
fill_array32 (array32_2, 10000); |
60 |
init_gen_rand (1234); |
61 |
for (i = 0; i < 10000; i++) |
62 |
{ |
63 |
#if VERBOSE |
64 |
if (i < 1000) |
65 |
{ |
66 |
printf ("%10u ", array32[i]); |
67 |
if (i % 5 == 4) |
68 |
{ |
69 |
printf ("\n"); |
70 |
} |
71 |
} |
72 |
#endif |
73 |
r32 = gen_rand32 (); |
74 |
#if VERBOSE |
75 |
if (r32 != array32[i]) |
76 |
{ |
77 |
printf ("\nmismatch at %d array32:%x gen:%x\n", i, array32[i], r32); |
78 |
exit (1); |
79 |
} |
80 |
#endif |
81 |
} |
82 |
for (i = 0; i < 700; i++) |
83 |
{ |
84 |
r32 = gen_rand32 (); |
85 |
#if VERBOSE |
86 |
if (r32 != array32_2[i]) |
87 |
{ |
88 |
printf ("\nmismatch at %d array32_2:%x gen:%x\n", i, array32_2[i], r32); |
89 |
exit (1); |
90 |
} |
91 |
#endif |
92 |
} |
93 |
#if VERBOSE |
94 |
printf ("\n"); |
95 |
#endif |
96 |
init_by_array (ini, 4); |
97 |
#if VERBOSE |
98 |
printf ("init_by_array__________\n"); |
99 |
#endif |
100 |
fill_array32 (array32, 10000); |
101 |
fill_array32 (array32_2, 10000); |
102 |
init_by_array (ini, 4); |
103 |
for (i = 0; i < 10000; i++) |
104 |
{ |
105 |
#if VERBOSE |
106 |
if (i < 1000) |
107 |
{ |
108 |
printf ("%10u ", array32[i]); |
109 |
if (i % 5 == 4) |
110 |
{ |
111 |
printf ("\n"); |
112 |
} |
113 |
} |
114 |
#endif |
115 |
r32 = gen_rand32 (); |
116 |
if (r32 != array32[i]) |
117 |
{ |
118 |
#if VERBOSE |
119 |
printf ("\nmismatch at %d array32:%x gen:%x\n", i, array32[i], r32); |
120 |
#endif |
121 |
exit (1); |
122 |
} |
123 |
} |
124 |
for (i = 0; i < 700; i++) |
125 |
{ |
126 |
r32 = gen_rand32 (); |
127 |
if (r32 != array32_2[i]) |
128 |
{ |
129 |
#if VERBOSE |
130 |
printf ("\nmismatch at %d array32_2:%x gen:%x\n", i, array32_2[i], r32); |
131 |
#endif |
132 |
exit (1); |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
#if TIMING |
138 |
test<2> (void) |
139 |
{ |
140 |
int i, j; |
141 |
clock_t clo; |
142 |
clock_t min = LONG_MAX; |
143 |
uint32_t *array32 = static_cast<uint32_t *> (array1); |
144 |
|
145 |
if (get_min_array_size32 () > BLOCK_SIZE) |
146 |
{ |
147 |
#if VERBOSE |
148 |
printf ("array size too small!\n"); |
149 |
#endif |
150 |
exit (1); |
151 |
} |
152 |
/* 32 bit generation */ |
153 |
init_gen_rand (1234); |
154 |
for (i = 0; i < 10; i++) |
155 |
{ |
156 |
clo = clock (); |
157 |
for (j = 0; j < COUNT; j++) |
158 |
{ |
159 |
fill_array32 (array32, BLOCK_SIZE); |
160 |
} |
161 |
clo = clock () - clo; |
162 |
if (clo < min) |
163 |
{ |
164 |
min = clo; |
165 |
} |
166 |
} |
167 |
#if VERBOSE |
168 |
printf ("32 bit BLOCK:%.0f", (double) min * 1000 / CLOCKS_PER_SEC); |
169 |
printf ("ms for %u randoms generation\n", BLOCK_SIZE * COUNT); |
170 |
#endif |
171 |
min = LONG_MAX; |
172 |
init_gen_rand (1234); |
173 |
for (i = 0; i < 10; i++) |
174 |
{ |
175 |
clo = clock (); |
176 |
for (j = 0; j < BLOCK_SIZE * COUNT; j++) |
177 |
{ |
178 |
gen_rand32 (); |
179 |
} |
180 |
clo = clock () - clo; |
181 |
if (clo < min) |
182 |
{ |
183 |
min = clo; |
184 |
} |
185 |
} |
186 |
#if VERBOSE |
187 |
printf ("32 bit SEQUE:%.0f", (double) min * 1000 / CLOCKS_PER_SEC); |
188 |
printf ("ms for %u randoms generation\n", BLOCK_SIZE * COUNT); |
189 |
#endif |
190 |
} |
191 |
#else |
192 |
test<2> () |
193 |
{ |
194 |
} |
195 |
#endif |
196 |
#endif |
197 |
|
198 |
test<3> (void) |
199 |
{ |
200 |
int i; |
201 |
uint64_t *array64; |
202 |
uint64_t *array64_2; |
203 |
uint64_t r; |
204 |
uint32_t ini[] = { 5, 4, 3, 2, 1 }; |
205 |
|
206 |
array64 = reinterpret_cast<uint64_t *> (array1); |
207 |
array64_2 = reinterpret_cast<uint64_t *> (array2); |
208 |
if (get_min_array_size64 () > 5000) |
209 |
{ |
210 |
#if VERBOSE |
211 |
printf ("array size too small!\n"); |
212 |
#endif |
213 |
exit (1); |
214 |
} |
215 |
#if VERBOSE |
216 |
printf ("%s\n64 bit generated randoms\n", get_idstring ()); |
217 |
printf ("init_gen_rand__________\n"); |
218 |
#endif |
219 |
/* 64 bit generation */ |
220 |
init_gen_rand (4321); |
221 |
fill_array64 (array64, 5000); |
222 |
fill_array64 (array64_2, 5000); |
223 |
init_gen_rand (4321); |
224 |
for (i = 0; i < 5000; i++) |
225 |
{ |
226 |
if (i < 1000) |
227 |
{ |
228 |
#if VERBOSE |
229 |
printf ("%20" PRIu64 " ", array64[i]); |
230 |
if (i % 3 == 2) |
231 |
{ |
232 |
printf ("\n"); |
233 |
} |
234 |
#endif |
235 |
} |
236 |
r = gen_rand64 (); |
237 |
if (r != array64[i]) |
238 |
{ |
239 |
#if VERBOSE |
240 |
printf ("\nmismatch at %d array64:%" PRIx64 " gen:%" PRIx64 "\n", i, array64[i], r); |
241 |
#endif |
242 |
exit (1); |
243 |
} |
244 |
} |
245 |
#if VERBOSE |
246 |
printf ("\n"); |
247 |
#endif |
248 |
for (i = 0; i < 700; i++) |
249 |
{ |
250 |
r = gen_rand64 (); |
251 |
if (r != array64_2[i]) |
252 |
{ |
253 |
#if VERBOSE |
254 |
printf ("\nmismatch at %d array64_2:%" PRIx64 " gen:%" PRIx64 "\n", i, array64_2[i], r); |
255 |
#endif |
256 |
exit (1); |
257 |
} |
258 |
} |
259 |
#if VERBOSE |
260 |
printf ("init_by_array__________\n"); |
261 |
#endif |
262 |
/* 64 bit generation */ |
263 |
init_by_array (ini, 5); |
264 |
fill_array64 (array64, 5000); |
265 |
fill_array64 (array64_2, 5000); |
266 |
init_by_array (ini, 5); |
267 |
for (i = 0; i < 5000; i++) |
268 |
{ |
269 |
#if VERBOSE |
270 |
if (i < 1000) |
271 |
{ |
272 |
printf ("%20" PRIu64 " ", array64[i]); |
273 |
if (i % 3 == 2) |
274 |
{ |
275 |
printf ("\n"); |
276 |
} |
277 |
} |
278 |
#endif |
279 |
r = gen_rand64 (); |
280 |
if (r != array64[i]) |
281 |
{ |
282 |
#if VERBOSE |
283 |
printf ("\nmismatch at %d array64:%" PRIx64 " gen:%" PRIx64 "\n", i, array64[i], r); |
284 |
#endif |
285 |
exit (1); |
286 |
} |
287 |
} |
288 |
#if VERBOSE |
289 |
printf ("\n"); |
290 |
#endif |
291 |
for (i = 0; i < 700; i++) |
292 |
{ |
293 |
r = gen_rand64 (); |
294 |
if (r != array64_2[i]) |
295 |
{ |
296 |
#if VERBOSE |
297 |
printf ("\nmismatch at %d array64_2:%" PRIx64 " gen:%" PRIx64 "\n", i, array64_2[i], r); |
298 |
#endif |
299 |
exit (1); |
300 |
} |
301 |
} |
302 |
} |
303 |
|
304 |
#if TIMING |
305 |
test<4> (void) |
306 |
{ |
307 |
int i, j; |
308 |
uint64_t clo; |
309 |
uint64_t min = LONG_MAX; |
310 |
uint64_t *array64 = static_cast<uint64_t *> (array1); |
311 |
|
312 |
if (get_min_array_size64 () > BLOCK_SIZE64) |
313 |
{ |
314 |
#if VERBOSE |
315 |
printf ("array size too small!\n"); |
316 |
#endif |
317 |
exit (1); |
318 |
} |
319 |
/* 64 bit generation */ |
320 |
init_gen_rand (1234); |
321 |
for (i = 0; i < 10; i++) |
322 |
{ |
323 |
clo = clock (); |
324 |
for (j = 0; j < COUNT; j++) |
325 |
{ |
326 |
fill_array64 (array64, BLOCK_SIZE64); |
327 |
} |
328 |
clo = clock () - clo; |
329 |
if (clo < min) |
330 |
{ |
331 |
min = clo; |
332 |
} |
333 |
} |
334 |
#if VERBOSE |
335 |
printf ("64 bit BLOCK:%.0f", (double) min * 1000 / CLOCKS_PER_SEC); |
336 |
printf ("ms for %u randoms generation\n", BLOCK_SIZE64 * COUNT); |
337 |
#endif |
338 |
min = LONG_MAX; |
339 |
init_gen_rand (1234); |
340 |
for (i = 0; i < 10; i++) |
341 |
{ |
342 |
clo = clock (); |
343 |
for (j = 0; j < BLOCK_SIZE64 * COUNT; j++) |
344 |
{ |
345 |
gen_rand64 (); |
346 |
} |
347 |
clo = clock () - clo; |
348 |
if (clo < min) |
349 |
{ |
350 |
min = clo; |
351 |
} |
352 |
} |
353 |
#if VERBOSE |
354 |
printf ("64 bit SEQUE:%.0f", (double) min * 1000 / CLOCKS_PER_SEC); |
355 |
printf ("ms for %u randoms generation\n", BLOCK_SIZE64 * COUNT); |
356 |
#endif |
357 |
} |
358 |
#else |
359 |
test<4> () |
360 |
{ |
361 |
} |
362 |
#endif |
363 |
|
364 |
#include "unit/runtest.h" |