2010-07-02 01:58:24 +08:00
|
|
|
//===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
|
|
|
|
//
|
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
|
2010-07-02 01:58:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-07-01 23:52:42 +08:00
|
|
|
// This file is a configuration header for soft-float routines in compiler-rt.
|
2010-07-02 01:58:24 +08:00
|
|
|
// This file does not provide any part of the compiler-rt interface, but defines
|
|
|
|
// many useful constants and utility routines that are used in the
|
|
|
|
// implementation of the soft-float routines in compiler-rt.
|
|
|
|
//
|
2014-03-28 18:29:31 +08:00
|
|
|
// Assumes that float, double and long double correspond to the IEEE-754
|
|
|
|
// binary32, binary64 and binary 128 types, respectively, and that integer
|
|
|
|
// endianness matches floating point endianness on the target platform.
|
2010-07-02 01:58:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-01 23:52:42 +08:00
|
|
|
|
|
|
|
#ifndef FP_LIB_HEADER
|
|
|
|
#define FP_LIB_HEADER
|
|
|
|
|
2011-11-16 02:34:44 +08:00
|
|
|
#include "int_lib.h"
|
[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
|
|
|
#include "int_math.h"
|
2019-04-29 05:53:32 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2010-07-01 23:52:42 +08:00
|
|
|
|
2014-07-08 16:52:57 +08:00
|
|
|
// x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
|
|
|
|
// 32-bit mode.
|
|
|
|
#if defined(__FreeBSD__) && defined(__i386__)
|
2019-04-29 05:53:32 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
#if __FreeBSD_version < 903000 // v9.3
|
|
|
|
#define uint64_t unsigned long long
|
|
|
|
#define int64_t long long
|
|
|
|
#undef UINT64_C
|
|
|
|
#define UINT64_C(c) (c##ULL)
|
|
|
|
#endif
|
2014-07-08 16:52:57 +08:00
|
|
|
#endif
|
|
|
|
|
2010-07-01 23:52:42 +08:00
|
|
|
#if defined SINGLE_PRECISION
|
|
|
|
|
2020-09-02 00:03:29 +08:00
|
|
|
typedef uint16_t half_rep_t;
|
2010-07-01 23:52:42 +08:00
|
|
|
typedef uint32_t rep_t;
|
2020-09-02 00:03:29 +08:00
|
|
|
typedef uint64_t twice_rep_t;
|
2010-07-01 23:52:42 +08:00
|
|
|
typedef int32_t srep_t;
|
|
|
|
typedef float fp_t;
|
2020-09-02 00:03:29 +08:00
|
|
|
#define HALF_REP_C UINT16_C
|
2010-07-01 23:52:42 +08:00
|
|
|
#define REP_C UINT32_C
|
|
|
|
#define significandBits 23
|
|
|
|
|
2020-06-26 20:31:04 +08:00
|
|
|
static __inline int rep_clz(rep_t a) { return clzsi(a); }
|
2010-07-01 23:52:42 +08:00
|
|
|
|
2010-07-05 00:53:39 +08:00
|
|
|
// 32x32 --> 64 bit multiply
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const uint64_t product = (uint64_t)a * b;
|
|
|
|
*hi = product >> 32;
|
|
|
|
*lo = product;
|
2010-07-05 00:53:39 +08:00
|
|
|
}
|
2014-04-02 02:39:58 +08:00
|
|
|
COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
|
2010-07-05 00:53:39 +08:00
|
|
|
|
2010-07-01 23:52:42 +08:00
|
|
|
#elif defined DOUBLE_PRECISION
|
|
|
|
|
2020-09-02 00:03:29 +08:00
|
|
|
typedef uint32_t half_rep_t;
|
2010-07-01 23:52:42 +08:00
|
|
|
typedef uint64_t rep_t;
|
|
|
|
typedef int64_t srep_t;
|
|
|
|
typedef double fp_t;
|
2020-09-02 00:03:29 +08:00
|
|
|
#define HALF_REP_C UINT32_C
|
2010-07-01 23:52:42 +08:00
|
|
|
#define REP_C UINT64_C
|
|
|
|
#define significandBits 52
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline int rep_clz(rep_t a) {
|
2010-07-01 23:52:42 +08:00
|
|
|
#if defined __LP64__
|
2019-04-29 05:53:32 +08:00
|
|
|
return __builtin_clzl(a);
|
2010-07-01 23:52:42 +08:00
|
|
|
#else
|
2019-04-29 05:53:32 +08:00
|
|
|
if (a & REP_C(0xffffffff00000000))
|
2020-04-23 02:25:22 +08:00
|
|
|
return clzsi(a >> 32);
|
2019-04-29 05:53:32 +08:00
|
|
|
else
|
2020-04-23 02:25:22 +08:00
|
|
|
return 32 + clzsi(a & REP_C(0xffffffff));
|
2010-07-01 23:52:42 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-05 00:53:39 +08:00
|
|
|
#define loWord(a) (a & 0xffffffffU)
|
|
|
|
#define hiWord(a) (a >> 32)
|
|
|
|
|
|
|
|
// 64x64 -> 128 wide multiply for platforms that don't have such an operation;
|
|
|
|
// many 64-bit platforms have this operation, but they tend to have hardware
|
|
|
|
// floating-point, so we don't bother with a special case for them here.
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
|
2019-04-29 05:53:32 +08:00
|
|
|
// Each of the component 32x32 -> 64 products
|
|
|
|
const uint64_t plolo = loWord(a) * loWord(b);
|
|
|
|
const uint64_t plohi = loWord(a) * hiWord(b);
|
|
|
|
const uint64_t philo = hiWord(a) * loWord(b);
|
|
|
|
const uint64_t phihi = hiWord(a) * hiWord(b);
|
|
|
|
// Sum terms that contribute to lo in a way that allows us to get the carry
|
|
|
|
const uint64_t r0 = loWord(plolo);
|
|
|
|
const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
|
|
|
|
*lo = r0 + (r1 << 32);
|
|
|
|
// Sum terms contributing to hi with the carry from lo
|
|
|
|
*hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
|
2010-07-05 00:53:39 +08:00
|
|
|
}
|
2014-02-27 04:38:24 +08:00
|
|
|
#undef loWord
|
|
|
|
#undef hiWord
|
2010-07-05 00:53:39 +08:00
|
|
|
|
2014-04-02 02:39:58 +08:00
|
|
|
COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
|
|
|
|
|
2014-03-28 18:29:31 +08:00
|
|
|
#elif defined QUAD_PRECISION
|
2019-07-12 16:30:17 +08:00
|
|
|
#if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__)
|
2014-03-28 18:29:31 +08:00
|
|
|
#define CRT_LDBL_128BIT
|
2020-09-02 00:03:29 +08:00
|
|
|
typedef uint64_t half_rep_t;
|
2014-03-28 18:29:31 +08:00
|
|
|
typedef __uint128_t rep_t;
|
|
|
|
typedef __int128_t srep_t;
|
|
|
|
typedef long double fp_t;
|
2020-09-02 00:03:29 +08:00
|
|
|
#define HALF_REP_C UINT64_C
|
2014-03-28 18:29:31 +08:00
|
|
|
#define REP_C (__uint128_t)
|
|
|
|
// Note: Since there is no explicit way to tell compiler the constant is a
|
|
|
|
// 128-bit integer, we let the constant be casted to 128-bit integer
|
|
|
|
#define significandBits 112
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline int rep_clz(rep_t a) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const union {
|
|
|
|
__uint128_t ll;
|
2014-03-28 18:29:31 +08:00
|
|
|
#if _YUGA_BIG_ENDIAN
|
2019-04-29 05:53:32 +08:00
|
|
|
struct {
|
|
|
|
uint64_t high, low;
|
|
|
|
} s;
|
2010-07-01 23:52:42 +08:00
|
|
|
#else
|
2019-04-29 05:53:32 +08:00
|
|
|
struct {
|
|
|
|
uint64_t low, high;
|
|
|
|
} s;
|
2010-07-01 23:52:42 +08:00
|
|
|
#endif
|
2019-04-29 05:53:32 +08:00
|
|
|
} uu = {.ll = a};
|
2014-03-28 18:29:31 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
uint64_t word;
|
|
|
|
uint64_t add;
|
2010-07-01 23:52:42 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
if (uu.s.high) {
|
|
|
|
word = uu.s.high;
|
|
|
|
add = 0;
|
|
|
|
} else {
|
|
|
|
word = uu.s.low;
|
|
|
|
add = 64;
|
|
|
|
}
|
|
|
|
return __builtin_clzll(word) + add;
|
2014-03-28 18:29:31 +08:00
|
|
|
}
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
#define Word_LoMask UINT64_C(0x00000000ffffffff)
|
|
|
|
#define Word_HiMask UINT64_C(0xffffffff00000000)
|
2014-03-28 18:29:31 +08:00
|
|
|
#define Word_FullMask UINT64_C(0xffffffffffffffff)
|
|
|
|
#define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
|
|
|
|
#define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
|
|
|
|
#define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
|
|
|
|
#define Word_4(a) (uint64_t)(a & Word_LoMask)
|
|
|
|
|
|
|
|
// 128x128 -> 256 wide multiply for platforms that don't have such an operation;
|
|
|
|
// many 64-bit platforms have this operation, but they tend to have hardware
|
|
|
|
// floating-point, so we don't bother with a special case for them here.
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
|
2014-03-28 18:29:31 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
const uint64_t product11 = Word_1(a) * Word_1(b);
|
|
|
|
const uint64_t product12 = Word_1(a) * Word_2(b);
|
|
|
|
const uint64_t product13 = Word_1(a) * Word_3(b);
|
|
|
|
const uint64_t product14 = Word_1(a) * Word_4(b);
|
|
|
|
const uint64_t product21 = Word_2(a) * Word_1(b);
|
|
|
|
const uint64_t product22 = Word_2(a) * Word_2(b);
|
|
|
|
const uint64_t product23 = Word_2(a) * Word_3(b);
|
|
|
|
const uint64_t product24 = Word_2(a) * Word_4(b);
|
|
|
|
const uint64_t product31 = Word_3(a) * Word_1(b);
|
|
|
|
const uint64_t product32 = Word_3(a) * Word_2(b);
|
|
|
|
const uint64_t product33 = Word_3(a) * Word_3(b);
|
|
|
|
const uint64_t product34 = Word_3(a) * Word_4(b);
|
|
|
|
const uint64_t product41 = Word_4(a) * Word_1(b);
|
|
|
|
const uint64_t product42 = Word_4(a) * Word_2(b);
|
|
|
|
const uint64_t product43 = Word_4(a) * Word_3(b);
|
|
|
|
const uint64_t product44 = Word_4(a) * Word_4(b);
|
|
|
|
|
|
|
|
const __uint128_t sum0 = (__uint128_t)product44;
|
|
|
|
const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
|
|
|
|
const __uint128_t sum2 =
|
|
|
|
(__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
|
|
|
|
const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
|
|
|
|
(__uint128_t)product32 + (__uint128_t)product41;
|
|
|
|
const __uint128_t sum4 =
|
|
|
|
(__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
|
|
|
|
const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
|
|
|
|
const __uint128_t sum6 = (__uint128_t)product11;
|
|
|
|
|
|
|
|
const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
|
|
|
|
const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
|
|
|
|
(sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
|
|
|
|
|
|
|
|
*lo = r0 + (r1 << 64);
|
|
|
|
*hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
|
|
|
|
(sum5 << 32) + (sum6 << 64);
|
2014-03-28 18:29:31 +08:00
|
|
|
}
|
|
|
|
#undef Word_1
|
|
|
|
#undef Word_2
|
|
|
|
#undef Word_3
|
|
|
|
#undef Word_4
|
|
|
|
#undef Word_HiMask
|
|
|
|
#undef Word_LoMask
|
|
|
|
#undef Word_FullMask
|
2019-07-12 16:30:17 +08:00
|
|
|
#endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
|
2014-03-28 18:29:31 +08:00
|
|
|
#else
|
|
|
|
#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \
|
|
|
|
defined(CRT_LDBL_128BIT)
|
|
|
|
#define typeWidth (sizeof(rep_t) * CHAR_BIT)
|
|
|
|
#define exponentBits (typeWidth - significandBits - 1)
|
|
|
|
#define maxExponent ((1 << exponentBits) - 1)
|
|
|
|
#define exponentBias (maxExponent >> 1)
|
2010-07-01 23:52:42 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
#define implicitBit (REP_C(1) << significandBits)
|
2010-07-01 23:52:42 +08:00
|
|
|
#define significandMask (implicitBit - 1U)
|
2019-04-29 05:53:32 +08:00
|
|
|
#define signBit (REP_C(1) << (significandBits + exponentBits))
|
|
|
|
#define absMask (signBit - 1U)
|
|
|
|
#define exponentMask (absMask ^ significandMask)
|
|
|
|
#define oneRep ((rep_t)exponentBias << significandBits)
|
|
|
|
#define infRep exponentMask
|
|
|
|
#define quietBit (implicitBit >> 1)
|
|
|
|
#define qnanRep (exponentMask | quietBit)
|
2010-07-01 23:52:42 +08:00
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline rep_t toRep(fp_t x) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const union {
|
|
|
|
fp_t f;
|
|
|
|
rep_t i;
|
|
|
|
} rep = {.f = x};
|
|
|
|
return rep.i;
|
2010-07-01 23:52:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline fp_t fromRep(rep_t x) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const union {
|
|
|
|
fp_t f;
|
|
|
|
rep_t i;
|
|
|
|
} rep = {.i = x};
|
|
|
|
return rep.f;
|
2010-07-01 23:52:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline int normalize(rep_t *significand) {
|
2019-04-29 05:53:32 +08:00
|
|
|
const int shift = rep_clz(*significand) - rep_clz(implicitBit);
|
|
|
|
*significand <<= shift;
|
|
|
|
return 1 - shift;
|
2010-07-01 23:52:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-11 05:21:28 +08:00
|
|
|
static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
|
2019-04-29 05:53:32 +08:00
|
|
|
*hi = *hi << count | *lo >> (typeWidth - count);
|
|
|
|
*lo = *lo << count;
|
2010-07-01 23:52:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
|
|
|
|
unsigned int count) {
|
|
|
|
if (count < typeWidth) {
|
2019-09-24 10:59:02 +08:00
|
|
|
const bool sticky = (*lo << (typeWidth - count)) != 0;
|
2019-04-29 05:53:32 +08:00
|
|
|
*lo = *hi << (typeWidth - count) | *lo >> count | sticky;
|
|
|
|
*hi = *hi >> count;
|
|
|
|
} else if (count < 2 * typeWidth) {
|
|
|
|
const bool sticky = *hi << (2 * typeWidth - count) | *lo;
|
|
|
|
*lo = *hi >> (count - typeWidth) | sticky;
|
|
|
|
*hi = 0;
|
|
|
|
} else {
|
|
|
|
const bool sticky = *hi | *lo;
|
|
|
|
*lo = sticky;
|
|
|
|
*hi = 0;
|
|
|
|
}
|
2010-07-01 23:52:42 +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
|
|
|
|
|
|
|
// Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
|
|
|
|
// pulling in a libm dependency from compiler-rt, but is not meant to replace
|
|
|
|
// it (i.e. code calling logb() should get the one from libm, not this), hence
|
|
|
|
// the __compiler_rt prefix.
|
|
|
|
static __inline fp_t __compiler_rt_logbX(fp_t x) {
|
|
|
|
rep_t rep = toRep(x);
|
|
|
|
int exp = (rep & exponentMask) >> significandBits;
|
|
|
|
|
|
|
|
// Abnormal cases:
|
|
|
|
// 1) +/- inf returns +inf; NaN returns NaN
|
|
|
|
// 2) 0.0 returns -inf
|
|
|
|
if (exp == maxExponent) {
|
|
|
|
if (((rep & signBit) == 0) || (x != x)) {
|
2019-04-29 05:53:32 +08:00
|
|
|
return x; // NaN or +inf: return x
|
[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
|
|
|
} else {
|
2019-04-29 05:53:32 +08:00
|
|
|
return -x; // -inf: return -x
|
[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
|
|
|
}
|
|
|
|
} else if (x == 0.0) {
|
|
|
|
// 0.0: return -inf
|
|
|
|
return fromRep(infRep | signBit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exp != 0) {
|
|
|
|
// Normal number
|
2019-04-29 05:53:32 +08:00
|
|
|
return exp - exponentBias; // Unbias exponent
|
[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
|
|
|
} else {
|
|
|
|
// Subnormal number; normalize and repeat
|
|
|
|
rep &= absMask;
|
|
|
|
const int shift = 1 - normalize(&rep);
|
|
|
|
exp = (rep & exponentMask) >> significandBits;
|
2019-04-29 05:53:32 +08:00
|
|
|
return exp - exponentBias - shift; // Unbias exponent
|
[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
|
|
|
}
|
|
|
|
}
|
2021-02-26 07:43:12 +08:00
|
|
|
|
|
|
|
// Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never
|
|
|
|
// sets errno on underflow/overflow.
|
|
|
|
static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
|
|
|
|
const rep_t rep = toRep(x);
|
|
|
|
int exp = (rep & exponentMask) >> significandBits;
|
|
|
|
|
|
|
|
if (x == 0.0 || exp == maxExponent)
|
|
|
|
return x; // +/- 0.0, NaN, or inf: return x
|
|
|
|
|
|
|
|
// Normalize subnormal input.
|
|
|
|
rep_t sig = rep & significandMask;
|
|
|
|
if (exp == 0) {
|
|
|
|
exp += normalize(&sig);
|
|
|
|
sig &= ~implicitBit; // clear the implicit bit again
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__builtin_sadd_overflow(exp, y, &exp)) {
|
|
|
|
// Saturate the exponent, which will guarantee an underflow/overflow below.
|
|
|
|
exp = (y >= 0) ? INT_MAX : INT_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias).
|
|
|
|
const rep_t sign = rep & signBit;
|
|
|
|
if (exp >= maxExponent) {
|
|
|
|
// Overflow, which could produce infinity or the largest-magnitude value,
|
|
|
|
// depending on the rounding mode.
|
|
|
|
return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f;
|
|
|
|
} else if (exp <= 0) {
|
|
|
|
// Subnormal or underflow. Use floating-point multiply to handle truncation
|
|
|
|
// correctly.
|
|
|
|
fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig);
|
|
|
|
exp += exponentBias - 1;
|
|
|
|
if (exp < 1)
|
|
|
|
exp = 1;
|
|
|
|
tmp *= fromRep((rep_t)exp << significandBits);
|
|
|
|
return tmp;
|
|
|
|
} else
|
|
|
|
return fromRep(sign | ((rep_t)exp << significandBits) | sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid using fmax from libm.
|
|
|
|
static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
|
|
|
|
// If either argument is NaN, return the other argument. If both are NaN,
|
|
|
|
// arbitrarily return the second one. Otherwise, if both arguments are +/-0,
|
|
|
|
// arbitrarily return the first one.
|
|
|
|
return (crt_isnan(x) || x < y) ? y : x;
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SINGLE_PRECISION)
|
2021-02-26 07:43:12 +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
|
|
|
static __inline fp_t __compiler_rt_logbf(fp_t x) {
|
|
|
|
return __compiler_rt_logbX(x);
|
|
|
|
}
|
2021-02-26 07:43:12 +08:00
|
|
|
static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) {
|
|
|
|
return __compiler_rt_scalbnX(x, y);
|
|
|
|
}
|
|
|
|
static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) {
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
// Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64.
|
|
|
|
return __builtin_fmaxf(x, y);
|
|
|
|
#else
|
|
|
|
// __builtin_fmaxf frequently turns into a libm call, so inline the function.
|
|
|
|
return __compiler_rt_fmaxX(x, y);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
#elif defined(DOUBLE_PRECISION)
|
2021-02-26 07:43:12 +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
|
|
|
static __inline fp_t __compiler_rt_logb(fp_t x) {
|
|
|
|
return __compiler_rt_logbX(x);
|
|
|
|
}
|
2021-02-26 07:43:12 +08:00
|
|
|
static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
|
|
|
|
return __compiler_rt_scalbnX(x, y);
|
|
|
|
}
|
|
|
|
static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
// Use __builtin_fmax which turns into an fmaxnm instruction on AArch64.
|
|
|
|
return __builtin_fmax(x, y);
|
|
|
|
#else
|
|
|
|
// __builtin_fmax frequently turns into a libm call, so inline the function.
|
|
|
|
return __compiler_rt_fmaxX(x, y);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
#elif defined(QUAD_PRECISION)
|
2021-02-26 07:43:12 +08:00
|
|
|
|
2019-04-29 05:53:32 +08:00
|
|
|
#if defined(CRT_LDBL_128BIT)
|
[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
|
|
|
static __inline fp_t __compiler_rt_logbl(fp_t x) {
|
|
|
|
return __compiler_rt_logbX(x);
|
|
|
|
}
|
2021-02-26 07:43:12 +08:00
|
|
|
static __inline fp_t __compiler_rt_scalbnl(fp_t x, int y) {
|
|
|
|
return __compiler_rt_scalbnX(x, y);
|
|
|
|
}
|
|
|
|
static __inline fp_t __compiler_rt_fmaxl(fp_t x, fp_t y) {
|
|
|
|
return __compiler_rt_fmaxX(x, y);
|
|
|
|
}
|
2019-04-29 05:53:32 +08:00
|
|
|
#else
|
[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
|
|
|
// The generic implementation only works for ieee754 floating point. For other
|
|
|
|
// floating point types, continue to rely on the libm implementation for now.
|
|
|
|
static __inline long double __compiler_rt_logbl(long double x) {
|
|
|
|
return crt_logbl(x);
|
|
|
|
}
|
2021-02-26 07:43:12 +08:00
|
|
|
static __inline long double __compiler_rt_scalbnl(long double x, int y) {
|
|
|
|
return crt_scalbnl(x, y);
|
|
|
|
}
|
|
|
|
static __inline long double __compiler_rt_fmaxl(long double x, long double y) {
|
|
|
|
return crt_fmaxl(x, y);
|
|
|
|
}
|
|
|
|
#endif // CRT_LDBL_128BIT
|
|
|
|
|
|
|
|
#endif // *_PRECISION
|
2014-03-01 23:30:50 +08:00
|
|
|
|
2010-07-01 23:52:42 +08:00
|
|
|
#endif // FP_LIB_HEADER
|