xref: /minix/minix/tests/test53.c (revision 83133719)
1 #include <assert.h>
2 #include <minix/u64.h>
3 #include <setjmp.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9 
10 #define ERR err(__LINE__)
11 int max_error = 4;
12 #include "common.h"
13 
14 #define TIMED 0
15 
16 
17 static volatile int expect_SIGFPE;
18 static u64_t i, j, k;
19 static jmp_buf jmpbuf_SIGFPE, jmpbuf_main;
20 
21 static void err(int line)
22 {
23 	/* print error information */
24 	printf("error line %d; i=0x%.8lx%.8lx; j=0x%.8lx%.8lx; k=0x%.8lx%.8lx\n",
25 		line,
26 		ex64hi(i), ex64lo(i),
27 		ex64hi(j), ex64lo(j),
28 		ex64hi(k), ex64lo(k));
29 
30 	/* quit after too many errors */
31 	e(7);
32 }
33 
34 #define LENGTHOF(arr) (sizeof(arr) / sizeof(arr[0]))
35 
36 static u64_t getargval(int index, int *done)
37 {
38 	u32_t values[] = {
39 		/* corner cases */
40 		0,
41 		1,
42 		0x7fffffff,
43 		0x80000000,
44 		0x80000001,
45 		0xffffffff,
46 		/* random values */
47 		0xa9,
48 		0x0d88,
49 		0x242811,
50 		0xeb44d1bc,
51 		0x5b,
52 		0xfb50,
53 		0x569c02,
54 		0xb23c8f7d,
55 		0xc3,
56 		0x2366,
57 		0xfabb73,
58 		0xcb4e8aef,
59 		0xe9,
60 		0xffdc,
61 		0x05842d,
62 		0x3fff902d};
63 
64 	assert(done);
65 
66 	/* values with corner case and random 32-bit components */
67 	if (index < LENGTHOF(values) * LENGTHOF(values))
68 		return make64(values[index / LENGTHOF(values)], values[index % LENGTHOF(values)]);
69 
70 	index -= LENGTHOF(values) * LENGTHOF(values);
71 
72 	/* small numbers */
73 	if (index < 16) return make64(index + 2, 0);
74 	index -= 16;
75 
76 	/* big numbers */
77 	if (index < 16) return make64(-index - 2, -1);
78 	index -= 16;
79 
80 	/* powers of two */
81 	if (index < 14) return make64(1 << (index * 2 + 5), 0);
82 	index -= 14;
83 	if (index < 16) return make64(0, 1 << (index * 2 + 1));
84 	index -= 16;
85 
86 	/* done */
87 	*done = 1;
88 	return make64(0, 0);
89 }
90 
91 static void handler_SIGFPE(int signum)
92 {
93 	assert(signum == SIGFPE);
94 
95 	/* restore the signal handler */
96 	if (signal(SIGFPE, handler_SIGFPE) == SIG_ERR) ERR;
97 
98 	/* division by zero occurred, was this expected? */
99 	if (expect_SIGFPE) {
100 		/* expected: jump back to test */
101 		expect_SIGFPE = 0;
102 		longjmp(jmpbuf_SIGFPE, -1);
103 	} else {
104 		/* not expected: error and jump back to main */
105 		longjmp(jmpbuf_main, -1);
106 	}
107 
108 	/* not reachable */
109 	assert(0);
110 	exit(-1);
111 }
112 
113 static inline int bsr64(u64_t i)
114 {
115 	int index;
116 	u64_t mask;
117 
118 	for (index = 63, mask = 1ULL << 63; index >= 0; --index, mask >>= 1) {
119 	    if (i & mask)
120 		return index;
121 	}
122 
123 	return -1;
124 }
125 
126 static void testmul(void)
127 {
128 	int kdone, kidx;
129 	u32_t ilo = ex64lo(i), jlo = ex64lo(j);
130 	u64_t prod = i * j;
131 	int prodbits;
132 
133 	/* compute maximum index of highest-order bit */
134 	prodbits = bsr64(i) + bsr64(j) + 1;
135 	if (i == 0 || j == 0) prodbits = -1;
136 	if (bsr64(prod) > prodbits) ERR;
137 
138 	/* compare to 32-bit multiplication if possible */
139 	if (ex64hi(i) == 0 && ex64hi(j) == 0) {
140 		if (prod != (u64_t)ilo * jlo) ERR;
141 
142 		/* if there is no overflow we can check against pure 32-bit */
143 		if (prodbits < 32 && prod != ilo * jlo) ERR;
144 	}
145 
146 	/* in 32-bit arith low-order DWORD matches regardless of overflow */
147 	if (ex64lo(prod) != ilo * jlo) ERR;
148 
149 	/* multiplication by zero yields zero */
150 	if (prodbits < 0 && prod != 0) ERR;
151 
152 	/* if there is no overflow, check absence of zero divisors */
153 	if (prodbits >= 0 && prodbits < 64 && prod == 0) ERR;
154 
155 	/* commutativity */
156 	if (prod != j * i) ERR;
157 
158 	/* loop though all argument value combinations for third argument */
159 	for (kdone = 0, kidx = 0; k = getargval(kidx, &kdone), !kdone; kidx++) {
160 		/* associativity */
161 		if ((i * j) * k != i * (j * k)) ERR;
162 
163 		/* left and right distributivity */
164 		if ((i + j) * k != (i * k) + (j * k)) ERR;
165 		if (i * (j + k) != (i * j) + (i * k)) ERR;
166 	}
167 }
168 
169 static void testdiv0(void)
170 {
171 	int funcidx;
172 	u64_t res;
173 
174 	assert(j == 0);
175 
176 	/* loop through the 5 different division functions */
177 	for (funcidx = 0; funcidx < 5; funcidx++) {
178 		expect_SIGFPE = 1;
179 		if (setjmp(jmpbuf_SIGFPE) == 0) {
180 			/* divide by zero using various functions */
181 			switch (funcidx) {
182 				case 0: res = i / j;		ERR; break;
183 				case 1: res = i / ex64lo(j);	ERR; break;
184 				case 2: res = i / ex64lo(j);	ERR; break;
185 				case 3: res = i % j;		ERR; break;
186 				case 4: res = i % ex64lo(j);	ERR; break;
187 				default: assert(0);		ERR; break;
188 			}
189 
190 			/* if we reach this point there was no signal and an
191 			 * error has been recorded
192 			 */
193 			expect_SIGFPE = 0;
194 		} else {
195 			/* a signal has been received and expect_SIGFPE has
196 			 * been reset; all is ok now
197 			 */
198 			assert(!expect_SIGFPE);
199 		}
200 	}
201 }
202 
203 static void testdiv(void)
204 {
205 	u64_t q, r;
206 #if TIMED
207 	struct timeval tvstart, tvend;
208 
209 	printf("i=0x%.8x%.8x; j=0x%.8x%.8x\n",
210 		ex64hi(i), ex64lo(i),
211 		ex64hi(j), ex64lo(j));
212 	fflush(stdout);
213 	if (gettimeofday(&tvstart, NULL) < 0) ERR;
214 #endif
215 
216 	/* division by zero has a separate test */
217 	if (j == 0) {
218 		testdiv0();
219 		return;
220 	}
221 
222 	/* perform division, store q in k to make ERR more informative */
223 	q = i / j;
224 	r = i % j;
225 	k = q;
226 
227 #if TIMED
228 	if (gettimeofday(&tvend, NULL) < 0) ERR;
229 	tvend.tv_sec -= tvstart.tv_sec;
230 	tvend.tv_usec -= tvstart.tv_usec;
231 	if (tvend.tv_usec < 0) {
232 		tvend.tv_sec -= 1;
233 		tvend.tv_usec += 1000000;
234 	}
235 	printf("q=0x%.8x%.8x; r=0x%.8x%.8x; time=%d.%.6d\n",
236 		ex64hi(q), ex64lo(q),
237 		ex64hi(r), ex64lo(r),
238 		tvend.tv_sec, tvend.tv_usec);
239 	fflush(stdout);
240 #endif
241 
242 	/* compare to 64/32-bit division if possible */
243 	if (!ex64hi(j)) {
244 		if (q != i / ex64lo(j)) ERR;
245 		if (!ex64hi(q)) {
246 			if (q != i / ex64lo(j)) ERR;
247 		}
248 		if (r != i % ex64lo(j)) ERR;
249 
250 		/* compare to 32-bit division if possible */
251 		if (!ex64hi(i)) {
252 			if (q != ex64lo(i) / ex64lo(j)) ERR;
253 			if (r != ex64lo(i) % ex64lo(j)) ERR;
254 		}
255 	}
256 
257 	/* check results using i = q j + r and r < j */
258 	if (i != (q * j) + r) ERR;
259 	if (r >= j) ERR;
260 }
261 
262 static void test(void)
263 {
264 	int idone, jdone, iidx, jidx;
265 
266 	/* loop though all argument value combinations */
267 	for (idone = 0, iidx = 0; i = getargval(iidx, &idone), !idone; iidx++)
268 	for (jdone = 0, jidx = 0; j = getargval(jidx, &jdone), !jdone; jidx++) {
269 		testmul();
270 		testdiv();
271 	}
272 }
273 
274 int main(void)
275 {
276 	start(53);
277 
278 	/* set up signal handler to deal with div by zero */
279 	if (setjmp(jmpbuf_main) == 0) {
280 		if (signal(SIGFPE, handler_SIGFPE) == SIG_ERR) ERR;
281 
282 		/* perform tests */
283 		test();
284 	} else {
285 		/* an unexpected SIGFPE has occurred */
286 		ERR;
287 	}
288 
289 	/* this was all */
290 	quit();
291 
292 	return(-1);	/* Unreachable */
293 }
294