1*7c3d14c8STreehugger Robot //===-- divtc3_test.c - Test __divtc3 -------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot // Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file tests __divtc3 for the compiler_rt library.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot
14*7c3d14c8STreehugger Robot #include <stdio.h>
15*7c3d14c8STreehugger Robot
16*7c3d14c8STreehugger Robot #include "int_lib.h"
17*7c3d14c8STreehugger Robot #include <math.h>
18*7c3d14c8STreehugger Robot #include <complex.h>
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot // Returns: the quotient of (a + ib) / (c + id)
21*7c3d14c8STreehugger Robot
22*7c3d14c8STreehugger Robot COMPILER_RT_ABI long double _Complex
23*7c3d14c8STreehugger Robot __divtc3(long double __a, long double __b, long double __c, long double __d);
24*7c3d14c8STreehugger Robot
25*7c3d14c8STreehugger Robot enum {zero, non_zero, inf, NaN, non_zero_nan};
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot int
classify(long double _Complex x)28*7c3d14c8STreehugger Robot classify(long double _Complex x)
29*7c3d14c8STreehugger Robot {
30*7c3d14c8STreehugger Robot if (x == 0)
31*7c3d14c8STreehugger Robot return zero;
32*7c3d14c8STreehugger Robot if (isinf(creall(x)) || isinf(cimagl(x)))
33*7c3d14c8STreehugger Robot return inf;
34*7c3d14c8STreehugger Robot if (isnan(creall(x)) && isnan(cimagl(x)))
35*7c3d14c8STreehugger Robot return NaN;
36*7c3d14c8STreehugger Robot if (isnan(creall(x)))
37*7c3d14c8STreehugger Robot {
38*7c3d14c8STreehugger Robot if (cimagl(x) == 0)
39*7c3d14c8STreehugger Robot return NaN;
40*7c3d14c8STreehugger Robot return non_zero_nan;
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot if (isnan(cimagl(x)))
43*7c3d14c8STreehugger Robot {
44*7c3d14c8STreehugger Robot if (creall(x) == 0)
45*7c3d14c8STreehugger Robot return NaN;
46*7c3d14c8STreehugger Robot return non_zero_nan;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot return non_zero;
49*7c3d14c8STreehugger Robot }
50*7c3d14c8STreehugger Robot
test__divtc3(long double a,long double b,long double c,long double d)51*7c3d14c8STreehugger Robot int test__divtc3(long double a, long double b, long double c, long double d)
52*7c3d14c8STreehugger Robot {
53*7c3d14c8STreehugger Robot long double _Complex r = __divtc3(a, b, c, d);
54*7c3d14c8STreehugger Robot // printf("test__divtc3(%Lf, %Lf, %Lf, %Lf) = %Lf + I%Lf\n",
55*7c3d14c8STreehugger Robot // a, b, c, d, creall(r), cimagl(r));
56*7c3d14c8STreehugger Robot
57*7c3d14c8STreehugger Robot long double _Complex dividend;
58*7c3d14c8STreehugger Robot long double _Complex divisor;
59*7c3d14c8STreehugger Robot
60*7c3d14c8STreehugger Robot __real__ dividend = a;
61*7c3d14c8STreehugger Robot __imag__ dividend = b;
62*7c3d14c8STreehugger Robot __real__ divisor = c;
63*7c3d14c8STreehugger Robot __imag__ divisor = d;
64*7c3d14c8STreehugger Robot
65*7c3d14c8STreehugger Robot switch (classify(dividend))
66*7c3d14c8STreehugger Robot {
67*7c3d14c8STreehugger Robot case zero:
68*7c3d14c8STreehugger Robot switch (classify(divisor))
69*7c3d14c8STreehugger Robot {
70*7c3d14c8STreehugger Robot case zero:
71*7c3d14c8STreehugger Robot if (classify(r) != NaN)
72*7c3d14c8STreehugger Robot return 1;
73*7c3d14c8STreehugger Robot break;
74*7c3d14c8STreehugger Robot case non_zero:
75*7c3d14c8STreehugger Robot if (classify(r) != zero)
76*7c3d14c8STreehugger Robot return 1;
77*7c3d14c8STreehugger Robot break;
78*7c3d14c8STreehugger Robot case inf:
79*7c3d14c8STreehugger Robot if (classify(r) != zero)
80*7c3d14c8STreehugger Robot return 1;
81*7c3d14c8STreehugger Robot break;
82*7c3d14c8STreehugger Robot case NaN:
83*7c3d14c8STreehugger Robot if (classify(r) != NaN)
84*7c3d14c8STreehugger Robot return 1;
85*7c3d14c8STreehugger Robot break;
86*7c3d14c8STreehugger Robot case non_zero_nan:
87*7c3d14c8STreehugger Robot if (classify(r) != NaN)
88*7c3d14c8STreehugger Robot return 1;
89*7c3d14c8STreehugger Robot break;
90*7c3d14c8STreehugger Robot }
91*7c3d14c8STreehugger Robot break;
92*7c3d14c8STreehugger Robot case non_zero:
93*7c3d14c8STreehugger Robot switch (classify(divisor))
94*7c3d14c8STreehugger Robot {
95*7c3d14c8STreehugger Robot case zero:
96*7c3d14c8STreehugger Robot if (classify(r) != inf)
97*7c3d14c8STreehugger Robot return 1;
98*7c3d14c8STreehugger Robot break;
99*7c3d14c8STreehugger Robot case non_zero:
100*7c3d14c8STreehugger Robot if (classify(r) != non_zero)
101*7c3d14c8STreehugger Robot return 1;
102*7c3d14c8STreehugger Robot {
103*7c3d14c8STreehugger Robot long double _Complex z = (a * c + b * d) / (c * c + d * d)
104*7c3d14c8STreehugger Robot + (b * c - a * d) / (c * c + d * d) * _Complex_I;
105*7c3d14c8STreehugger Robot if (cabsl((r - z)/r) > 1.e-6)
106*7c3d14c8STreehugger Robot return 1;
107*7c3d14c8STreehugger Robot }
108*7c3d14c8STreehugger Robot break;
109*7c3d14c8STreehugger Robot case inf:
110*7c3d14c8STreehugger Robot if (classify(r) != zero)
111*7c3d14c8STreehugger Robot return 1;
112*7c3d14c8STreehugger Robot break;
113*7c3d14c8STreehugger Robot case NaN:
114*7c3d14c8STreehugger Robot if (classify(r) != NaN)
115*7c3d14c8STreehugger Robot return 1;
116*7c3d14c8STreehugger Robot break;
117*7c3d14c8STreehugger Robot case non_zero_nan:
118*7c3d14c8STreehugger Robot if (classify(r) != NaN)
119*7c3d14c8STreehugger Robot return 1;
120*7c3d14c8STreehugger Robot break;
121*7c3d14c8STreehugger Robot }
122*7c3d14c8STreehugger Robot break;
123*7c3d14c8STreehugger Robot case inf:
124*7c3d14c8STreehugger Robot switch (classify(divisor))
125*7c3d14c8STreehugger Robot {
126*7c3d14c8STreehugger Robot case zero:
127*7c3d14c8STreehugger Robot if (classify(r) != inf)
128*7c3d14c8STreehugger Robot return 1;
129*7c3d14c8STreehugger Robot break;
130*7c3d14c8STreehugger Robot case non_zero:
131*7c3d14c8STreehugger Robot if (classify(r) != inf)
132*7c3d14c8STreehugger Robot return 1;
133*7c3d14c8STreehugger Robot break;
134*7c3d14c8STreehugger Robot case inf:
135*7c3d14c8STreehugger Robot if (classify(r) != NaN)
136*7c3d14c8STreehugger Robot return 1;
137*7c3d14c8STreehugger Robot break;
138*7c3d14c8STreehugger Robot case NaN:
139*7c3d14c8STreehugger Robot if (classify(r) != NaN)
140*7c3d14c8STreehugger Robot return 1;
141*7c3d14c8STreehugger Robot break;
142*7c3d14c8STreehugger Robot case non_zero_nan:
143*7c3d14c8STreehugger Robot if (classify(r) != NaN)
144*7c3d14c8STreehugger Robot return 1;
145*7c3d14c8STreehugger Robot break;
146*7c3d14c8STreehugger Robot }
147*7c3d14c8STreehugger Robot break;
148*7c3d14c8STreehugger Robot case NaN:
149*7c3d14c8STreehugger Robot switch (classify(divisor))
150*7c3d14c8STreehugger Robot {
151*7c3d14c8STreehugger Robot case zero:
152*7c3d14c8STreehugger Robot if (classify(r) != NaN)
153*7c3d14c8STreehugger Robot return 1;
154*7c3d14c8STreehugger Robot break;
155*7c3d14c8STreehugger Robot case non_zero:
156*7c3d14c8STreehugger Robot if (classify(r) != NaN)
157*7c3d14c8STreehugger Robot return 1;
158*7c3d14c8STreehugger Robot break;
159*7c3d14c8STreehugger Robot case inf:
160*7c3d14c8STreehugger Robot if (classify(r) != NaN)
161*7c3d14c8STreehugger Robot return 1;
162*7c3d14c8STreehugger Robot break;
163*7c3d14c8STreehugger Robot case NaN:
164*7c3d14c8STreehugger Robot if (classify(r) != NaN)
165*7c3d14c8STreehugger Robot return 1;
166*7c3d14c8STreehugger Robot break;
167*7c3d14c8STreehugger Robot case non_zero_nan:
168*7c3d14c8STreehugger Robot if (classify(r) != NaN)
169*7c3d14c8STreehugger Robot return 1;
170*7c3d14c8STreehugger Robot break;
171*7c3d14c8STreehugger Robot }
172*7c3d14c8STreehugger Robot break;
173*7c3d14c8STreehugger Robot case non_zero_nan:
174*7c3d14c8STreehugger Robot switch (classify(divisor))
175*7c3d14c8STreehugger Robot {
176*7c3d14c8STreehugger Robot case zero:
177*7c3d14c8STreehugger Robot if (classify(r) != inf)
178*7c3d14c8STreehugger Robot return 1;
179*7c3d14c8STreehugger Robot break;
180*7c3d14c8STreehugger Robot case non_zero:
181*7c3d14c8STreehugger Robot if (classify(r) != NaN)
182*7c3d14c8STreehugger Robot return 1;
183*7c3d14c8STreehugger Robot break;
184*7c3d14c8STreehugger Robot case inf:
185*7c3d14c8STreehugger Robot if (classify(r) != NaN)
186*7c3d14c8STreehugger Robot return 1;
187*7c3d14c8STreehugger Robot break;
188*7c3d14c8STreehugger Robot case NaN:
189*7c3d14c8STreehugger Robot if (classify(r) != NaN)
190*7c3d14c8STreehugger Robot return 1;
191*7c3d14c8STreehugger Robot break;
192*7c3d14c8STreehugger Robot case non_zero_nan:
193*7c3d14c8STreehugger Robot if (classify(r) != NaN)
194*7c3d14c8STreehugger Robot return 1;
195*7c3d14c8STreehugger Robot break;
196*7c3d14c8STreehugger Robot }
197*7c3d14c8STreehugger Robot break;
198*7c3d14c8STreehugger Robot }
199*7c3d14c8STreehugger Robot
200*7c3d14c8STreehugger Robot return 0;
201*7c3d14c8STreehugger Robot }
202*7c3d14c8STreehugger Robot
203*7c3d14c8STreehugger Robot long double x[][2] =
204*7c3d14c8STreehugger Robot {
205*7c3d14c8STreehugger Robot { 1.e-6, 1.e-6},
206*7c3d14c8STreehugger Robot {-1.e-6, 1.e-6},
207*7c3d14c8STreehugger Robot {-1.e-6, -1.e-6},
208*7c3d14c8STreehugger Robot { 1.e-6, -1.e-6},
209*7c3d14c8STreehugger Robot
210*7c3d14c8STreehugger Robot { 1.e+6, 1.e-6},
211*7c3d14c8STreehugger Robot {-1.e+6, 1.e-6},
212*7c3d14c8STreehugger Robot {-1.e+6, -1.e-6},
213*7c3d14c8STreehugger Robot { 1.e+6, -1.e-6},
214*7c3d14c8STreehugger Robot
215*7c3d14c8STreehugger Robot { 1.e-6, 1.e+6},
216*7c3d14c8STreehugger Robot {-1.e-6, 1.e+6},
217*7c3d14c8STreehugger Robot {-1.e-6, -1.e+6},
218*7c3d14c8STreehugger Robot { 1.e-6, -1.e+6},
219*7c3d14c8STreehugger Robot
220*7c3d14c8STreehugger Robot { 1.e+6, 1.e+6},
221*7c3d14c8STreehugger Robot {-1.e+6, 1.e+6},
222*7c3d14c8STreehugger Robot {-1.e+6, -1.e+6},
223*7c3d14c8STreehugger Robot { 1.e+6, -1.e+6},
224*7c3d14c8STreehugger Robot
225*7c3d14c8STreehugger Robot {NAN, NAN},
226*7c3d14c8STreehugger Robot {-INFINITY, NAN},
227*7c3d14c8STreehugger Robot {-2, NAN},
228*7c3d14c8STreehugger Robot {-1, NAN},
229*7c3d14c8STreehugger Robot {-0.5, NAN},
230*7c3d14c8STreehugger Robot {-0., NAN},
231*7c3d14c8STreehugger Robot {+0., NAN},
232*7c3d14c8STreehugger Robot {0.5, NAN},
233*7c3d14c8STreehugger Robot {1, NAN},
234*7c3d14c8STreehugger Robot {2, NAN},
235*7c3d14c8STreehugger Robot {INFINITY, NAN},
236*7c3d14c8STreehugger Robot
237*7c3d14c8STreehugger Robot {NAN, -INFINITY},
238*7c3d14c8STreehugger Robot {-INFINITY, -INFINITY},
239*7c3d14c8STreehugger Robot {-2, -INFINITY},
240*7c3d14c8STreehugger Robot {-1, -INFINITY},
241*7c3d14c8STreehugger Robot {-0.5, -INFINITY},
242*7c3d14c8STreehugger Robot {-0., -INFINITY},
243*7c3d14c8STreehugger Robot {+0., -INFINITY},
244*7c3d14c8STreehugger Robot {0.5, -INFINITY},
245*7c3d14c8STreehugger Robot {1, -INFINITY},
246*7c3d14c8STreehugger Robot {2, -INFINITY},
247*7c3d14c8STreehugger Robot {INFINITY, -INFINITY},
248*7c3d14c8STreehugger Robot
249*7c3d14c8STreehugger Robot {NAN, -2},
250*7c3d14c8STreehugger Robot {-INFINITY, -2},
251*7c3d14c8STreehugger Robot {-2, -2},
252*7c3d14c8STreehugger Robot {-1, -2},
253*7c3d14c8STreehugger Robot {-0.5, -2},
254*7c3d14c8STreehugger Robot {-0., -2},
255*7c3d14c8STreehugger Robot {+0., -2},
256*7c3d14c8STreehugger Robot {0.5, -2},
257*7c3d14c8STreehugger Robot {1, -2},
258*7c3d14c8STreehugger Robot {2, -2},
259*7c3d14c8STreehugger Robot {INFINITY, -2},
260*7c3d14c8STreehugger Robot
261*7c3d14c8STreehugger Robot {NAN, -1},
262*7c3d14c8STreehugger Robot {-INFINITY, -1},
263*7c3d14c8STreehugger Robot {-2, -1},
264*7c3d14c8STreehugger Robot {-1, -1},
265*7c3d14c8STreehugger Robot {-0.5, -1},
266*7c3d14c8STreehugger Robot {-0., -1},
267*7c3d14c8STreehugger Robot {+0., -1},
268*7c3d14c8STreehugger Robot {0.5, -1},
269*7c3d14c8STreehugger Robot {1, -1},
270*7c3d14c8STreehugger Robot {2, -1},
271*7c3d14c8STreehugger Robot {INFINITY, -1},
272*7c3d14c8STreehugger Robot
273*7c3d14c8STreehugger Robot {NAN, -0.5},
274*7c3d14c8STreehugger Robot {-INFINITY, -0.5},
275*7c3d14c8STreehugger Robot {-2, -0.5},
276*7c3d14c8STreehugger Robot {-1, -0.5},
277*7c3d14c8STreehugger Robot {-0.5, -0.5},
278*7c3d14c8STreehugger Robot {-0., -0.5},
279*7c3d14c8STreehugger Robot {+0., -0.5},
280*7c3d14c8STreehugger Robot {0.5, -0.5},
281*7c3d14c8STreehugger Robot {1, -0.5},
282*7c3d14c8STreehugger Robot {2, -0.5},
283*7c3d14c8STreehugger Robot {INFINITY, -0.5},
284*7c3d14c8STreehugger Robot
285*7c3d14c8STreehugger Robot {NAN, -0.},
286*7c3d14c8STreehugger Robot {-INFINITY, -0.},
287*7c3d14c8STreehugger Robot {-2, -0.},
288*7c3d14c8STreehugger Robot {-1, -0.},
289*7c3d14c8STreehugger Robot {-0.5, -0.},
290*7c3d14c8STreehugger Robot {-0., -0.},
291*7c3d14c8STreehugger Robot {+0., -0.},
292*7c3d14c8STreehugger Robot {0.5, -0.},
293*7c3d14c8STreehugger Robot {1, -0.},
294*7c3d14c8STreehugger Robot {2, -0.},
295*7c3d14c8STreehugger Robot {INFINITY, -0.},
296*7c3d14c8STreehugger Robot
297*7c3d14c8STreehugger Robot {NAN, 0.},
298*7c3d14c8STreehugger Robot {-INFINITY, 0.},
299*7c3d14c8STreehugger Robot {-2, 0.},
300*7c3d14c8STreehugger Robot {-1, 0.},
301*7c3d14c8STreehugger Robot {-0.5, 0.},
302*7c3d14c8STreehugger Robot {-0., 0.},
303*7c3d14c8STreehugger Robot {+0., 0.},
304*7c3d14c8STreehugger Robot {0.5, 0.},
305*7c3d14c8STreehugger Robot {1, 0.},
306*7c3d14c8STreehugger Robot {2, 0.},
307*7c3d14c8STreehugger Robot {INFINITY, 0.},
308*7c3d14c8STreehugger Robot
309*7c3d14c8STreehugger Robot {NAN, 0.5},
310*7c3d14c8STreehugger Robot {-INFINITY, 0.5},
311*7c3d14c8STreehugger Robot {-2, 0.5},
312*7c3d14c8STreehugger Robot {-1, 0.5},
313*7c3d14c8STreehugger Robot {-0.5, 0.5},
314*7c3d14c8STreehugger Robot {-0., 0.5},
315*7c3d14c8STreehugger Robot {+0., 0.5},
316*7c3d14c8STreehugger Robot {0.5, 0.5},
317*7c3d14c8STreehugger Robot {1, 0.5},
318*7c3d14c8STreehugger Robot {2, 0.5},
319*7c3d14c8STreehugger Robot {INFINITY, 0.5},
320*7c3d14c8STreehugger Robot
321*7c3d14c8STreehugger Robot {NAN, 1},
322*7c3d14c8STreehugger Robot {-INFINITY, 1},
323*7c3d14c8STreehugger Robot {-2, 1},
324*7c3d14c8STreehugger Robot {-1, 1},
325*7c3d14c8STreehugger Robot {-0.5, 1},
326*7c3d14c8STreehugger Robot {-0., 1},
327*7c3d14c8STreehugger Robot {+0., 1},
328*7c3d14c8STreehugger Robot {0.5, 1},
329*7c3d14c8STreehugger Robot {1, 1},
330*7c3d14c8STreehugger Robot {2, 1},
331*7c3d14c8STreehugger Robot {INFINITY, 1},
332*7c3d14c8STreehugger Robot
333*7c3d14c8STreehugger Robot {NAN, 2},
334*7c3d14c8STreehugger Robot {-INFINITY, 2},
335*7c3d14c8STreehugger Robot {-2, 2},
336*7c3d14c8STreehugger Robot {-1, 2},
337*7c3d14c8STreehugger Robot {-0.5, 2},
338*7c3d14c8STreehugger Robot {-0., 2},
339*7c3d14c8STreehugger Robot {+0., 2},
340*7c3d14c8STreehugger Robot {0.5, 2},
341*7c3d14c8STreehugger Robot {1, 2},
342*7c3d14c8STreehugger Robot {2, 2},
343*7c3d14c8STreehugger Robot {INFINITY, 2},
344*7c3d14c8STreehugger Robot
345*7c3d14c8STreehugger Robot {NAN, INFINITY},
346*7c3d14c8STreehugger Robot {-INFINITY, INFINITY},
347*7c3d14c8STreehugger Robot {-2, INFINITY},
348*7c3d14c8STreehugger Robot {-1, INFINITY},
349*7c3d14c8STreehugger Robot {-0.5, INFINITY},
350*7c3d14c8STreehugger Robot {-0., INFINITY},
351*7c3d14c8STreehugger Robot {+0., INFINITY},
352*7c3d14c8STreehugger Robot {0.5, INFINITY},
353*7c3d14c8STreehugger Robot {1, INFINITY},
354*7c3d14c8STreehugger Robot {2, INFINITY},
355*7c3d14c8STreehugger Robot {INFINITY, INFINITY}
356*7c3d14c8STreehugger Robot
357*7c3d14c8STreehugger Robot };
358*7c3d14c8STreehugger Robot
main()359*7c3d14c8STreehugger Robot int main()
360*7c3d14c8STreehugger Robot {
361*7c3d14c8STreehugger Robot const unsigned N = sizeof(x) / sizeof(x[0]);
362*7c3d14c8STreehugger Robot unsigned i, j;
363*7c3d14c8STreehugger Robot for (i = 0; i < N; ++i)
364*7c3d14c8STreehugger Robot {
365*7c3d14c8STreehugger Robot for (j = 0; j < N; ++j)
366*7c3d14c8STreehugger Robot {
367*7c3d14c8STreehugger Robot if (test__divtc3(x[i][0], x[i][1], x[j][0], x[j][1]))
368*7c3d14c8STreehugger Robot return 1;
369*7c3d14c8STreehugger Robot }
370*7c3d14c8STreehugger Robot }
371*7c3d14c8STreehugger Robot
372*7c3d14c8STreehugger Robot // printf("No errors found.\n");
373*7c3d14c8STreehugger Robot return 0;
374*7c3d14c8STreehugger Robot }
375