xref: /aosp_15_r20/external/compiler-rt/lib/builtins/muldc3.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===-- muldc3.c - Implement __muldc3 -------------------------------------===
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 __muldc3 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 product of a + ib and c + id */
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot COMPILER_RT_ABI Dcomplex
__muldc3(double __a,double __b,double __c,double __d)21*7c3d14c8STreehugger Robot __muldc3(double __a, double __b, double __c, double __d)
22*7c3d14c8STreehugger Robot {
23*7c3d14c8STreehugger Robot     double __ac = __a * __c;
24*7c3d14c8STreehugger Robot     double __bd = __b * __d;
25*7c3d14c8STreehugger Robot     double __ad = __a * __d;
26*7c3d14c8STreehugger Robot     double __bc = __b * __c;
27*7c3d14c8STreehugger Robot     Dcomplex z;
28*7c3d14c8STreehugger Robot     COMPLEX_REAL(z) = __ac - __bd;
29*7c3d14c8STreehugger Robot     COMPLEX_IMAGINARY(z) = __ad + __bc;
30*7c3d14c8STreehugger Robot     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
31*7c3d14c8STreehugger Robot     {
32*7c3d14c8STreehugger Robot         int __recalc = 0;
33*7c3d14c8STreehugger Robot         if (crt_isinf(__a) || crt_isinf(__b))
34*7c3d14c8STreehugger Robot         {
35*7c3d14c8STreehugger Robot             __a = crt_copysign(crt_isinf(__a) ? 1 : 0, __a);
36*7c3d14c8STreehugger Robot             __b = crt_copysign(crt_isinf(__b) ? 1 : 0, __b);
37*7c3d14c8STreehugger Robot             if (crt_isnan(__c))
38*7c3d14c8STreehugger Robot                 __c = crt_copysign(0, __c);
39*7c3d14c8STreehugger Robot             if (crt_isnan(__d))
40*7c3d14c8STreehugger Robot                 __d = crt_copysign(0, __d);
41*7c3d14c8STreehugger Robot             __recalc = 1;
42*7c3d14c8STreehugger Robot         }
43*7c3d14c8STreehugger Robot         if (crt_isinf(__c) || crt_isinf(__d))
44*7c3d14c8STreehugger Robot         {
45*7c3d14c8STreehugger Robot             __c = crt_copysign(crt_isinf(__c) ? 1 : 0, __c);
46*7c3d14c8STreehugger Robot             __d = crt_copysign(crt_isinf(__d) ? 1 : 0, __d);
47*7c3d14c8STreehugger Robot             if (crt_isnan(__a))
48*7c3d14c8STreehugger Robot                 __a = crt_copysign(0, __a);
49*7c3d14c8STreehugger Robot             if (crt_isnan(__b))
50*7c3d14c8STreehugger Robot                 __b = crt_copysign(0, __b);
51*7c3d14c8STreehugger Robot             __recalc = 1;
52*7c3d14c8STreehugger Robot         }
53*7c3d14c8STreehugger Robot         if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
54*7c3d14c8STreehugger Robot                           crt_isinf(__ad) || crt_isinf(__bc)))
55*7c3d14c8STreehugger Robot         {
56*7c3d14c8STreehugger Robot             if (crt_isnan(__a))
57*7c3d14c8STreehugger Robot                 __a = crt_copysign(0, __a);
58*7c3d14c8STreehugger Robot             if (crt_isnan(__b))
59*7c3d14c8STreehugger Robot                 __b = crt_copysign(0, __b);
60*7c3d14c8STreehugger Robot             if (crt_isnan(__c))
61*7c3d14c8STreehugger Robot                 __c = crt_copysign(0, __c);
62*7c3d14c8STreehugger Robot             if (crt_isnan(__d))
63*7c3d14c8STreehugger Robot                 __d = crt_copysign(0, __d);
64*7c3d14c8STreehugger Robot             __recalc = 1;
65*7c3d14c8STreehugger Robot         }
66*7c3d14c8STreehugger Robot         if (__recalc)
67*7c3d14c8STreehugger Robot         {
68*7c3d14c8STreehugger Robot             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c - __b * __d);
69*7c3d14c8STreehugger Robot             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__a * __d + __b * __c);
70*7c3d14c8STreehugger Robot         }
71*7c3d14c8STreehugger Robot     }
72*7c3d14c8STreehugger Robot     return z;
73*7c3d14c8STreehugger Robot }
74