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" 25*439352acSDimitry Andric #include "int_types.h" 260b57cec5SDimitry Andric #include <limits.h> 270b57cec5SDimitry Andric #include <stdbool.h> 280b57cec5SDimitry Andric #include <stdint.h> 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric #if defined SINGLE_PRECISION 310b57cec5SDimitry Andric 32e8d8bef9SDimitry Andric typedef uint16_t half_rep_t; 330b57cec5SDimitry Andric typedef uint32_t rep_t; 34e8d8bef9SDimitry Andric typedef uint64_t twice_rep_t; 350b57cec5SDimitry Andric typedef int32_t srep_t; 360b57cec5SDimitry Andric typedef float fp_t; 37e8d8bef9SDimitry Andric #define HALF_REP_C UINT16_C 380b57cec5SDimitry Andric #define REP_C UINT32_C 390b57cec5SDimitry Andric #define significandBits 23 400b57cec5SDimitry Andric 415ffd83dbSDimitry Andric static __inline int rep_clz(rep_t a) { return clzsi(a); } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric // 32x32 --> 64 bit multiply 440b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 450b57cec5SDimitry Andric const uint64_t product = (uint64_t)a * b; 460b57cec5SDimitry Andric *hi = product >> 32; 470b57cec5SDimitry Andric *lo = product; 480b57cec5SDimitry Andric } 490b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric #elif defined DOUBLE_PRECISION 520b57cec5SDimitry Andric 53e8d8bef9SDimitry Andric typedef uint32_t half_rep_t; 540b57cec5SDimitry Andric typedef uint64_t rep_t; 550b57cec5SDimitry Andric typedef int64_t srep_t; 560b57cec5SDimitry Andric typedef double fp_t; 57e8d8bef9SDimitry Andric #define HALF_REP_C UINT32_C 580b57cec5SDimitry Andric #define REP_C UINT64_C 590b57cec5SDimitry Andric #define significandBits 52 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { 620b57cec5SDimitry Andric #if defined __LP64__ 630b57cec5SDimitry Andric return __builtin_clzl(a); 640b57cec5SDimitry Andric #else 650b57cec5SDimitry Andric if (a & REP_C(0xffffffff00000000)) 665ffd83dbSDimitry Andric return clzsi(a >> 32); 670b57cec5SDimitry Andric else 685ffd83dbSDimitry Andric return 32 + clzsi(a & REP_C(0xffffffff)); 690b57cec5SDimitry Andric #endif 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric #define loWord(a) (a & 0xffffffffU) 730b57cec5SDimitry Andric #define hiWord(a) (a >> 32) 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // 64x64 -> 128 wide multiply for platforms that don't have such an operation; 760b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 770b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 780b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 790b57cec5SDimitry Andric // Each of the component 32x32 -> 64 products 800b57cec5SDimitry Andric const uint64_t plolo = loWord(a) * loWord(b); 810b57cec5SDimitry Andric const uint64_t plohi = loWord(a) * hiWord(b); 820b57cec5SDimitry Andric const uint64_t philo = hiWord(a) * loWord(b); 830b57cec5SDimitry Andric const uint64_t phihi = hiWord(a) * hiWord(b); 840b57cec5SDimitry Andric // Sum terms that contribute to lo in a way that allows us to get the carry 850b57cec5SDimitry Andric const uint64_t r0 = loWord(plolo); 860b57cec5SDimitry Andric const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); 870b57cec5SDimitry Andric *lo = r0 + (r1 << 32); 880b57cec5SDimitry Andric // Sum terms contributing to hi with the carry from lo 890b57cec5SDimitry Andric *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric #undef loWord 920b57cec5SDimitry Andric #undef hiWord 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric #elif defined QUAD_PRECISION 97*439352acSDimitry Andric #if defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT) 98e8d8bef9SDimitry Andric typedef uint64_t half_rep_t; 990b57cec5SDimitry Andric typedef __uint128_t rep_t; 1000b57cec5SDimitry Andric typedef __int128_t srep_t; 1015f757f3fSDimitry Andric typedef tf_float fp_t; 102e8d8bef9SDimitry Andric #define HALF_REP_C UINT64_C 1030b57cec5SDimitry Andric #define REP_C (__uint128_t) 104*439352acSDimitry Andric #if defined(CRT_HAS_IEEE_TF) 1050b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a 1060b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer 1070b57cec5SDimitry Andric #define significandBits 112 10806c3fb27SDimitry Andric #define TF_MANT_DIG (significandBits + 1) 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { 1110b57cec5SDimitry Andric const union { 1120b57cec5SDimitry Andric __uint128_t ll; 1130b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN 1140b57cec5SDimitry Andric struct { 1150b57cec5SDimitry Andric uint64_t high, low; 1160b57cec5SDimitry Andric } s; 1170b57cec5SDimitry Andric #else 1180b57cec5SDimitry Andric struct { 1190b57cec5SDimitry Andric uint64_t low, high; 1200b57cec5SDimitry Andric } s; 1210b57cec5SDimitry Andric #endif 1220b57cec5SDimitry Andric } uu = {.ll = a}; 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric uint64_t word; 1250b57cec5SDimitry Andric uint64_t add; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric if (uu.s.high) { 1280b57cec5SDimitry Andric word = uu.s.high; 1290b57cec5SDimitry Andric add = 0; 1300b57cec5SDimitry Andric } else { 1310b57cec5SDimitry Andric word = uu.s.low; 1320b57cec5SDimitry Andric add = 64; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric return __builtin_clzll(word) + add; 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff) 1380b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000) 1390b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff) 1400b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask) 1410b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask) 1420b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask) 1430b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask) 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation; 1460b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 1470b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 1480b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric const uint64_t product11 = Word_1(a) * Word_1(b); 1510b57cec5SDimitry Andric const uint64_t product12 = Word_1(a) * Word_2(b); 1520b57cec5SDimitry Andric const uint64_t product13 = Word_1(a) * Word_3(b); 1530b57cec5SDimitry Andric const uint64_t product14 = Word_1(a) * Word_4(b); 1540b57cec5SDimitry Andric const uint64_t product21 = Word_2(a) * Word_1(b); 1550b57cec5SDimitry Andric const uint64_t product22 = Word_2(a) * Word_2(b); 1560b57cec5SDimitry Andric const uint64_t product23 = Word_2(a) * Word_3(b); 1570b57cec5SDimitry Andric const uint64_t product24 = Word_2(a) * Word_4(b); 1580b57cec5SDimitry Andric const uint64_t product31 = Word_3(a) * Word_1(b); 1590b57cec5SDimitry Andric const uint64_t product32 = Word_3(a) * Word_2(b); 1600b57cec5SDimitry Andric const uint64_t product33 = Word_3(a) * Word_3(b); 1610b57cec5SDimitry Andric const uint64_t product34 = Word_3(a) * Word_4(b); 1620b57cec5SDimitry Andric const uint64_t product41 = Word_4(a) * Word_1(b); 1630b57cec5SDimitry Andric const uint64_t product42 = Word_4(a) * Word_2(b); 1640b57cec5SDimitry Andric const uint64_t product43 = Word_4(a) * Word_3(b); 1650b57cec5SDimitry Andric const uint64_t product44 = Word_4(a) * Word_4(b); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric const __uint128_t sum0 = (__uint128_t)product44; 1680b57cec5SDimitry Andric const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43; 1690b57cec5SDimitry Andric const __uint128_t sum2 = 1700b57cec5SDimitry Andric (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42; 1710b57cec5SDimitry Andric const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 + 1720b57cec5SDimitry Andric (__uint128_t)product32 + (__uint128_t)product41; 1730b57cec5SDimitry Andric const __uint128_t sum4 = 1740b57cec5SDimitry Andric (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31; 1750b57cec5SDimitry Andric const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21; 1760b57cec5SDimitry Andric const __uint128_t sum6 = (__uint128_t)product11; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32); 1790b57cec5SDimitry Andric const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) + 1800b57cec5SDimitry Andric (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric *lo = r0 + (r1 << 64); 1830b57cec5SDimitry Andric *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 + 1840b57cec5SDimitry Andric (sum5 << 32) + (sum6 << 64); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric #undef Word_1 1870b57cec5SDimitry Andric #undef Word_2 1880b57cec5SDimitry Andric #undef Word_3 1890b57cec5SDimitry Andric #undef Word_4 1900b57cec5SDimitry Andric #undef Word_HiMask 1910b57cec5SDimitry Andric #undef Word_LoMask 1920b57cec5SDimitry Andric #undef Word_FullMask 193*439352acSDimitry Andric #endif // defined(CRT_HAS_IEEE_TF) 194*439352acSDimitry Andric #else 195*439352acSDimitry Andric typedef long double fp_t; 196*439352acSDimitry Andric #endif // defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT) 1970b57cec5SDimitry Andric #else 1980b57cec5SDimitry Andric #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined. 1990b57cec5SDimitry Andric #endif 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \ 2025f757f3fSDimitry Andric (defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE)) 2030b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT) 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) { 2060b57cec5SDimitry Andric const union { 2070b57cec5SDimitry Andric fp_t f; 2080b57cec5SDimitry Andric rep_t i; 2090b57cec5SDimitry Andric } rep = {.f = x}; 2100b57cec5SDimitry Andric return rep.i; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) { 2140b57cec5SDimitry Andric const union { 2150b57cec5SDimitry Andric fp_t f; 2160b57cec5SDimitry Andric rep_t i; 2170b57cec5SDimitry Andric } rep = {.i = x}; 2180b57cec5SDimitry Andric return rep.f; 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 221*439352acSDimitry Andric #if !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF) 222*439352acSDimitry Andric #define exponentBits (typeWidth - significandBits - 1) 223*439352acSDimitry Andric #define maxExponent ((1 << exponentBits) - 1) 224*439352acSDimitry Andric #define exponentBias (maxExponent >> 1) 225*439352acSDimitry Andric 226*439352acSDimitry Andric #define implicitBit (REP_C(1) << significandBits) 227*439352acSDimitry Andric #define significandMask (implicitBit - 1U) 228*439352acSDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits)) 229*439352acSDimitry Andric #define absMask (signBit - 1U) 230*439352acSDimitry Andric #define exponentMask (absMask ^ significandMask) 231*439352acSDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits) 232*439352acSDimitry Andric #define infRep exponentMask 233*439352acSDimitry Andric #define quietBit (implicitBit >> 1) 234*439352acSDimitry Andric #define qnanRep (exponentMask | quietBit) 235*439352acSDimitry Andric 2360b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) { 2370b57cec5SDimitry Andric const int shift = rep_clz(*significand) - rep_clz(implicitBit); 2380b57cec5SDimitry Andric *significand <<= shift; 2390b57cec5SDimitry Andric return 1 - shift; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) { 2430b57cec5SDimitry Andric *hi = *hi << count | *lo >> (typeWidth - count); 2440b57cec5SDimitry Andric *lo = *lo << count; 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, 2480b57cec5SDimitry Andric unsigned int count) { 2490b57cec5SDimitry Andric if (count < typeWidth) { 25068d75effSDimitry Andric const bool sticky = (*lo << (typeWidth - count)) != 0; 2510b57cec5SDimitry Andric *lo = *hi << (typeWidth - count) | *lo >> count | sticky; 2520b57cec5SDimitry Andric *hi = *hi >> count; 2530b57cec5SDimitry Andric } else if (count < 2 * typeWidth) { 2540b57cec5SDimitry Andric const bool sticky = *hi << (2 * typeWidth - count) | *lo; 2550b57cec5SDimitry Andric *lo = *hi >> (count - typeWidth) | sticky; 2560b57cec5SDimitry Andric *hi = 0; 2570b57cec5SDimitry Andric } else { 2580b57cec5SDimitry Andric const bool sticky = *hi | *lo; 2590b57cec5SDimitry Andric *lo = sticky; 2600b57cec5SDimitry Andric *hi = 0; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids 2650b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace 2660b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence 2670b57cec5SDimitry Andric // the __compiler_rt prefix. 2680b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) { 2690b57cec5SDimitry Andric rep_t rep = toRep(x); 2700b57cec5SDimitry Andric int exp = (rep & exponentMask) >> significandBits; 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // Abnormal cases: 2730b57cec5SDimitry Andric // 1) +/- inf returns +inf; NaN returns NaN 2740b57cec5SDimitry Andric // 2) 0.0 returns -inf 2750b57cec5SDimitry Andric if (exp == maxExponent) { 2760b57cec5SDimitry Andric if (((rep & signBit) == 0) || (x != x)) { 2770b57cec5SDimitry Andric return x; // NaN or +inf: return x 2780b57cec5SDimitry Andric } else { 2790b57cec5SDimitry Andric return -x; // -inf: return -x 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric } else if (x == 0.0) { 2820b57cec5SDimitry Andric // 0.0: return -inf 2830b57cec5SDimitry Andric return fromRep(infRep | signBit); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric if (exp != 0) { 2870b57cec5SDimitry Andric // Normal number 2880b57cec5SDimitry Andric return exp - exponentBias; // Unbias exponent 2890b57cec5SDimitry Andric } else { 2900b57cec5SDimitry Andric // Subnormal number; normalize and repeat 2910b57cec5SDimitry Andric rep &= absMask; 2920b57cec5SDimitry Andric const int shift = 1 - normalize(&rep); 2930b57cec5SDimitry Andric exp = (rep & exponentMask) >> significandBits; 2940b57cec5SDimitry Andric return exp - exponentBias - shift; // Unbias exponent 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric } 297fe6060f1SDimitry Andric 298fe6060f1SDimitry Andric // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never 299fe6060f1SDimitry Andric // sets errno on underflow/overflow. 300fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) { 301fe6060f1SDimitry Andric const rep_t rep = toRep(x); 302fe6060f1SDimitry Andric int exp = (rep & exponentMask) >> significandBits; 303fe6060f1SDimitry Andric 304fe6060f1SDimitry Andric if (x == 0.0 || exp == maxExponent) 305fe6060f1SDimitry Andric return x; // +/- 0.0, NaN, or inf: return x 306fe6060f1SDimitry Andric 307fe6060f1SDimitry Andric // Normalize subnormal input. 308fe6060f1SDimitry Andric rep_t sig = rep & significandMask; 309fe6060f1SDimitry Andric if (exp == 0) { 310fe6060f1SDimitry Andric exp += normalize(&sig); 311fe6060f1SDimitry Andric sig &= ~implicitBit; // clear the implicit bit again 312fe6060f1SDimitry Andric } 313fe6060f1SDimitry Andric 314fe6060f1SDimitry Andric if (__builtin_sadd_overflow(exp, y, &exp)) { 315fe6060f1SDimitry Andric // Saturate the exponent, which will guarantee an underflow/overflow below. 316fe6060f1SDimitry Andric exp = (y >= 0) ? INT_MAX : INT_MIN; 317fe6060f1SDimitry Andric } 318fe6060f1SDimitry Andric 319fe6060f1SDimitry Andric // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias). 320fe6060f1SDimitry Andric const rep_t sign = rep & signBit; 321fe6060f1SDimitry Andric if (exp >= maxExponent) { 322fe6060f1SDimitry Andric // Overflow, which could produce infinity or the largest-magnitude value, 323fe6060f1SDimitry Andric // depending on the rounding mode. 324fe6060f1SDimitry Andric return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f; 325fe6060f1SDimitry Andric } else if (exp <= 0) { 326fe6060f1SDimitry Andric // Subnormal or underflow. Use floating-point multiply to handle truncation 327fe6060f1SDimitry Andric // correctly. 328fe6060f1SDimitry Andric fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig); 329fe6060f1SDimitry Andric exp += exponentBias - 1; 330fe6060f1SDimitry Andric if (exp < 1) 331fe6060f1SDimitry Andric exp = 1; 332fe6060f1SDimitry Andric tmp *= fromRep((rep_t)exp << significandBits); 333fe6060f1SDimitry Andric return tmp; 334fe6060f1SDimitry Andric } else 335fe6060f1SDimitry Andric return fromRep(sign | ((rep_t)exp << significandBits) | sig); 336fe6060f1SDimitry Andric } 337fe6060f1SDimitry Andric 338*439352acSDimitry Andric #endif // !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF) 339*439352acSDimitry Andric 340fe6060f1SDimitry Andric // Avoid using fmax from libm. 341fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) { 342fe6060f1SDimitry Andric // If either argument is NaN, return the other argument. If both are NaN, 343fe6060f1SDimitry Andric // arbitrarily return the second one. Otherwise, if both arguments are +/-0, 344fe6060f1SDimitry Andric // arbitrarily return the first one. 345fe6060f1SDimitry Andric return (crt_isnan(x) || x < y) ? y : x; 346fe6060f1SDimitry Andric } 347fe6060f1SDimitry Andric 3480b57cec5SDimitry Andric #endif 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) 351fe6060f1SDimitry Andric 3520b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) { 3530b57cec5SDimitry Andric return __compiler_rt_logbX(x); 3540b57cec5SDimitry Andric } 355fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) { 356fe6060f1SDimitry Andric return __compiler_rt_scalbnX(x, y); 357fe6060f1SDimitry Andric } 358fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) { 359fe6060f1SDimitry Andric #if defined(__aarch64__) 360fe6060f1SDimitry Andric // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64. 361fe6060f1SDimitry Andric return __builtin_fmaxf(x, y); 362fe6060f1SDimitry Andric #else 363fe6060f1SDimitry Andric // __builtin_fmaxf frequently turns into a libm call, so inline the function. 364fe6060f1SDimitry Andric return __compiler_rt_fmaxX(x, y); 365fe6060f1SDimitry Andric #endif 366fe6060f1SDimitry Andric } 367fe6060f1SDimitry Andric 3680b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION) 369fe6060f1SDimitry Andric 3700b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) { 3710b57cec5SDimitry Andric return __compiler_rt_logbX(x); 3720b57cec5SDimitry Andric } 373fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) { 374fe6060f1SDimitry Andric return __compiler_rt_scalbnX(x, y); 375fe6060f1SDimitry Andric } 376fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) { 377fe6060f1SDimitry Andric #if defined(__aarch64__) 378fe6060f1SDimitry Andric // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64. 379fe6060f1SDimitry Andric return __builtin_fmax(x, y); 380fe6060f1SDimitry Andric #else 381fe6060f1SDimitry Andric // __builtin_fmax frequently turns into a libm call, so inline the function. 382fe6060f1SDimitry Andric return __compiler_rt_fmaxX(x, y); 383fe6060f1SDimitry Andric #endif 384fe6060f1SDimitry Andric } 385fe6060f1SDimitry Andric 3865f757f3fSDimitry Andric #elif defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE) 3870b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other 3880b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now. 3895f757f3fSDimitry Andric #if defined(CRT_HAS_IEEE_TF) 3905f757f3fSDimitry Andric static __inline tf_float __compiler_rt_logbtf(tf_float x) { 3915f757f3fSDimitry Andric return __compiler_rt_logbX(x); 3925f757f3fSDimitry Andric } 3935f757f3fSDimitry Andric static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) { 3945f757f3fSDimitry Andric return __compiler_rt_scalbnX(x, y); 3955f757f3fSDimitry Andric } 3965f757f3fSDimitry Andric static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) { 3975f757f3fSDimitry Andric return __compiler_rt_fmaxX(x, y); 3985f757f3fSDimitry Andric } 3995f757f3fSDimitry Andric #define __compiler_rt_logbl __compiler_rt_logbtf 4005f757f3fSDimitry Andric #define __compiler_rt_scalbnl __compiler_rt_scalbntf 4015f757f3fSDimitry Andric #define __compiler_rt_fmaxl __compiler_rt_fmaxtf 4025f757f3fSDimitry Andric #define crt_fabstf crt_fabsf128 4035f757f3fSDimitry Andric #define crt_copysigntf crt_copysignf128 4045f757f3fSDimitry Andric #elif defined(CRT_LDBL_128BIT) 4055f757f3fSDimitry Andric static __inline tf_float __compiler_rt_logbtf(tf_float x) { 4060b57cec5SDimitry Andric return crt_logbl(x); 4070b57cec5SDimitry Andric } 4085f757f3fSDimitry Andric static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) { 409fe6060f1SDimitry Andric return crt_scalbnl(x, y); 410fe6060f1SDimitry Andric } 4115f757f3fSDimitry Andric static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) { 412fe6060f1SDimitry Andric return crt_fmaxl(x, y); 413fe6060f1SDimitry Andric } 4145f757f3fSDimitry Andric #define __compiler_rt_logbl crt_logbl 4155f757f3fSDimitry Andric #define __compiler_rt_scalbnl crt_scalbnl 4165f757f3fSDimitry Andric #define __compiler_rt_fmaxl crt_fmaxl 417*439352acSDimitry Andric #define crt_fabstf crt_fabsl 418*439352acSDimitry Andric #define crt_copysigntf crt_copysignl 4195f757f3fSDimitry Andric #else 4205f757f3fSDimitry Andric #error Unsupported TF mode type 4215f757f3fSDimitry Andric #endif 422fe6060f1SDimitry Andric 423fe6060f1SDimitry Andric #endif // *_PRECISION 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric #endif // FP_LIB_HEADER 426