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