2015-11-23 03:13:49 +08:00
|
|
|
/*===-- divtc3.c - Implement __divtc3 -------------------------------------===
|
|
|
|
*
|
2019-01-19 18:56:40 +08:00
|
|
|
* 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
|
2015-11-23 03:13:49 +08:00
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*
|
|
|
|
* This file implements __divtc3 for the compiler_rt library.
|
|
|
|
*
|
|
|
|
*===----------------------------------------------------------------------===
|
|
|
|
*/
|
|
|
|
|
[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 QUAD_PRECISION
|
|
|
|
#include "fp_lib.h"
|
2015-11-23 03:13:49 +08:00
|
|
|
#include "int_lib.h"
|
|
|
|
#include "int_math.h"
|
|
|
|
|
|
|
|
/* Returns: the quotient of (a + ib) / (c + id) */
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
COMPILER_RT_ABI Lcomplex __divtc3(long double __a, long double __b,
|
|
|
|
long double __c, long double __d) {
|
|
|
|
int __ilogbw = 0;
|
|
|
|
long double __logbw =
|
|
|
|
__compiler_rt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
|
|
|
|
if (crt_isfinite(__logbw)) {
|
|
|
|
__ilogbw = (int)__logbw;
|
|
|
|
__c = crt_scalbnl(__c, -__ilogbw);
|
|
|
|
__d = crt_scalbnl(__d, -__ilogbw);
|
|
|
|
}
|
|
|
|
long double __denom = __c * __c + __d * __d;
|
|
|
|
Lcomplex z;
|
|
|
|
COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
|
|
|
|
COMPLEX_IMAGINARY(z) =
|
|
|
|
crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
|
|
|
|
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_copysignl(CRT_INFINITY, __c) * __a;
|
|
|
|
COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
|
|
|
|
} else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
|
|
|
|
crt_isfinite(__d)) {
|
|
|
|
__a = crt_copysignl(crt_isinf(__a) ? 1.0 : 0.0, __a);
|
|
|
|
__b = crt_copysignl(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_copysignl(crt_isinf(__c) ? 1.0 : 0.0, __c);
|
|
|
|
__d = crt_copysignl(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);
|
2015-11-23 03:13:49 +08:00
|
|
|
}
|
2019-04-29 05:53:32 +08:00
|
|
|
}
|
|
|
|
return z;
|
2015-11-23 03:13:49 +08:00
|
|
|
}
|