xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/divxc3.c (revision ef84fd3bd8895f4e6be1e38baf19e6dc3255bc64)
1156cd587Sjoerg /* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
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 __divxc3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  */
13156cd587Sjoerg 
14156cd587Sjoerg #if !_ARCH_PPC
15156cd587Sjoerg 
16156cd587Sjoerg #include "int_lib.h"
17156cd587Sjoerg #include "int_math.h"
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: the quotient of (a + ib) / (c + id) */
20156cd587Sjoerg 
21*ef84fd3bSjoerg COMPILER_RT_ABI Lcomplex
__divxc3(long double __a,long double __b,long double __c,long double __d)22156cd587Sjoerg __divxc3(long double __a, long double __b, long double __c, long double __d)
23156cd587Sjoerg {
24156cd587Sjoerg     int __ilogbw = 0;
25156cd587Sjoerg     long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
26156cd587Sjoerg     if (crt_isfinite(__logbw))
27156cd587Sjoerg     {
28156cd587Sjoerg         __ilogbw = (int)__logbw;
29156cd587Sjoerg         __c = crt_scalbnl(__c, -__ilogbw);
30156cd587Sjoerg         __d = crt_scalbnl(__d, -__ilogbw);
31156cd587Sjoerg     }
32156cd587Sjoerg     long double __denom = __c * __c + __d * __d;
33*ef84fd3bSjoerg     Lcomplex z;
34*ef84fd3bSjoerg     COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
35*ef84fd3bSjoerg     COMPLEX_IMAGINARY(z) = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
36*ef84fd3bSjoerg     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
37156cd587Sjoerg     {
38156cd587Sjoerg         if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
39156cd587Sjoerg         {
40*ef84fd3bSjoerg             COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a;
41*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
42156cd587Sjoerg         }
43156cd587Sjoerg         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
44156cd587Sjoerg                  crt_isfinite(__c) && crt_isfinite(__d))
45156cd587Sjoerg         {
46156cd587Sjoerg             __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
47156cd587Sjoerg             __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
48*ef84fd3bSjoerg             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
49*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
50156cd587Sjoerg         }
51156cd587Sjoerg         else if (crt_isinf(__logbw) && __logbw > 0 &&
52156cd587Sjoerg                  crt_isfinite(__a) && crt_isfinite(__b))
53156cd587Sjoerg         {
54156cd587Sjoerg             __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
55156cd587Sjoerg             __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
56*ef84fd3bSjoerg             COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
57*ef84fd3bSjoerg             COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
58156cd587Sjoerg         }
59156cd587Sjoerg     }
60156cd587Sjoerg     return z;
61156cd587Sjoerg }
62156cd587Sjoerg 
63156cd587Sjoerg #endif
64