1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_divtf3
3
4#include "int_lib.h"
5#include <stdio.h>
6
7#if __LDBL_MANT_DIG__ == 113
8
9#include "fp_test.h"
10
11// Returns: a / b
12COMPILER_RT_ABI long double __divtf3(long double a, long double b);
13
14int test__divtf3(long double a, long double b,
15 uint64_t expectedHi, uint64_t expectedLo)
16{
17 long double x = __divtf3(a, b);
18 int ret = compareResultLD(x, expectedHi, expectedLo);
19
20 if (ret){
21 printf("error in test__divtf3(%.20Le, %.20Le) = %.20Le, "
22 "expected %.20Le\n", a, b, x,
23 fromRep128(expectedHi, expectedLo));
24 }
25 return ret;
26}
27
28char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0};
29
30#endif
31
32int main()
33{
34#if __LDBL_MANT_DIG__ == 113
35 // Returned NaNs are assumed to be qNaN by default
36
37 // qNaN / any = qNaN
38 if (test__divtf3(makeQNaN128(),
39 0x1.23456789abcdefp+5L,
40 UINT64_C(0x7fff800000000000),
41 UINT64_C(0x0)))
42 return 1;
43 // NaN / any = NaN
44 if (test__divtf3(makeNaN128(UINT64_C(0x30000000)),
45 0x1.23456789abcdefp+5L,
46 UINT64_C(0x7fff800000000000),
47 UINT64_C(0x0)))
48 return 1;
49 // any / qNaN = qNaN
50 if (test__divtf3(0x1.23456789abcdefp+5L,
51 makeQNaN128(),
52 UINT64_C(0x7fff800000000000),
53 UINT64_C(0x0)))
54 return 1;
55 // any / NaN = NaN
56 if (test__divtf3(0x1.23456789abcdefp+5L,
57 makeNaN128(UINT64_C(0x30000000)),
58 UINT64_C(0x7fff800000000000),
59 UINT64_C(0x0)))
60 return 1;
61
62 // +Inf / positive = +Inf
63 if (test__divtf3(makeInf128(), 3.L,
64 UINT64_C(0x7fff000000000000),
65 UINT64_C(0x0)))
66 return 1;
67 // +Inf / negative = -Inf
68 if (test__divtf3(makeInf128(), -3.L,
69 UINT64_C(0xffff000000000000),
70 UINT64_C(0x0)))
71 return 1;
72 // -Inf / positive = -Inf
73 if (test__divtf3(makeNegativeInf128(), 3.L,
74 UINT64_C(0xffff000000000000),
75 UINT64_C(0x0)))
76 return 1;
77 // -Inf / negative = +Inf
78 if (test__divtf3(makeNegativeInf128(), -3.L,
79 UINT64_C(0x7fff000000000000),
80 UINT64_C(0x0)))
81 return 1;
82
83 // Inf / Inf = NaN
84 if (test__divtf3(makeInf128(), makeInf128(),
85 UINT64_C(0x7fff800000000000),
86 UINT64_C(0x0)))
87 return 1;
88 // 0.0 / 0.0 = NaN
89 if (test__divtf3(+0x0.0p+0L, +0x0.0p+0L,
90 UINT64_C(0x7fff800000000000),
91 UINT64_C(0x0)))
92 return 1;
93 // +0.0 / +Inf = +0.0
94 if (test__divtf3(+0x0.0p+0L, makeInf128(),
95 UINT64_C(0x0),
96 UINT64_C(0x0)))
97 return 1;
98 // +Inf / +0.0 = +Inf
99 if (test__divtf3(makeInf128(), +0x0.0p+0L,
100 UINT64_C(0x7fff000000000000),
101 UINT64_C(0x0)))
102 return 1;
103
104 // positive / +0.0 = +Inf
105 if (test__divtf3(+1.0L, +0x0.0p+0L,
106 UINT64_C(0x7fff000000000000),
107 UINT64_C(0x0)))
108 return 1;
109 // positive / -0.0 = -Inf
110 if (test__divtf3(+1.0L, -0x0.0p+0L,
111 UINT64_C(0xffff000000000000),
112 UINT64_C(0x0)))
113 return 1;
114 // negative / +0.0 = -Inf
115 if (test__divtf3(-1.0L, +0x0.0p+0L,
116 UINT64_C(0xffff000000000000),
117 UINT64_C(0x0)))
118 return 1;
119 // negative / -0.0 = +Inf
120 if (test__divtf3(-1.0L, -0x0.0p+0L,
121 UINT64_C(0x7fff000000000000),
122 UINT64_C(0x0)))
123 return 1;
124
125 // 1/3
126 if (test__divtf3(1.L, 3.L,
127 UINT64_C(0x3ffd555555555555),
128 UINT64_C(0x5555555555555555)))
129 return 1;
130 // smallest normal result
131 if (test__divtf3(0x1.0p-16381L, 2.L,
132 UINT64_C(0x0001000000000000),
133 UINT64_C(0x0)))
134 return 1;
135
136 // divisor is exactly 1.0
137 if (test__divtf3(0x1.0p+0L,
138 0x1.0p+0L,
139 UINT64_C(0x3fff000000000000),
140 UINT64_C(0x0)))
141 return 1;
142 // divisor is truncated to exactly 1.0 in UQ1.63
143 if (test__divtf3(0x1.0p+0L,
144 0x1.0000000000000001p+0L,
145 UINT64_C(0x3ffeffffffffffff),
146 UINT64_C(0xfffe000000000000)))
147 return 1;
148
149 // smallest normal value divided by 2.0
150 if (test__divtf3(0x1.0p-16382L, 2.L, UINT64_C(0x0000800000000000), UINT64_C(0x0)))
151 return 1;
152 // smallest subnormal result
153 if (test__divtf3(0x1.0p-16382L, 0x1p+112L, UINT64_C(0x0), UINT64_C(0x1)))
154 return 1;
155
156 // any / any
157 if (test__divtf3(0x1.a23b45362464523375893ab4cdefp+5L,
158 0x1.eedcbaba3a94546558237654321fp-1L,
159 UINT64_C(0x4004b0b72924d407),
160 UINT64_C(0x0717e84356c6eba2)))
161 return 1;
162 if (test__divtf3(0x1.a2b34c56d745382f9abf2c3dfeffp-50L,
163 0x1.ed2c3ba15935332532287654321fp-9L,
164 UINT64_C(0x3fd5b2af3f828c9b),
165 UINT64_C(0x40e51f64cde8b1f2)))
166 return 15;
167 if (test__divtf3(0x1.2345f6aaaa786555f42432abcdefp+456L,
168 0x1.edacbba9874f765463544dd3621fp+6400L,
169 UINT64_C(0x28c62e15dc464466),
170 UINT64_C(0xb5a07586348557ac)))
171 return 1;
172 if (test__divtf3(0x1.2d3456f789ba6322bc665544edefp-234L,
173 0x1.eddcdba39f3c8b7a36564354321fp-4455L,
174 UINT64_C(0x507b38442b539266),
175 UINT64_C(0x22ce0f1d024e1252)))
176 return 1;
177 if (test__divtf3(0x1.2345f6b77b7a8953365433abcdefp+234L,
178 0x1.edcba987d6bb3aa467754354321fp-4055L,
179 UINT64_C(0x50bf2e02f0798d36),
180 UINT64_C(0x5e6fcb6b60044078)))
181 return 1;
182 if (test__divtf3(6.72420628622418701252535563464350521E-4932L,
183 2.L,
184 UINT64_C(0x0001000000000000),
185 UINT64_C(0)))
186 return 1;
187
188#else
189 printf("skipped\n");
190
191#endif
192 return 0;
193}
194