10b57cec5SDimitry Andric //===-- divsc3.c - Implement __divsc3 -------------------------------------===//
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 __divsc3 for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #define SINGLE_PRECISION
140b57cec5SDimitry Andric #include "fp_lib.h"
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
__divsc3(float __a,float __b,float __c,float __d)200b57cec5SDimitry Andric COMPILER_RT_ABI Fcomplex __divsc3(float __a, float __b, float __c, float __d) {
210b57cec5SDimitry Andric int __ilogbw = 0;
220b57cec5SDimitry Andric float __logbw =
23*fe6060f1SDimitry Andric __compiler_rt_logbf(__compiler_rt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
240b57cec5SDimitry Andric if (crt_isfinite(__logbw)) {
250b57cec5SDimitry Andric __ilogbw = (int)__logbw;
26*fe6060f1SDimitry Andric __c = __compiler_rt_scalbnf(__c, -__ilogbw);
27*fe6060f1SDimitry Andric __d = __compiler_rt_scalbnf(__d, -__ilogbw);
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric float __denom = __c * __c + __d * __d;
300b57cec5SDimitry Andric Fcomplex z;
31*fe6060f1SDimitry Andric COMPLEX_REAL(z) =
32*fe6060f1SDimitry Andric __compiler_rt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
330b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) =
34*fe6060f1SDimitry Andric __compiler_rt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
350b57cec5SDimitry Andric if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
360b57cec5SDimitry Andric if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
370b57cec5SDimitry Andric COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
380b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
390b57cec5SDimitry Andric } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
400b57cec5SDimitry Andric crt_isfinite(__d)) {
410b57cec5SDimitry Andric __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
420b57cec5SDimitry Andric __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
430b57cec5SDimitry Andric COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
440b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
450b57cec5SDimitry Andric } else if (crt_isinf(__logbw) && __logbw > 0 && crt_isfinite(__a) &&
460b57cec5SDimitry Andric crt_isfinite(__b)) {
470b57cec5SDimitry Andric __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
480b57cec5SDimitry Andric __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
490b57cec5SDimitry Andric COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
500b57cec5SDimitry Andric COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric return z;
540b57cec5SDimitry Andric }
55