xref: /aosp_15_r20/external/compiler-rt/lib/builtins/multc3.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===-- multc3.c - Implement __multc3 -------------------------------------===
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 __multc3 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 long double _Complex
__multc3(long double a,long double b,long double c,long double d)21*7c3d14c8STreehugger Robot __multc3(long double a, long double b, long double c, long double d)
22*7c3d14c8STreehugger Robot {
23*7c3d14c8STreehugger Robot     long double ac = a * c;
24*7c3d14c8STreehugger Robot     long double bd = b * d;
25*7c3d14c8STreehugger Robot     long double ad = a * d;
26*7c3d14c8STreehugger Robot     long double bc = b * c;
27*7c3d14c8STreehugger Robot     long double _Complex z;
28*7c3d14c8STreehugger Robot     __real__ z = ac - bd;
29*7c3d14c8STreehugger Robot     __imag__ z = ad + bc;
30*7c3d14c8STreehugger Robot     if (crt_isnan(__real__ z) && crt_isnan(__imag__ z)) {
31*7c3d14c8STreehugger Robot         int recalc = 0;
32*7c3d14c8STreehugger Robot         if (crt_isinf(a) || crt_isinf(b)) {
33*7c3d14c8STreehugger Robot             a = crt_copysignl(crt_isinf(a) ? 1 : 0, a);
34*7c3d14c8STreehugger Robot             b = crt_copysignl(crt_isinf(b) ? 1 : 0, b);
35*7c3d14c8STreehugger Robot             if (crt_isnan(c))
36*7c3d14c8STreehugger Robot                 c = crt_copysignl(0, c);
37*7c3d14c8STreehugger Robot             if (crt_isnan(d))
38*7c3d14c8STreehugger Robot                 d = crt_copysignl(0, d);
39*7c3d14c8STreehugger Robot             recalc = 1;
40*7c3d14c8STreehugger Robot         }
41*7c3d14c8STreehugger Robot         if (crt_isinf(c) || crt_isinf(d)) {
42*7c3d14c8STreehugger Robot             c = crt_copysignl(crt_isinf(c) ? 1 : 0, c);
43*7c3d14c8STreehugger Robot             d = crt_copysignl(crt_isinf(d) ? 1 : 0, d);
44*7c3d14c8STreehugger Robot             if (crt_isnan(a))
45*7c3d14c8STreehugger Robot                 a = crt_copysignl(0, a);
46*7c3d14c8STreehugger Robot             if (crt_isnan(b))
47*7c3d14c8STreehugger Robot                 b = crt_copysignl(0, b);
48*7c3d14c8STreehugger Robot             recalc = 1;
49*7c3d14c8STreehugger Robot         }
50*7c3d14c8STreehugger Robot         if (!recalc && (crt_isinf(ac) || crt_isinf(bd) ||
51*7c3d14c8STreehugger Robot                           crt_isinf(ad) || crt_isinf(bc))) {
52*7c3d14c8STreehugger Robot             if (crt_isnan(a))
53*7c3d14c8STreehugger Robot                 a = crt_copysignl(0, a);
54*7c3d14c8STreehugger Robot             if (crt_isnan(b))
55*7c3d14c8STreehugger Robot                 b = crt_copysignl(0, b);
56*7c3d14c8STreehugger Robot             if (crt_isnan(c))
57*7c3d14c8STreehugger Robot                 c = crt_copysignl(0, c);
58*7c3d14c8STreehugger Robot             if (crt_isnan(d))
59*7c3d14c8STreehugger Robot                 d = crt_copysignl(0, d);
60*7c3d14c8STreehugger Robot             recalc = 1;
61*7c3d14c8STreehugger Robot         }
62*7c3d14c8STreehugger Robot         if (recalc) {
63*7c3d14c8STreehugger Robot             __real__ z = CRT_INFINITY * (a * c - b * d);
64*7c3d14c8STreehugger Robot             __imag__ z = CRT_INFINITY * (a * d + b * c);
65*7c3d14c8STreehugger Robot         }
66*7c3d14c8STreehugger Robot     }
67*7c3d14c8STreehugger Robot     return z;
68*7c3d14c8STreehugger Robot }
69