blob: 3c88390b5e77f7f705b59e2513030155eea2c694 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001/* ===-- divdc3.c - Implement __divdc3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divdc3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
14
15#include "int_lib.h"
16#include "int_math.h"
17
18/* Returns: the quotient of (a + ib) / (c + id) */
19
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080020COMPILER_RT_ABI Dcomplex
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021__divdc3(double __a, double __b, double __c, double __d)
22{
23 int __ilogbw = 0;
24 double __logbw = crt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d)));
25 if (crt_isfinite(__logbw))
26 {
27 __ilogbw = (int)__logbw;
28 __c = crt_scalbn(__c, -__ilogbw);
29 __d = crt_scalbn(__d, -__ilogbw);
30 }
31 double __denom = __c * __c + __d * __d;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080032 Dcomplex z;
33 COMPLEX_REAL(z) = crt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
34 COMPLEX_IMAGINARY(z) = crt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
35 if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036 {
37 if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
38 {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080039 COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
40 COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041 }
42 else if ((crt_isinf(__a) || crt_isinf(__b)) &&
43 crt_isfinite(__c) && crt_isfinite(__d))
44 {
45 __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
46 __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080047 COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
48 COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070049 }
50 else if (crt_isinf(__logbw) && __logbw > 0.0 &&
51 crt_isfinite(__a) && crt_isfinite(__b))
52 {
53 __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
54 __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080055 COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
56 COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070057 }
58 }
59 return z;
60}