compute_float64.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2020-2023 Daniel Lemire
  2. // Copyright 2023 Matt Borland
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT64_HPP
  6. #define BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT64_HPP
  7. #include <boost/charconv/detail/config.hpp>
  8. #include <boost/charconv/detail/significand_tables.hpp>
  9. #include <boost/charconv/detail/emulated128.hpp>
  10. #include <boost/core/bit.hpp>
  11. #include <cstdint>
  12. #include <cfloat>
  13. #include <cstring>
  14. #include <cmath>
  15. namespace boost { namespace charconv { namespace detail {
  16. static constexpr double powers_of_ten[] = {
  17. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
  18. 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
  19. };
  20. // Attempts to compute i * 10^(power) exactly; and if "negative" is true, negate the result.
  21. //
  22. // This function will only work in some cases, when it does not work, success is
  23. // set to false. This should work *most of the time* (like 99% of the time).
  24. // We assume that power is in the [-325, 308] interval.
  25. inline double compute_float64(std::int64_t power, std::uint64_t i, bool negative, bool& success) noexcept
  26. {
  27. static constexpr auto smallest_power = -325;
  28. static constexpr auto largest_power = 308;
  29. // We start with a fast path
  30. // It was described in Clinger WD.
  31. // How to read floating point numbers accurately.
  32. // ACM SIGPLAN Notices. 1990
  33. #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
  34. if (0 <= power && power <= 22 && i <= UINT64_C(9007199254740991))
  35. #else
  36. if (-22 <= power && power <= 22 && i <= UINT64_C(9007199254740991))
  37. #endif
  38. {
  39. // The general idea is as follows.
  40. // If 0 <= s < 2^53 and if 10^0 <= p <= 10^22 then
  41. // 1) Both s and p can be represented exactly as 64-bit floating-point
  42. // values
  43. // (binary64).
  44. // 2) Because s and p can be represented exactly as floating-point values,
  45. // then s * p
  46. // and s / p will produce correctly rounded values.
  47. auto d = static_cast<double>(i);
  48. if (power < 0)
  49. {
  50. d = d / powers_of_ten[-power];
  51. }
  52. else
  53. {
  54. d = d * powers_of_ten[power];
  55. }
  56. if (negative)
  57. {
  58. d = -d;
  59. }
  60. success = true;
  61. return d;
  62. }
  63. // When 22 < power && power < 22 + 16, we could
  64. // hope for another, secondary fast path. It was
  65. // described by David M. Gay in "Correctly rounded
  66. // binary-decimal and decimal-binary conversions." (1990)
  67. // If you need to compute i * 10^(22 + x) for x < 16,
  68. // first compute i * 10^x, if you know that result is exact
  69. // (e.g., when i * 10^x < 2^53),
  70. // then you can still proceed and do (i * 10^x) * 10^22.
  71. // Is this worth your time?
  72. // You need 22 < power *and* power < 22 + 16 *and* (i * 10^(x-22) < 2^53)
  73. // for this second fast path to work.
  74. // If you have 22 < power *and* power < 22 + 16, and then you
  75. // optimistically compute "i * 10^(x-22)", there is still a chance that you
  76. // have wasted your time if i * 10^(x-22) >= 2^53. It makes the use cases of
  77. // this optimization maybe less common than we would like. Source:
  78. // http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  79. // also used in RapidJSON: https://rapidjson.org/strtod_8h_source.html
  80. if (i == 0 || power < smallest_power)
  81. {
  82. return negative ? -0.0 : 0.0;
  83. }
  84. else if (power > largest_power)
  85. {
  86. return negative ? -HUGE_VAL : HUGE_VAL;
  87. }
  88. const std::uint64_t factor_significand = significands_table::significand_64[power - smallest_power];
  89. const std::int64_t exponent = (((152170 + 65536) * power) >> 16) + 1024 + 63;
  90. int leading_zeros = boost::core::countl_zero(i);
  91. i <<= static_cast<std::uint64_t>(leading_zeros);
  92. uint128 product = umul128(i, factor_significand);
  93. std::uint64_t low = product.low;
  94. std::uint64_t high = product.high;
  95. // We know that upper has at most one leading zero because
  96. // both i and factor_mantissa have a leading one. This means
  97. // that the result is at least as large as ((1<<63)*(1<<63))/(1<<64).
  98. //
  99. // As long as the first 9 bits of "upper" are not "1", then we
  100. // know that we have an exact computed value for the leading
  101. // 55 bits because any imprecision would play out as a +1, in the worst case.
  102. // Having 55 bits is necessary because we need 53 bits for the mantissa,
  103. // but we have to have one rounding bit and, we can waste a bit if the most
  104. // significant bit of the product is zero.
  105. //
  106. // We expect this next branch to be rarely taken (say 1% of the time).
  107. // When (upper & 0x1FF) == 0x1FF, it can be common for
  108. // lower + i < lower to be true (proba. much higher than 1%).
  109. if (BOOST_UNLIKELY((high & 0x1FF) == 0x1FF) && (low + i < low))
  110. {
  111. const std::uint64_t factor_significand_low = significands_table::significand_128[power - smallest_power];
  112. product = umul128(i, factor_significand_low);
  113. //const std::uint64_t product_low = product.low;
  114. const std::uint64_t product_middle2 = product.high;
  115. const std::uint64_t product_middle1 = low;
  116. std::uint64_t product_high = high;
  117. const std::uint64_t product_middle = product_middle1 + product_middle2;
  118. if (product_middle < product_middle1)
  119. {
  120. product_high++;
  121. }
  122. // Commented out because possibly unneeded
  123. // See: https://arxiv.org/pdf/2212.06644.pdf
  124. /*
  125. // we want to check whether mantissa *i + i would affect our result
  126. // This does happen, e.g. with 7.3177701707893310e+15
  127. if (((product_middle + 1 == 0) && ((product_high & 0x1FF) == 0x1FF) && (product_low + i < product_low)))
  128. {
  129. success = false;
  130. return 0;
  131. }
  132. */
  133. low = product_middle;
  134. high = product_high;
  135. }
  136. // The final significand should be 53 bits with a leading 1
  137. // We shift it so that it occupies 54 bits with a leading 1
  138. const std::uint64_t upper_bit = high >> 63;
  139. std::uint64_t significand = high >> (upper_bit + 9);
  140. leading_zeros += static_cast<int>(1 ^ upper_bit);
  141. // If we have lots of trailing zeros we may fall between two values
  142. if (BOOST_UNLIKELY((low == 0) && ((high & 0x1FF) == 0) && ((significand & 3) == 1)))
  143. {
  144. // if significand & 1 == 1 we might need to round up
  145. success = false;
  146. return 0;
  147. }
  148. significand += significand & 1;
  149. significand >>= 1;
  150. // Here the significand < (1<<53), unless there is an overflow
  151. if (significand >= (UINT64_C(1) << 53))
  152. {
  153. significand = (UINT64_C(1) << 52);
  154. leading_zeros--;
  155. }
  156. significand &= ~(UINT64_C(1) << 52);
  157. const auto real_exponent = static_cast<std::uint64_t>(exponent - leading_zeros);
  158. // We have to check that real_exponent is in range, otherwise fail
  159. if (BOOST_UNLIKELY((real_exponent < 1) || (real_exponent > 2046)))
  160. {
  161. success = false;
  162. return 0;
  163. }
  164. significand |= real_exponent << 52;
  165. significand |= ((static_cast<std::uint64_t>(negative) << 63));
  166. double d;
  167. std::memcpy(&d, &significand, sizeof(d));
  168. success = true;
  169. return d;
  170. }
  171. }}} // Namespaces
  172. #endif // BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT64_HPP