xref: /aosp_15_r20/external/compiler-rt/lib/builtins/divsc3.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===-- divsc3.c - Implement __divsc3 -------------------------------------===
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 __divsc3 for the compiler_rt library.
11*7c3d14c8STreehugger Robot  *
12*7c3d14c8STreehugger Robot  *===----------------------------------------------------------------------===
13*7c3d14c8STreehugger Robot  */
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "int_lib.h"
16*7c3d14c8STreehugger Robot #include "int_math.h"
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot /* Returns: the quotient of (a + ib) / (c + id) */
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot COMPILER_RT_ABI Fcomplex
__divsc3(float __a,float __b,float __c,float __d)21*7c3d14c8STreehugger Robot __divsc3(float __a, float __b, float __c, float __d)
22*7c3d14c8STreehugger Robot {
23*7c3d14c8STreehugger Robot     int __ilogbw = 0;
24*7c3d14c8STreehugger Robot     float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
25*7c3d14c8STreehugger Robot     if (crt_isfinite(__logbw))
26*7c3d14c8STreehugger Robot     {
27*7c3d14c8STreehugger Robot         __ilogbw = (int)__logbw;
28*7c3d14c8STreehugger Robot         __c = crt_scalbnf(__c, -__ilogbw);
29*7c3d14c8STreehugger Robot         __d = crt_scalbnf(__d, -__ilogbw);
30*7c3d14c8STreehugger Robot     }
31*7c3d14c8STreehugger Robot     float __denom = __c * __c + __d * __d;
32*7c3d14c8STreehugger Robot     Fcomplex z;
33*7c3d14c8STreehugger Robot     COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
34*7c3d14c8STreehugger Robot     COMPLEX_IMAGINARY(z) = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
35*7c3d14c8STreehugger Robot     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
36*7c3d14c8STreehugger Robot     {
37*7c3d14c8STreehugger Robot         if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
38*7c3d14c8STreehugger Robot         {
39*7c3d14c8STreehugger Robot             COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
40*7c3d14c8STreehugger Robot             COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
41*7c3d14c8STreehugger Robot         }
42*7c3d14c8STreehugger Robot         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
43*7c3d14c8STreehugger Robot                  crt_isfinite(__c) && crt_isfinite(__d))
44*7c3d14c8STreehugger Robot         {
45*7c3d14c8STreehugger Robot             __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
46*7c3d14c8STreehugger Robot             __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
47*7c3d14c8STreehugger Robot             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
48*7c3d14c8STreehugger Robot             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
49*7c3d14c8STreehugger Robot         }
50*7c3d14c8STreehugger Robot         else if (crt_isinf(__logbw) && __logbw > 0 &&
51*7c3d14c8STreehugger Robot                  crt_isfinite(__a) && crt_isfinite(__b))
52*7c3d14c8STreehugger Robot         {
53*7c3d14c8STreehugger Robot             __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
54*7c3d14c8STreehugger Robot             __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
55*7c3d14c8STreehugger Robot             COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
56*7c3d14c8STreehugger Robot             COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
57*7c3d14c8STreehugger Robot         }
58*7c3d14c8STreehugger Robot     }
59*7c3d14c8STreehugger Robot     return z;
60*7c3d14c8STreehugger Robot }
61