-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
366 lines (287 loc) · 7.09 KB
/
test.c
File metadata and controls
366 lines (287 loc) · 7.09 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* libmath64 test (not pretty)
*
* $Id: test.c,v 1.13 2001/09/29 14:15:20 zapek Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <proto/exec.h>
#include <devices/timer.h>
#include <proto/timer.h>
static struct timerequest tio;
#include "math64.h"
#define LOOPTIMES 5000 /* reasonable value */
struct in {
union {
QWORD q;
UQWORD uq;
double d;
LONG i;
ULONG ui;
} a;
union {
QWORD q;
UQWORD uq;
double d;
LONG i;
ULONG ui;
} b;
};
struct out {
union {
QWORD q;
UQWORD uq;
double d;
LONG i;
ULONG ui;
} r;
};
static struct in *in_array;
static struct out *out_array;
enum {
FI_INT,
FI_UINT,
FI_DOUBLE,
FI_QWORD,
FI_UQWORD
};
void fill_in(struct in *ina, int modea, int modeb)
{
ULONG i;
for (i = 0; i < LOOPTIMES; i++)
{
switch (modea)
{
case FI_DOUBLE:
ina->a.d = rand();
break;
case FI_INT:
ina->a.i = rand();
break;
case FI_UINT:
ina->a.ui = rand();
break;
case FI_QWORD:
ina->a.q.hi = rand();
ina->a.q.lo = rand();
break;
case FI_UQWORD:
ina->a.uq.hi = rand();
ina->a.uq.lo = rand();
break;
}
switch (modeb)
{
case FI_DOUBLE:
ina->b.d = rand();
break;
case FI_INT:
ina->b.i = rand();
break;
case FI_UINT:
ina->b.ui = rand();
break;
case FI_QWORD:
ina->b.q.hi = rand();
ina->b.q.lo = rand();
break;
case FI_UQWORD:
ina->b.uq.hi = rand();
ina->b.uq.lo = rand();
break;
}
ina++;
}
}
enum {
PE_NONE,
PE_DIE
};
void puterror(STRPTR s, int f)
{
printf("error: %s\n", s);
if (f & PE_DIE)
{
exit(0);
}
}
struct Library *TimerBase;
static struct EClockVal start_timed;
ULONG timedm( void )
{
struct EClockVal ev;
ULONG r;
QWORD ret;
r = ReadEClock( &ev );
q_sub( ( QWORD * )&ev, ( QWORD * )&start_timed, &ret );
return( ( ULONG )q_div( &ret, r / 1000 ) );
}
#define FBUFSIZE 64
void test_divu(void)
{
ULONG i;
ULONG fail = 0;
ULONG start, stop;
char buf1[FBUFSIZE]; /* yeah well, but SAS/C's %g.xx is buggy anyway */
char buf2[FBUFSIZE];
fill_in(in_array, FI_UQWORD, FI_UINT);
start = timedm();
for (i = 0; i <LOOPTIMES; i++)
{
out_array[i].r.ui = q_divu(&in_array[i].a.uq, in_array[i].b.ui);
}
stop = timedm();
printf("elapsed time: %lds %ldms\n", (stop - start) / 1000, (stop - start) % 1000);
for (i = 0; i < LOOPTIMES; i++)
{
memset(buf1, '\0', FBUFSIZE);
memset(buf2, '\0', FBUFSIZE);
sprintf(buf1, "%g", (double)out_array[i].r.ui);
sprintf(buf2, "%g", uqtoud(&in_array[i].a.uq) / (double)in_array[i].b.ui);
if (strcmp(buf1, buf2))
{
printf("error: integer %s does not equal float %s\n", buf1, buf2);
printf("(quad: hi:%lu lo:%lu) %g / %lu == %g\n", in_array[i].a.uq.hi, in_array[i].a.uq.lo, uqtoud(&in_array[i].a.uq), in_array[i].b.ui, uqtoud(&in_array[i].a.uq) / in_array[i].b.ui);
fail++;
}
}
if (fail)
{
printf("test_divu(): %lu tests out of %lu failed\n", fail, LOOPTIMES);
}
}
void test_divu64(void)
{
ULONG i;
ULONG fail = 0;
ULONG start, stop;
char buf1[FBUFSIZE]; /* yeah well, but SAS/C's %g.xx is buggy anyway */
char buf2[FBUFSIZE];
fill_in(in_array, FI_UQWORD, FI_UINT);
start = timedm();
for (i = 0; i <LOOPTIMES; i++)
{
q_divu64(&in_array[i].a.uq, in_array[i].b.ui, &out_array[i].r.uq);
}
stop = timedm();
printf("elapsed time: %lds %ldms\n", (stop - start) / 1000, (stop - start) % 1000);
for (i = 0; i < LOOPTIMES; i++)
{
memset(buf1, '\0', FBUFSIZE);
memset(buf2, '\0', FBUFSIZE);
sprintf(buf1, "%g", uqtoud(&out_array[i].r.uq));
sprintf(buf2, "%g", uqtoud(&in_array[i].a.uq) / (double)in_array[i].b.ui);
if (strcmp(buf1, buf2))
{
printf("error: integer %s does not equal float %s\n", buf1, buf2);
printf("(quad: hi:%lu lo:%lu) %g / %lu == %g\n", in_array[i].a.uq.hi, in_array[i].a.uq.lo, uqtoud(&in_array[i].a.uq), in_array[i].b.ui, uqtoud(&in_array[i].a.uq) / in_array[i].b.ui);
fail++;
}
}
if (fail)
{
printf("test_divu64(): %lu tests out of %lu failed\n", fail, LOOPTIMES);
}
}
void test_mul020(void)
{
ULONG i;
ULONG fail = 0;
ULONG start, stop;
char buf1[FBUFSIZE]; /* yeah well, but SAS/C's %g.xx is buggy anyway */
char buf2[FBUFSIZE];
fill_in(in_array, FI_INT, FI_INT);
start = timedm();
for (i = 0; i <LOOPTIMES; i++)
{
q_mul020(in_array[i].a.i, in_array[i].b.i, &out_array[i].r.q);
}
stop = timedm();
printf("elapsed time: %lds %ldms\n", (stop - start) / 1000, (stop - start) % 1000);
for (i = 0; i < LOOPTIMES; i++)
{
memset(buf1, '\0', FBUFSIZE);
memset(buf2, '\0', FBUFSIZE);
sprintf(buf1, "%g", qtod(&out_array[i].r.q));
sprintf(buf2, "%g", (double)in_array[i].a.i * (double)in_array[i].b.i);
if (strcmp(buf1, buf2))
{
printf("error: integer %s does not equal float %s\n", buf1, buf2);
printf("in: %ld * %ld == %g\n", in_array[i].a.i, in_array[i].b.i, qtod(&out_array[i].r.q));
fail++;
}
}
if (fail)
{
printf("test_mul020(): %lu tests out of %lu failed\n", fail, LOOPTIMES);
}
}
void test_mulu(void)
{
ULONG i;
ULONG fail = 0;
ULONG start, stop;
char buf1[FBUFSIZE]; /* yeah well, but SAS/C's %g.xx is buggy anyway */
char buf2[FBUFSIZE];
fill_in(in_array, FI_INT, FI_INT);
start = timedm();
for (i = 0; i <LOOPTIMES; i++)
{
q_mulu(in_array[i].a.i, in_array[i].b.i, &out_array[i].r.q);
}
stop = timedm();
printf("elapsed time: %lds %ldms\n", (stop - start) / 1000, (stop - start) % 1000);
for (i = 0; i < LOOPTIMES; i++)
{
memset(buf1, '\0', FBUFSIZE);
memset(buf2, '\0', FBUFSIZE);
sprintf(buf1, "%g", qtod(&out_array[i].r.q));
sprintf(buf2, "%g", (double)in_array[i].a.i * (double)in_array[i].b.i);
if (strcmp(buf1, buf2))
{
printf("error: integer %s does not equal float %s\n", buf1, buf2);
printf("in: %ld * %ld == %g\n", in_array[i].a.i, in_array[i].b.i, qtod(&out_array[i].r.q));
fail++;
}
}
if (fail)
{
printf("test_mulu(): %lu tests out of %lu failed\n", fail, LOOPTIMES);
}
}
main()
{
struct timeval tv;
if (!(in_array = malloc((LOOPTIMES) * sizeof(struct in))))
puterror("not enough memory", PE_DIE);
if (!(out_array = malloc((LOOPTIMES) * sizeof(struct out))))
puterror("not enough memory", PE_DIE);
if (tio.tr_node.io_Message.mn_ReplyPort = CreateMsgPort())
{
if (OpenDevice(TIMERNAME, UNIT_MICROHZ, (struct IORequest *)&tio, 0) == 0)
{
ULONG start, stop;
TimerBase = (struct Library *)tio.tr_node.io_Device;
ReadEClock( &start_timed );
GetSysTime(&tv);
srand(tv.tv_micro);
printf("libmath64 tester\n\nnumber of tests per suite: %ld\n", LOOPTIMES);
//printf("testing 64-bit division with 32-bit result..\n");
//test_divu();
//printf("test finished\n");
printf("------\n64-bit division with 64-bit result..\n");
test_divu64();
printf("test finished\n");
printf("------\ntesting 32-bit multiplication with 64-bit result (020-040 hardware instruction)..\n");
test_mul020();
printf("test finished\n");
printf("------\ntesting 32-bit multiplication with 64-bit result..\n");
test_mulu();
printf("test finished\n");
CloseDevice((struct IORequest *)&tio);
}
DeleteMsgPort(tio.tr_node.io_Message.mn_ReplyPort);
}
}