xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fp_lib.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 
430b57cec5SDimitry Andric typedef uint32_t rep_t;
440b57cec5SDimitry Andric typedef int32_t srep_t;
450b57cec5SDimitry Andric typedef float fp_t;
460b57cec5SDimitry Andric #define REP_C UINT32_C
470b57cec5SDimitry Andric #define significandBits 23
480b57cec5SDimitry Andric 
49*5ffd83dbSDimitry Andric static __inline int rep_clz(rep_t a) { return clzsi(a); }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric // 32x32 --> 64 bit multiply
520b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
530b57cec5SDimitry Andric   const uint64_t product = (uint64_t)a * b;
540b57cec5SDimitry Andric   *hi = product >> 32;
550b57cec5SDimitry Andric   *lo = product;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric #elif defined DOUBLE_PRECISION
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric typedef uint64_t rep_t;
620b57cec5SDimitry Andric typedef int64_t srep_t;
630b57cec5SDimitry Andric typedef double fp_t;
640b57cec5SDimitry Andric #define REP_C UINT64_C
650b57cec5SDimitry Andric #define significandBits 52
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) {
680b57cec5SDimitry Andric #if defined __LP64__
690b57cec5SDimitry Andric   return __builtin_clzl(a);
700b57cec5SDimitry Andric #else
710b57cec5SDimitry Andric   if (a & REP_C(0xffffffff00000000))
72*5ffd83dbSDimitry Andric     return clzsi(a >> 32);
730b57cec5SDimitry Andric   else
74*5ffd83dbSDimitry Andric     return 32 + clzsi(a & REP_C(0xffffffff));
750b57cec5SDimitry Andric #endif
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric #define loWord(a) (a & 0xffffffffU)
790b57cec5SDimitry Andric #define hiWord(a) (a >> 32)
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
820b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware
830b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here.
840b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
850b57cec5SDimitry Andric   // Each of the component 32x32 -> 64 products
860b57cec5SDimitry Andric   const uint64_t plolo = loWord(a) * loWord(b);
870b57cec5SDimitry Andric   const uint64_t plohi = loWord(a) * hiWord(b);
880b57cec5SDimitry Andric   const uint64_t philo = hiWord(a) * loWord(b);
890b57cec5SDimitry Andric   const uint64_t phihi = hiWord(a) * hiWord(b);
900b57cec5SDimitry Andric   // Sum terms that contribute to lo in a way that allows us to get the carry
910b57cec5SDimitry Andric   const uint64_t r0 = loWord(plolo);
920b57cec5SDimitry Andric   const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
930b57cec5SDimitry Andric   *lo = r0 + (r1 << 32);
940b57cec5SDimitry Andric   // Sum terms contributing to hi with the carry from lo
950b57cec5SDimitry Andric   *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric #undef loWord
980b57cec5SDimitry Andric #undef hiWord
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric #elif defined QUAD_PRECISION
1030b57cec5SDimitry Andric #if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__)
1040b57cec5SDimitry Andric #define CRT_LDBL_128BIT
1050b57cec5SDimitry Andric typedef __uint128_t rep_t;
1060b57cec5SDimitry Andric typedef __int128_t srep_t;
1070b57cec5SDimitry Andric typedef long double fp_t;
1080b57cec5SDimitry Andric #define REP_C (__uint128_t)
1090b57cec5SDimitry Andric // Note: Since there is no explicit way to tell compiler the constant is a
1100b57cec5SDimitry Andric // 128-bit integer, we let the constant be casted to 128-bit integer
1110b57cec5SDimitry Andric #define significandBits 112
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric static __inline int rep_clz(rep_t a) {
1140b57cec5SDimitry Andric   const union {
1150b57cec5SDimitry Andric     __uint128_t ll;
1160b57cec5SDimitry Andric #if _YUGA_BIG_ENDIAN
1170b57cec5SDimitry Andric     struct {
1180b57cec5SDimitry Andric       uint64_t high, low;
1190b57cec5SDimitry Andric     } s;
1200b57cec5SDimitry Andric #else
1210b57cec5SDimitry Andric     struct {
1220b57cec5SDimitry Andric       uint64_t low, high;
1230b57cec5SDimitry Andric     } s;
1240b57cec5SDimitry Andric #endif
1250b57cec5SDimitry Andric   } uu = {.ll = a};
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   uint64_t word;
1280b57cec5SDimitry Andric   uint64_t add;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   if (uu.s.high) {
1310b57cec5SDimitry Andric     word = uu.s.high;
1320b57cec5SDimitry Andric     add = 0;
1330b57cec5SDimitry Andric   } else {
1340b57cec5SDimitry Andric     word = uu.s.low;
1350b57cec5SDimitry Andric     add = 64;
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric   return __builtin_clzll(word) + add;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric #define Word_LoMask UINT64_C(0x00000000ffffffff)
1410b57cec5SDimitry Andric #define Word_HiMask UINT64_C(0xffffffff00000000)
1420b57cec5SDimitry Andric #define Word_FullMask UINT64_C(0xffffffffffffffff)
1430b57cec5SDimitry Andric #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
1440b57cec5SDimitry Andric #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
1450b57cec5SDimitry Andric #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
1460b57cec5SDimitry Andric #define Word_4(a) (uint64_t)(a & Word_LoMask)
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
1490b57cec5SDimitry Andric // many 64-bit platforms have this operation, but they tend to have hardware
1500b57cec5SDimitry Andric // floating-point, so we don't bother with a special case for them here.
1510b57cec5SDimitry Andric static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   const uint64_t product11 = Word_1(a) * Word_1(b);
1540b57cec5SDimitry Andric   const uint64_t product12 = Word_1(a) * Word_2(b);
1550b57cec5SDimitry Andric   const uint64_t product13 = Word_1(a) * Word_3(b);
1560b57cec5SDimitry Andric   const uint64_t product14 = Word_1(a) * Word_4(b);
1570b57cec5SDimitry Andric   const uint64_t product21 = Word_2(a) * Word_1(b);
1580b57cec5SDimitry Andric   const uint64_t product22 = Word_2(a) * Word_2(b);
1590b57cec5SDimitry Andric   const uint64_t product23 = Word_2(a) * Word_3(b);
1600b57cec5SDimitry Andric   const uint64_t product24 = Word_2(a) * Word_4(b);
1610b57cec5SDimitry Andric   const uint64_t product31 = Word_3(a) * Word_1(b);
1620b57cec5SDimitry Andric   const uint64_t product32 = Word_3(a) * Word_2(b);
1630b57cec5SDimitry Andric   const uint64_t product33 = Word_3(a) * Word_3(b);
1640b57cec5SDimitry Andric   const uint64_t product34 = Word_3(a) * Word_4(b);
1650b57cec5SDimitry Andric   const uint64_t product41 = Word_4(a) * Word_1(b);
1660b57cec5SDimitry Andric   const uint64_t product42 = Word_4(a) * Word_2(b);
1670b57cec5SDimitry Andric   const uint64_t product43 = Word_4(a) * Word_3(b);
1680b57cec5SDimitry Andric   const uint64_t product44 = Word_4(a) * Word_4(b);
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   const __uint128_t sum0 = (__uint128_t)product44;
1710b57cec5SDimitry Andric   const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
1720b57cec5SDimitry Andric   const __uint128_t sum2 =
1730b57cec5SDimitry Andric       (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
1740b57cec5SDimitry Andric   const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
1750b57cec5SDimitry Andric                            (__uint128_t)product32 + (__uint128_t)product41;
1760b57cec5SDimitry Andric   const __uint128_t sum4 =
1770b57cec5SDimitry Andric       (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
1780b57cec5SDimitry Andric   const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
1790b57cec5SDimitry Andric   const __uint128_t sum6 = (__uint128_t)product11;
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
1820b57cec5SDimitry Andric   const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
1830b57cec5SDimitry Andric                          (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   *lo = r0 + (r1 << 64);
1860b57cec5SDimitry Andric   *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
1870b57cec5SDimitry Andric         (sum5 << 32) + (sum6 << 64);
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric #undef Word_1
1900b57cec5SDimitry Andric #undef Word_2
1910b57cec5SDimitry Andric #undef Word_3
1920b57cec5SDimitry Andric #undef Word_4
1930b57cec5SDimitry Andric #undef Word_HiMask
1940b57cec5SDimitry Andric #undef Word_LoMask
1950b57cec5SDimitry Andric #undef Word_FullMask
1960b57cec5SDimitry Andric #endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
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) ||                  \
2020b57cec5SDimitry Andric     defined(CRT_LDBL_128BIT)
2030b57cec5SDimitry Andric #define typeWidth (sizeof(rep_t) * CHAR_BIT)
2040b57cec5SDimitry Andric #define exponentBits (typeWidth - significandBits - 1)
2050b57cec5SDimitry Andric #define maxExponent ((1 << exponentBits) - 1)
2060b57cec5SDimitry Andric #define exponentBias (maxExponent >> 1)
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric #define implicitBit (REP_C(1) << significandBits)
2090b57cec5SDimitry Andric #define significandMask (implicitBit - 1U)
2100b57cec5SDimitry Andric #define signBit (REP_C(1) << (significandBits + exponentBits))
2110b57cec5SDimitry Andric #define absMask (signBit - 1U)
2120b57cec5SDimitry Andric #define exponentMask (absMask ^ significandMask)
2130b57cec5SDimitry Andric #define oneRep ((rep_t)exponentBias << significandBits)
2140b57cec5SDimitry Andric #define infRep exponentMask
2150b57cec5SDimitry Andric #define quietBit (implicitBit >> 1)
2160b57cec5SDimitry Andric #define qnanRep (exponentMask | quietBit)
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric static __inline rep_t toRep(fp_t x) {
2190b57cec5SDimitry Andric   const union {
2200b57cec5SDimitry Andric     fp_t f;
2210b57cec5SDimitry Andric     rep_t i;
2220b57cec5SDimitry Andric   } rep = {.f = x};
2230b57cec5SDimitry Andric   return rep.i;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric static __inline fp_t fromRep(rep_t x) {
2270b57cec5SDimitry Andric   const union {
2280b57cec5SDimitry Andric     fp_t f;
2290b57cec5SDimitry Andric     rep_t i;
2300b57cec5SDimitry Andric   } rep = {.i = x};
2310b57cec5SDimitry Andric   return rep.f;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric static __inline int normalize(rep_t *significand) {
2350b57cec5SDimitry Andric   const int shift = rep_clz(*significand) - rep_clz(implicitBit);
2360b57cec5SDimitry Andric   *significand <<= shift;
2370b57cec5SDimitry Andric   return 1 - shift;
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
2410b57cec5SDimitry Andric   *hi = *hi << count | *lo >> (typeWidth - count);
2420b57cec5SDimitry Andric   *lo = *lo << count;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
2460b57cec5SDimitry Andric                                               unsigned int count) {
2470b57cec5SDimitry Andric   if (count < typeWidth) {
24868d75effSDimitry Andric     const bool sticky = (*lo << (typeWidth - count)) != 0;
2490b57cec5SDimitry Andric     *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
2500b57cec5SDimitry Andric     *hi = *hi >> count;
2510b57cec5SDimitry Andric   } else if (count < 2 * typeWidth) {
2520b57cec5SDimitry Andric     const bool sticky = *hi << (2 * typeWidth - count) | *lo;
2530b57cec5SDimitry Andric     *lo = *hi >> (count - typeWidth) | sticky;
2540b57cec5SDimitry Andric     *hi = 0;
2550b57cec5SDimitry Andric   } else {
2560b57cec5SDimitry Andric     const bool sticky = *hi | *lo;
2570b57cec5SDimitry Andric     *lo = sticky;
2580b57cec5SDimitry Andric     *hi = 0;
2590b57cec5SDimitry Andric   }
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
2630b57cec5SDimitry Andric // pulling in a libm dependency from compiler-rt, but is not meant to replace
2640b57cec5SDimitry Andric // it (i.e. code calling logb() should get the one from libm, not this), hence
2650b57cec5SDimitry Andric // the __compiler_rt prefix.
2660b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbX(fp_t x) {
2670b57cec5SDimitry Andric   rep_t rep = toRep(x);
2680b57cec5SDimitry Andric   int exp = (rep & exponentMask) >> significandBits;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   // Abnormal cases:
2710b57cec5SDimitry Andric   // 1) +/- inf returns +inf; NaN returns NaN
2720b57cec5SDimitry Andric   // 2) 0.0 returns -inf
2730b57cec5SDimitry Andric   if (exp == maxExponent) {
2740b57cec5SDimitry Andric     if (((rep & signBit) == 0) || (x != x)) {
2750b57cec5SDimitry Andric       return x; // NaN or +inf: return x
2760b57cec5SDimitry Andric     } else {
2770b57cec5SDimitry Andric       return -x; // -inf: return -x
2780b57cec5SDimitry Andric     }
2790b57cec5SDimitry Andric   } else if (x == 0.0) {
2800b57cec5SDimitry Andric     // 0.0: return -inf
2810b57cec5SDimitry Andric     return fromRep(infRep | signBit);
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   if (exp != 0) {
2850b57cec5SDimitry Andric     // Normal number
2860b57cec5SDimitry Andric     return exp - exponentBias; // Unbias exponent
2870b57cec5SDimitry Andric   } else {
2880b57cec5SDimitry Andric     // Subnormal number; normalize and repeat
2890b57cec5SDimitry Andric     rep &= absMask;
2900b57cec5SDimitry Andric     const int shift = 1 - normalize(&rep);
2910b57cec5SDimitry Andric     exp = (rep & exponentMask) >> significandBits;
2920b57cec5SDimitry Andric     return exp - exponentBias - shift; // Unbias exponent
2930b57cec5SDimitry Andric   }
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric #endif
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric #if defined(SINGLE_PRECISION)
2980b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbf(fp_t x) {
2990b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric #elif defined(DOUBLE_PRECISION)
3020b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logb(fp_t x) {
3030b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric #elif defined(QUAD_PRECISION)
3060b57cec5SDimitry Andric #if defined(CRT_LDBL_128BIT)
3070b57cec5SDimitry Andric static __inline fp_t __compiler_rt_logbl(fp_t x) {
3080b57cec5SDimitry Andric   return __compiler_rt_logbX(x);
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric #else
3110b57cec5SDimitry Andric // The generic implementation only works for ieee754 floating point. For other
3120b57cec5SDimitry Andric // floating point types, continue to rely on the libm implementation for now.
3130b57cec5SDimitry Andric static __inline long double __compiler_rt_logbl(long double x) {
3140b57cec5SDimitry Andric   return crt_logbl(x);
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric #endif
3170b57cec5SDimitry Andric #endif
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric #endif // FP_LIB_HEADER
320