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
|
#include <assert.h>
#include <minix/u64.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define ERR err(__LINE__)
int max_error = 4;
#include "common.h"
#define TIMED 0
static volatile int expect_SIGFPE;
static u64_t i, j, k;
static jmp_buf jmpbuf_SIGFPE, jmpbuf_main;
static void err(int line)
{
/* print error information */
printf("error line %d; i=0x%.8lx%.8lx; j=0x%.8lx%.8lx; k=0x%.8lx%.8lx\n",
line,
ex64hi(i), ex64lo(i),
ex64hi(j), ex64lo(j),
ex64hi(k), ex64lo(k));
/* quit after too many errors */
e(7);
}
#define LENGTHOF(arr) (sizeof(arr) / sizeof(arr[0]))
static u64_t getargval(int index, int *done)
{
u32_t values[] = {
/* corner cases */
0,
1,
0x7fffffff,
0x80000000,
0x80000001,
0xffffffff,
/* random values */
0xa9,
0x0d88,
0x242811,
0xeb44d1bc,
0x5b,
0xfb50,
0x569c02,
0xb23c8f7d,
0xc3,
0x2366,
0xfabb73,
0xcb4e8aef,
0xe9,
0xffdc,
0x05842d,
0x3fff902d};
assert(done);
/* values with corner case and random 32-bit components */
if (index < LENGTHOF(values) * LENGTHOF(values))
return make64(values[index / LENGTHOF(values)], values[index % LENGTHOF(values)]);
index -= LENGTHOF(values) * LENGTHOF(values);
/* small numbers */
if (index < 16) return make64(index + 2, 0);
index -= 16;
/* big numbers */
if (index < 16) return make64(-index - 2, -1);
index -= 16;
/* powers of two */
if (index < 14) return make64(1 << (index * 2 + 5), 0);
index -= 14;
if (index < 16) return make64(0, 1 << (index * 2 + 1));
index -= 16;
/* done */
*done = 1;
return make64(0, 0);
}
static void handler_SIGFPE(int signum)
{
assert(signum == SIGFPE);
/* restore the signal handler */
if (signal(SIGFPE, handler_SIGFPE) == SIG_ERR) ERR;
/* division by zero occurred, was this expected? */
if (expect_SIGFPE) {
/* expected: jump back to test */
expect_SIGFPE = 0;
longjmp(jmpbuf_SIGFPE, -1);
} else {
/* not expected: error and jump back to main */
longjmp(jmpbuf_main, -1);
}
/* not reachable */
assert(0);
exit(-1);
}
static inline int bsr64(u64_t i)
{
int index;
u64_t mask;
for (index = 63, mask = 1ULL << 63; index >= 0; --index, mask >>= 1) {
if (i & mask)
return index;
}
return -1;
}
static void testmul(void)
{
int kdone, kidx;
u32_t ilo = ex64lo(i), jlo = ex64lo(j);
u64_t prod = i * j;
int prodbits;
/* compute maximum index of highest-order bit */
prodbits = bsr64(i) + bsr64(j) + 1;
if (i == 0 || j == 0) prodbits = -1;
if (bsr64(prod) > prodbits) ERR;
/* compare to 32-bit multiplication if possible */
if (ex64hi(i) == 0 && ex64hi(j) == 0) {
if (prod != (u64_t)ilo * jlo) ERR;
/* if there is no overflow we can check against pure 32-bit */
if (prodbits < 32 && prod != ilo * jlo) ERR;
}
/* in 32-bit arith low-order DWORD matches regardless of overflow */
if (ex64lo(prod) != ilo * jlo) ERR;
/* multiplication by zero yields zero */
if (prodbits < 0 && prod != 0) ERR;
/* if there is no overflow, check absence of zero divisors */
if (prodbits >= 0 && prodbits < 64 && prod == 0) ERR;
/* commutativity */
if (prod != j * i) ERR;
/* loop though all argument value combinations for third argument */
for (kdone = 0, kidx = 0; k = getargval(kidx, &kdone), !kdone; kidx++) {
/* associativity */
if ((i * j) * k != i * (j * k)) ERR;
/* left and right distributivity */
if ((i + j) * k != (i * k) + (j * k)) ERR;
if (i * (j + k) != (i * j) + (i * k)) ERR;
}
}
static void do_not_optimize_away(volatile u64_t * ptr)
{
/* TODO: does this actually do the job? */
*ptr ^= 1;
}
static void testdiv0(void)
{
int funcidx;
u64_t res;
assert(j == 0);
/* loop through the 5 different division functions */
for (funcidx = 0; funcidx < 5; funcidx++) {
expect_SIGFPE = 1;
if (setjmp(jmpbuf_SIGFPE) == 0) {
/* divide by zero using various functions */
switch (funcidx) {
case 0: res = i / j; ERR; break;
case 1: res = i / ex64lo(j); ERR; break;
case 2: res = i / ex64lo(j); ERR; break;
case 3: res = i % j; ERR; break;
case 4: res = i % ex64lo(j); ERR; break;
default: assert(0); ERR; break;
}
do_not_optimize_away((volatile u64_t *)&res);
/* if we reach this point there was no signal and an
* error has been recorded
*/
expect_SIGFPE = 0;
} else {
/* a signal has been received and expect_SIGFPE has
* been reset; all is ok now
*/
assert(!expect_SIGFPE);
}
}
}
static void testdiv(void)
{
u64_t q, r;
#if TIMED
struct timeval tvstart, tvend;
printf("i=0x%.8x%.8x; j=0x%.8x%.8x\n",
ex64hi(i), ex64lo(i),
ex64hi(j), ex64lo(j));
fflush(stdout);
if (gettimeofday(&tvstart, NULL) < 0) ERR;
#endif
/* division by zero has a separate test */
if (j == 0) {
testdiv0();
return;
}
/* perform division, store q in k to make ERR more informative */
q = i / j;
r = i % j;
k = q;
#if TIMED
if (gettimeofday(&tvend, NULL) < 0) ERR;
tvend.tv_sec -= tvstart.tv_sec;
tvend.tv_usec -= tvstart.tv_usec;
if (tvend.tv_usec < 0) {
tvend.tv_sec -= 1;
tvend.tv_usec += 1000000;
}
printf("q=0x%.8x%.8x; r=0x%.8x%.8x; time=%d.%.6d\n",
ex64hi(q), ex64lo(q),
ex64hi(r), ex64lo(r),
tvend.tv_sec, tvend.tv_usec);
fflush(stdout);
#endif
/* compare to 64/32-bit division if possible */
if (!ex64hi(j)) {
if (q != i / ex64lo(j)) ERR;
if (!ex64hi(q)) {
if (q != i / ex64lo(j)) ERR;
}
if (r != i % ex64lo(j)) ERR;
/* compare to 32-bit division if possible */
if (!ex64hi(i)) {
if (q != ex64lo(i) / ex64lo(j)) ERR;
if (r != ex64lo(i) % ex64lo(j)) ERR;
}
}
/* check results using i = q j + r and r < j */
if (i != (q * j) + r) ERR;
if (r >= j) ERR;
}
static void test(void)
{
int idone, jdone, iidx, jidx;
/* loop though all argument value combinations */
for (idone = 0, iidx = 0; i = getargval(iidx, &idone), !idone; iidx++)
for (jdone = 0, jidx = 0; j = getargval(jidx, &jdone), !jdone; jidx++) {
testmul();
testdiv();
}
}
int main(void)
{
start(53);
/* set up signal handler to deal with div by zero */
if (setjmp(jmpbuf_main) == 0) {
if (signal(SIGFPE, handler_SIGFPE) == SIG_ERR) ERR;
/* perform tests */
test();
} else {
/* an unexpected SIGFPE has occurred */
ERR;
}
/* this was all */
quit();
return(-1); /* Unreachable */
}
|