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" 25439352acSDimitry 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; 46*0fca6ea1SDimitry Andric *hi = (rep_t)(product >> 32); 47*0fca6ea1SDimitry Andric *lo = (rep_t)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 61*0fca6ea1SDimitry Andric static inline int rep_clz(rep_t a) { return __builtin_clzll(a); } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric #define loWord(a) (a & 0xffffffffU) 640b57cec5SDimitry Andric #define hiWord(a) (a >> 32) 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // 64x64 -> 128 wide multiply for platforms that don't have such an operation; 670b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 680b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 690b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 700b57cec5SDimitry Andric // Each of the component 32x32 -> 64 products 710b57cec5SDimitry Andric const uint64_t plolo = loWord(a) * loWord(b); 720b57cec5SDimitry Andric const uint64_t plohi = loWord(a) * hiWord(b); 730b57cec5SDimitry Andric const uint64_t philo = hiWord(a) * loWord(b); 740b57cec5SDimitry Andric const uint64_t phihi = hiWord(a) * hiWord(b); 750b57cec5SDimitry Andric // Sum terms that contribute to lo in a way that allows us to get the carry 760b57cec5SDimitry Andric const uint64_t r0 = loWord(plolo); 770b57cec5SDimitry Andric const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); 780b57cec5SDimitry Andric *lo = r0 + (r1 << 32); 790b57cec5SDimitry Andric // Sum terms contributing to hi with the carry from lo 800b57cec5SDimitry Andric *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric #undef loWord 830b57cec5SDimitry Andric #undef hiWord 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric #elif defined QUAD_PRECISION 88439352acSDimitry Andric #if defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT) 89e8d8bef9SDimitry Andric typedef uint64_t half_rep_t; 900b57cec5SDimitry Andric typedef __uint128_t rep_t; 910b57cec5SDimitry Andric typedef __int128_t srep_t; 925f757f3fSDimitry Andric typedef tf_float fp_t; 93e8d8bef9SDimitry Andric #define HALF_REP_C UINT64_C 940b57cec5SDimitry Andric #define REP_C (__uint128_t) 95439352acSDimitry Andric #if defined(CRT_HAS_IEEE_TF) 960b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a 970b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer 980b57cec5SDimitry Andric #define significandBits 112 9906c3fb27SDimitry Andric #define TF_MANT_DIG (significandBits + 1) 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { 1020b57cec5SDimitry Andric const union { 1030b57cec5SDimitry Andric __uint128_t ll; 1040b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN 1050b57cec5SDimitry Andric struct { 1060b57cec5SDimitry Andric uint64_t high, low; 1070b57cec5SDimitry Andric } s; 1080b57cec5SDimitry Andric #else 1090b57cec5SDimitry Andric struct { 1100b57cec5SDimitry Andric uint64_t low, high; 1110b57cec5SDimitry Andric } s; 1120b57cec5SDimitry Andric #endif 1130b57cec5SDimitry Andric } uu = {.ll = a}; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric uint64_t word; 1160b57cec5SDimitry Andric uint64_t add; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric if (uu.s.high) { 1190b57cec5SDimitry Andric word = uu.s.high; 1200b57cec5SDimitry Andric add = 0; 1210b57cec5SDimitry Andric } else { 1220b57cec5SDimitry Andric word = uu.s.low; 1230b57cec5SDimitry Andric add = 64; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric return __builtin_clzll(word) + add; 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff) 1290b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000) 1300b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff) 1310b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask) 1320b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask) 1330b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask) 1340b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask) 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation; 1370b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 1380b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 1390b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric const uint64_t product11 = Word_1(a) * Word_1(b); 1420b57cec5SDimitry Andric const uint64_t product12 = Word_1(a) * Word_2(b); 1430b57cec5SDimitry Andric const uint64_t product13 = Word_1(a) * Word_3(b); 1440b57cec5SDimitry Andric const uint64_t product14 = Word_1(a) * Word_4(b); 1450b57cec5SDimitry Andric const uint64_t product21 = Word_2(a) * Word_1(b); 1460b57cec5SDimitry Andric const uint64_t product22 = Word_2(a) * Word_2(b); 1470b57cec5SDimitry Andric const uint64_t product23 = Word_2(a) * Word_3(b); 1480b57cec5SDimitry Andric const uint64_t product24 = Word_2(a) * Word_4(b); 1490b57cec5SDimitry Andric const uint64_t product31 = Word_3(a) * Word_1(b); 1500b57cec5SDimitry Andric const uint64_t product32 = Word_3(a) * Word_2(b); 1510b57cec5SDimitry Andric const uint64_t product33 = Word_3(a) * Word_3(b); 1520b57cec5SDimitry Andric const uint64_t product34 = Word_3(a) * Word_4(b); 1530b57cec5SDimitry Andric const uint64_t product41 = Word_4(a) * Word_1(b); 1540b57cec5SDimitry Andric const uint64_t product42 = Word_4(a) * Word_2(b); 1550b57cec5SDimitry Andric const uint64_t product43 = Word_4(a) * Word_3(b); 1560b57cec5SDimitry Andric const uint64_t product44 = Word_4(a) * Word_4(b); 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric const __uint128_t sum0 = (__uint128_t)product44; 1590b57cec5SDimitry Andric const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43; 1600b57cec5SDimitry Andric const __uint128_t sum2 = 1610b57cec5SDimitry Andric (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42; 1620b57cec5SDimitry Andric const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 + 1630b57cec5SDimitry Andric (__uint128_t)product32 + (__uint128_t)product41; 1640b57cec5SDimitry Andric const __uint128_t sum4 = 1650b57cec5SDimitry Andric (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31; 1660b57cec5SDimitry Andric const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21; 1670b57cec5SDimitry Andric const __uint128_t sum6 = (__uint128_t)product11; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32); 1700b57cec5SDimitry Andric const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) + 1710b57cec5SDimitry Andric (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric *lo = r0 + (r1 << 64); 1740b57cec5SDimitry Andric *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 + 1750b57cec5SDimitry Andric (sum5 << 32) + (sum6 << 64); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric #undef Word_1 1780b57cec5SDimitry Andric #undef Word_2 1790b57cec5SDimitry Andric #undef Word_3 1800b57cec5SDimitry Andric #undef Word_4 1810b57cec5SDimitry Andric #undef Word_HiMask 1820b57cec5SDimitry Andric #undef Word_LoMask 1830b57cec5SDimitry Andric #undef Word_FullMask 184439352acSDimitry Andric #endif // defined(CRT_HAS_IEEE_TF) 185439352acSDimitry Andric #else 186439352acSDimitry Andric typedef long double fp_t; 187439352acSDimitry Andric #endif // defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT) 1880b57cec5SDimitry Andric #else 1890b57cec5SDimitry Andric #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined. 1900b57cec5SDimitry Andric #endif 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \ 1935f757f3fSDimitry Andric (defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE)) 1940b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT) 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) { 1970b57cec5SDimitry Andric const union { 1980b57cec5SDimitry Andric fp_t f; 1990b57cec5SDimitry Andric rep_t i; 2000b57cec5SDimitry Andric } rep = {.f = x}; 2010b57cec5SDimitry Andric return rep.i; 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) { 2050b57cec5SDimitry Andric const union { 2060b57cec5SDimitry Andric fp_t f; 2070b57cec5SDimitry Andric rep_t i; 2080b57cec5SDimitry Andric } rep = {.i = x}; 2090b57cec5SDimitry Andric return rep.f; 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric 212439352acSDimitry Andric #if !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF) 213439352acSDimitry Andric #define exponentBits (typeWidth - significandBits - 1) 214439352acSDimitry Andric #define maxExponent ((1 << exponentBits) - 1) 215439352acSDimitry Andric #define exponentBias (maxExponent >> 1) 216439352acSDimitry Andric 217439352acSDimitry Andric #define implicitBit (REP_C(1) << significandBits) 218439352acSDimitry Andric #define significandMask (implicitBit - 1U) 219439352acSDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits)) 220439352acSDimitry Andric #define absMask (signBit - 1U) 221439352acSDimitry Andric #define exponentMask (absMask ^ significandMask) 222439352acSDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits) 223439352acSDimitry Andric #define infRep exponentMask 224439352acSDimitry Andric #define quietBit (implicitBit >> 1) 225439352acSDimitry Andric #define qnanRep (exponentMask | quietBit) 226439352acSDimitry Andric 2270b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) { 2280b57cec5SDimitry Andric const int shift = rep_clz(*significand) - rep_clz(implicitBit); 2290b57cec5SDimitry Andric *significand <<= shift; 2300b57cec5SDimitry Andric return 1 - shift; 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 233*0fca6ea1SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, unsigned int count) { 2340b57cec5SDimitry Andric *hi = *hi << count | *lo >> (typeWidth - count); 2350b57cec5SDimitry Andric *lo = *lo << count; 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, 2390b57cec5SDimitry Andric unsigned int count) { 2400b57cec5SDimitry Andric if (count < typeWidth) { 24168d75effSDimitry Andric const bool sticky = (*lo << (typeWidth - count)) != 0; 2420b57cec5SDimitry Andric *lo = *hi << (typeWidth - count) | *lo >> count | sticky; 2430b57cec5SDimitry Andric *hi = *hi >> count; 2440b57cec5SDimitry Andric } else if (count < 2 * typeWidth) { 2450b57cec5SDimitry Andric const bool sticky = *hi << (2 * typeWidth - count) | *lo; 2460b57cec5SDimitry Andric *lo = *hi >> (count - typeWidth) | sticky; 2470b57cec5SDimitry Andric *hi = 0; 2480b57cec5SDimitry Andric } else { 2490b57cec5SDimitry Andric const bool sticky = *hi | *lo; 2500b57cec5SDimitry Andric *lo = sticky; 2510b57cec5SDimitry Andric *hi = 0; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids 2560b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace 2570b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence 2580b57cec5SDimitry Andric // the __compiler_rt prefix. 2590b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) { 2600b57cec5SDimitry Andric rep_t rep = toRep(x); 2610b57cec5SDimitry Andric int exp = (rep & exponentMask) >> significandBits; 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // Abnormal cases: 2640b57cec5SDimitry Andric // 1) +/- inf returns +inf; NaN returns NaN 2650b57cec5SDimitry Andric // 2) 0.0 returns -inf 2660b57cec5SDimitry Andric if (exp == maxExponent) { 2670b57cec5SDimitry Andric if (((rep & signBit) == 0) || (x != x)) { 2680b57cec5SDimitry Andric return x; // NaN or +inf: return x 2690b57cec5SDimitry Andric } else { 2700b57cec5SDimitry Andric return -x; // -inf: return -x 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric } else if (x == 0.0) { 2730b57cec5SDimitry Andric // 0.0: return -inf 2740b57cec5SDimitry Andric return fromRep(infRep | signBit); 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric if (exp != 0) { 2780b57cec5SDimitry Andric // Normal number 2790b57cec5SDimitry Andric return exp - exponentBias; // Unbias exponent 2800b57cec5SDimitry Andric } else { 2810b57cec5SDimitry Andric // Subnormal number; normalize and repeat 2820b57cec5SDimitry Andric rep &= absMask; 2830b57cec5SDimitry Andric const int shift = 1 - normalize(&rep); 2840b57cec5SDimitry Andric exp = (rep & exponentMask) >> significandBits; 2850b57cec5SDimitry Andric return exp - exponentBias - shift; // Unbias exponent 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric } 288fe6060f1SDimitry Andric 289fe6060f1SDimitry Andric // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never 290fe6060f1SDimitry Andric // sets errno on underflow/overflow. 291fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) { 292fe6060f1SDimitry Andric const rep_t rep = toRep(x); 293fe6060f1SDimitry Andric int exp = (rep & exponentMask) >> significandBits; 294fe6060f1SDimitry Andric 295fe6060f1SDimitry Andric if (x == 0.0 || exp == maxExponent) 296fe6060f1SDimitry Andric return x; // +/- 0.0, NaN, or inf: return x 297fe6060f1SDimitry Andric 298fe6060f1SDimitry Andric // Normalize subnormal input. 299fe6060f1SDimitry Andric rep_t sig = rep & significandMask; 300fe6060f1SDimitry Andric if (exp == 0) { 301fe6060f1SDimitry Andric exp += normalize(&sig); 302fe6060f1SDimitry Andric sig &= ~implicitBit; // clear the implicit bit again 303fe6060f1SDimitry Andric } 304fe6060f1SDimitry Andric 305fe6060f1SDimitry Andric if (__builtin_sadd_overflow(exp, y, &exp)) { 306fe6060f1SDimitry Andric // Saturate the exponent, which will guarantee an underflow/overflow below. 307fe6060f1SDimitry Andric exp = (y >= 0) ? INT_MAX : INT_MIN; 308fe6060f1SDimitry Andric } 309fe6060f1SDimitry Andric 310fe6060f1SDimitry Andric // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias). 311fe6060f1SDimitry Andric const rep_t sign = rep & signBit; 312fe6060f1SDimitry Andric if (exp >= maxExponent) { 313fe6060f1SDimitry Andric // Overflow, which could produce infinity or the largest-magnitude value, 314fe6060f1SDimitry Andric // depending on the rounding mode. 315fe6060f1SDimitry Andric return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f; 316fe6060f1SDimitry Andric } else if (exp <= 0) { 317fe6060f1SDimitry Andric // Subnormal or underflow. Use floating-point multiply to handle truncation 318fe6060f1SDimitry Andric // correctly. 319fe6060f1SDimitry Andric fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig); 320fe6060f1SDimitry Andric exp += exponentBias - 1; 321fe6060f1SDimitry Andric if (exp < 1) 322fe6060f1SDimitry Andric exp = 1; 323fe6060f1SDimitry Andric tmp *= fromRep((rep_t)exp << significandBits); 324fe6060f1SDimitry Andric return tmp; 325fe6060f1SDimitry Andric } else 326fe6060f1SDimitry Andric return fromRep(sign | ((rep_t)exp << significandBits) | sig); 327fe6060f1SDimitry Andric } 328fe6060f1SDimitry Andric 329439352acSDimitry Andric #endif // !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF) 330439352acSDimitry Andric 331fe6060f1SDimitry Andric // Avoid using fmax from libm. 332fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) { 333fe6060f1SDimitry Andric // If either argument is NaN, return the other argument. If both are NaN, 334fe6060f1SDimitry Andric // arbitrarily return the second one. Otherwise, if both arguments are +/-0, 335fe6060f1SDimitry Andric // arbitrarily return the first one. 336fe6060f1SDimitry Andric return (crt_isnan(x) || x < y) ? y : x; 337fe6060f1SDimitry Andric } 338fe6060f1SDimitry Andric 3390b57cec5SDimitry Andric #endif 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) 342fe6060f1SDimitry Andric 3430b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) { 3440b57cec5SDimitry Andric return __compiler_rt_logbX(x); 3450b57cec5SDimitry Andric } 346fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) { 347fe6060f1SDimitry Andric return __compiler_rt_scalbnX(x, y); 348fe6060f1SDimitry Andric } 349fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) { 350fe6060f1SDimitry Andric #if defined(__aarch64__) 351fe6060f1SDimitry Andric // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64. 352fe6060f1SDimitry Andric return __builtin_fmaxf(x, y); 353fe6060f1SDimitry Andric #else 354fe6060f1SDimitry Andric // __builtin_fmaxf frequently turns into a libm call, so inline the function. 355fe6060f1SDimitry Andric return __compiler_rt_fmaxX(x, y); 356fe6060f1SDimitry Andric #endif 357fe6060f1SDimitry Andric } 358fe6060f1SDimitry Andric 3590b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION) 360fe6060f1SDimitry Andric 3610b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) { 3620b57cec5SDimitry Andric return __compiler_rt_logbX(x); 3630b57cec5SDimitry Andric } 364fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) { 365fe6060f1SDimitry Andric return __compiler_rt_scalbnX(x, y); 366fe6060f1SDimitry Andric } 367fe6060f1SDimitry Andric static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) { 368fe6060f1SDimitry Andric #if defined(__aarch64__) 369fe6060f1SDimitry Andric // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64. 370fe6060f1SDimitry Andric return __builtin_fmax(x, y); 371fe6060f1SDimitry Andric #else 372fe6060f1SDimitry Andric // __builtin_fmax frequently turns into a libm call, so inline the function. 373fe6060f1SDimitry Andric return __compiler_rt_fmaxX(x, y); 374fe6060f1SDimitry Andric #endif 375fe6060f1SDimitry Andric } 376fe6060f1SDimitry Andric 3775f757f3fSDimitry Andric #elif defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE) 3780b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other 3790b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now. 3805f757f3fSDimitry Andric #if defined(CRT_HAS_IEEE_TF) 3815f757f3fSDimitry Andric static __inline tf_float __compiler_rt_logbtf(tf_float x) { 3825f757f3fSDimitry Andric return __compiler_rt_logbX(x); 3835f757f3fSDimitry Andric } 3845f757f3fSDimitry Andric static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) { 3855f757f3fSDimitry Andric return __compiler_rt_scalbnX(x, y); 3865f757f3fSDimitry Andric } 3875f757f3fSDimitry Andric static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) { 3885f757f3fSDimitry Andric return __compiler_rt_fmaxX(x, y); 3895f757f3fSDimitry Andric } 3905f757f3fSDimitry Andric #define __compiler_rt_logbl __compiler_rt_logbtf 3915f757f3fSDimitry Andric #define __compiler_rt_scalbnl __compiler_rt_scalbntf 3925f757f3fSDimitry Andric #define __compiler_rt_fmaxl __compiler_rt_fmaxtf 3935f757f3fSDimitry Andric #define crt_fabstf crt_fabsf128 3945f757f3fSDimitry Andric #define crt_copysigntf crt_copysignf128 3955f757f3fSDimitry Andric #elif defined(CRT_LDBL_128BIT) 3965f757f3fSDimitry Andric static __inline tf_float __compiler_rt_logbtf(tf_float x) { 3970b57cec5SDimitry Andric return crt_logbl(x); 3980b57cec5SDimitry Andric } 3995f757f3fSDimitry Andric static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) { 400fe6060f1SDimitry Andric return crt_scalbnl(x, y); 401fe6060f1SDimitry Andric } 4025f757f3fSDimitry Andric static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) { 403fe6060f1SDimitry Andric return crt_fmaxl(x, y); 404fe6060f1SDimitry Andric } 4055f757f3fSDimitry Andric #define __compiler_rt_logbl crt_logbl 4065f757f3fSDimitry Andric #define __compiler_rt_scalbnl crt_scalbnl 4075f757f3fSDimitry Andric #define __compiler_rt_fmaxl crt_fmaxl 408439352acSDimitry Andric #define crt_fabstf crt_fabsl 409439352acSDimitry Andric #define crt_copysigntf crt_copysignl 4105f757f3fSDimitry Andric #else 4115f757f3fSDimitry Andric #error Unsupported TF mode type 4125f757f3fSDimitry Andric #endif 413fe6060f1SDimitry Andric 414fe6060f1SDimitry Andric #endif // *_PRECISION 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric #endif // FP_LIB_HEADER 417