xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/divxc3.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- divxc3.c - Implement __divxc3 -------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements __divxc3 for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #if !_ARCH_PPC
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "int_lib.h"
160b57cec5SDimitry Andric #include "int_math.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric // Returns: the quotient of (a + ib) / (c + id)
190b57cec5SDimitry Andric 
20*5f757f3fSDimitry Andric COMPILER_RT_ABI Lcomplex __divxc3(xf_float __a, xf_float __b, xf_float __c,
21*5f757f3fSDimitry Andric                                   xf_float __d) {
220b57cec5SDimitry Andric   int __ilogbw = 0;
23*5f757f3fSDimitry Andric   xf_float __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
240b57cec5SDimitry Andric   if (crt_isfinite(__logbw)) {
250b57cec5SDimitry Andric     __ilogbw = (int)__logbw;
260b57cec5SDimitry Andric     __c = crt_scalbnl(__c, -__ilogbw);
270b57cec5SDimitry Andric     __d = crt_scalbnl(__d, -__ilogbw);
280b57cec5SDimitry Andric   }
29*5f757f3fSDimitry Andric   xf_float __denom = __c * __c + __d * __d;
300b57cec5SDimitry Andric   Lcomplex z;
310b57cec5SDimitry Andric   COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
320b57cec5SDimitry Andric   COMPLEX_IMAGINARY(z) =
330b57cec5SDimitry Andric       crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
340b57cec5SDimitry Andric   if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
350b57cec5SDimitry Andric     if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
360b57cec5SDimitry Andric       COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a;
370b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
380b57cec5SDimitry Andric     } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
390b57cec5SDimitry Andric                crt_isfinite(__d)) {
400b57cec5SDimitry Andric       __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
410b57cec5SDimitry Andric       __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
420b57cec5SDimitry Andric       COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
430b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
440b57cec5SDimitry Andric     } else if (crt_isinf(__logbw) && __logbw > 0 && crt_isfinite(__a) &&
450b57cec5SDimitry Andric                crt_isfinite(__b)) {
460b57cec5SDimitry Andric       __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
470b57cec5SDimitry Andric       __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
480b57cec5SDimitry Andric       COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
490b57cec5SDimitry Andric       COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric   return z;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric #endif
56