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