1*0b57cec5SDimitry Andric //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file is a configuration header for soft-float routines in compiler-rt. 10*0b57cec5SDimitry Andric // This file does not provide any part of the compiler-rt interface, but defines 11*0b57cec5SDimitry Andric // many useful constants and utility routines that are used in the 12*0b57cec5SDimitry Andric // implementation of the soft-float routines in compiler-rt. 13*0b57cec5SDimitry Andric // 14*0b57cec5SDimitry Andric // Assumes that float, double and long double correspond to the IEEE-754 15*0b57cec5SDimitry Andric // binary32, binary64 and binary 128 types, respectively, and that integer 16*0b57cec5SDimitry Andric // endianness matches floating point endianness on the target platform. 17*0b57cec5SDimitry Andric // 18*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric #ifndef FP_LIB_HEADER 21*0b57cec5SDimitry Andric #define FP_LIB_HEADER 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric #include "int_lib.h" 24*0b57cec5SDimitry Andric #include "int_math.h" 25*0b57cec5SDimitry Andric #include <limits.h> 26*0b57cec5SDimitry Andric #include <stdbool.h> 27*0b57cec5SDimitry Andric #include <stdint.h> 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in 30*0b57cec5SDimitry Andric // 32-bit mode. 31*0b57cec5SDimitry Andric #if defined(__FreeBSD__) && defined(__i386__) 32*0b57cec5SDimitry Andric #include <sys/param.h> 33*0b57cec5SDimitry Andric #if __FreeBSD_version < 903000 // v9.3 34*0b57cec5SDimitry Andric #define uint64_t unsigned long long 35*0b57cec5SDimitry Andric #define int64_t long long 36*0b57cec5SDimitry Andric #undef UINT64_C 37*0b57cec5SDimitry Andric #define UINT64_C(c) (c##ULL) 38*0b57cec5SDimitry Andric #endif 39*0b57cec5SDimitry Andric #endif 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric #if defined SINGLE_PRECISION 42*0b57cec5SDimitry Andric 43*0b57cec5SDimitry Andric typedef uint32_t rep_t; 44*0b57cec5SDimitry Andric typedef int32_t srep_t; 45*0b57cec5SDimitry Andric typedef float fp_t; 46*0b57cec5SDimitry Andric #define REP_C UINT32_C 47*0b57cec5SDimitry Andric #define significandBits 23 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { return __builtin_clz(a); } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric // 32x32 --> 64 bit multiply 52*0b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 53*0b57cec5SDimitry Andric const uint64_t product = (uint64_t)a * b; 54*0b57cec5SDimitry Andric *hi = product >> 32; 55*0b57cec5SDimitry Andric *lo = product; 56*0b57cec5SDimitry Andric } 57*0b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b); 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric #elif defined DOUBLE_PRECISION 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric typedef uint64_t rep_t; 62*0b57cec5SDimitry Andric typedef int64_t srep_t; 63*0b57cec5SDimitry Andric typedef double fp_t; 64*0b57cec5SDimitry Andric #define REP_C UINT64_C 65*0b57cec5SDimitry Andric #define significandBits 52 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { 68*0b57cec5SDimitry Andric #if defined __LP64__ 69*0b57cec5SDimitry Andric return __builtin_clzl(a); 70*0b57cec5SDimitry Andric #else 71*0b57cec5SDimitry Andric if (a & REP_C(0xffffffff00000000)) 72*0b57cec5SDimitry Andric return __builtin_clz(a >> 32); 73*0b57cec5SDimitry Andric else 74*0b57cec5SDimitry Andric return 32 + __builtin_clz(a & REP_C(0xffffffff)); 75*0b57cec5SDimitry Andric #endif 76*0b57cec5SDimitry Andric } 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric #define loWord(a) (a & 0xffffffffU) 79*0b57cec5SDimitry Andric #define hiWord(a) (a >> 32) 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric // 64x64 -> 128 wide multiply for platforms that don't have such an operation; 82*0b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 83*0b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 84*0b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 85*0b57cec5SDimitry Andric // Each of the component 32x32 -> 64 products 86*0b57cec5SDimitry Andric const uint64_t plolo = loWord(a) * loWord(b); 87*0b57cec5SDimitry Andric const uint64_t plohi = loWord(a) * hiWord(b); 88*0b57cec5SDimitry Andric const uint64_t philo = hiWord(a) * loWord(b); 89*0b57cec5SDimitry Andric const uint64_t phihi = hiWord(a) * hiWord(b); 90*0b57cec5SDimitry Andric // Sum terms that contribute to lo in a way that allows us to get the carry 91*0b57cec5SDimitry Andric const uint64_t r0 = loWord(plolo); 92*0b57cec5SDimitry Andric const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); 93*0b57cec5SDimitry Andric *lo = r0 + (r1 << 32); 94*0b57cec5SDimitry Andric // Sum terms contributing to hi with the carry from lo 95*0b57cec5SDimitry Andric *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; 96*0b57cec5SDimitry Andric } 97*0b57cec5SDimitry Andric #undef loWord 98*0b57cec5SDimitry Andric #undef hiWord 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b); 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric #elif defined QUAD_PRECISION 103*0b57cec5SDimitry Andric #if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__) 104*0b57cec5SDimitry Andric #define CRT_LDBL_128BIT 105*0b57cec5SDimitry Andric typedef __uint128_t rep_t; 106*0b57cec5SDimitry Andric typedef __int128_t srep_t; 107*0b57cec5SDimitry Andric typedef long double fp_t; 108*0b57cec5SDimitry Andric #define REP_C (__uint128_t) 109*0b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a 110*0b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer 111*0b57cec5SDimitry Andric #define significandBits 112 112*0b57cec5SDimitry Andric 113*0b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) { 114*0b57cec5SDimitry Andric const union { 115*0b57cec5SDimitry Andric __uint128_t ll; 116*0b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN 117*0b57cec5SDimitry Andric struct { 118*0b57cec5SDimitry Andric uint64_t high, low; 119*0b57cec5SDimitry Andric } s; 120*0b57cec5SDimitry Andric #else 121*0b57cec5SDimitry Andric struct { 122*0b57cec5SDimitry Andric uint64_t low, high; 123*0b57cec5SDimitry Andric } s; 124*0b57cec5SDimitry Andric #endif 125*0b57cec5SDimitry Andric } uu = {.ll = a}; 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric uint64_t word; 128*0b57cec5SDimitry Andric uint64_t add; 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric if (uu.s.high) { 131*0b57cec5SDimitry Andric word = uu.s.high; 132*0b57cec5SDimitry Andric add = 0; 133*0b57cec5SDimitry Andric } else { 134*0b57cec5SDimitry Andric word = uu.s.low; 135*0b57cec5SDimitry Andric add = 64; 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric return __builtin_clzll(word) + add; 138*0b57cec5SDimitry Andric } 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff) 141*0b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000) 142*0b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff) 143*0b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask) 144*0b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask) 145*0b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask) 146*0b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask) 147*0b57cec5SDimitry Andric 148*0b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation; 149*0b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware 150*0b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here. 151*0b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric const uint64_t product11 = Word_1(a) * Word_1(b); 154*0b57cec5SDimitry Andric const uint64_t product12 = Word_1(a) * Word_2(b); 155*0b57cec5SDimitry Andric const uint64_t product13 = Word_1(a) * Word_3(b); 156*0b57cec5SDimitry Andric const uint64_t product14 = Word_1(a) * Word_4(b); 157*0b57cec5SDimitry Andric const uint64_t product21 = Word_2(a) * Word_1(b); 158*0b57cec5SDimitry Andric const uint64_t product22 = Word_2(a) * Word_2(b); 159*0b57cec5SDimitry Andric const uint64_t product23 = Word_2(a) * Word_3(b); 160*0b57cec5SDimitry Andric const uint64_t product24 = Word_2(a) * Word_4(b); 161*0b57cec5SDimitry Andric const uint64_t product31 = Word_3(a) * Word_1(b); 162*0b57cec5SDimitry Andric const uint64_t product32 = Word_3(a) * Word_2(b); 163*0b57cec5SDimitry Andric const uint64_t product33 = Word_3(a) * Word_3(b); 164*0b57cec5SDimitry Andric const uint64_t product34 = Word_3(a) * Word_4(b); 165*0b57cec5SDimitry Andric const uint64_t product41 = Word_4(a) * Word_1(b); 166*0b57cec5SDimitry Andric const uint64_t product42 = Word_4(a) * Word_2(b); 167*0b57cec5SDimitry Andric const uint64_t product43 = Word_4(a) * Word_3(b); 168*0b57cec5SDimitry Andric const uint64_t product44 = Word_4(a) * Word_4(b); 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric const __uint128_t sum0 = (__uint128_t)product44; 171*0b57cec5SDimitry Andric const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43; 172*0b57cec5SDimitry Andric const __uint128_t sum2 = 173*0b57cec5SDimitry Andric (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42; 174*0b57cec5SDimitry Andric const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 + 175*0b57cec5SDimitry Andric (__uint128_t)product32 + (__uint128_t)product41; 176*0b57cec5SDimitry Andric const __uint128_t sum4 = 177*0b57cec5SDimitry Andric (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31; 178*0b57cec5SDimitry Andric const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21; 179*0b57cec5SDimitry Andric const __uint128_t sum6 = (__uint128_t)product11; 180*0b57cec5SDimitry Andric 181*0b57cec5SDimitry Andric const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32); 182*0b57cec5SDimitry Andric const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) + 183*0b57cec5SDimitry Andric (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask); 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric *lo = r0 + (r1 << 64); 186*0b57cec5SDimitry Andric *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 + 187*0b57cec5SDimitry Andric (sum5 << 32) + (sum6 << 64); 188*0b57cec5SDimitry Andric } 189*0b57cec5SDimitry Andric #undef Word_1 190*0b57cec5SDimitry Andric #undef Word_2 191*0b57cec5SDimitry Andric #undef Word_3 192*0b57cec5SDimitry Andric #undef Word_4 193*0b57cec5SDimitry Andric #undef Word_HiMask 194*0b57cec5SDimitry Andric #undef Word_LoMask 195*0b57cec5SDimitry Andric #undef Word_FullMask 196*0b57cec5SDimitry Andric #endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__ 197*0b57cec5SDimitry Andric #else 198*0b57cec5SDimitry Andric #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined. 199*0b57cec5SDimitry Andric #endif 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \ 202*0b57cec5SDimitry Andric defined(CRT_LDBL_128BIT) 203*0b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT) 204*0b57cec5SDimitry Andric #define exponentBits (typeWidth - significandBits - 1) 205*0b57cec5SDimitry Andric #define maxExponent ((1 << exponentBits) - 1) 206*0b57cec5SDimitry Andric #define exponentBias (maxExponent >> 1) 207*0b57cec5SDimitry Andric 208*0b57cec5SDimitry Andric #define implicitBit (REP_C(1) << significandBits) 209*0b57cec5SDimitry Andric #define significandMask (implicitBit - 1U) 210*0b57cec5SDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits)) 211*0b57cec5SDimitry Andric #define absMask (signBit - 1U) 212*0b57cec5SDimitry Andric #define exponentMask (absMask ^ significandMask) 213*0b57cec5SDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits) 214*0b57cec5SDimitry Andric #define infRep exponentMask 215*0b57cec5SDimitry Andric #define quietBit (implicitBit >> 1) 216*0b57cec5SDimitry Andric #define qnanRep (exponentMask | quietBit) 217*0b57cec5SDimitry Andric 218*0b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) { 219*0b57cec5SDimitry Andric const union { 220*0b57cec5SDimitry Andric fp_t f; 221*0b57cec5SDimitry Andric rep_t i; 222*0b57cec5SDimitry Andric } rep = {.f = x}; 223*0b57cec5SDimitry Andric return rep.i; 224*0b57cec5SDimitry Andric } 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) { 227*0b57cec5SDimitry Andric const union { 228*0b57cec5SDimitry Andric fp_t f; 229*0b57cec5SDimitry Andric rep_t i; 230*0b57cec5SDimitry Andric } rep = {.i = x}; 231*0b57cec5SDimitry Andric return rep.f; 232*0b57cec5SDimitry Andric } 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) { 235*0b57cec5SDimitry Andric const int shift = rep_clz(*significand) - rep_clz(implicitBit); 236*0b57cec5SDimitry Andric *significand <<= shift; 237*0b57cec5SDimitry Andric return 1 - shift; 238*0b57cec5SDimitry Andric } 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) { 241*0b57cec5SDimitry Andric *hi = *hi << count | *lo >> (typeWidth - count); 242*0b57cec5SDimitry Andric *lo = *lo << count; 243*0b57cec5SDimitry Andric } 244*0b57cec5SDimitry Andric 245*0b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, 246*0b57cec5SDimitry Andric unsigned int count) { 247*0b57cec5SDimitry Andric if (count < typeWidth) { 248*0b57cec5SDimitry Andric const bool sticky = *lo << (typeWidth - count); 249*0b57cec5SDimitry Andric *lo = *hi << (typeWidth - count) | *lo >> count | sticky; 250*0b57cec5SDimitry Andric *hi = *hi >> count; 251*0b57cec5SDimitry Andric } else if (count < 2 * typeWidth) { 252*0b57cec5SDimitry Andric const bool sticky = *hi << (2 * typeWidth - count) | *lo; 253*0b57cec5SDimitry Andric *lo = *hi >> (count - typeWidth) | sticky; 254*0b57cec5SDimitry Andric *hi = 0; 255*0b57cec5SDimitry Andric } else { 256*0b57cec5SDimitry Andric const bool sticky = *hi | *lo; 257*0b57cec5SDimitry Andric *lo = sticky; 258*0b57cec5SDimitry Andric *hi = 0; 259*0b57cec5SDimitry Andric } 260*0b57cec5SDimitry Andric } 261*0b57cec5SDimitry Andric 262*0b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids 263*0b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace 264*0b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence 265*0b57cec5SDimitry Andric // the __compiler_rt prefix. 266*0b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) { 267*0b57cec5SDimitry Andric rep_t rep = toRep(x); 268*0b57cec5SDimitry Andric int exp = (rep & exponentMask) >> significandBits; 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric // Abnormal cases: 271*0b57cec5SDimitry Andric // 1) +/- inf returns +inf; NaN returns NaN 272*0b57cec5SDimitry Andric // 2) 0.0 returns -inf 273*0b57cec5SDimitry Andric if (exp == maxExponent) { 274*0b57cec5SDimitry Andric if (((rep & signBit) == 0) || (x != x)) { 275*0b57cec5SDimitry Andric return x; // NaN or +inf: return x 276*0b57cec5SDimitry Andric } else { 277*0b57cec5SDimitry Andric return -x; // -inf: return -x 278*0b57cec5SDimitry Andric } 279*0b57cec5SDimitry Andric } else if (x == 0.0) { 280*0b57cec5SDimitry Andric // 0.0: return -inf 281*0b57cec5SDimitry Andric return fromRep(infRep | signBit); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric if (exp != 0) { 285*0b57cec5SDimitry Andric // Normal number 286*0b57cec5SDimitry Andric return exp - exponentBias; // Unbias exponent 287*0b57cec5SDimitry Andric } else { 288*0b57cec5SDimitry Andric // Subnormal number; normalize and repeat 289*0b57cec5SDimitry Andric rep &= absMask; 290*0b57cec5SDimitry Andric const int shift = 1 - normalize(&rep); 291*0b57cec5SDimitry Andric exp = (rep & exponentMask) >> significandBits; 292*0b57cec5SDimitry Andric return exp - exponentBias - shift; // Unbias exponent 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric } 295*0b57cec5SDimitry Andric #endif 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric #if defined(SINGLE_PRECISION) 298*0b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) { 299*0b57cec5SDimitry Andric return __compiler_rt_logbX(x); 300*0b57cec5SDimitry Andric } 301*0b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION) 302*0b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) { 303*0b57cec5SDimitry Andric return __compiler_rt_logbX(x); 304*0b57cec5SDimitry Andric } 305*0b57cec5SDimitry Andric #elif defined(QUAD_PRECISION) 306*0b57cec5SDimitry Andric #if defined(CRT_LDBL_128BIT) 307*0b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbl(fp_t x) { 308*0b57cec5SDimitry Andric return __compiler_rt_logbX(x); 309*0b57cec5SDimitry Andric } 310*0b57cec5SDimitry Andric #else 311*0b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other 312*0b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now. 313*0b57cec5SDimitry Andric static __inline long double __compiler_rt_logbl(long double x) { 314*0b57cec5SDimitry Andric return crt_logbl(x); 315*0b57cec5SDimitry Andric } 316*0b57cec5SDimitry Andric #endif 317*0b57cec5SDimitry Andric #endif 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric #endif // FP_LIB_HEADER 320