2019-04-29 06:47:49 +08:00
|
|
|
//===-- divdc3.c - Implement __divdc3 -------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements __divdc3 for the compiler_rt library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-27 00:47:03 +08:00
|
|
|
|
[compiler-rt] [builtins] Add logb/logbf/logbl methods to compiler-rt to avoid libm dependencies when possible.
Summary:
The complex division builtins (div?c3) use logb methods from libm to scale numbers during division and avoid rounding issues. However, these come from libm, meaning anyone that uses --rtlib=compiler-rt also has to include -lm. Implement logb* methods for standard ieee 754 floats so we can avoid -lm on those platforms, falling back to the old behavior (using either logb() or `__builtin_logb()`) when not supported.
These new methods are defined internally as `__compiler_rt_logb` so as not to conflict with the libm definitions in any way.
This fixes just the libm methods mentioned in PR32279 and PR28652. libc is still required, although that seems to not be an issue.
Note: this is proposed as an alternative to just adding -lm: D49330.
Reviewers: efriedma, compnerd, scanon, echristo
Reviewed By: echristo
Subscribers: jsji, echristo, nemanjai, dberris, mgorny, kbarton, delcypher, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D49514
llvm-svn: 342917
2018-09-25 04:39:19 +08:00
|
|
|
#define DOUBLE_PRECISION
|
|
|
|
#include "fp_lib.h"
|
2009-06-27 00:47:03 +08:00
|
|
|
#include "int_lib.h"
|
2011-11-16 15:33:00 +08:00
|
|
|
#include "int_math.h"
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2019-04-29 06:47:49 +08:00
|
|
|
// Returns: the quotient of (a + ib) / (c + id)
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
COMPILER_RT_ABI Dcomplex __divdc3(double __a, double __b, double __c,
|
|
|
|
double __d) {
|
|
|
|
int __ilogbw = 0;
|
2021-02-26 07:43:12 +08:00
|
|
|
double __logbw = __compiler_rt_logb(__compiler_rt_fmax(crt_fabs(__c),
|
|
|
|
crt_fabs(__d)));
|
2019-04-29 05:53:32 +08:00
|
|
|
if (crt_isfinite(__logbw)) {
|
|
|
|
__ilogbw = (int)__logbw;
|
2021-02-26 07:43:12 +08:00
|
|
|
__c = __compiler_rt_scalbn(__c, -__ilogbw);
|
|
|
|
__d = __compiler_rt_scalbn(__d, -__ilogbw);
|
2019-04-29 05:53:32 +08:00
|
|
|
}
|
|
|
|
double __denom = __c * __c + __d * __d;
|
|
|
|
Dcomplex z;
|
2021-02-26 07:43:12 +08:00
|
|
|
COMPLEX_REAL(z) =
|
|
|
|
__compiler_rt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
|
2019-04-29 05:53:32 +08:00
|
|
|
COMPLEX_IMAGINARY(z) =
|
2021-02-26 07:43:12 +08:00
|
|
|
__compiler_rt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
|
2019-04-29 05:53:32 +08:00
|
|
|
if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
|
|
|
|
if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
|
|
|
|
COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
|
|
|
|
COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
|
|
|
|
} else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
|
|
|
|
crt_isfinite(__d)) {
|
|
|
|
__a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
|
|
|
|
__b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
|
|
|
|
COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
|
|
|
|
COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
|
|
|
|
} else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&
|
|
|
|
crt_isfinite(__b)) {
|
|
|
|
__c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
|
|
|
|
__d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
|
|
|
|
COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
|
|
|
|
COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2019-04-29 05:53:32 +08:00
|
|
|
}
|
|
|
|
return z;
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|