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