1*0b57cec5SDimitry Andric //===-- muldc3.c - Implement __muldc3 -------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements __muldc3 for the compiler_rt library.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "int_lib.h"
14*0b57cec5SDimitry Andric #include "int_math.h"
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric // Returns: the product of a + ib and c + id
17*0b57cec5SDimitry Andric
__muldc3(double __a,double __b,double __c,double __d)18*0b57cec5SDimitry Andric COMPILER_RT_ABI Dcomplex __muldc3(double __a, double __b, double __c,
19*0b57cec5SDimitry Andric double __d) {
20*0b57cec5SDimitry Andric double __ac = __a * __c;
21*0b57cec5SDimitry Andric double __bd = __b * __d;
22*0b57cec5SDimitry Andric double __ad = __a * __d;
23*0b57cec5SDimitry Andric double __bc = __b * __c;
24*0b57cec5SDimitry Andric Dcomplex z;
25*0b57cec5SDimitry Andric COMPLEX_REAL(z) = __ac - __bd;
26*0b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) = __ad + __bc;
27*0b57cec5SDimitry Andric if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
28*0b57cec5SDimitry Andric int __recalc = 0;
29*0b57cec5SDimitry Andric if (crt_isinf(__a) || crt_isinf(__b)) {
30*0b57cec5SDimitry Andric __a = crt_copysign(crt_isinf(__a) ? 1 : 0, __a);
31*0b57cec5SDimitry Andric __b = crt_copysign(crt_isinf(__b) ? 1 : 0, __b);
32*0b57cec5SDimitry Andric if (crt_isnan(__c))
33*0b57cec5SDimitry Andric __c = crt_copysign(0, __c);
34*0b57cec5SDimitry Andric if (crt_isnan(__d))
35*0b57cec5SDimitry Andric __d = crt_copysign(0, __d);
36*0b57cec5SDimitry Andric __recalc = 1;
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric if (crt_isinf(__c) || crt_isinf(__d)) {
39*0b57cec5SDimitry Andric __c = crt_copysign(crt_isinf(__c) ? 1 : 0, __c);
40*0b57cec5SDimitry Andric __d = crt_copysign(crt_isinf(__d) ? 1 : 0, __d);
41*0b57cec5SDimitry Andric if (crt_isnan(__a))
42*0b57cec5SDimitry Andric __a = crt_copysign(0, __a);
43*0b57cec5SDimitry Andric if (crt_isnan(__b))
44*0b57cec5SDimitry Andric __b = crt_copysign(0, __b);
45*0b57cec5SDimitry Andric __recalc = 1;
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) || crt_isinf(__ad) ||
48*0b57cec5SDimitry Andric crt_isinf(__bc))) {
49*0b57cec5SDimitry Andric if (crt_isnan(__a))
50*0b57cec5SDimitry Andric __a = crt_copysign(0, __a);
51*0b57cec5SDimitry Andric if (crt_isnan(__b))
52*0b57cec5SDimitry Andric __b = crt_copysign(0, __b);
53*0b57cec5SDimitry Andric if (crt_isnan(__c))
54*0b57cec5SDimitry Andric __c = crt_copysign(0, __c);
55*0b57cec5SDimitry Andric if (crt_isnan(__d))
56*0b57cec5SDimitry Andric __d = crt_copysign(0, __d);
57*0b57cec5SDimitry Andric __recalc = 1;
58*0b57cec5SDimitry Andric }
59*0b57cec5SDimitry Andric if (__recalc) {
60*0b57cec5SDimitry Andric COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c - __b * __d);
61*0b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__a * __d + __b * __c);
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric }
64*0b57cec5SDimitry Andric return z;
65*0b57cec5SDimitry Andric }
66