xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/divxc3.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- divxc3.c - Implement __divxc3 -------------------------------------===//
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 __divxc3 for the compiler_rt library.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #if !_ARCH_PPC
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "int_lib.h"
16*0b57cec5SDimitry Andric #include "int_math.h"
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric // Returns: the quotient of (a + ib) / (c + id)
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric COMPILER_RT_ABI Lcomplex __divxc3(long double __a, long double __b,
21*0b57cec5SDimitry Andric                                   long double __c, long double __d) {
22*0b57cec5SDimitry Andric   int __ilogbw = 0;
23*0b57cec5SDimitry Andric   long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
24*0b57cec5SDimitry Andric   if (crt_isfinite(__logbw)) {
25*0b57cec5SDimitry Andric     __ilogbw = (int)__logbw;
26*0b57cec5SDimitry Andric     __c = crt_scalbnl(__c, -__ilogbw);
27*0b57cec5SDimitry Andric     __d = crt_scalbnl(__d, -__ilogbw);
28*0b57cec5SDimitry Andric   }
29*0b57cec5SDimitry Andric   long double __denom = __c * __c + __d * __d;
30*0b57cec5SDimitry Andric   Lcomplex z;
31*0b57cec5SDimitry Andric   COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
32*0b57cec5SDimitry Andric   COMPLEX_IMAGINARY(z) =
33*0b57cec5SDimitry Andric       crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
34*0b57cec5SDimitry Andric   if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
35*0b57cec5SDimitry Andric     if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
36*0b57cec5SDimitry Andric       COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a;
37*0b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
38*0b57cec5SDimitry Andric     } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
39*0b57cec5SDimitry Andric                crt_isfinite(__d)) {
40*0b57cec5SDimitry Andric       __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
41*0b57cec5SDimitry Andric       __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
42*0b57cec5SDimitry Andric       COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
43*0b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
44*0b57cec5SDimitry Andric     } else if (crt_isinf(__logbw) && __logbw > 0 && crt_isfinite(__a) &&
45*0b57cec5SDimitry Andric                crt_isfinite(__b)) {
46*0b57cec5SDimitry Andric       __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
47*0b57cec5SDimitry Andric       __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
48*0b57cec5SDimitry Andric       COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
49*0b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
50*0b57cec5SDimitry Andric     }
51*0b57cec5SDimitry Andric   }
52*0b57cec5SDimitry Andric   return z;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric #endif
56