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