1 //===-- divdc3.c - Implement __divdc3 -------------------------------------===// 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 __divdc3 for the compiler_rt library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #define DOUBLE_PRECISION 14 #include "fp_lib.h" 15 #include "int_lib.h" 16 #include "int_math.h" 17 18 // Returns: the quotient of (a + ib) / (c + id) 19 20 COMPILER_RT_ABI Dcomplex __divdc3(double __a, double __b, double __c, 21 double __d) { 22 int __ilogbw = 0; 23 double __logbw = __compiler_rt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d))); 24 if (crt_isfinite(__logbw)) { 25 __ilogbw = (int)__logbw; 26 __c = crt_scalbn(__c, -__ilogbw); 27 __d = crt_scalbn(__d, -__ilogbw); 28 } 29 double __denom = __c * __c + __d * __d; 30 Dcomplex z; 31 COMPLEX_REAL(z) = crt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw); 32 COMPLEX_IMAGINARY(z) = 33 crt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw); 34 if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) { 35 if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) { 36 COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a; 37 COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b; 38 } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) && 39 crt_isfinite(__d)) { 40 __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a); 41 __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b); 42 COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d); 43 COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d); 44 } else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) && 45 crt_isfinite(__b)) { 46 __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c); 47 __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d); 48 COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d); 49 COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d); 50 } 51 } 52 return z; 53 } 54