fp_lib.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file is a configuration header for soft-float routines in compiler-rt.
  11. // This file does not provide any part of the compiler-rt interface, but defines
  12. // many useful constants and utility routines that are used in the
  13. // implementation of the soft-float routines in compiler-rt.
  14. //
  15. // Assumes that float, double and long double correspond to the IEEE-754
  16. // binary32, binary64 and binary 128 types, respectively, and that integer
  17. // endianness matches floating point endianness on the target platform.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef FP_LIB_HEADER
  21. #define FP_LIB_HEADER
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <limits.h>
  25. #include "int_lib.h"
  26. // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
  27. // 32-bit mode.
  28. #if defined(__FreeBSD__) && defined(__i386__)
  29. # include <sys/param.h>
  30. # if __FreeBSD_version < 903000 // v9.3
  31. # define uint64_t unsigned long long
  32. # define int64_t long long
  33. # undef UINT64_C
  34. # define UINT64_C(c) (c ## ULL)
  35. # endif
  36. #endif
  37. #if defined SINGLE_PRECISION
  38. typedef uint32_t rep_t;
  39. typedef int32_t srep_t;
  40. typedef float fp_t;
  41. #define REP_C UINT32_C
  42. #define significandBits 23
  43. static inline int rep_clz(rep_t a) {
  44. return __builtin_clz(a);
  45. }
  46. // 32x32 --> 64 bit multiply
  47. static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  48. const uint64_t product = (uint64_t)a*b;
  49. *hi = product >> 32;
  50. *lo = product;
  51. }
  52. COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
  53. #elif defined DOUBLE_PRECISION
  54. typedef uint64_t rep_t;
  55. typedef int64_t srep_t;
  56. typedef double fp_t;
  57. #define REP_C UINT64_C
  58. #define significandBits 52
  59. static inline int rep_clz(rep_t a) {
  60. #if defined __LP64__
  61. return __builtin_clzl(a);
  62. #else
  63. if (a & REP_C(0xffffffff00000000))
  64. return __builtin_clz(a >> 32);
  65. else
  66. return 32 + __builtin_clz(a & REP_C(0xffffffff));
  67. #endif
  68. }
  69. #define loWord(a) (a & 0xffffffffU)
  70. #define hiWord(a) (a >> 32)
  71. // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
  72. // many 64-bit platforms have this operation, but they tend to have hardware
  73. // floating-point, so we don't bother with a special case for them here.
  74. static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  75. // Each of the component 32x32 -> 64 products
  76. const uint64_t plolo = loWord(a) * loWord(b);
  77. const uint64_t plohi = loWord(a) * hiWord(b);
  78. const uint64_t philo = hiWord(a) * loWord(b);
  79. const uint64_t phihi = hiWord(a) * hiWord(b);
  80. // Sum terms that contribute to lo in a way that allows us to get the carry
  81. const uint64_t r0 = loWord(plolo);
  82. const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
  83. *lo = r0 + (r1 << 32);
  84. // Sum terms contributing to hi with the carry from lo
  85. *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
  86. }
  87. #undef loWord
  88. #undef hiWord
  89. COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
  90. #elif defined QUAD_PRECISION
  91. #if __LDBL_MANT_DIG__ == 113
  92. #define CRT_LDBL_128BIT
  93. typedef __uint128_t rep_t;
  94. typedef __int128_t srep_t;
  95. typedef long double fp_t;
  96. #define REP_C (__uint128_t)
  97. // Note: Since there is no explicit way to tell compiler the constant is a
  98. // 128-bit integer, we let the constant be casted to 128-bit integer
  99. #define significandBits 112
  100. static inline int rep_clz(rep_t a) {
  101. const union
  102. {
  103. __uint128_t ll;
  104. #if _YUGA_BIG_ENDIAN
  105. struct { uint64_t high, low; } s;
  106. #else
  107. struct { uint64_t low, high; } s;
  108. #endif
  109. } uu = { .ll = a };
  110. uint64_t word;
  111. uint64_t add;
  112. if (uu.s.high){
  113. word = uu.s.high;
  114. add = 0;
  115. }
  116. else{
  117. word = uu.s.low;
  118. add = 64;
  119. }
  120. return __builtin_clzll(word) + add;
  121. }
  122. #define Word_LoMask UINT64_C(0x00000000ffffffff)
  123. #define Word_HiMask UINT64_C(0xffffffff00000000)
  124. #define Word_FullMask UINT64_C(0xffffffffffffffff)
  125. #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
  126. #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
  127. #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
  128. #define Word_4(a) (uint64_t)(a & Word_LoMask)
  129. // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
  130. // many 64-bit platforms have this operation, but they tend to have hardware
  131. // floating-point, so we don't bother with a special case for them here.
  132. static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
  133. const uint64_t product11 = Word_1(a) * Word_1(b);
  134. const uint64_t product12 = Word_1(a) * Word_2(b);
  135. const uint64_t product13 = Word_1(a) * Word_3(b);
  136. const uint64_t product14 = Word_1(a) * Word_4(b);
  137. const uint64_t product21 = Word_2(a) * Word_1(b);
  138. const uint64_t product22 = Word_2(a) * Word_2(b);
  139. const uint64_t product23 = Word_2(a) * Word_3(b);
  140. const uint64_t product24 = Word_2(a) * Word_4(b);
  141. const uint64_t product31 = Word_3(a) * Word_1(b);
  142. const uint64_t product32 = Word_3(a) * Word_2(b);
  143. const uint64_t product33 = Word_3(a) * Word_3(b);
  144. const uint64_t product34 = Word_3(a) * Word_4(b);
  145. const uint64_t product41 = Word_4(a) * Word_1(b);
  146. const uint64_t product42 = Word_4(a) * Word_2(b);
  147. const uint64_t product43 = Word_4(a) * Word_3(b);
  148. const uint64_t product44 = Word_4(a) * Word_4(b);
  149. const __uint128_t sum0 = (__uint128_t)product44;
  150. const __uint128_t sum1 = (__uint128_t)product34 +
  151. (__uint128_t)product43;
  152. const __uint128_t sum2 = (__uint128_t)product24 +
  153. (__uint128_t)product33 +
  154. (__uint128_t)product42;
  155. const __uint128_t sum3 = (__uint128_t)product14 +
  156. (__uint128_t)product23 +
  157. (__uint128_t)product32 +
  158. (__uint128_t)product41;
  159. const __uint128_t sum4 = (__uint128_t)product13 +
  160. (__uint128_t)product22 +
  161. (__uint128_t)product31;
  162. const __uint128_t sum5 = (__uint128_t)product12 +
  163. (__uint128_t)product21;
  164. const __uint128_t sum6 = (__uint128_t)product11;
  165. const __uint128_t r0 = (sum0 & Word_FullMask) +
  166. ((sum1 & Word_LoMask) << 32);
  167. const __uint128_t r1 = (sum0 >> 64) +
  168. ((sum1 >> 32) & Word_FullMask) +
  169. (sum2 & Word_FullMask) +
  170. ((sum3 << 32) & Word_HiMask);
  171. *lo = r0 + (r1 << 64);
  172. *hi = (r1 >> 64) +
  173. (sum1 >> 96) +
  174. (sum2 >> 64) +
  175. (sum3 >> 32) +
  176. sum4 +
  177. (sum5 << 32) +
  178. (sum6 << 64);
  179. }
  180. #undef Word_1
  181. #undef Word_2
  182. #undef Word_3
  183. #undef Word_4
  184. #undef Word_HiMask
  185. #undef Word_LoMask
  186. #undef Word_FullMask
  187. #endif // __LDBL_MANT_DIG__ == 113
  188. #else
  189. #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
  190. #endif
  191. #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)
  192. #define typeWidth (sizeof(rep_t)*CHAR_BIT)
  193. #define exponentBits (typeWidth - significandBits - 1)
  194. #define maxExponent ((1 << exponentBits) - 1)
  195. #define exponentBias (maxExponent >> 1)
  196. #define implicitBit (REP_C(1) << significandBits)
  197. #define significandMask (implicitBit - 1U)
  198. #define signBit (REP_C(1) << (significandBits + exponentBits))
  199. #define absMask (signBit - 1U)
  200. #define exponentMask (absMask ^ significandMask)
  201. #define oneRep ((rep_t)exponentBias << significandBits)
  202. #define infRep exponentMask
  203. #define quietBit (implicitBit >> 1)
  204. #define qnanRep (exponentMask | quietBit)
  205. static inline rep_t toRep(fp_t x) {
  206. const union { fp_t f; rep_t i; } rep = {.f = x};
  207. return rep.i;
  208. }
  209. static inline fp_t fromRep(rep_t x) {
  210. const union { fp_t f; rep_t i; } rep = {.i = x};
  211. return rep.f;
  212. }
  213. static inline int normalize(rep_t *significand) {
  214. const int shift = rep_clz(*significand) - rep_clz(implicitBit);
  215. *significand <<= shift;
  216. return 1 - shift;
  217. }
  218. static inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
  219. *hi = *hi << count | *lo >> (typeWidth - count);
  220. *lo = *lo << count;
  221. }
  222. static inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
  223. if (count < typeWidth) {
  224. const bool sticky = *lo << (typeWidth - count);
  225. *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
  226. *hi = *hi >> count;
  227. }
  228. else if (count < 2*typeWidth) {
  229. const bool sticky = *hi << (2*typeWidth - count) | *lo;
  230. *lo = *hi >> (count - typeWidth) | sticky;
  231. *hi = 0;
  232. } else {
  233. const bool sticky = *hi | *lo;
  234. *lo = sticky;
  235. *hi = 0;
  236. }
  237. }
  238. #endif
  239. #endif // FP_LIB_HEADER