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