1*9b17ae9cSRichard Henderson #include <assert.h>
2*9b17ae9cSRichard Henderson #include <limits.h>
3*9b17ae9cSRichard Henderson
4*9b17ae9cSRichard Henderson struct TestS {
5*9b17ae9cSRichard Henderson long x, y, q, r;
6*9b17ae9cSRichard Henderson };
7*9b17ae9cSRichard Henderson
8*9b17ae9cSRichard Henderson static struct TestS test_s[] = {
9*9b17ae9cSRichard Henderson { 4, 2, 2, 0 }, /* normal cases */
10*9b17ae9cSRichard Henderson { 9, 7, 1, 2 },
11*9b17ae9cSRichard Henderson { 0, 0, -1, 0 }, /* div by zero cases */
12*9b17ae9cSRichard Henderson { 9, 0, -1, 9 },
13*9b17ae9cSRichard Henderson { LONG_MIN, -1, LONG_MIN, 0 }, /* overflow case */
14*9b17ae9cSRichard Henderson };
15*9b17ae9cSRichard Henderson
16*9b17ae9cSRichard Henderson struct TestU {
17*9b17ae9cSRichard Henderson unsigned long x, y, q, r;
18*9b17ae9cSRichard Henderson };
19*9b17ae9cSRichard Henderson
20*9b17ae9cSRichard Henderson static struct TestU test_u[] = {
21*9b17ae9cSRichard Henderson { 4, 2, 2, 0 }, /* normal cases */
22*9b17ae9cSRichard Henderson { 9, 7, 1, 2 },
23*9b17ae9cSRichard Henderson { 0, 0, ULONG_MAX, 0 }, /* div by zero cases */
24*9b17ae9cSRichard Henderson { 9, 0, ULONG_MAX, 9 },
25*9b17ae9cSRichard Henderson };
26*9b17ae9cSRichard Henderson
27*9b17ae9cSRichard Henderson #define ARRAY_SIZE(X) (sizeof(X) / sizeof(*(X)))
28*9b17ae9cSRichard Henderson
main(void)29*9b17ae9cSRichard Henderson int main (void)
30*9b17ae9cSRichard Henderson {
31*9b17ae9cSRichard Henderson int i;
32*9b17ae9cSRichard Henderson
33*9b17ae9cSRichard Henderson for (i = 0; i < ARRAY_SIZE(test_s); i++) {
34*9b17ae9cSRichard Henderson long q, r;
35*9b17ae9cSRichard Henderson
36*9b17ae9cSRichard Henderson asm("div %0, %2, %3\n\t"
37*9b17ae9cSRichard Henderson "rem %1, %2, %3"
38*9b17ae9cSRichard Henderson : "=&r" (q), "=r" (r)
39*9b17ae9cSRichard Henderson : "r" (test_s[i].x), "r" (test_s[i].y));
40*9b17ae9cSRichard Henderson
41*9b17ae9cSRichard Henderson assert(q == test_s[i].q);
42*9b17ae9cSRichard Henderson assert(r == test_s[i].r);
43*9b17ae9cSRichard Henderson }
44*9b17ae9cSRichard Henderson
45*9b17ae9cSRichard Henderson for (i = 0; i < ARRAY_SIZE(test_u); i++) {
46*9b17ae9cSRichard Henderson unsigned long q, r;
47*9b17ae9cSRichard Henderson
48*9b17ae9cSRichard Henderson asm("divu %0, %2, %3\n\t"
49*9b17ae9cSRichard Henderson "remu %1, %2, %3"
50*9b17ae9cSRichard Henderson : "=&r" (q), "=r" (r)
51*9b17ae9cSRichard Henderson : "r" (test_u[i].x), "r" (test_u[i].y));
52*9b17ae9cSRichard Henderson
53*9b17ae9cSRichard Henderson assert(q == test_u[i].q);
54*9b17ae9cSRichard Henderson assert(r == test_u[i].r);
55*9b17ae9cSRichard Henderson }
56*9b17ae9cSRichard Henderson
57*9b17ae9cSRichard Henderson return 0;
58*9b17ae9cSRichard Henderson }
59