xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/divxc3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  *                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc  *
5*0a6a1f1dSLionel Sambuc  * This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc  * Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * This file implements __divxc3 for the compiler_rt library.
11*0a6a1f1dSLionel Sambuc  *
12*0a6a1f1dSLionel Sambuc  */
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc #if !_ARCH_PPC
15*0a6a1f1dSLionel Sambuc 
16*0a6a1f1dSLionel Sambuc #include "int_lib.h"
17*0a6a1f1dSLionel Sambuc #include "int_math.h"
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc /* Returns: the quotient of (a + ib) / (c + id) */
20*0a6a1f1dSLionel Sambuc 
21*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI long double _Complex
__divxc3(long double __a,long double __b,long double __c,long double __d)22*0a6a1f1dSLionel Sambuc __divxc3(long double __a, long double __b, long double __c, long double __d)
23*0a6a1f1dSLionel Sambuc {
24*0a6a1f1dSLionel Sambuc     int __ilogbw = 0;
25*0a6a1f1dSLionel Sambuc     long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
26*0a6a1f1dSLionel Sambuc     if (crt_isfinite(__logbw))
27*0a6a1f1dSLionel Sambuc     {
28*0a6a1f1dSLionel Sambuc         __ilogbw = (int)__logbw;
29*0a6a1f1dSLionel Sambuc         __c = crt_scalbnl(__c, -__ilogbw);
30*0a6a1f1dSLionel Sambuc         __d = crt_scalbnl(__d, -__ilogbw);
31*0a6a1f1dSLionel Sambuc     }
32*0a6a1f1dSLionel Sambuc     long double __denom = __c * __c + __d * __d;
33*0a6a1f1dSLionel Sambuc     long double _Complex z;
34*0a6a1f1dSLionel Sambuc     __real__ z = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
35*0a6a1f1dSLionel Sambuc     __imag__ z = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
36*0a6a1f1dSLionel Sambuc     if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
37*0a6a1f1dSLionel Sambuc     {
38*0a6a1f1dSLionel Sambuc         if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
39*0a6a1f1dSLionel Sambuc         {
40*0a6a1f1dSLionel Sambuc             __real__ z = crt_copysignl(CRT_INFINITY, __c) * __a;
41*0a6a1f1dSLionel Sambuc             __imag__ z = crt_copysignl(CRT_INFINITY, __c) * __b;
42*0a6a1f1dSLionel Sambuc         }
43*0a6a1f1dSLionel Sambuc         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
44*0a6a1f1dSLionel Sambuc                  crt_isfinite(__c) && crt_isfinite(__d))
45*0a6a1f1dSLionel Sambuc         {
46*0a6a1f1dSLionel Sambuc             __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
47*0a6a1f1dSLionel Sambuc             __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
48*0a6a1f1dSLionel Sambuc             __real__ z = CRT_INFINITY * (__a * __c + __b * __d);
49*0a6a1f1dSLionel Sambuc             __imag__ z = CRT_INFINITY * (__b * __c - __a * __d);
50*0a6a1f1dSLionel Sambuc         }
51*0a6a1f1dSLionel Sambuc         else if (crt_isinf(__logbw) && __logbw > 0 &&
52*0a6a1f1dSLionel Sambuc                  crt_isfinite(__a) && crt_isfinite(__b))
53*0a6a1f1dSLionel Sambuc         {
54*0a6a1f1dSLionel Sambuc             __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
55*0a6a1f1dSLionel Sambuc             __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
56*0a6a1f1dSLionel Sambuc             __real__ z = 0 * (__a * __c + __b * __d);
57*0a6a1f1dSLionel Sambuc             __imag__ z = 0 * (__b * __c - __a * __d);
58*0a6a1f1dSLionel Sambuc         }
59*0a6a1f1dSLionel Sambuc     }
60*0a6a1f1dSLionel Sambuc     return z;
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc #endif
64