1 // RUN: %clang_builtins %s %librt -lm -o %t && %run %t
2 // REQUIRES: librt_has_divxc3
3 // REQUIRES: x86-target-arch
4 // UNSUPPORTED: target=powerpc64{{.*}}
5 // UNSUPPORTED: target=mips{{.*}}
6 // REQUIRES: c99-complex
16 // Returns: the quotient of (a + ib) / (c + id)
18 COMPILER_RT_ABI
long double _Complex
19 __divxc3(long double __a
, long double __b
, long double __c
, long double __d
);
21 enum {zero
, non_zero
, inf
, NaN
, non_zero_nan
};
24 classify(long double _Complex x
)
28 if (isinf(creall(x
)) || isinf(cimagl(x
)))
30 if (isnan(creall(x
)) && isnan(cimagl(x
)))
47 int test__divxc3(long double a
, long double b
, long double c
, long double d
)
49 long double _Complex r
= __divxc3(a
, b
, c
, d
);
50 // printf("test__divxc3(%Lf, %Lf, %Lf, %Lf) = %Lf + I%Lf\n",
51 // a, b, c, d, creall(r), cimagl(r));
52 long double _Complex dividend
;
53 long double _Complex divisor
;
55 __real__ dividend
= a
;
56 __imag__ dividend
= b
;
60 switch (classify(dividend
))
63 switch (classify(divisor
))
66 if (classify(r
) != NaN
)
70 if (classify(r
) != zero
)
74 if (classify(r
) != zero
)
78 if (classify(r
) != NaN
)
82 if (classify(r
) != NaN
)
88 switch (classify(divisor
))
91 if (classify(r
) != inf
)
95 if (classify(r
) != non_zero
)
98 long double _Complex z
= (a
* c
+ b
* d
) / (c
* c
+ d
* d
)
99 + (b
* c
- a
* d
) / (c
* c
+ d
* d
) * _Complex_I
;
100 if (cabs((r
- z
)/r
) > 1.e
-6)
105 if (classify(r
) != zero
)
109 if (classify(r
) != NaN
)
113 if (classify(r
) != NaN
)
119 switch (classify(divisor
))
122 if (classify(r
) != inf
)
126 if (classify(r
) != inf
)
130 if (classify(r
) != NaN
)
134 if (classify(r
) != NaN
)
138 if (classify(r
) != NaN
)
144 switch (classify(divisor
))
147 if (classify(r
) != NaN
)
151 if (classify(r
) != NaN
)
155 if (classify(r
) != NaN
)
159 if (classify(r
) != NaN
)
163 if (classify(r
) != NaN
)
169 switch (classify(divisor
))
172 if (classify(r
) != inf
)
176 if (classify(r
) != NaN
)
180 if (classify(r
) != NaN
)
184 if (classify(r
) != NaN
)
188 if (classify(r
) != NaN
)
233 {-INFINITY
, -INFINITY
},
242 {INFINITY
, -INFINITY
},
341 {-INFINITY
, INFINITY
},
359 const unsigned N
= sizeof(x
) / sizeof(x
[0]);
361 for (i
= 0; i
< N
; ++i
)
363 for (j
= 0; j
< N
; ++j
)
365 if (test__divxc3(x
[i
][0], x
[i
][1], x
[j
][0], x
[j
][1]))