xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fp_lib.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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__)
1090b57cec5SDimitry Andric #define CRT_LDBL_128BIT
110e8d8bef9SDimitry Andric typedef uint64_t half_rep_t;
1110b57cec5SDimitry Andric typedef __uint128_t rep_t;
1120b57cec5SDimitry Andric typedef __int128_t srep_t;
1130b57cec5SDimitry Andric typedef long double fp_t;
114e8d8bef9SDimitry Andric #define HALF_REP_C UINT64_C
1150b57cec5SDimitry Andric #define REP_C (__uint128_t)
1160b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a
1170b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer
1180b57cec5SDimitry Andric #define significandBits 112
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) {
1210b57cec5SDimitry Andric   const union {
1220b57cec5SDimitry Andric     __uint128_t ll;
1230b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN
1240b57cec5SDimitry Andric     struct {
1250b57cec5SDimitry Andric       uint64_t high, low;
1260b57cec5SDimitry Andric     } s;
1270b57cec5SDimitry Andric #else
1280b57cec5SDimitry Andric     struct {
1290b57cec5SDimitry Andric       uint64_t low, high;
1300b57cec5SDimitry Andric     } s;
1310b57cec5SDimitry Andric #endif
1320b57cec5SDimitry Andric   } uu = {.ll = a};
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   uint64_t word;
1350b57cec5SDimitry Andric   uint64_t add;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   if (uu.s.high) {
1380b57cec5SDimitry Andric     word = uu.s.high;
1390b57cec5SDimitry Andric     add = 0;
1400b57cec5SDimitry Andric   } else {
1410b57cec5SDimitry Andric     word = uu.s.low;
1420b57cec5SDimitry Andric     add = 64;
1430b57cec5SDimitry Andric   }
1440b57cec5SDimitry Andric   return __builtin_clzll(word) + add;
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff)
1480b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000)
1490b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff)
1500b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
1510b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
1520b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
1530b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask)
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
1560b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware
1570b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here.
1580b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   const uint64_t product11 = Word_1(a) * Word_1(b);
1610b57cec5SDimitry Andric   const uint64_t product12 = Word_1(a) * Word_2(b);
1620b57cec5SDimitry Andric   const uint64_t product13 = Word_1(a) * Word_3(b);
1630b57cec5SDimitry Andric   const uint64_t product14 = Word_1(a) * Word_4(b);
1640b57cec5SDimitry Andric   const uint64_t product21 = Word_2(a) * Word_1(b);
1650b57cec5SDimitry Andric   const uint64_t product22 = Word_2(a) * Word_2(b);
1660b57cec5SDimitry Andric   const uint64_t product23 = Word_2(a) * Word_3(b);
1670b57cec5SDimitry Andric   const uint64_t product24 = Word_2(a) * Word_4(b);
1680b57cec5SDimitry Andric   const uint64_t product31 = Word_3(a) * Word_1(b);
1690b57cec5SDimitry Andric   const uint64_t product32 = Word_3(a) * Word_2(b);
1700b57cec5SDimitry Andric   const uint64_t product33 = Word_3(a) * Word_3(b);
1710b57cec5SDimitry Andric   const uint64_t product34 = Word_3(a) * Word_4(b);
1720b57cec5SDimitry Andric   const uint64_t product41 = Word_4(a) * Word_1(b);
1730b57cec5SDimitry Andric   const uint64_t product42 = Word_4(a) * Word_2(b);
1740b57cec5SDimitry Andric   const uint64_t product43 = Word_4(a) * Word_3(b);
1750b57cec5SDimitry Andric   const uint64_t product44 = Word_4(a) * Word_4(b);
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   const __uint128_t sum0 = (__uint128_t)product44;
1780b57cec5SDimitry Andric   const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
1790b57cec5SDimitry Andric   const __uint128_t sum2 =
1800b57cec5SDimitry Andric       (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
1810b57cec5SDimitry Andric   const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
1820b57cec5SDimitry Andric                            (__uint128_t)product32 + (__uint128_t)product41;
1830b57cec5SDimitry Andric   const __uint128_t sum4 =
1840b57cec5SDimitry Andric       (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
1850b57cec5SDimitry Andric   const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
1860b57cec5SDimitry Andric   const __uint128_t sum6 = (__uint128_t)product11;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
1890b57cec5SDimitry Andric   const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
1900b57cec5SDimitry Andric                          (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   *lo = r0 + (r1 << 64);
1930b57cec5SDimitry Andric   *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
1940b57cec5SDimitry Andric         (sum5 << 32) + (sum6 << 64);
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric #undef Word_1
1970b57cec5SDimitry Andric #undef Word_2
1980b57cec5SDimitry Andric #undef Word_3
1990b57cec5SDimitry Andric #undef Word_4
2000b57cec5SDimitry Andric #undef Word_HiMask
2010b57cec5SDimitry Andric #undef Word_LoMask
2020b57cec5SDimitry Andric #undef Word_FullMask
2030b57cec5SDimitry Andric #endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
2040b57cec5SDimitry Andric #else
2050b57cec5SDimitry Andric #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
2060b57cec5SDimitry Andric #endif
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) ||                  \
2090b57cec5SDimitry Andric     defined(CRT_LDBL_128BIT)
2100b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT)
2110b57cec5SDimitry Andric #define exponentBits (typeWidth - significandBits - 1)
2120b57cec5SDimitry Andric #define maxExponent ((1 << exponentBits) - 1)
2130b57cec5SDimitry Andric #define exponentBias (maxExponent >> 1)
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric #define implicitBit (REP_C(1) << significandBits)
2160b57cec5SDimitry Andric #define significandMask (implicitBit - 1U)
2170b57cec5SDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits))
2180b57cec5SDimitry Andric #define absMask (signBit - 1U)
2190b57cec5SDimitry Andric #define exponentMask (absMask ^ significandMask)
2200b57cec5SDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits)
2210b57cec5SDimitry Andric #define infRep exponentMask
2220b57cec5SDimitry Andric #define quietBit (implicitBit >> 1)
2230b57cec5SDimitry Andric #define qnanRep (exponentMask | quietBit)
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) {
2260b57cec5SDimitry Andric   const union {
2270b57cec5SDimitry Andric     fp_t f;
2280b57cec5SDimitry Andric     rep_t i;
2290b57cec5SDimitry Andric   } rep = {.f = x};
2300b57cec5SDimitry Andric   return rep.i;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) {
2340b57cec5SDimitry Andric   const union {
2350b57cec5SDimitry Andric     fp_t f;
2360b57cec5SDimitry Andric     rep_t i;
2370b57cec5SDimitry Andric   } rep = {.i = x};
2380b57cec5SDimitry Andric   return rep.f;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) {
2420b57cec5SDimitry Andric   const int shift = rep_clz(*significand) - rep_clz(implicitBit);
2430b57cec5SDimitry Andric   *significand <<= shift;
2440b57cec5SDimitry Andric   return 1 - shift;
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
2480b57cec5SDimitry Andric   *hi = *hi << count | *lo >> (typeWidth - count);
2490b57cec5SDimitry Andric   *lo = *lo << count;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
2530b57cec5SDimitry Andric                                               unsigned int count) {
2540b57cec5SDimitry Andric   if (count < typeWidth) {
25568d75effSDimitry Andric     const bool sticky = (*lo << (typeWidth - count)) != 0;
2560b57cec5SDimitry Andric     *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
2570b57cec5SDimitry Andric     *hi = *hi >> count;
2580b57cec5SDimitry Andric   } else if (count < 2 * typeWidth) {
2590b57cec5SDimitry Andric     const bool sticky = *hi << (2 * typeWidth - count) | *lo;
2600b57cec5SDimitry Andric     *lo = *hi >> (count - typeWidth) | sticky;
2610b57cec5SDimitry Andric     *hi = 0;
2620b57cec5SDimitry Andric   } else {
2630b57cec5SDimitry Andric     const bool sticky = *hi | *lo;
2640b57cec5SDimitry Andric     *lo = sticky;
2650b57cec5SDimitry Andric     *hi = 0;
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
2700b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace
2710b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence
2720b57cec5SDimitry Andric // the __compiler_rt prefix.
2730b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) {
2740b57cec5SDimitry Andric   rep_t rep = toRep(x);
2750b57cec5SDimitry Andric   int exp = (rep & exponentMask) >> significandBits;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   // Abnormal cases:
2780b57cec5SDimitry Andric   // 1) +/- inf returns +inf; NaN returns NaN
2790b57cec5SDimitry Andric   // 2) 0.0 returns -inf
2800b57cec5SDimitry Andric   if (exp == maxExponent) {
2810b57cec5SDimitry Andric     if (((rep & signBit) == 0) || (x != x)) {
2820b57cec5SDimitry Andric       return x; // NaN or +inf: return x
2830b57cec5SDimitry Andric     } else {
2840b57cec5SDimitry Andric       return -x; // -inf: return -x
2850b57cec5SDimitry Andric     }
2860b57cec5SDimitry Andric   } else if (x == 0.0) {
2870b57cec5SDimitry Andric     // 0.0: return -inf
2880b57cec5SDimitry Andric     return fromRep(infRep | signBit);
2890b57cec5SDimitry Andric   }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   if (exp != 0) {
2920b57cec5SDimitry Andric     // Normal number
2930b57cec5SDimitry Andric     return exp - exponentBias; // Unbias exponent
2940b57cec5SDimitry Andric   } else {
2950b57cec5SDimitry Andric     // Subnormal number; normalize and repeat
2960b57cec5SDimitry Andric     rep &= absMask;
2970b57cec5SDimitry Andric     const int shift = 1 - normalize(&rep);
2980b57cec5SDimitry Andric     exp = (rep & exponentMask) >> significandBits;
2990b57cec5SDimitry Andric     return exp - exponentBias - shift; // Unbias exponent
3000b57cec5SDimitry Andric   }
3010b57cec5SDimitry Andric }
302*fe6060f1SDimitry Andric 
303*fe6060f1SDimitry Andric // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never
304*fe6060f1SDimitry Andric // sets errno on underflow/overflow.
305*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
306*fe6060f1SDimitry Andric   const rep_t rep = toRep(x);
307*fe6060f1SDimitry Andric   int exp = (rep & exponentMask) >> significandBits;
308*fe6060f1SDimitry Andric 
309*fe6060f1SDimitry Andric   if (x == 0.0 || exp == maxExponent)
310*fe6060f1SDimitry Andric     return x; // +/- 0.0, NaN, or inf: return x
311*fe6060f1SDimitry Andric 
312*fe6060f1SDimitry Andric   // Normalize subnormal input.
313*fe6060f1SDimitry Andric   rep_t sig = rep & significandMask;
314*fe6060f1SDimitry Andric   if (exp == 0) {
315*fe6060f1SDimitry Andric     exp += normalize(&sig);
316*fe6060f1SDimitry Andric     sig &= ~implicitBit; // clear the implicit bit again
317*fe6060f1SDimitry Andric   }
318*fe6060f1SDimitry Andric 
319*fe6060f1SDimitry Andric   if (__builtin_sadd_overflow(exp, y, &exp)) {
320*fe6060f1SDimitry Andric     // Saturate the exponent, which will guarantee an underflow/overflow below.
321*fe6060f1SDimitry Andric     exp = (y >= 0) ? INT_MAX : INT_MIN;
322*fe6060f1SDimitry Andric   }
323*fe6060f1SDimitry Andric 
324*fe6060f1SDimitry Andric   // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias).
325*fe6060f1SDimitry Andric   const rep_t sign = rep & signBit;
326*fe6060f1SDimitry Andric   if (exp >= maxExponent) {
327*fe6060f1SDimitry Andric     // Overflow, which could produce infinity or the largest-magnitude value,
328*fe6060f1SDimitry Andric     // depending on the rounding mode.
329*fe6060f1SDimitry Andric     return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f;
330*fe6060f1SDimitry Andric   } else if (exp <= 0) {
331*fe6060f1SDimitry Andric     // Subnormal or underflow. Use floating-point multiply to handle truncation
332*fe6060f1SDimitry Andric     // correctly.
333*fe6060f1SDimitry Andric     fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig);
334*fe6060f1SDimitry Andric     exp += exponentBias - 1;
335*fe6060f1SDimitry Andric     if (exp < 1)
336*fe6060f1SDimitry Andric       exp = 1;
337*fe6060f1SDimitry Andric     tmp *= fromRep((rep_t)exp << significandBits);
338*fe6060f1SDimitry Andric     return tmp;
339*fe6060f1SDimitry Andric   } else
340*fe6060f1SDimitry Andric     return fromRep(sign | ((rep_t)exp << significandBits) | sig);
341*fe6060f1SDimitry Andric }
342*fe6060f1SDimitry Andric 
343*fe6060f1SDimitry Andric // Avoid using fmax from libm.
344*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
345*fe6060f1SDimitry Andric   // If either argument is NaN, return the other argument. If both are NaN,
346*fe6060f1SDimitry Andric   // arbitrarily return the second one. Otherwise, if both arguments are +/-0,
347*fe6060f1SDimitry Andric   // arbitrarily return the first one.
348*fe6060f1SDimitry Andric   return (crt_isnan(x) || x < y) ? y : x;
349*fe6060f1SDimitry Andric }
350*fe6060f1SDimitry Andric 
3510b57cec5SDimitry Andric #endif
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric #if defined(SINGLE_PRECISION)
354*fe6060f1SDimitry Andric 
3550b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) {
3560b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3570b57cec5SDimitry Andric }
358*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) {
359*fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
360*fe6060f1SDimitry Andric }
361*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) {
362*fe6060f1SDimitry Andric #if defined(__aarch64__)
363*fe6060f1SDimitry Andric   // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64.
364*fe6060f1SDimitry Andric   return __builtin_fmaxf(x, y);
365*fe6060f1SDimitry Andric #else
366*fe6060f1SDimitry Andric   // __builtin_fmaxf frequently turns into a libm call, so inline the function.
367*fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
368*fe6060f1SDimitry Andric #endif
369*fe6060f1SDimitry Andric }
370*fe6060f1SDimitry Andric 
3710b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION)
372*fe6060f1SDimitry Andric 
3730b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) {
3740b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3750b57cec5SDimitry Andric }
376*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
377*fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
378*fe6060f1SDimitry Andric }
379*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
380*fe6060f1SDimitry Andric #if defined(__aarch64__)
381*fe6060f1SDimitry Andric   // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64.
382*fe6060f1SDimitry Andric   return __builtin_fmax(x, y);
383*fe6060f1SDimitry Andric #else
384*fe6060f1SDimitry Andric   // __builtin_fmax frequently turns into a libm call, so inline the function.
385*fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
386*fe6060f1SDimitry Andric #endif
387*fe6060f1SDimitry Andric }
388*fe6060f1SDimitry Andric 
3890b57cec5SDimitry Andric #elif defined(QUAD_PRECISION)
390*fe6060f1SDimitry Andric 
3910b57cec5SDimitry Andric #if defined(CRT_LDBL_128BIT)
3920b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbl(fp_t x) {
3930b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3940b57cec5SDimitry Andric }
395*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnl(fp_t x, int y) {
396*fe6060f1SDimitry Andric   return __compiler_rt_scalbnX(x, y);
397*fe6060f1SDimitry Andric }
398*fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxl(fp_t x, fp_t y) {
399*fe6060f1SDimitry Andric   return __compiler_rt_fmaxX(x, y);
400*fe6060f1SDimitry Andric }
4010b57cec5SDimitry Andric #else
4020b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other
4030b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now.
4040b57cec5SDimitry Andric static __inline long double __compiler_rt_logbl(long double x) {
4050b57cec5SDimitry Andric   return crt_logbl(x);
4060b57cec5SDimitry Andric }
407*fe6060f1SDimitry Andric static __inline long double __compiler_rt_scalbnl(long double x, int y) {
408*fe6060f1SDimitry Andric   return crt_scalbnl(x, y);
409*fe6060f1SDimitry Andric }
410*fe6060f1SDimitry Andric static __inline long double __compiler_rt_fmaxl(long double x, long double y) {
411*fe6060f1SDimitry Andric   return crt_fmaxl(x, y);
412*fe6060f1SDimitry Andric }
413*fe6060f1SDimitry Andric #endif // CRT_LDBL_128BIT
414*fe6060f1SDimitry Andric 
415*fe6060f1SDimitry Andric #endif // *_PRECISION
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric #endif // FP_LIB_HEADER
418