10b57cec5SDimitry Andric //===-- divtc3.c - Implement __divtc3 -------------------------------------===//
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 __divtc3 for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #define QUAD_PRECISION
140b57cec5SDimitry Andric #include "fp_lib.h"
155f757f3fSDimitry Andric
16*c80e69b0SDimitry Andric #if defined(CRT_HAS_128BIT) && defined(CRT_HAS_F128)
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric // Returns: the quotient of (a + ib) / (c + id)
190b57cec5SDimitry Andric
__divtc3(fp_t __a,fp_t __b,fp_t __c,fp_t __d)205f757f3fSDimitry Andric COMPILER_RT_ABI Qcomplex __divtc3(fp_t __a, fp_t __b, fp_t __c, fp_t __d) {
210b57cec5SDimitry Andric int __ilogbw = 0;
225f757f3fSDimitry Andric fp_t __logbw = __compiler_rt_logbtf(
235f757f3fSDimitry Andric __compiler_rt_fmaxtf(crt_fabstf(__c), crt_fabstf(__d)));
240b57cec5SDimitry Andric if (crt_isfinite(__logbw)) {
250b57cec5SDimitry Andric __ilogbw = (int)__logbw;
265f757f3fSDimitry Andric __c = __compiler_rt_scalbntf(__c, -__ilogbw);
275f757f3fSDimitry Andric __d = __compiler_rt_scalbntf(__d, -__ilogbw);
280b57cec5SDimitry Andric }
295f757f3fSDimitry Andric fp_t __denom = __c * __c + __d * __d;
305f757f3fSDimitry Andric Qcomplex z;
315f757f3fSDimitry Andric COMPLEXTF_REAL(z) =
325f757f3fSDimitry Andric __compiler_rt_scalbntf((__a * __c + __b * __d) / __denom, -__ilogbw);
335f757f3fSDimitry Andric COMPLEXTF_IMAGINARY(z) =
345f757f3fSDimitry Andric __compiler_rt_scalbntf((__b * __c - __a * __d) / __denom, -__ilogbw);
355f757f3fSDimitry Andric if (crt_isnan(COMPLEXTF_REAL(z)) && crt_isnan(COMPLEXTF_IMAGINARY(z))) {
360b57cec5SDimitry Andric if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
375f757f3fSDimitry Andric COMPLEXTF_REAL(z) = crt_copysigntf(CRT_INFINITY, __c) * __a;
385f757f3fSDimitry Andric COMPLEXTF_IMAGINARY(z) = crt_copysigntf(CRT_INFINITY, __c) * __b;
390b57cec5SDimitry Andric } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
400b57cec5SDimitry Andric crt_isfinite(__d)) {
415f757f3fSDimitry Andric __a = crt_copysigntf(crt_isinf(__a) ? (fp_t)1.0 : (fp_t)0.0, __a);
425f757f3fSDimitry Andric __b = crt_copysigntf(crt_isinf(__b) ? (fp_t)1.0 : (fp_t)0.0, __b);
435f757f3fSDimitry Andric COMPLEXTF_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
445f757f3fSDimitry Andric COMPLEXTF_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
450b57cec5SDimitry Andric } else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&
460b57cec5SDimitry Andric crt_isfinite(__b)) {
475f757f3fSDimitry Andric __c = crt_copysigntf(crt_isinf(__c) ? (fp_t)1.0 : (fp_t)0.0, __c);
485f757f3fSDimitry Andric __d = crt_copysigntf(crt_isinf(__d) ? (fp_t)1.0 : (fp_t)0.0, __d);
495f757f3fSDimitry Andric COMPLEXTF_REAL(z) = 0.0 * (__a * __c + __b * __d);
505f757f3fSDimitry Andric COMPLEXTF_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric return z;
540b57cec5SDimitry Andric }
555f757f3fSDimitry Andric
565f757f3fSDimitry Andric #endif
57