fp_lib.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a configuration header for soft-float routines in compiler-rt.
  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. //
  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.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef FP_LIB_HEADER
  20. #define FP_LIB_HEADER
  21. #include "int_lib.h"
  22. #include "int_math.h"
  23. #include "int_types.h"
  24. #include <limits.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #if defined SINGLE_PRECISION
  28. typedef uint16_t half_rep_t;
  29. typedef uint32_t rep_t;
  30. typedef uint64_t twice_rep_t;
  31. typedef int32_t srep_t;
  32. typedef float fp_t;
  33. #define HALF_REP_C UINT16_C
  34. #define REP_C UINT32_C
  35. #define significandBits 23
  36. static __inline int rep_clz(rep_t a) { return clzsi(a); }
  37. // 32x32 --> 64 bit multiply
  38. static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  39. const uint64_t product = (uint64_t)a * b;
  40. *hi = (rep_t)(product >> 32);
  41. *lo = (rep_t)product;
  42. }
  43. COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
  44. #elif defined DOUBLE_PRECISION
  45. typedef uint32_t half_rep_t;
  46. typedef uint64_t rep_t;
  47. typedef int64_t srep_t;
  48. typedef double fp_t;
  49. #define HALF_REP_C UINT32_C
  50. #define REP_C UINT64_C
  51. #define significandBits 52
  52. static inline int rep_clz(rep_t a) { return __builtin_clzll(a); }
  53. #define loWord(a) (a & 0xffffffffU)
  54. #define hiWord(a) (a >> 32)
  55. // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
  56. // many 64-bit platforms have this operation, but they tend to have hardware
  57. // floating-point, so we don't bother with a special case for them here.
  58. static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  59. // Each of the component 32x32 -> 64 products
  60. const uint64_t plolo = loWord(a) * loWord(b);
  61. const uint64_t plohi = loWord(a) * hiWord(b);
  62. const uint64_t philo = hiWord(a) * loWord(b);
  63. const uint64_t phihi = hiWord(a) * hiWord(b);
  64. // Sum terms that contribute to lo in a way that allows us to get the carry
  65. const uint64_t r0 = loWord(plolo);
  66. const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
  67. *lo = r0 + (r1 << 32);
  68. // Sum terms contributing to hi with the carry from lo
  69. *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
  70. }
  71. #undef loWord
  72. #undef hiWord
  73. COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
  74. #elif defined QUAD_PRECISION
  75. #if defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT)
  76. typedef uint64_t half_rep_t;
  77. typedef __uint128_t rep_t;
  78. typedef __int128_t srep_t;
  79. typedef tf_float fp_t;
  80. #define HALF_REP_C UINT64_C
  81. #define REP_C (__uint128_t)
  82. #if defined(CRT_HAS_IEEE_TF)
  83. // Note: Since there is no explicit way to tell compiler the constant is a
  84. // 128-bit integer, we let the constant be casted to 128-bit integer
  85. #define significandBits 112
  86. #define TF_MANT_DIG (significandBits + 1)
  87. static __inline int rep_clz(rep_t a) {
  88. const union {
  89. __uint128_t ll;
  90. #if _YUGA_BIG_ENDIAN
  91. struct {
  92. uint64_t high, low;
  93. } s;
  94. #else
  95. struct {
  96. uint64_t low, high;
  97. } s;
  98. #endif
  99. } uu = {.ll = a};
  100. uint64_t word;
  101. uint64_t add;
  102. if (uu.s.high) {
  103. word = uu.s.high;
  104. add = 0;
  105. } else {
  106. word = uu.s.low;
  107. add = 64;
  108. }
  109. return __builtin_clzll(word) + add;
  110. }
  111. #define Word_LoMask UINT64_C(0x00000000ffffffff)
  112. #define Word_HiMask UINT64_C(0xffffffff00000000)
  113. #define Word_FullMask UINT64_C(0xffffffffffffffff)
  114. #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
  115. #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
  116. #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
  117. #define Word_4(a) (uint64_t)(a & Word_LoMask)
  118. // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
  119. // many 64-bit platforms have this operation, but they tend to have hardware
  120. // floating-point, so we don't bother with a special case for them here.
  121. static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  122. const uint64_t product11 = Word_1(a) * Word_1(b);
  123. const uint64_t product12 = Word_1(a) * Word_2(b);
  124. const uint64_t product13 = Word_1(a) * Word_3(b);
  125. const uint64_t product14 = Word_1(a) * Word_4(b);
  126. const uint64_t product21 = Word_2(a) * Word_1(b);
  127. const uint64_t product22 = Word_2(a) * Word_2(b);
  128. const uint64_t product23 = Word_2(a) * Word_3(b);
  129. const uint64_t product24 = Word_2(a) * Word_4(b);
  130. const uint64_t product31 = Word_3(a) * Word_1(b);
  131. const uint64_t product32 = Word_3(a) * Word_2(b);
  132. const uint64_t product33 = Word_3(a) * Word_3(b);
  133. const uint64_t product34 = Word_3(a) * Word_4(b);
  134. const uint64_t product41 = Word_4(a) * Word_1(b);
  135. const uint64_t product42 = Word_4(a) * Word_2(b);
  136. const uint64_t product43 = Word_4(a) * Word_3(b);
  137. const uint64_t product44 = Word_4(a) * Word_4(b);
  138. const __uint128_t sum0 = (__uint128_t)product44;
  139. const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
  140. const __uint128_t sum2 =
  141. (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
  142. const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
  143. (__uint128_t)product32 + (__uint128_t)product41;
  144. const __uint128_t sum4 =
  145. (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
  146. const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
  147. const __uint128_t sum6 = (__uint128_t)product11;
  148. const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
  149. const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
  150. (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
  151. *lo = r0 + (r1 << 64);
  152. *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
  153. (sum5 << 32) + (sum6 << 64);
  154. }
  155. #undef Word_1
  156. #undef Word_2
  157. #undef Word_3
  158. #undef Word_4
  159. #undef Word_HiMask
  160. #undef Word_LoMask
  161. #undef Word_FullMask
  162. #endif // defined(CRT_HAS_IEEE_TF)
  163. #else
  164. typedef long double fp_t;
  165. #endif // defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT)
  166. #else
  167. #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
  168. #endif
  169. #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \
  170. (defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE))
  171. #define typeWidth (sizeof(rep_t) * CHAR_BIT)
  172. static __inline rep_t toRep(fp_t x) {
  173. const union {
  174. fp_t f;
  175. rep_t i;
  176. } rep = {.f = x};
  177. return rep.i;
  178. }
  179. static __inline fp_t fromRep(rep_t x) {
  180. const union {
  181. fp_t f;
  182. rep_t i;
  183. } rep = {.i = x};
  184. return rep.f;
  185. }
  186. #if !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF)
  187. #define exponentBits (typeWidth - significandBits - 1)
  188. #define maxExponent ((1 << exponentBits) - 1)
  189. #define exponentBias (maxExponent >> 1)
  190. #define implicitBit (REP_C(1) << significandBits)
  191. #define significandMask (implicitBit - 1U)
  192. #define signBit (REP_C(1) << (significandBits + exponentBits))
  193. #define absMask (signBit - 1U)
  194. #define exponentMask (absMask ^ significandMask)
  195. #define oneRep ((rep_t)exponentBias << significandBits)
  196. #define infRep exponentMask
  197. #define quietBit (implicitBit >> 1)
  198. #define qnanRep (exponentMask | quietBit)
  199. static __inline int normalize(rep_t *significand) {
  200. const int shift = rep_clz(*significand) - rep_clz(implicitBit);
  201. *significand <<= shift;
  202. return 1 - shift;
  203. }
  204. static __inline void wideLeftShift(rep_t *hi, rep_t *lo, unsigned int count) {
  205. *hi = *hi << count | *lo >> (typeWidth - count);
  206. *lo = *lo << count;
  207. }
  208. static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
  209. unsigned int count) {
  210. if (count < typeWidth) {
  211. const bool sticky = (*lo << (typeWidth - count)) != 0;
  212. *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
  213. *hi = *hi >> count;
  214. } else if (count < 2 * typeWidth) {
  215. const bool sticky = *hi << (2 * typeWidth - count) | *lo;
  216. *lo = *hi >> (count - typeWidth) | sticky;
  217. *hi = 0;
  218. } else {
  219. const bool sticky = *hi | *lo;
  220. *lo = sticky;
  221. *hi = 0;
  222. }
  223. }
  224. // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
  225. // pulling in a libm dependency from compiler-rt, but is not meant to replace
  226. // it (i.e. code calling logb() should get the one from libm, not this), hence
  227. // the __compiler_rt prefix.
  228. static __inline fp_t __compiler_rt_logbX(fp_t x) {
  229. rep_t rep = toRep(x);
  230. int exp = (rep & exponentMask) >> significandBits;
  231. // Abnormal cases:
  232. // 1) +/- inf returns +inf; NaN returns NaN
  233. // 2) 0.0 returns -inf
  234. if (exp == maxExponent) {
  235. if (((rep & signBit) == 0) || (x != x)) {
  236. return x; // NaN or +inf: return x
  237. } else {
  238. return -x; // -inf: return -x
  239. }
  240. } else if (x == 0.0) {
  241. // 0.0: return -inf
  242. return fromRep(infRep | signBit);
  243. }
  244. if (exp != 0) {
  245. // Normal number
  246. return exp - exponentBias; // Unbias exponent
  247. } else {
  248. // Subnormal number; normalize and repeat
  249. rep &= absMask;
  250. const int shift = 1 - normalize(&rep);
  251. exp = (rep & exponentMask) >> significandBits;
  252. return exp - exponentBias - shift; // Unbias exponent
  253. }
  254. }
  255. // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never
  256. // sets errno on underflow/overflow.
  257. static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
  258. const rep_t rep = toRep(x);
  259. int exp = (rep & exponentMask) >> significandBits;
  260. if (x == 0.0 || exp == maxExponent)
  261. return x; // +/- 0.0, NaN, or inf: return x
  262. // Normalize subnormal input.
  263. rep_t sig = rep & significandMask;
  264. if (exp == 0) {
  265. exp += normalize(&sig);
  266. sig &= ~implicitBit; // clear the implicit bit again
  267. }
  268. if (__builtin_sadd_overflow(exp, y, &exp)) {
  269. // Saturate the exponent, which will guarantee an underflow/overflow below.
  270. exp = (y >= 0) ? INT_MAX : INT_MIN;
  271. }
  272. // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias).
  273. const rep_t sign = rep & signBit;
  274. if (exp >= maxExponent) {
  275. // Overflow, which could produce infinity or the largest-magnitude value,
  276. // depending on the rounding mode.
  277. return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f;
  278. } else if (exp <= 0) {
  279. // Subnormal or underflow. Use floating-point multiply to handle truncation
  280. // correctly.
  281. fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig);
  282. exp += exponentBias - 1;
  283. if (exp < 1)
  284. exp = 1;
  285. tmp *= fromRep((rep_t)exp << significandBits);
  286. return tmp;
  287. } else
  288. return fromRep(sign | ((rep_t)exp << significandBits) | sig);
  289. }
  290. #endif // !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF)
  291. // Avoid using fmax from libm.
  292. static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
  293. // If either argument is NaN, return the other argument. If both are NaN,
  294. // arbitrarily return the second one. Otherwise, if both arguments are +/-0,
  295. // arbitrarily return the first one.
  296. return (crt_isnan(x) || x < y) ? y : x;
  297. }
  298. #endif
  299. #if defined(SINGLE_PRECISION)
  300. static __inline fp_t __compiler_rt_logbf(fp_t x) {
  301. return __compiler_rt_logbX(x);
  302. }
  303. static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) {
  304. return __compiler_rt_scalbnX(x, y);
  305. }
  306. static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) {
  307. #if defined(__aarch64__)
  308. // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64.
  309. return __builtin_fmaxf(x, y);
  310. #else
  311. // __builtin_fmaxf frequently turns into a libm call, so inline the function.
  312. return __compiler_rt_fmaxX(x, y);
  313. #endif
  314. }
  315. #elif defined(DOUBLE_PRECISION)
  316. static __inline fp_t __compiler_rt_logb(fp_t x) {
  317. return __compiler_rt_logbX(x);
  318. }
  319. static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
  320. return __compiler_rt_scalbnX(x, y);
  321. }
  322. static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
  323. #if defined(__aarch64__)
  324. // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64.
  325. return __builtin_fmax(x, y);
  326. #else
  327. // __builtin_fmax frequently turns into a libm call, so inline the function.
  328. return __compiler_rt_fmaxX(x, y);
  329. #endif
  330. }
  331. #elif defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE)
  332. // The generic implementation only works for ieee754 floating point. For other
  333. // floating point types, continue to rely on the libm implementation for now.
  334. #if defined(CRT_HAS_IEEE_TF)
  335. static __inline tf_float __compiler_rt_logbtf(tf_float x) {
  336. return __compiler_rt_logbX(x);
  337. }
  338. static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) {
  339. return __compiler_rt_scalbnX(x, y);
  340. }
  341. static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) {
  342. return __compiler_rt_fmaxX(x, y);
  343. }
  344. #define __compiler_rt_logbl __compiler_rt_logbtf
  345. #define __compiler_rt_scalbnl __compiler_rt_scalbntf
  346. #define __compiler_rt_fmaxl __compiler_rt_fmaxtf
  347. #define crt_fabstf crt_fabsf128
  348. #define crt_copysigntf crt_copysignf128
  349. #elif defined(CRT_LDBL_128BIT)
  350. static __inline tf_float __compiler_rt_logbtf(tf_float x) {
  351. return crt_logbl(x);
  352. }
  353. static __inline tf_float __compiler_rt_scalbntf(tf_float x, int y) {
  354. return crt_scalbnl(x, y);
  355. }
  356. static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) {
  357. return crt_fmaxl(x, y);
  358. }
  359. #define __compiler_rt_logbl crt_logbl
  360. #define __compiler_rt_scalbnl crt_scalbnl
  361. #define __compiler_rt_fmaxl crt_fmaxl
  362. #define crt_fabstf crt_fabsl
  363. #define crt_copysigntf crt_copysignl
  364. #else
  365. #error Unsupported TF mode type
  366. #endif
  367. #endif // *_PRECISION
  368. #endif // FP_LIB_HEADER