1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_divdf3
3 
4 #include "int_lib.h"
5 #include <stdio.h>
6 
7 #include "fp_test.h"
8 
9 // Returns: a / b
10 COMPILER_RT_ABI double __divdf3(double a, double b);
11 
test__divdf3(double a,double b,uint64_t expected)12 int test__divdf3(double a, double b, uint64_t expected)
13 {
14     double x = __divdf3(a, b);
15     int ret = compareResultD(x, expected);
16 
17     if (ret){
18         printf("error in test__divdf3(%.20e, %.20e) = %.20e, "
19                "expected %.20e\n", a, b, x,
20                fromRep64(expected));
21     }
22     return ret;
23 }
24 
main()25 int main()
26 {
27     // Returned NaNs are assumed to be qNaN by default
28 
29     // qNaN / any = qNaN
30     if (test__divdf3(makeQNaN64(), 3., UINT64_C(0x7ff8000000000000)))
31       return 1;
32     // NaN / any = NaN
33     if (test__divdf3(makeNaN64(UINT64_C(0x123)), 3., UINT64_C(0x7ff8000000000000)))
34       return 1;
35     // any / qNaN = qNaN
36     if (test__divdf3(3., makeQNaN64(), UINT64_C(0x7ff8000000000000)))
37       return 1;
38     // any / NaN = NaN
39     if (test__divdf3(3., makeNaN64(UINT64_C(0x123)), UINT64_C(0x7ff8000000000000)))
40       return 1;
41 
42     // +Inf / positive = +Inf
43     if (test__divdf3(makeInf64(), 3., UINT64_C(0x7ff0000000000000)))
44       return 1;
45     // +Inf / negative = -Inf
46     if (test__divdf3(makeInf64(), -3., UINT64_C(0xfff0000000000000)))
47       return 1;
48     // -Inf / positive = -Inf
49     if (test__divdf3(makeNegativeInf64(), 3., UINT64_C(0xfff0000000000000)))
50       return 1;
51     // -Inf / negative = +Inf
52     if (test__divdf3(makeNegativeInf64(), -3., UINT64_C(0x7ff0000000000000)))
53       return 1;
54 
55     // Inf / Inf = NaN
56     if (test__divdf3(makeInf64(), makeInf64(), UINT64_C(0x7ff8000000000000)))
57       return 1;
58     // 0.0 / 0.0 = NaN
59     if (test__divdf3(+0x0.0p+0, +0x0.0p+0, UINT64_C(0x7ff8000000000000)))
60       return 1;
61     // +0.0 / +Inf = +0.0
62     if (test__divdf3(+0x0.0p+0, makeInf64(), UINT64_C(0x0)))
63       return 1;
64     // +Inf / +0.0 = +Inf
65     if (test__divdf3(makeInf64(), +0x0.0p+0, UINT64_C(0x7ff0000000000000)))
66       return 1;
67 
68     // positive / +0.0 = +Inf
69     if (test__divdf3(+1.0, +0x0.0p+0, UINT64_C(0x7ff0000000000000)))
70       return 1;
71     // positive / -0.0 = -Inf
72     if (test__divdf3(+1.0, -0x0.0p+0, UINT64_C(0xfff0000000000000)))
73       return 1;
74     // negative / +0.0 = -Inf
75     if (test__divdf3(-1.0, +0x0.0p+0, UINT64_C(0xfff0000000000000)))
76       return 1;
77     // negative / -0.0 = +Inf
78     if (test__divdf3(-1.0, -0x0.0p+0, UINT64_C(0x7ff0000000000000)))
79       return 1;
80 
81     // 1/3
82     if (test__divdf3(1., 3., UINT64_C(0x3fd5555555555555)))
83       return 1;
84     // smallest normal result
85     if (test__divdf3(0x1.0p-1021, 2., UINT64_C(0x10000000000000)))
86       return 1;
87 
88     // divisor is exactly 1.0
89     if (test__divdf3(0x1.0p+0, 0x1.0p+0, UINT64_C(0x3ff0000000000000)))
90       return 1;
91     // divisor is truncated to exactly 1.0 in UQ1.31
92     if (test__divdf3(0x1.0p+0, 0x1.00000001p+0, UINT64_C(0x3fefffffffe00000)))
93       return 1;
94 
95     // smallest normal value divided by 2.0
96     if (test__divdf3(0x1.0p-1022, 2., UINT64_C(0x0008000000000000)))
97       return 1;
98     // smallest subnormal result
99     if (test__divdf3(0x1.0p-1022, 0x1.0p+52, UINT64_C(0x0000000000000001)))
100       return 1;
101 
102     // some misc test cases obtained by fuzzing against h/w implementation
103     if (test__divdf3(0x1.fdc239dd64735p-658, -0x1.fff9364c0843fp-948, UINT64_C(0xd20fdc8fc0ceffb1)))
104       return 1;
105     if (test__divdf3(-0x1.78abb261d47c8p+794, 0x1.fb01d537cc5aep+266, UINT64_C(0xe0e7c6148ffc23e3)))
106       return 1;
107     if (test__divdf3(-0x1.da7dfe6048b8bp-875, 0x1.ffc7ea3ff60a4p-610, UINT64_C(0xaf5dab1fe0269e2a)))
108       return 1;
109     if (test__divdf3(0x1.0p-1022, 0x1.9p+5, UINT64_C(0x000051eb851eb852)))
110       return 1;
111     if (test__divdf3(0x1.0p-1022, 0x1.0028p+41, UINT64_C(0x00000000000007ff)))
112       return 1;
113     if (test__divdf3(0x1.0p-1022, 0x1.0028p+52, UINT64_C(0x1)))
114       return 1;
115 
116     return 0;
117 }
118