xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/mulsc3.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 //===-- mulsc3.c - Implement __mulsc3 -------------------------------------===//
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 __mulsc3 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 Fcomplex __mulsc3(float __a, float __b, float __c, float __d) {
19   float __ac = __a * __c;
20   float __bd = __b * __d;
21   float __ad = __a * __d;
22   float __bc = __b * __c;
23   Fcomplex z;
24   COMPLEX_REAL(z) = __ac - __bd;
25   COMPLEX_IMAGINARY(z) = __ad + __bc;
26   if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
27     int __recalc = 0;
28     if (crt_isinf(__a) || crt_isinf(__b)) {
29       __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
30       __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
31       if (crt_isnan(__c))
32         __c = crt_copysignf(0, __c);
33       if (crt_isnan(__d))
34         __d = crt_copysignf(0, __d);
35       __recalc = 1;
36     }
37     if (crt_isinf(__c) || crt_isinf(__d)) {
38       __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
39       __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
40       if (crt_isnan(__a))
41         __a = crt_copysignf(0, __a);
42       if (crt_isnan(__b))
43         __b = crt_copysignf(0, __b);
44       __recalc = 1;
45     }
46     if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) || crt_isinf(__ad) ||
47                       crt_isinf(__bc))) {
48       if (crt_isnan(__a))
49         __a = crt_copysignf(0, __a);
50       if (crt_isnan(__b))
51         __b = crt_copysignf(0, __b);
52       if (crt_isnan(__c))
53         __c = crt_copysignf(0, __c);
54       if (crt_isnan(__d))
55         __d = crt_copysignf(0, __d);
56       __recalc = 1;
57     }
58     if (__recalc) {
59       COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c - __b * __d);
60       COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__a * __d + __b * __c);
61     }
62   }
63   return z;
64 }
65