llvm-project
55 строк · 2.3 Кб
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
20COMPILER_RT_ABI Dcomplex __divdc3(double __a, double __b, double __c,
21double __d) {
22int __ilogbw = 0;
23double __logbw = __compiler_rt_logb(__compiler_rt_fmax(crt_fabs(__c),
24crt_fabs(__d)));
25if (crt_isfinite(__logbw)) {
26__ilogbw = (int)__logbw;
27__c = __compiler_rt_scalbn(__c, -__ilogbw);
28__d = __compiler_rt_scalbn(__d, -__ilogbw);
29}
30double __denom = __c * __c + __d * __d;
31Dcomplex z;
32COMPLEX_REAL(z) =
33__compiler_rt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
34COMPLEX_IMAGINARY(z) =
35__compiler_rt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
36if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
37if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
38COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
39COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
40} else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
41crt_isfinite(__d)) {
42__a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
43__b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
44COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
45COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
46} else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&
47crt_isfinite(__b)) {
48__c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
49__d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
50COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
51COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
52}
53}
54return z;
55}
56