xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/divdc3.c (revision ef84fd3bd8895f4e6be1e38baf19e6dc3255bc64)
1156cd587Sjoerg /* ===-- divdc3.c - Implement __divdc3 -------------------------------------===
2156cd587Sjoerg  *
3156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4156cd587Sjoerg  *
5156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg  *
8156cd587Sjoerg  * ===----------------------------------------------------------------------===
9156cd587Sjoerg  *
10156cd587Sjoerg  * This file implements __divdc3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg #include "int_math.h"
17156cd587Sjoerg 
18156cd587Sjoerg /* Returns: the quotient of (a + ib) / (c + id) */
19156cd587Sjoerg 
20*ef84fd3bSjoerg COMPILER_RT_ABI Dcomplex
__divdc3(double __a,double __b,double __c,double __d)21156cd587Sjoerg __divdc3(double __a, double __b, double __c, double __d)
22156cd587Sjoerg {
23156cd587Sjoerg     int __ilogbw = 0;
24156cd587Sjoerg     double __logbw = crt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d)));
25156cd587Sjoerg     if (crt_isfinite(__logbw))
26156cd587Sjoerg     {
27156cd587Sjoerg         __ilogbw = (int)__logbw;
28156cd587Sjoerg         __c = crt_scalbn(__c, -__ilogbw);
29156cd587Sjoerg         __d = crt_scalbn(__d, -__ilogbw);
30156cd587Sjoerg     }
31156cd587Sjoerg     double __denom = __c * __c + __d * __d;
32*ef84fd3bSjoerg     Dcomplex z;
33*ef84fd3bSjoerg     COMPLEX_REAL(z) = crt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
34*ef84fd3bSjoerg     COMPLEX_IMAGINARY(z) = crt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
35*ef84fd3bSjoerg     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
36156cd587Sjoerg     {
37156cd587Sjoerg         if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
38156cd587Sjoerg         {
39*ef84fd3bSjoerg             COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
40*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
41156cd587Sjoerg         }
42156cd587Sjoerg         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
43156cd587Sjoerg                  crt_isfinite(__c) && crt_isfinite(__d))
44156cd587Sjoerg         {
45156cd587Sjoerg             __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
46156cd587Sjoerg             __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
47*ef84fd3bSjoerg             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
48*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
49156cd587Sjoerg         }
50156cd587Sjoerg         else if (crt_isinf(__logbw) && __logbw > 0.0 &&
51156cd587Sjoerg                  crt_isfinite(__a) && crt_isfinite(__b))
52156cd587Sjoerg         {
53156cd587Sjoerg             __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
54156cd587Sjoerg             __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
55*ef84fd3bSjoerg             COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
56*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
57156cd587Sjoerg         }
58156cd587Sjoerg     }
59156cd587Sjoerg     return z;
60156cd587Sjoerg }
61