xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/muldc3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* ===-- muldc3.c - Implement __muldc3 -------------------------------------===
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 __muldc3 for the compiler_rt library.
11*0a6a1f1dSLionel Sambuc  *
12*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
13*0a6a1f1dSLionel Sambuc  */
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "int_lib.h"
16*0a6a1f1dSLionel Sambuc #include "int_math.h"
17*0a6a1f1dSLionel Sambuc 
18*0a6a1f1dSLionel Sambuc /* Returns: the product of a + ib and c + id */
19*0a6a1f1dSLionel Sambuc 
20*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI double _Complex
__muldc3(double __a,double __b,double __c,double __d)21*0a6a1f1dSLionel Sambuc __muldc3(double __a, double __b, double __c, double __d)
22*0a6a1f1dSLionel Sambuc {
23*0a6a1f1dSLionel Sambuc     double __ac = __a * __c;
24*0a6a1f1dSLionel Sambuc     double __bd = __b * __d;
25*0a6a1f1dSLionel Sambuc     double __ad = __a * __d;
26*0a6a1f1dSLionel Sambuc     double __bc = __b * __c;
27*0a6a1f1dSLionel Sambuc     double _Complex z;
28*0a6a1f1dSLionel Sambuc     __real__ z = __ac - __bd;
29*0a6a1f1dSLionel Sambuc     __imag__ z = __ad + __bc;
30*0a6a1f1dSLionel Sambuc     if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
31*0a6a1f1dSLionel Sambuc     {
32*0a6a1f1dSLionel Sambuc         int __recalc = 0;
33*0a6a1f1dSLionel Sambuc         if (crt_isinf(__a) || crt_isinf(__b))
34*0a6a1f1dSLionel Sambuc         {
35*0a6a1f1dSLionel Sambuc             __a = crt_copysign(crt_isinf(__a) ? 1 : 0, __a);
36*0a6a1f1dSLionel Sambuc             __b = crt_copysign(crt_isinf(__b) ? 1 : 0, __b);
37*0a6a1f1dSLionel Sambuc             if (crt_isnan(__c))
38*0a6a1f1dSLionel Sambuc                 __c = crt_copysign(0, __c);
39*0a6a1f1dSLionel Sambuc             if (crt_isnan(__d))
40*0a6a1f1dSLionel Sambuc                 __d = crt_copysign(0, __d);
41*0a6a1f1dSLionel Sambuc             __recalc = 1;
42*0a6a1f1dSLionel Sambuc         }
43*0a6a1f1dSLionel Sambuc         if (crt_isinf(__c) || crt_isinf(__d))
44*0a6a1f1dSLionel Sambuc         {
45*0a6a1f1dSLionel Sambuc             __c = crt_copysign(crt_isinf(__c) ? 1 : 0, __c);
46*0a6a1f1dSLionel Sambuc             __d = crt_copysign(crt_isinf(__d) ? 1 : 0, __d);
47*0a6a1f1dSLionel Sambuc             if (crt_isnan(__a))
48*0a6a1f1dSLionel Sambuc                 __a = crt_copysign(0, __a);
49*0a6a1f1dSLionel Sambuc             if (crt_isnan(__b))
50*0a6a1f1dSLionel Sambuc                 __b = crt_copysign(0, __b);
51*0a6a1f1dSLionel Sambuc             __recalc = 1;
52*0a6a1f1dSLionel Sambuc         }
53*0a6a1f1dSLionel Sambuc         if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
54*0a6a1f1dSLionel Sambuc                           crt_isinf(__ad) || crt_isinf(__bc)))
55*0a6a1f1dSLionel Sambuc         {
56*0a6a1f1dSLionel Sambuc             if (crt_isnan(__a))
57*0a6a1f1dSLionel Sambuc                 __a = crt_copysign(0, __a);
58*0a6a1f1dSLionel Sambuc             if (crt_isnan(__b))
59*0a6a1f1dSLionel Sambuc                 __b = crt_copysign(0, __b);
60*0a6a1f1dSLionel Sambuc             if (crt_isnan(__c))
61*0a6a1f1dSLionel Sambuc                 __c = crt_copysign(0, __c);
62*0a6a1f1dSLionel Sambuc             if (crt_isnan(__d))
63*0a6a1f1dSLionel Sambuc                 __d = crt_copysign(0, __d);
64*0a6a1f1dSLionel Sambuc             __recalc = 1;
65*0a6a1f1dSLionel Sambuc         }
66*0a6a1f1dSLionel Sambuc         if (__recalc)
67*0a6a1f1dSLionel Sambuc         {
68*0a6a1f1dSLionel Sambuc             __real__ z = CRT_INFINITY * (__a * __c - __b * __d);
69*0a6a1f1dSLionel Sambuc             __imag__ z = CRT_INFINITY * (__a * __d + __b * __c);
70*0a6a1f1dSLionel Sambuc         }
71*0a6a1f1dSLionel Sambuc     }
72*0a6a1f1dSLionel Sambuc     return z;
73*0a6a1f1dSLionel Sambuc }
74