fp_lib.h 14 KB

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