Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/divdc3.c
35260 views
//===-- divdc3.c - Implement __divdc3 -------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements __divdc3 for the compiler_rt library.9//10//===----------------------------------------------------------------------===//1112#define DOUBLE_PRECISION13#include "fp_lib.h"14#include "int_lib.h"15#include "int_math.h"1617// Returns: the quotient of (a + ib) / (c + id)1819COMPILER_RT_ABI Dcomplex __divdc3(double __a, double __b, double __c,20double __d) {21int __ilogbw = 0;22double __logbw = __compiler_rt_logb(__compiler_rt_fmax(crt_fabs(__c),23crt_fabs(__d)));24if (crt_isfinite(__logbw)) {25__ilogbw = (int)__logbw;26__c = __compiler_rt_scalbn(__c, -__ilogbw);27__d = __compiler_rt_scalbn(__d, -__ilogbw);28}29double __denom = __c * __c + __d * __d;30Dcomplex z;31COMPLEX_REAL(z) =32__compiler_rt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);33COMPLEX_IMAGINARY(z) =34__compiler_rt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);35if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {36if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {37COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;38COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;39} else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&40crt_isfinite(__d)) {41__a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);42__b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);43COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);44COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);45} else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&46crt_isfinite(__b)) {47__c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);48__d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);49COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);50COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);51}52}53return z;54}555657