123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #ifndef BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT64_HPP
- #define BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT64_HPP
- #include <boost/charconv/detail/config.hpp>
- #include <boost/charconv/detail/significand_tables.hpp>
- #include <boost/charconv/detail/emulated128.hpp>
- #include <boost/core/bit.hpp>
- #include <cstdint>
- #include <cfloat>
- #include <cstring>
- #include <cmath>
- namespace boost { namespace charconv { namespace detail {
- static constexpr double powers_of_ten[] = {
- 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
- 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
- };
- inline double compute_float64(std::int64_t power, std::uint64_t i, bool negative, bool& success) noexcept
- {
- static constexpr auto smallest_power = -325;
- static constexpr auto largest_power = 308;
-
-
-
-
- #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
- if (0 <= power && power <= 22 && i <= UINT64_C(9007199254740991))
- #else
- if (-22 <= power && power <= 22 && i <= UINT64_C(9007199254740991))
- #endif
- {
-
-
-
-
-
-
-
-
-
- auto d = static_cast<double>(i);
-
- if (power < 0)
- {
- d = d / powers_of_ten[-power];
- }
- else
- {
- d = d * powers_of_ten[power];
- }
-
- if (negative)
- {
- d = -d;
- }
- success = true;
- return d;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (i == 0 || power < smallest_power)
- {
- return negative ? -0.0 : 0.0;
- }
- else if (power > largest_power)
- {
- return negative ? -HUGE_VAL : HUGE_VAL;
- }
- const std::uint64_t factor_significand = significands_table::significand_64[power - smallest_power];
- const std::int64_t exponent = (((152170 + 65536) * power) >> 16) + 1024 + 63;
- int leading_zeros = boost::core::countl_zero(i);
- i <<= static_cast<std::uint64_t>(leading_zeros);
- uint128 product = umul128(i, factor_significand);
- std::uint64_t low = product.low;
- std::uint64_t high = product.high;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (BOOST_UNLIKELY((high & 0x1FF) == 0x1FF) && (low + i < low))
- {
- const std::uint64_t factor_significand_low = significands_table::significand_128[power - smallest_power];
- product = umul128(i, factor_significand_low);
-
- const std::uint64_t product_middle2 = product.high;
- const std::uint64_t product_middle1 = low;
- std::uint64_t product_high = high;
- const std::uint64_t product_middle = product_middle1 + product_middle2;
- if (product_middle < product_middle1)
- {
- product_high++;
- }
-
-
-
- low = product_middle;
- high = product_high;
- }
-
-
- const std::uint64_t upper_bit = high >> 63;
- std::uint64_t significand = high >> (upper_bit + 9);
- leading_zeros += static_cast<int>(1 ^ upper_bit);
-
- if (BOOST_UNLIKELY((low == 0) && ((high & 0x1FF) == 0) && ((significand & 3) == 1)))
- {
-
- success = false;
- return 0;
- }
- significand += significand & 1;
- significand >>= 1;
-
- if (significand >= (UINT64_C(1) << 53))
- {
- significand = (UINT64_C(1) << 52);
- leading_zeros--;
- }
- significand &= ~(UINT64_C(1) << 52);
- const auto real_exponent = static_cast<std::uint64_t>(exponent - leading_zeros);
-
- if (BOOST_UNLIKELY((real_exponent < 1) || (real_exponent > 2046)))
- {
- success = false;
- return 0;
- }
- significand |= real_exponent << 52;
- significand |= ((static_cast<std::uint64_t>(negative) << 63));
-
- double d;
- std::memcpy(&d, &significand, sizeof(d));
- success = true;
- return d;
- }
- }}}
- #endif
|