]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/builtins/divdc3.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / builtins / divdc3.c
1 /* ===-- 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 #define DOUBLE_PRECISION
16 #include "fp_lib.h"
17 #include "int_lib.h"
18 #include "int_math.h"
19
20 /* Returns: the quotient of (a + ib) / (c + id) */
21
22 COMPILER_RT_ABI Dcomplex
23 __divdc3(double __a, double __b, double __c, double __d)
24 {
25     int __ilogbw = 0;
26     double __logbw = __compiler_rt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d)));
27     if (crt_isfinite(__logbw))
28     {
29         __ilogbw = (int)__logbw;
30         __c = crt_scalbn(__c, -__ilogbw);
31         __d = crt_scalbn(__d, -__ilogbw);
32     }
33     double __denom = __c * __c + __d * __d;
34     Dcomplex z;
35     COMPLEX_REAL(z) = crt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
36     COMPLEX_IMAGINARY(z) = crt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
37     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
38     {
39         if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
40         {
41             COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
42             COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
43         }
44         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
45                  crt_isfinite(__c) && crt_isfinite(__d))
46         {
47             __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
48             __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
49             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
50             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
51         }
52         else if (crt_isinf(__logbw) && __logbw > 0.0 &&
53                  crt_isfinite(__a) && crt_isfinite(__b))
54         {
55             __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
56             __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
57             COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
58             COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
59         }
60     }
61     return z;
62 }