xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fp_lib.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a configuration header for soft-float routines in compiler-rt.
100b57cec5SDimitry Andric // This file does not provide any part of the compiler-rt interface, but defines
110b57cec5SDimitry Andric // many useful constants and utility routines that are used in the
120b57cec5SDimitry Andric // implementation of the soft-float routines in compiler-rt.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric // Assumes that float, double and long double correspond to the IEEE-754
150b57cec5SDimitry Andric // binary32, binary64 and binary 128 types, respectively, and that integer
160b57cec5SDimitry Andric // endianness matches floating point endianness on the target platform.
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #ifndef FP_LIB_HEADER
210b57cec5SDimitry Andric #define FP_LIB_HEADER
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "int_lib.h"
240b57cec5SDimitry Andric #include "int_math.h"
250b57cec5SDimitry Andric #include <limits.h>
260b57cec5SDimitry Andric #include <stdbool.h>
270b57cec5SDimitry Andric #include <stdint.h>
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
300b57cec5SDimitry Andric // 32-bit mode.
310b57cec5SDimitry Andric #if defined(__FreeBSD__) && defined(__i386__)
320b57cec5SDimitry Andric #include <sys/param.h>
330b57cec5SDimitry Andric #if __FreeBSD_version < 903000 // v9.3
340b57cec5SDimitry Andric #define uint64_t unsigned long long
350b57cec5SDimitry Andric #define int64_t long long
360b57cec5SDimitry Andric #undef UINT64_C
370b57cec5SDimitry Andric #define UINT64_C(c) (c##ULL)
380b57cec5SDimitry Andric #endif
390b57cec5SDimitry Andric #endif
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #if defined SINGLE_PRECISION
420b57cec5SDimitry Andric 
43e8d8bef9SDimitry Andric typedef uint16_t half_rep_t;
440b57cec5SDimitry Andric typedef uint32_t rep_t;
45e8d8bef9SDimitry Andric typedef uint64_t twice_rep_t;
460b57cec5SDimitry Andric typedef int32_t srep_t;
470b57cec5SDimitry Andric typedef float fp_t;
48e8d8bef9SDimitry Andric #define HALF_REP_C UINT16_C
490b57cec5SDimitry Andric #define REP_C UINT32_C
500b57cec5SDimitry Andric #define significandBits 23
510b57cec5SDimitry Andric 
525ffd83dbSDimitry Andric static __inline int rep_clz(rep_t a) { return clzsi(a); }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // 32x32 --> 64 bit multiply
550b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
560b57cec5SDimitry Andric   const uint64_t product = (uint64_t)a * b;
570b57cec5SDimitry Andric   *hi = product >> 32;
580b57cec5SDimitry Andric   *lo = product;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric #elif defined DOUBLE_PRECISION
630b57cec5SDimitry Andric 
64e8d8bef9SDimitry Andric typedef uint32_t half_rep_t;
650b57cec5SDimitry Andric typedef uint64_t rep_t;
660b57cec5SDimitry Andric typedef int64_t srep_t;
670b57cec5SDimitry Andric typedef double fp_t;
68e8d8bef9SDimitry Andric #define HALF_REP_C UINT32_C
690b57cec5SDimitry Andric #define REP_C UINT64_C
700b57cec5SDimitry Andric #define significandBits 52
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) {
730b57cec5SDimitry Andric #if defined __LP64__
740b57cec5SDimitry Andric   return __builtin_clzl(a);
750b57cec5SDimitry Andric #else
760b57cec5SDimitry Andric   if (a & REP_C(0xffffffff00000000))
775ffd83dbSDimitry Andric     return clzsi(a >> 32);
780b57cec5SDimitry Andric   else
795ffd83dbSDimitry Andric     return 32 + clzsi(a & REP_C(0xffffffff));
800b57cec5SDimitry Andric #endif
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric #define loWord(a) (a & 0xffffffffU)
840b57cec5SDimitry Andric #define hiWord(a) (a >> 32)
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
870b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware
880b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here.
890b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
900b57cec5SDimitry Andric   // Each of the component 32x32 -> 64 products
910b57cec5SDimitry Andric   const uint64_t plolo = loWord(a) * loWord(b);
920b57cec5SDimitry Andric   const uint64_t plohi = loWord(a) * hiWord(b);
930b57cec5SDimitry Andric   const uint64_t philo = hiWord(a) * loWord(b);
940b57cec5SDimitry Andric   const uint64_t phihi = hiWord(a) * hiWord(b);
950b57cec5SDimitry Andric   // Sum terms that contribute to lo in a way that allows us to get the carry
960b57cec5SDimitry Andric   const uint64_t r0 = loWord(plolo);
970b57cec5SDimitry Andric   const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
980b57cec5SDimitry Andric   *lo = r0 + (r1 << 32);
990b57cec5SDimitry Andric   // Sum terms contributing to hi with the carry from lo
1000b57cec5SDimitry Andric   *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric #undef loWord
1030b57cec5SDimitry Andric #undef hiWord
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric #elif defined QUAD_PRECISION
1080b57cec5SDimitry Andric #if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__)
109*06c3fb27SDimitry Andric // TODO: Availability of the *tf functions should not depend on long double
110*06c3fb27SDimitry Andric // being IEEE 128, but instead on being able to use a 128-bit floating-point
111*06c3fb27SDimitry Andric // type, which includes __float128.
112*06c3fb27SDimitry Andric // Right now this (incorrectly) stops the builtins from being used for x86.
1130b57cec5SDimitry Andric #define CRT_LDBL_128BIT
114*06c3fb27SDimitry Andric #define CRT_HAS_TF_MODE
115*06c3fb27SDimitry Andric #define TF_C(c) c##L
116e8d8bef9SDimitry Andric typedef uint64_t half_rep_t;
1170b57cec5SDimitry Andric typedef __uint128_t rep_t;
1180b57cec5SDimitry Andric typedef __int128_t srep_t;
1190b57cec5SDimitry Andric typedef long double fp_t;
120e8d8bef9SDimitry Andric #define HALF_REP_C UINT64_C
1210b57cec5SDimitry Andric #define REP_C (__uint128_t)
1220b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a
1230b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer
1240b57cec5SDimitry Andric #define significandBits 112
125*06c3fb27SDimitry Andric #define TF_MANT_DIG (significandBits + 1)
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) {
1280b57cec5SDimitry Andric   const union {
1290b57cec5SDimitry Andric     __uint128_t ll;
1300b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN
1310b57cec5SDimitry Andric     struct {
1320b57cec5SDimitry Andric       uint64_t high, low;
1330b57cec5SDimitry Andric     } s;
1340b57cec5SDimitry Andric #else
1350b57cec5SDimitry Andric     struct {
1360b57cec5SDimitry Andric       uint64_t low, high;
1370b57cec5SDimitry Andric     } s;
1380b57cec5SDimitry Andric #endif
1390b57cec5SDimitry Andric   } uu = {.ll = a};
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   uint64_t word;
1420b57cec5SDimitry Andric   uint64_t add;
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   if (uu.s.high) {
1450b57cec5SDimitry Andric     word = uu.s.high;
1460b57cec5SDimitry Andric     add = 0;
1470b57cec5SDimitry Andric   } else {
1480b57cec5SDimitry Andric     word = uu.s.low;
1490b57cec5SDimitry Andric     add = 64;
1500b57cec5SDimitry Andric   }
1510b57cec5SDimitry Andric   return __builtin_clzll(word) + add;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff)
1550b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000)
1560b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff)
1570b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
1580b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
1590b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
1600b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask)
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
1630b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware
1640b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here.
1650b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   const uint64_t product11 = Word_1(a) * Word_1(b);
1680b57cec5SDimitry Andric   const uint64_t product12 = Word_1(a) * Word_2(b);
1690b57cec5SDimitry Andric   const uint64_t product13 = Word_1(a) * Word_3(b);
1700b57cec5SDimitry Andric   const uint64_t product14 = Word_1(a) * Word_4(b);
1710b57cec5SDimitry Andric   const uint64_t product21 = Word_2(a) * Word_1(b);
1720b57cec5SDimitry Andric   const uint64_t product22 = Word_2(a) * Word_2(b);
1730b57cec5SDimitry Andric   const uint64_t product23 = Word_2(a) * Word_3(b);
1740b57cec5SDimitry Andric   const uint64_t product24 = Word_2(a) * Word_4(b);
1750b57cec5SDimitry Andric   const uint64_t product31 = Word_3(a) * Word_1(b);
1760b57cec5SDimitry Andric   const uint64_t product32 = Word_3(a) * Word_2(b);
1770b57cec5SDimitry Andric   const uint64_t product33 = Word_3(a) * Word_3(b);
1780b57cec5SDimitry Andric   const uint64_t product34 = Word_3(a) * Word_4(b);
1790b57cec5SDimitry Andric   const uint64_t product41 = Word_4(a) * Word_1(b);
1800b57cec5SDimitry Andric   const uint64_t product42 = Word_4(a) * Word_2(b);
1810b57cec5SDimitry Andric   const uint64_t product43 = Word_4(a) * Word_3(b);
1820b57cec5SDimitry Andric   const uint64_t product44 = Word_4(a) * Word_4(b);
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   const __uint128_t sum0 = (__uint128_t)product44;
1850b57cec5SDimitry Andric   const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
1860b57cec5SDimitry Andric   const __uint128_t sum2 =
1870b57cec5SDimitry Andric       (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
1880b57cec5SDimitry Andric   const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
1890b57cec5SDimitry Andric                            (__uint128_t)product32 + (__uint128_t)product41;
1900b57cec5SDimitry Andric   const __uint128_t sum4 =
1910b57cec5SDimitry Andric       (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
1920b57cec5SDimitry Andric   const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
1930b57cec5SDimitry Andric   const __uint128_t sum6 = (__uint128_t)product11;
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
1960b57cec5SDimitry Andric   const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
1970b57cec5SDimitry Andric                          (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   *lo = r0 + (r1 << 64);
2000b57cec5SDimitry Andric   *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
2010b57cec5SDimitry Andric         (sum5 << 32) + (sum6 << 64);
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric #undef Word_1
2040b57cec5SDimitry Andric #undef Word_2
2050b57cec5SDimitry Andric #undef Word_3
2060b57cec5SDimitry Andric #undef Word_4
2070b57cec5SDimitry Andric #undef Word_HiMask
2080b57cec5SDimitry Andric #undef Word_LoMask
2090b57cec5SDimitry Andric #undef Word_FullMask
2100b57cec5SDimitry Andric #endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
2110b57cec5SDimitry Andric #else
2120b57cec5SDimitry Andric #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
2130b57cec5SDimitry Andric #endif
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) ||                  \
2160b57cec5SDimitry Andric     defined(CRT_LDBL_128BIT)
2170b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT)
2180b57cec5SDimitry Andric #define exponentBits (typeWidth - significandBits - 1)
2190b57cec5SDimitry Andric #define maxExponent ((1 << exponentBits) - 1)
2200b57cec5SDimitry Andric #define exponentBias (maxExponent >> 1)
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric #define implicitBit (REP_C(1) << significandBits)
2230b57cec5SDimitry Andric #define significandMask (implicitBit - 1U)
2240b57cec5SDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits))
2250b57cec5SDimitry Andric #define absMask (signBit - 1U)
2260b57cec5SDimitry Andric #define exponentMask (absMask ^ significandMask)
2270b57cec5SDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits)
2280b57cec5SDimitry Andric #define infRep exponentMask
2290b57cec5SDimitry Andric #define quietBit (implicitBit >> 1)
2300b57cec5SDimitry Andric #define qnanRep (exponentMask | quietBit)
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) {
2330b57cec5SDimitry Andric   const union {
2340b57cec5SDimitry Andric     fp_t f;
2350b57cec5SDimitry Andric     rep_t i;
2360b57cec5SDimitry Andric   } rep = {.f = x};
2370b57cec5SDimitry Andric   return rep.i;
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) {
2410b57cec5SDimitry Andric   const union {
2420b57cec5SDimitry Andric     fp_t f;
2430b57cec5SDimitry Andric     rep_t i;
2440b57cec5SDimitry Andric   } rep = {.i = x};
2450b57cec5SDimitry Andric   return rep.f;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) {
2490b57cec5SDimitry Andric   const int shift = rep_clz(*significand) - rep_clz(implicitBit);
2500b57cec5SDimitry Andric   *significand <<= shift;
2510b57cec5SDimitry Andric   return 1 - shift;
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
2550b57cec5SDimitry Andric   *hi = *hi << count | *lo >> (typeWidth - count);
2560b57cec5SDimitry Andric   *lo = *lo << count;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
2600b57cec5SDimitry Andric                                               unsigned int count) {
2610b57cec5SDimitry Andric   if (count < typeWidth) {
26268d75effSDimitry Andric     const bool sticky = (*lo << (typeWidth - count)) != 0;
2630b57cec5SDimitry Andric     *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
2640b57cec5SDimitry Andric     *hi = *hi >> count;
2650b57cec5SDimitry Andric   } else if (count < 2 * typeWidth) {
2660b57cec5SDimitry Andric     const bool sticky = *hi << (2 * typeWidth - count) | *lo;
2670b57cec5SDimitry Andric     *lo = *hi >> (count - typeWidth) | sticky;
2680b57cec5SDimitry Andric     *hi = 0;
2690b57cec5SDimitry Andric   } else {
2700b57cec5SDimitry Andric     const bool sticky = *hi | *lo;
2710b57cec5SDimitry Andric     *lo = sticky;
2720b57cec5SDimitry Andric     *hi = 0;
2730b57cec5SDimitry Andric   }
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
2770b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace
2780b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence
2790b57cec5SDimitry Andric // the __compiler_rt prefix.
2800b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) {
2810b57cec5SDimitry Andric   rep_t rep = toRep(x);
2820b57cec5SDimitry Andric   int exp = (rep & exponentMask) >> significandBits;
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   // Abnormal cases:
2850b57cec5SDimitry Andric   // 1) +/- inf returns +inf; NaN returns NaN
2860b57cec5SDimitry Andric   // 2) 0.0 returns -inf
2870b57cec5SDimitry Andric   if (exp == maxExponent) {
2880b57cec5SDimitry Andric     if (((rep & signBit) == 0) || (x != x)) {
2890b57cec5SDimitry Andric       return x; // NaN or +inf: return x
2900b57cec5SDimitry Andric     } else {
2910b57cec5SDimitry Andric       return -x; // -inf: return -x
2920b57cec5SDimitry Andric     }
2930b57cec5SDimitry Andric   } else if (x == 0.0) {
2940b57cec5SDimitry Andric     // 0.0: return -inf
2950b57cec5SDimitry Andric     return fromRep(infRep | signBit);
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   if (exp != 0) {
2990b57cec5SDimitry Andric     // Normal number
3000b57cec5SDimitry Andric     return exp - exponentBias; // Unbias exponent
3010b57cec5SDimitry Andric   } else {
3020b57cec5SDimitry Andric     // Subnormal number; normalize and repeat
3030b57cec5SDimitry Andric     rep &= absMask;
3040b57cec5SDimitry Andric     const int shift = 1 - normalize(&rep);
3050b57cec5SDimitry Andric     exp = (rep & exponentMask) >> significandBits;
3060b57cec5SDimitry Andric     return exp - exponentBias - shift; // Unbias exponent
3070b57cec5SDimitry Andric   }
3080b57cec5SDimitry Andric }
309fe6060f1SDimitry Andric 
310fe6060f1SDimitry Andric // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never
311fe6060f1SDimitry Andric // sets errno on underflow/overflow.
312fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
313fe6060f1SDimitry Andric   const rep_t rep = toRep(x);
314fe6060f1SDimitry Andric   int exp = (rep & exponentMask) >> significandBits;
315fe6060f1SDimitry Andric 
316fe6060f1SDimitry Andric   if (x == 0.0 || exp == maxExponent)
317fe6060f1SDimitry Andric     return x; // +/- 0.0, NaN, or inf: return x
318fe6060f1SDimitry Andric 
319fe6060f1SDimitry Andric   // Normalize subnormal input.
320fe6060f1SDimitry Andric   rep_t sig = rep & significandMask;
321fe6060f1SDimitry Andric   if (exp == 0) {
322fe6060f1SDimitry Andric     exp += normalize(&sig);
323fe6060f1SDimitry Andric     sig &= ~implicitBit; // clear the implicit bit again
324fe6060f1SDimitry Andric   }
325fe6060f1SDimitry Andric 
326fe6060f1SDimitry Andric   if (__builtin_sadd_overflow(exp, y, &exp)) {
327fe6060f1SDimitry Andric     // Saturate the exponent, which will guarantee an underflow/overflow below.
328fe6060f1SDimitry Andric     exp = (y >= 0) ? INT_MAX : INT_MIN;
329fe6060f1SDimitry Andric   }
330fe6060f1SDimitry Andric 
331fe6060f1SDimitry Andric   // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias).
332fe6060f1SDimitry Andric   const rep_t sign = rep & signBit;
333fe6060f1SDimitry Andric   if (exp >= maxExponent) {
334fe6060f1SDimitry Andric     // Overflow, which could produce infinity or the largest-magnitude value,
335fe6060f1SDimitry Andric     // depending on the rounding mode.
336fe6060f1SDimitry Andric     return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f;
337fe6060f1SDimitry Andric   } else if (exp <= 0) {
338fe6060f1SDimitry Andric     // Subnormal or underflow. Use floating-point multiply to handle truncation
339fe6060f1SDimitry Andric     // correctly.
340fe6060f1SDimitry Andric     fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig);
341fe6060f1SDimitry Andric     exp += exponentBias - 1;
342fe6060f1SDimitry Andric     if (exp < 1)
343fe6060f1SDimitry Andric       exp = 1;
344fe6060f1SDimitry Andric     tmp *= fromRep((rep_t)exp << significandBits);
345fe6060f1SDimitry Andric     return tmp;
346fe6060f1SDimitry Andric   } else
347fe6060f1SDimitry Andric     return fromRep(sign | ((rep_t)exp << significandBits) | sig);
348fe6060f1SDimitry Andric }
349fe6060f1SDimitry Andric 
350fe6060f1SDimitry Andric // Avoid using fmax from libm.
351fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
352fe6060f1SDimitry Andric   // If either argument is NaN, return the other argument. If both are NaN,
353fe6060f1SDimitry Andric   // arbitrarily return the second one. Otherwise, if both arguments are +/-0,
354fe6060f1SDimitry Andric   // arbitrarily return the first one.
355fe6060f1SDimitry Andric   return (crt_isnan(x) || x < y) ? y : x;
356fe6060f1SDimitry Andric }
357fe6060f1SDimitry Andric 
3580b57cec5SDimitry Andric #endif
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric #if defined(SINGLE_PRECISION)
361fe6060f1SDimitry Andric 
3620b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) {
3630b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3640b57cec5SDimitry Andric }
365fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) {
366fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
367fe6060f1SDimitry Andric }
368fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) {
369fe6060f1SDimitry Andric #if defined(__aarch64__)
370fe6060f1SDimitry Andric   // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64.
371fe6060f1SDimitry Andric   return __builtin_fmaxf(x, y);
372fe6060f1SDimitry Andric #else
373fe6060f1SDimitry Andric   // __builtin_fmaxf frequently turns into a libm call, so inline the function.
374fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
375fe6060f1SDimitry Andric #endif
376fe6060f1SDimitry Andric }
377fe6060f1SDimitry Andric 
3780b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION)
379fe6060f1SDimitry Andric 
3800b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) {
3810b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3820b57cec5SDimitry Andric }
383fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
384fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
385fe6060f1SDimitry Andric }
386fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
387fe6060f1SDimitry Andric #if defined(__aarch64__)
388fe6060f1SDimitry Andric   // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64.
389fe6060f1SDimitry Andric   return __builtin_fmax(x, y);
390fe6060f1SDimitry Andric #else
391fe6060f1SDimitry Andric   // __builtin_fmax frequently turns into a libm call, so inline the function.
392fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
393fe6060f1SDimitry Andric #endif
394fe6060f1SDimitry Andric }
395fe6060f1SDimitry Andric 
3960b57cec5SDimitry Andric #elif defined(QUAD_PRECISION)
397fe6060f1SDimitry Andric 
3980b57cec5SDimitry Andric #if defined(CRT_LDBL_128BIT)
3990b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbl(fp_t x) {
4000b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
4010b57cec5SDimitry Andric }
402fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnl(fp_t x, int y) {
403fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
404fe6060f1SDimitry Andric }
405fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxl(fp_t x, fp_t y) {
406fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
407fe6060f1SDimitry Andric }
4080b57cec5SDimitry Andric #else
4090b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other
4100b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now.
4110b57cec5SDimitry Andric static __inline long double __compiler_rt_logbl(long double x) {
4120b57cec5SDimitry Andric   return crt_logbl(x);
4130b57cec5SDimitry Andric }
414fe6060f1SDimitry Andric static __inline long double __compiler_rt_scalbnl(long double x, int y) {
415fe6060f1SDimitry Andric   return crt_scalbnl(x, y);
416fe6060f1SDimitry Andric }
417fe6060f1SDimitry Andric static __inline long double __compiler_rt_fmaxl(long double x, long double y) {
418fe6060f1SDimitry Andric   return crt_fmaxl(x, y);
419fe6060f1SDimitry Andric }
420fe6060f1SDimitry Andric #endif // CRT_LDBL_128BIT
421fe6060f1SDimitry Andric 
422fe6060f1SDimitry Andric #endif // *_PRECISION
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric #endif // FP_LIB_HEADER
425