From d88314d6fe4766c701779ed83e006af812fd47c6 Mon Sep 17 00:00:00 2001 From: Dmitry Arkhipov Date: Fri, 15 Mar 2024 15:37:41 +0300 Subject: [PATCH] Dragonbox is used to serialize doubles --- .../json/detail/dragonbox/bit_layouts.hpp | 164 ++ .../boost/json/detail/dragonbox/dragonbox.hpp | 2394 +++++++++++++++++ .../detail/dragonbox/dragonbox_common.hpp | 450 ++++ .../json/detail/dragonbox/emulated128.hpp | 1014 +++++++ .../json/detail/dragonbox/impl/to_chars.ipp | 500 ++++ .../dragonbox/to_chars_integer_impl.hpp | 50 + include/boost/json/detail/format.hpp | 6 +- include/boost/json/detail/impl/format.ipp | 79 +- .../boost/json/detail/ryu/detail/common.hpp | 144 - include/boost/json/detail/ryu/detail/d2s.hpp | 264 -- .../json/detail/ryu/detail/d2s_full_table.hpp | 365 --- .../json/detail/ryu/detail/d2s_intrinsics.hpp | 231 -- .../json/detail/ryu/detail/digit_table.hpp | 61 - include/boost/json/detail/ryu/impl/d2s.ipp | 732 ----- include/boost/json/detail/ryu/ryu.hpp | 43 - include/boost/json/src.hpp | 2 +- test/Jamfile | 3 - test/ryu/d2s_intrinsics_test.cpp | 58 - test/ryu/d2s_table_test.cpp | 108 - test/ryu/d2s_test.cpp | 285 -- test/ryu/gtest.hpp | 34 - test/serializer.cpp | 4 +- 22 files changed, 4654 insertions(+), 2337 deletions(-) create mode 100644 include/boost/json/detail/dragonbox/bit_layouts.hpp create mode 100644 include/boost/json/detail/dragonbox/dragonbox.hpp create mode 100644 include/boost/json/detail/dragonbox/dragonbox_common.hpp create mode 100644 include/boost/json/detail/dragonbox/emulated128.hpp create mode 100644 include/boost/json/detail/dragonbox/impl/to_chars.ipp create mode 100644 include/boost/json/detail/dragonbox/to_chars_integer_impl.hpp delete mode 100644 include/boost/json/detail/ryu/detail/common.hpp delete mode 100644 include/boost/json/detail/ryu/detail/d2s.hpp delete mode 100644 include/boost/json/detail/ryu/detail/d2s_full_table.hpp delete mode 100644 include/boost/json/detail/ryu/detail/d2s_intrinsics.hpp delete mode 100644 include/boost/json/detail/ryu/detail/digit_table.hpp delete mode 100644 include/boost/json/detail/ryu/impl/d2s.ipp delete mode 100644 include/boost/json/detail/ryu/ryu.hpp delete mode 100644 test/ryu/d2s_intrinsics_test.cpp delete mode 100644 test/ryu/d2s_table_test.cpp delete mode 100644 test/ryu/d2s_test.cpp delete mode 100644 test/ryu/gtest.hpp diff --git a/include/boost/json/detail/dragonbox/bit_layouts.hpp b/include/boost/json/detail/dragonbox/bit_layouts.hpp new file mode 100644 index 000000000..c731d99e7 --- /dev/null +++ b/include/boost/json/detail/dragonbox/bit_layouts.hpp @@ -0,0 +1,164 @@ +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_JSON_DETAIL_DRAGONBOX_BIT_LAYOUTS_HPP +#define BOOST_JSON_DETAIL_DRAGONBOX_BIT_LAYOUTS_HPP + +// #include +// #include +#include +#include + +// Layouts of floating point types as specified by IEEE 754 +// See page 23 of IEEE 754-2008 + +namespace boost { +namespace json { +namespace detail { + +struct ieee754_binary16 +{ + static constexpr int significand_bits = 10; + static constexpr int exponent_bits = 5; + static constexpr int min_exponent = -14; + static constexpr int max_exponent = 15; + static constexpr int exponent_bias = -15; + static constexpr int decimal_digits = 5; +}; + +struct brainfloat16 +{ + static constexpr int significand_bits = 7; + static constexpr int exponent_bits = 8; + static constexpr int min_exponent = -126; + static constexpr int max_exponent = 127; + static constexpr int exponent_bias = -127; + static constexpr int decimal_digits = 4; +}; + +struct ieee754_binary32 +{ + static constexpr int significand_bits = 23; + static constexpr int exponent_bits = 8; + static constexpr int min_exponent = -126; + static constexpr int max_exponent = 127; + static constexpr int exponent_bias = -127; + static constexpr int decimal_digits = 9; +}; + +struct ieee754_binary64 +{ + static constexpr int significand_bits = 52; + static constexpr int exponent_bits = 11; + static constexpr int min_exponent = -1022; + static constexpr int max_exponent = 1023; + static constexpr int exponent_bias = -1023; + static constexpr int decimal_digits = 17; +}; + +// 80 bit long double (e.g. x86-64) +#if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 + +struct IEEEl2bits +{ +#if BOOST_CHARCONV_ENDIAN_LITTLE_BYTE + std::uint64_t mantissa_l : 64; + std::uint32_t exponent : 15; + std::uint32_t sign : 1; + std::uint64_t pad : 48; +#else // Big endian + std::uint64_t pad : 48; + std::uint32_t sign : 1; + std::uint32_t exponent : 15; + std::uint64_t mantissa_h : 64; +#endif +}; + +struct ieee754_binary80 +{ + static constexpr int significand_bits = 64; // Fraction is 63 and 1 integer bit + static constexpr int exponent_bits = 15; + static constexpr int min_exponent = -16382; + static constexpr int max_exponent = 16383; + static constexpr int exponent_bias = -16383; + static constexpr int decimal_digits = 18; +}; + +#define BOOST_CHARCONV_LDBL_BITS 80 + +// 128 bit long double (e.g. s390x, ppcle64) +#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 + +struct IEEEl2bits +{ +#if BOOST_CHARCONV_ENDIAN_LITTLE_BYTE + std::uint64_t mantissa_l : 64; + std::uint64_t mantissa_h : 48; + std::uint32_t exponent : 15; + std::uint32_t sign : 1; +#else // Big endian + std::uint32_t sign : 1; + std::uint32_t exponent : 15; + std::uint64_t mantissa_h : 48; + std::uint64_t mantissa_l : 64; +#endif +}; + +#define BOOST_CHARCONV_LDBL_BITS 128 + +// 64 bit long double (double == long double on ARM) +#elif LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 + +struct IEEEl2bits +{ +#if BOOST_CHARCONV_ENDIAN_LITTLE_BYTE + std::uint32_t mantissa_l : 32; + std::uint32_t mantissa_h : 20; + std::uint32_t exponent : 11; + std::uint32_t sign : 1; +#else // Big endian + std::uint32_t sign : 1; + std::uint32_t exponent : 11; + std::uint32_t mantissa_h : 20; + std::uint32_t mantissa_l : 32; +#endif +}; + +#define BOOST_CHARCONV_LDBL_BITS 64 + +#else // Unsupported long double representation +# define BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE +# define BOOST_CHARCONV_LDBL_BITS -1 +#endif + +struct IEEEbinary128 +{ +#if BOOST_CHARCONV_ENDIAN_LITTLE_BYTE + std::uint64_t mantissa_l : 64; + std::uint64_t mantissa_h : 48; + std::uint32_t exponent : 15; + std::uint32_t sign : 1; +#else // Big endian + std::uint32_t sign : 1; + std::uint32_t exponent : 15; + std::uint64_t mantissa_h : 48; + std::uint64_t mantissa_l : 64; +#endif +}; + +struct ieee754_binary128 +{ + static constexpr int significand_bits = 112; + static constexpr int exponent_bits = 15; + static constexpr int min_exponent = -16382; + static constexpr int max_exponent = 16383; + static constexpr int exponent_bias = 16383; + static constexpr int decimal_digits = 33; +}; + +} // namespace detail +} // namespace json +} // namespace boost + +#endif // BOOST_JSON_DETAIL_DRAGONBOX_BIT_LAYOUTS_HPP diff --git a/include/boost/json/detail/dragonbox/dragonbox.hpp b/include/boost/json/detail/dragonbox/dragonbox.hpp new file mode 100644 index 000000000..c1612fe95 --- /dev/null +++ b/include/boost/json/detail/dragonbox/dragonbox.hpp @@ -0,0 +1,2394 @@ +// Copyright 2020-2022 Junekey Jeon +// +// The contents of this file may be used under the terms of +// the Apache License v2.0 with LLVM Exceptions. +// +// (See accompanying file LICENSE-Apache or copy at +// https://llvm.org/foundation/relicensing/LICENSE.txt) +// +// Alternatively, the contents of this file may be used under the terms of +// the Boost Software License, Version 1.0. +// (See accompanying file LICENSE-Boost or copy at +// https://www.boost.org/LICENSE_1_0.txt) +// +// Unless required by applicable law or agreed to in writing, this software +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. +// +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_HPP +#define BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_HPP + +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_MSVC +# pragma warning(push) +# pragma warning(disable: 4127) // Conditional expression is constant (e.g. BOOST_IF_CONSTEXPR statements) +# pragma warning(disable: 4307) // Integral constant overflow (Only MSVC-14.1 issued this warning) +#endif + +namespace boost { +namespace json { +namespace detail { + +// A floating-point traits class defines ways to interpret a bit pattern of given size as an +// encoding of floating-point number. This is a default implementation of such a traits class, +// supporting ways to interpret 32-bits into a binary32-encoded floating-point number and to +// interpret 64-bits into a binary64-encoded floating-point number. Users might specialize this +// class to change the default behavior for certain types. + +template +struct dragonbox_float_traits +{ + // I don't know if there is a truly reliable way of detecting + // IEEE-754 binary32/binary64 formats; I just did my best here. + static_assert(std::numeric_limits::is_iec559 && std::numeric_limits::radix == 2 && + (physical_bits::value == 32 || physical_bits::value == 64), + "default_ieee754_traits only works for 32-bits or 64-bits types " + "supporting binary32 or binary64 formats!"); + + // The type that is being viewed. + using type = T; + + // Refers to the format specification class. + using format = typename std::conditional::value == 32, ieee754_binary32, ieee754_binary64>::type; + + // Defines an unsigned integer type that is large enough to carry a variable of type T. + // Most of the operations will be done on this integer type. + using carrier_uint = + typename std::conditional::value == 32, std::uint32_t, std::uint64_t>::type; + + static_assert(sizeof(carrier_uint) == sizeof(T), "Type T must have a unsigned type with the same number of bits"); + + // Number of bits in the above unsigned integer type. + static constexpr int carrier_bits = static_cast(physical_bits::value); + + // Convert from carrier_uint into the original type. + // Depending on the floating-point encoding format, this operation might not be possible for + // some specific bit patterns. However, the contract is that u always denotes a + // valid bit pattern, so this function must be assumed to be noexcept. + static T carrier_to_float(carrier_uint u) noexcept + { + T x; + std::memcpy(&x, &u, sizeof(carrier_uint)); + return x; + } + + // Same as above. + static carrier_uint float_to_carrier(T x) noexcept + { + carrier_uint u; + std::memcpy(&u, &x, sizeof(carrier_uint)); + return u; + } + + // Extract exponent bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. This function does not do bias adjustment. + static constexpr unsigned extract_exponent_bits(carrier_uint u) noexcept + { + return static_cast(u >> format::significand_bits) & ((static_cast(1) << format::exponent_bits) - 1); + } + + // Extract significand bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. The result does not contain the implicit bit. + static constexpr carrier_uint extract_significand_bits(carrier_uint u) noexcept + { + return carrier_uint(u & carrier_uint((carrier_uint(1) << format::significand_bits) - 1)); + } + + // Remove the exponent bits and extract significand bits together with the sign bit. + static constexpr carrier_uint remove_exponent_bits(carrier_uint u, unsigned int exponent_bits) noexcept + { + return u ^ (carrier_uint(exponent_bits) << format::significand_bits); + } + + // Shift the obtained signed significand bits to the left by 1 to remove the sign bit. + static constexpr carrier_uint remove_sign_bit_and_shift(carrier_uint u) noexcept { + return carrier_uint(carrier_uint(u) << 1); + } + + // The actual value of exponent is obtained by adding this value to the extracted exponent + // bits. + static constexpr int exponent_bias = 1 - (1 << (carrier_bits - format::significand_bits - 2)); + + // Obtain the actual value of the binary exponent from the extracted exponent bits. + static constexpr int binary_exponent(unsigned exponent_bits) noexcept + { + return static_cast(exponent_bits == 0 ? format::min_exponent : int(exponent_bits) + format::exponent_bias); + } + + // Obtain the actual value of the binary exponent from the extracted significand bits and + // exponent bits. + static constexpr carrier_uint binary_significand(carrier_uint significand_bits, unsigned exponent_bits) noexcept + { + return exponent_bits == 0 ? significand_bits : significand_bits | (carrier_uint(1) << format::significand_bits); + } + + /* Various boolean observer functions */ + + static constexpr bool is_nonzero(carrier_uint u) noexcept + { + return (u << 1) != 0; + } + + static constexpr bool is_positive(carrier_uint u) noexcept + { + return u < (carrier_uint(1) << (format::significand_bits + format::exponent_bits)); + } + + static constexpr bool is_negative(carrier_uint u) noexcept + { + return !is_positive(u); + } + + static constexpr bool is_finite(unsigned exponent_bits) noexcept + { + return exponent_bits != ((1u << format::exponent_bits) - 1); + } + + static constexpr bool has_all_zero_significand_bits(carrier_uint u) noexcept + { + return (u << 1) == 0; + } + + static constexpr bool has_even_significand_bits(carrier_uint u) noexcept + { + return u % 2 == 0; + } +}; + +// Convenient wrappers for floating-point traits classes. +// In order to reduce the argument passing overhead, these classes should be as simple as +// possible (e.g., no inheritance, no private non-static data member, etc.; this is an +// unfortunate fact about common ABI convention). + +template > +struct dragonbox_float_bits; + +template > +struct dragonbox_signed_significand_bits; + +template +struct dragonbox_float_bits +{ + using type = T; + using traits_type = Traits; + using carrier_uint = typename traits_type::carrier_uint; + + carrier_uint u; + + dragonbox_float_bits() = default; + constexpr explicit dragonbox_float_bits(carrier_uint bit_pattern) noexcept : u{bit_pattern} {} + constexpr explicit dragonbox_float_bits(T float_value) noexcept + : u{traits_type::float_to_carrier(float_value)} {} + + T to_float() const noexcept + { + return traits_type::carrier_to_float(u); + } + + // Extract exponent bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. This function does not do bias adjustment. + constexpr unsigned int extract_exponent_bits() const noexcept + { + return traits_type::extract_exponent_bits(u); + } + + // Extract significand bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. The result does not contain the implicit bit. + constexpr carrier_uint extract_significand_bits() const noexcept + { + return traits_type::extract_significand_bits(u); + } + + // Remove the exponent bits and extract significand bits together with the sign bit. + constexpr auto remove_exponent_bits(unsigned int exponent_bits) const noexcept -> dragonbox_signed_significand_bits + { + return dragonbox_signed_significand_bits(traits_type::remove_exponent_bits(u, exponent_bits)); + } + + // Obtain the actual value of the binary exponent from the extracted exponent bits. + static constexpr int binary_exponent(unsigned exponent_bits) noexcept + { + return traits_type::binary_exponent(exponent_bits); + } + + constexpr int binary_exponent() const noexcept + { + return binary_exponent(extract_exponent_bits()); + } + + // Obtain the actual value of the binary exponent from the extracted significand bits and + // exponent bits. + static constexpr carrier_uint binary_significand(carrier_uint significand_bits, unsigned exponent_bits) noexcept + { + return traits_type::binary_significand(significand_bits, exponent_bits); + } + + constexpr carrier_uint binary_significand() const noexcept + { + return binary_significand(extract_significand_bits(), extract_exponent_bits()); + } + + constexpr bool is_nonzero() const noexcept + { + return traits_type::is_nonzero(u); + } + + constexpr bool is_positive() const noexcept + { + return traits_type::is_positive(u); + } + + constexpr bool is_negative() const noexcept + { + return traits_type::is_negative(u); + } + + constexpr bool is_finite(unsigned exponent_bits) const noexcept + { + return traits_type::is_finite(exponent_bits); + } + + constexpr bool is_finite() const noexcept + { + return traits_type::is_finite(extract_exponent_bits()); + } + + constexpr bool has_even_significand_bits() const noexcept + { + return traits_type::has_even_significand_bits(u); + } +}; + +template +struct dragonbox_signed_significand_bits +{ + using type = T; + using traits_type = Traits; + using carrier_uint = typename traits_type::carrier_uint; + + carrier_uint u; + + dragonbox_signed_significand_bits() = default; + constexpr explicit dragonbox_signed_significand_bits(carrier_uint bit_pattern) noexcept + : u{bit_pattern} {} + + // Shift the obtained signed significand bits to the left by 1 to remove the sign bit. + constexpr carrier_uint remove_sign_bit_and_shift() const noexcept + { + return traits_type::remove_sign_bit_and_shift(u); + } + + constexpr bool is_positive() const noexcept + { + return traits_type::is_positive(u); + } + + constexpr bool is_negative() const noexcept + { + return traits_type::is_negative(u); + } + + constexpr bool has_all_zero_significand_bits() const noexcept + { + return traits_type::has_all_zero_significand_bits(u); + } + + constexpr bool has_even_significand_bits() const noexcept + { + return traits_type::has_even_significand_bits(u); + } +}; + + //////////////////////////////////////////////////////////////////////////////////////// + // Utilities for fast divisibility tests. + //////////////////////////////////////////////////////////////////////////////////////// + + namespace div { + // Replace n by floor(n / 10^N). + // Returns true if and only if n is divisible by 10^N. + // Precondition: n <= 10^(N+1) + // !!It takes an in-out parameter!! + template + struct divide_by_pow10_info; + + template <> + struct divide_by_pow10_info<1> + { + static constexpr std::uint32_t magic_number = 6554; + static constexpr int shift_amount = 16; + }; + + template <> + struct divide_by_pow10_info<2> + { + static constexpr std::uint32_t magic_number = 656; + static constexpr int shift_amount = 16; + }; + + template + BOOST_CXX14_CONSTEXPR bool check_divisibility_and_divide_by_pow10(std::uint32_t& n) noexcept + { + // Make sure the computation for max_n does not overflow. + // static_assert(N + 1 <= log::floor_log10_pow2(31)); + BOOST_ASSERT(n <= compute_power(UINT32_C(10), N + 1)); + + using info = divide_by_pow10_info; + n *= info::magic_number; + + constexpr auto mask = std::uint32_t(std::uint32_t(1) << info::shift_amount) - 1; + bool result = ((n & mask) < info::magic_number); + + n >>= info::shift_amount; + return result; + } + + // Compute floor(n / 10^N) for small n and N. + // Precondition: n <= 10^(N+1) + template + BOOST_CXX14_CONSTEXPR std::uint32_t small_division_by_pow10(std::uint32_t n) noexcept + { + // Make sure the computation for max_n does not overflow. + // static_assert(N + 1 <= log::floor_log10_pow2(31)); + BOOST_ASSERT(n <= compute_power(UINT32_C(10), N + 1)); + + return (n * divide_by_pow10_info::magic_number) >> divide_by_pow10_info::shift_amount; + } + + // Compute floor(n / 10^N) for small N. + // Precondition: n <= n_max + template + BOOST_CXX14_CONSTEXPR UInt divide_by_pow10(UInt n) noexcept + { + + // Specialize for 32-bit division by 100. + // Compiler is supposed to generate the identical code for just writing + // "n / 100", but for some reason MSVC generates an inefficient code + // (mul + mov for no apparent reason, instead of single imul), + // so we does this manually. + BOOST_IF_CONSTEXPR (std::is_same::value && N == 2) + { + return static_cast(umul64(static_cast(n), UINT32_C(1374389535)) >> 37); + } + // Specialize for 64-bit division by 1000. + // Ensure that the correctness condition is met. + else BOOST_IF_CONSTEXPR (std::is_same::value && N == 3 && n_max <= UINT64_C(15534100272597517998)) + { + return static_cast(umul128_upper64(n, UINT64_C(2361183241434822607)) >> 7); + } + else + { + BOOST_CXX14_CONSTEXPR auto divisor = compute_power(static_cast(10), N); + return n / divisor; + } + } + + #ifdef BOOST_MSVC + # pragma warning(push) + # pragma warning(disable: 4100) // MSVC 14.0 does not have BOOST_ATTRIBUTE_UNUSED so we disable the warning + #endif + + template + BOOST_CXX14_CONSTEXPR UInt divide_by_pow10(unsigned N, BOOST_ATTRIBUTE_UNUSED UInt n_max, UInt n) noexcept + { + BOOST_IF_CONSTEXPR (std::is_same::value && N == 2) + { + return static_cast(umul64(static_cast(n), static_cast(1374389535)) >> UINT32_C(37)); + } + // Specialize for 64-bit division by 1000. + // Ensure that the correctness condition is met. + else BOOST_IF_CONSTEXPR (std::is_same::value && N == 3 && n_max <= UINT64_C(15534100272597517998)) + { + return static_cast(umul128_upper64(n, UINT64_C(2361183241434822607)) >> 7); + } + else + { + auto divisor = compute_power(static_cast(10), N); + return n / divisor; + } + } + + #ifdef BOOST_MSVC + # pragma warning(pop) + #endif + } + +//////////////////////////////////////////////////////////////////////////////////////// +// Return types for the main interface function. +//////////////////////////////////////////////////////////////////////////////////////// + +template +struct decimal_fp; + +template +struct decimal_fp +{ + using carrier_uint = UInt; + + carrier_uint significand; + int exponent; +}; + +template +struct decimal_fp +{ + using carrier_uint = UInt; + + carrier_uint significand; + int exponent; + bool is_negative; +}; + +template +struct decimal_fp +{ + using carrier_uint = UInt; + + carrier_uint significand; + int exponent; + bool may_have_trailing_zeros; +}; + +template +struct decimal_fp +{ + using carrier_uint = UInt; + + carrier_uint significand; + int exponent; + bool is_negative; + bool may_have_trailing_zeros; +}; + +template +using unsigned_decimal_fp = decimal_fp; + +template +using signed_decimal_fp = decimal_fp; + +//////////////////////////////////////////////////////////////////////////////////////// +// Computed cache entries. +//////////////////////////////////////////////////////////////////////////////////////// + +#if (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) +template +struct cache_holder_ieee754_binary32_impl +#else +struct cache_holder_ieee754_binary32 +#endif +{ + using cache_entry_type = std::uint64_t; + static constexpr int cache_bits = 64; + static constexpr int min_k = -31; + static constexpr int max_k = 46; + static constexpr cache_entry_type cache[] = { + 0x81ceb32c4b43fcf5, 0xa2425ff75e14fc32, 0xcad2f7f5359a3b3f, 0xfd87b5f28300ca0e, + 0x9e74d1b791e07e49, 0xc612062576589ddb, 0xf79687aed3eec552, 0x9abe14cd44753b53, + 0xc16d9a0095928a28, 0xf1c90080baf72cb2, 0x971da05074da7bef, 0xbce5086492111aeb, + 0xec1e4a7db69561a6, 0x9392ee8e921d5d08, 0xb877aa3236a4b44a, 0xe69594bec44de15c, + 0x901d7cf73ab0acda, 0xb424dc35095cd810, 0xe12e13424bb40e14, 0x8cbccc096f5088cc, + 0xafebff0bcb24aaff, 0xdbe6fecebdedd5bf, 0x89705f4136b4a598, 0xabcc77118461cefd, + 0xd6bf94d5e57a42bd, 0x8637bd05af6c69b6, 0xa7c5ac471b478424, 0xd1b71758e219652c, + 0x83126e978d4fdf3c, 0xa3d70a3d70a3d70b, 0xcccccccccccccccd, 0x8000000000000000, + 0xa000000000000000, 0xc800000000000000, 0xfa00000000000000, 0x9c40000000000000, + 0xc350000000000000, 0xf424000000000000, 0x9896800000000000, 0xbebc200000000000, + 0xee6b280000000000, 0x9502f90000000000, 0xba43b74000000000, 0xe8d4a51000000000, + 0x9184e72a00000000, 0xb5e620f480000000, 0xe35fa931a0000000, 0x8e1bc9bf04000000, + 0xb1a2bc2ec5000000, 0xde0b6b3a76400000, 0x8ac7230489e80000, 0xad78ebc5ac620000, + 0xd8d726b7177a8000, 0x878678326eac9000, 0xa968163f0a57b400, 0xd3c21bcecceda100, + 0x84595161401484a0, 0xa56fa5b99019a5c8, 0xcecb8f27f4200f3a, 0x813f3978f8940985, + 0xa18f07d736b90be6, 0xc9f2c9cd04674edf, 0xfc6f7c4045812297, 0x9dc5ada82b70b59e, + 0xc5371912364ce306, 0xf684df56c3e01bc7, 0x9a130b963a6c115d, 0xc097ce7bc90715b4, + 0xf0bdc21abb48db21, 0x96769950b50d88f5, 0xbc143fa4e250eb32, 0xeb194f8e1ae525fe, + 0x92efd1b8d0cf37bf, 0xb7abc627050305ae, 0xe596b7b0c643c71a, 0x8f7e32ce7bea5c70, + 0xb35dbf821ae4f38c, 0xe0352f62a19e306f}; +}; + +#if defined(BOOST_NO_CXX17_INLINE_VARIABLES) && (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) + +template constexpr int cache_holder_ieee754_binary32_impl::cache_bits; +template constexpr int cache_holder_ieee754_binary32_impl::min_k; +template constexpr int cache_holder_ieee754_binary32_impl::max_k; +template constexpr typename cache_holder_ieee754_binary32_impl::cache_entry_type cache_holder_ieee754_binary32_impl::cache[]; + +#endif + +#if (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) +using cache_holder_ieee754_binary32 = cache_holder_ieee754_binary32_impl; +#endif + +#if (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) +template +struct cache_holder_ieee754_binary64_impl +#else +struct cache_holder_ieee754_binary64 +#endif +{ + using cache_entry_type = uint128; + static constexpr int cache_bits = 128; + static constexpr int min_k = -292; + static constexpr int max_k = 326; + static constexpr cache_entry_type cache[] = { + {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, {0x9faacf3df73609b1, 0x77b191618c54e9ad}, + {0xc795830d75038c1d, 0xd59df5b9ef6a2418}, {0xf97ae3d0d2446f25, 0x4b0573286b44ad1e}, + {0x9becce62836ac577, 0x4ee367f9430aec33}, {0xc2e801fb244576d5, 0x229c41f793cda740}, + {0xf3a20279ed56d48a, 0x6b43527578c11110}, {0x9845418c345644d6, 0x830a13896b78aaaa}, + {0xbe5691ef416bd60c, 0x23cc986bc656d554}, {0xedec366b11c6cb8f, 0x2cbfbe86b7ec8aa9}, + {0x94b3a202eb1c3f39, 0x7bf7d71432f3d6aa}, {0xb9e08a83a5e34f07, 0xdaf5ccd93fb0cc54}, + {0xe858ad248f5c22c9, 0xd1b3400f8f9cff69}, {0x91376c36d99995be, 0x23100809b9c21fa2}, + {0xb58547448ffffb2d, 0xabd40a0c2832a78b}, {0xe2e69915b3fff9f9, 0x16c90c8f323f516d}, + {0x8dd01fad907ffc3b, 0xae3da7d97f6792e4}, {0xb1442798f49ffb4a, 0x99cd11cfdf41779d}, + {0xdd95317f31c7fa1d, 0x40405643d711d584}, {0x8a7d3eef7f1cfc52, 0x482835ea666b2573}, + {0xad1c8eab5ee43b66, 0xda3243650005eed0}, {0xd863b256369d4a40, 0x90bed43e40076a83}, + {0x873e4f75e2224e68, 0x5a7744a6e804a292}, {0xa90de3535aaae202, 0x711515d0a205cb37}, + {0xd3515c2831559a83, 0x0d5a5b44ca873e04}, {0x8412d9991ed58091, 0xe858790afe9486c3}, + {0xa5178fff668ae0b6, 0x626e974dbe39a873}, {0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, + {0x80fa687f881c7f8e, 0x7ce66634bc9d0b9a}, {0xa139029f6a239f72, 0x1c1fffc1ebc44e81}, + {0xc987434744ac874e, 0xa327ffb266b56221}, {0xfbe9141915d7a922, 0x4bf1ff9f0062baa9}, + {0x9d71ac8fada6c9b5, 0x6f773fc3603db4aa}, {0xc4ce17b399107c22, 0xcb550fb4384d21d4}, + {0xf6019da07f549b2b, 0x7e2a53a146606a49}, {0x99c102844f94e0fb, 0x2eda7444cbfc426e}, + {0xc0314325637a1939, 0xfa911155fefb5309}, {0xf03d93eebc589f88, 0x793555ab7eba27cb}, + {0x96267c7535b763b5, 0x4bc1558b2f3458df}, {0xbbb01b9283253ca2, 0x9eb1aaedfb016f17}, + {0xea9c227723ee8bcb, 0x465e15a979c1cadd}, {0x92a1958a7675175f, 0x0bfacd89ec191eca}, + {0xb749faed14125d36, 0xcef980ec671f667c}, {0xe51c79a85916f484, 0x82b7e12780e7401b}, + {0x8f31cc0937ae58d2, 0xd1b2ecb8b0908811}, {0xb2fe3f0b8599ef07, 0x861fa7e6dcb4aa16}, + {0xdfbdcece67006ac9, 0x67a791e093e1d49b}, {0x8bd6a141006042bd, 0xe0c8bb2c5c6d24e1}, + {0xaecc49914078536d, 0x58fae9f773886e19}, {0xda7f5bf590966848, 0xaf39a475506a899f}, + {0x888f99797a5e012d, 0x6d8406c952429604}, {0xaab37fd7d8f58178, 0xc8e5087ba6d33b84}, + {0xd5605fcdcf32e1d6, 0xfb1e4a9a90880a65}, {0x855c3be0a17fcd26, 0x5cf2eea09a550680}, + {0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481f}, {0xd0601d8efc57b08b, 0xf13b94daf124da27}, + {0x823c12795db6ce57, 0x76c53d08d6b70859}, {0xa2cb1717b52481ed, 0x54768c4b0c64ca6f}, + {0xcb7ddcdda26da268, 0xa9942f5dcf7dfd0a}, {0xfe5d54150b090b02, 0xd3f93b35435d7c4d}, + {0x9efa548d26e5a6e1, 0xc47bc5014a1a6db0}, {0xc6b8e9b0709f109a, 0x359ab6419ca1091c}, + {0xf867241c8cc6d4c0, 0xc30163d203c94b63}, {0x9b407691d7fc44f8, 0x79e0de63425dcf1e}, + {0xc21094364dfb5636, 0x985915fc12f542e5}, {0xf294b943e17a2bc4, 0x3e6f5b7b17b2939e}, + {0x979cf3ca6cec5b5a, 0xa705992ceecf9c43}, {0xbd8430bd08277231, 0x50c6ff782a838354}, + {0xece53cec4a314ebd, 0xa4f8bf5635246429}, {0x940f4613ae5ed136, 0x871b7795e136be9a}, + {0xb913179899f68584, 0x28e2557b59846e40}, {0xe757dd7ec07426e5, 0x331aeada2fe589d0}, + {0x9096ea6f3848984f, 0x3ff0d2c85def7622}, {0xb4bca50b065abe63, 0x0fed077a756b53aa}, + {0xe1ebce4dc7f16dfb, 0xd3e8495912c62895}, {0x8d3360f09cf6e4bd, 0x64712dd7abbbd95d}, + {0xb080392cc4349dec, 0xbd8d794d96aacfb4}, {0xdca04777f541c567, 0xecf0d7a0fc5583a1}, + {0x89e42caaf9491b60, 0xf41686c49db57245}, {0xac5d37d5b79b6239, 0x311c2875c522ced6}, + {0xd77485cb25823ac7, 0x7d633293366b828c}, {0x86a8d39ef77164bc, 0xae5dff9c02033198}, + {0xa8530886b54dbdeb, 0xd9f57f830283fdfd}, {0xd267caa862a12d66, 0xd072df63c324fd7c}, + {0x8380dea93da4bc60, 0x4247cb9e59f71e6e}, {0xa46116538d0deb78, 0x52d9be85f074e609}, + {0xcd795be870516656, 0x67902e276c921f8c}, {0x806bd9714632dff6, 0x00ba1cd8a3db53b7}, + {0xa086cfcd97bf97f3, 0x80e8a40eccd228a5}, {0xc8a883c0fdaf7df0, 0x6122cd128006b2ce}, + {0xfad2a4b13d1b5d6c, 0x796b805720085f82}, {0x9cc3a6eec6311a63, 0xcbe3303674053bb1}, + {0xc3f490aa77bd60fc, 0xbedbfc4411068a9d}, {0xf4f1b4d515acb93b, 0xee92fb5515482d45}, + {0x991711052d8bf3c5, 0x751bdd152d4d1c4b}, {0xbf5cd54678eef0b6, 0xd262d45a78a0635e}, + {0xef340a98172aace4, 0x86fb897116c87c35}, {0x9580869f0e7aac0e, 0xd45d35e6ae3d4da1}, + {0xbae0a846d2195712, 0x8974836059cca10a}, {0xe998d258869facd7, 0x2bd1a438703fc94c}, + {0x91ff83775423cc06, 0x7b6306a34627ddd0}, {0xb67f6455292cbf08, 0x1a3bc84c17b1d543}, + {0xe41f3d6a7377eeca, 0x20caba5f1d9e4a94}, {0x8e938662882af53e, 0x547eb47b7282ee9d}, + {0xb23867fb2a35b28d, 0xe99e619a4f23aa44}, {0xdec681f9f4c31f31, 0x6405fa00e2ec94d5}, + {0x8b3c113c38f9f37e, 0xde83bc408dd3dd05}, {0xae0b158b4738705e, 0x9624ab50b148d446}, + {0xd98ddaee19068c76, 0x3badd624dd9b0958}, {0x87f8a8d4cfa417c9, 0xe54ca5d70a80e5d7}, + {0xa9f6d30a038d1dbc, 0x5e9fcf4ccd211f4d}, {0xd47487cc8470652b, 0x7647c32000696720}, + {0x84c8d4dfd2c63f3b, 0x29ecd9f40041e074}, {0xa5fb0a17c777cf09, 0xf468107100525891}, + {0xcf79cc9db955c2cc, 0x7182148d4066eeb5}, {0x81ac1fe293d599bf, 0xc6f14cd848405531}, + {0xa21727db38cb002f, 0xb8ada00e5a506a7d}, {0xca9cf1d206fdc03b, 0xa6d90811f0e4851d}, + {0xfd442e4688bd304a, 0x908f4a166d1da664}, {0x9e4a9cec15763e2e, 0x9a598e4e043287ff}, + {0xc5dd44271ad3cdba, 0x40eff1e1853f29fe}, {0xf7549530e188c128, 0xd12bee59e68ef47d}, + {0x9a94dd3e8cf578b9, 0x82bb74f8301958cf}, {0xc13a148e3032d6e7, 0xe36a52363c1faf02}, + {0xf18899b1bc3f8ca1, 0xdc44e6c3cb279ac2}, {0x96f5600f15a7b7e5, 0x29ab103a5ef8c0ba}, + {0xbcb2b812db11a5de, 0x7415d448f6b6f0e8}, {0xebdf661791d60f56, 0x111b495b3464ad22}, + {0x936b9fcebb25c995, 0xcab10dd900beec35}, {0xb84687c269ef3bfb, 0x3d5d514f40eea743}, + {0xe65829b3046b0afa, 0x0cb4a5a3112a5113}, {0x8ff71a0fe2c2e6dc, 0x47f0e785eaba72ac}, + {0xb3f4e093db73a093, 0x59ed216765690f57}, {0xe0f218b8d25088b8, 0x306869c13ec3532d}, + {0x8c974f7383725573, 0x1e414218c73a13fc}, {0xafbd2350644eeacf, 0xe5d1929ef90898fb}, + {0xdbac6c247d62a583, 0xdf45f746b74abf3a}, {0x894bc396ce5da772, 0x6b8bba8c328eb784}, + {0xab9eb47c81f5114f, 0x066ea92f3f326565}, {0xd686619ba27255a2, 0xc80a537b0efefebe}, + {0x8613fd0145877585, 0xbd06742ce95f5f37}, {0xa798fc4196e952e7, 0x2c48113823b73705}, + {0xd17f3b51fca3a7a0, 0xf75a15862ca504c6}, {0x82ef85133de648c4, 0x9a984d73dbe722fc}, + {0xa3ab66580d5fdaf5, 0xc13e60d0d2e0ebbb}, {0xcc963fee10b7d1b3, 0x318df905079926a9}, + {0xffbbcfe994e5c61f, 0xfdf17746497f7053}, {0x9fd561f1fd0f9bd3, 0xfeb6ea8bedefa634}, + {0xc7caba6e7c5382c8, 0xfe64a52ee96b8fc1}, {0xf9bd690a1b68637b, 0x3dfdce7aa3c673b1}, + {0x9c1661a651213e2d, 0x06bea10ca65c084f}, {0xc31bfa0fe5698db8, 0x486e494fcff30a63}, + {0xf3e2f893dec3f126, 0x5a89dba3c3efccfb}, {0x986ddb5c6b3a76b7, 0xf89629465a75e01d}, + {0xbe89523386091465, 0xf6bbb397f1135824}, {0xee2ba6c0678b597f, 0x746aa07ded582e2d}, + {0x94db483840b717ef, 0xa8c2a44eb4571cdd}, {0xba121a4650e4ddeb, 0x92f34d62616ce414}, + {0xe896a0d7e51e1566, 0x77b020baf9c81d18}, {0x915e2486ef32cd60, 0x0ace1474dc1d122f}, + {0xb5b5ada8aaff80b8, 0x0d819992132456bb}, {0xe3231912d5bf60e6, 0x10e1fff697ed6c6a}, + {0x8df5efabc5979c8f, 0xca8d3ffa1ef463c2}, {0xb1736b96b6fd83b3, 0xbd308ff8a6b17cb3}, + {0xddd0467c64bce4a0, 0xac7cb3f6d05ddbdf}, {0x8aa22c0dbef60ee4, 0x6bcdf07a423aa96c}, + {0xad4ab7112eb3929d, 0x86c16c98d2c953c7}, {0xd89d64d57a607744, 0xe871c7bf077ba8b8}, + {0x87625f056c7c4a8b, 0x11471cd764ad4973}, {0xa93af6c6c79b5d2d, 0xd598e40d3dd89bd0}, + {0xd389b47879823479, 0x4aff1d108d4ec2c4}, {0x843610cb4bf160cb, 0xcedf722a585139bb}, + {0xa54394fe1eedb8fe, 0xc2974eb4ee658829}, {0xce947a3da6a9273e, 0x733d226229feea33}, + {0x811ccc668829b887, 0x0806357d5a3f5260}, {0xa163ff802a3426a8, 0xca07c2dcb0cf26f8}, + {0xc9bcff6034c13052, 0xfc89b393dd02f0b6}, {0xfc2c3f3841f17c67, 0xbbac2078d443ace3}, + {0x9d9ba7832936edc0, 0xd54b944b84aa4c0e}, {0xc5029163f384a931, 0x0a9e795e65d4df12}, + {0xf64335bcf065d37d, 0x4d4617b5ff4a16d6}, {0x99ea0196163fa42e, 0x504bced1bf8e4e46}, + {0xc06481fb9bcf8d39, 0xe45ec2862f71e1d7}, {0xf07da27a82c37088, 0x5d767327bb4e5a4d}, + {0x964e858c91ba2655, 0x3a6a07f8d510f870}, {0xbbe226efb628afea, 0x890489f70a55368c}, + {0xeadab0aba3b2dbe5, 0x2b45ac74ccea842f}, {0x92c8ae6b464fc96f, 0x3b0b8bc90012929e}, + {0xb77ada0617e3bbcb, 0x09ce6ebb40173745}, {0xe55990879ddcaabd, 0xcc420a6a101d0516}, + {0x8f57fa54c2a9eab6, 0x9fa946824a12232e}, {0xb32df8e9f3546564, 0x47939822dc96abfa}, + {0xdff9772470297ebd, 0x59787e2b93bc56f8}, {0x8bfbea76c619ef36, 0x57eb4edb3c55b65b}, + {0xaefae51477a06b03, 0xede622920b6b23f2}, {0xdab99e59958885c4, 0xe95fab368e45ecee}, + {0x88b402f7fd75539b, 0x11dbcb0218ebb415}, {0xaae103b5fcd2a881, 0xd652bdc29f26a11a}, + {0xd59944a37c0752a2, 0x4be76d3346f04960}, {0x857fcae62d8493a5, 0x6f70a4400c562ddc}, + {0xa6dfbd9fb8e5b88e, 0xcb4ccd500f6bb953}, {0xd097ad07a71f26b2, 0x7e2000a41346a7a8}, + {0x825ecc24c873782f, 0x8ed400668c0c28c9}, {0xa2f67f2dfa90563b, 0x728900802f0f32fb}, + {0xcbb41ef979346bca, 0x4f2b40a03ad2ffba}, {0xfea126b7d78186bc, 0xe2f610c84987bfa9}, + {0x9f24b832e6b0f436, 0x0dd9ca7d2df4d7ca}, {0xc6ede63fa05d3143, 0x91503d1c79720dbc}, + {0xf8a95fcf88747d94, 0x75a44c6397ce912b}, {0x9b69dbe1b548ce7c, 0xc986afbe3ee11abb}, + {0xc24452da229b021b, 0xfbe85badce996169}, {0xf2d56790ab41c2a2, 0xfae27299423fb9c4}, + {0x97c560ba6b0919a5, 0xdccd879fc967d41b}, {0xbdb6b8e905cb600f, 0x5400e987bbc1c921}, + {0xed246723473e3813, 0x290123e9aab23b69}, {0x9436c0760c86e30b, 0xf9a0b6720aaf6522}, + {0xb94470938fa89bce, 0xf808e40e8d5b3e6a}, {0xe7958cb87392c2c2, 0xb60b1d1230b20e05}, + {0x90bd77f3483bb9b9, 0xb1c6f22b5e6f48c3}, {0xb4ecd5f01a4aa828, 0x1e38aeb6360b1af4}, + {0xe2280b6c20dd5232, 0x25c6da63c38de1b1}, {0x8d590723948a535f, 0x579c487e5a38ad0f}, + {0xb0af48ec79ace837, 0x2d835a9df0c6d852}, {0xdcdb1b2798182244, 0xf8e431456cf88e66}, + {0x8a08f0f8bf0f156b, 0x1b8e9ecb641b5900}, {0xac8b2d36eed2dac5, 0xe272467e3d222f40}, + {0xd7adf884aa879177, 0x5b0ed81dcc6abb10}, {0x86ccbb52ea94baea, 0x98e947129fc2b4ea}, + {0xa87fea27a539e9a5, 0x3f2398d747b36225}, {0xd29fe4b18e88640e, 0x8eec7f0d19a03aae}, + {0x83a3eeeef9153e89, 0x1953cf68300424ad}, {0xa48ceaaab75a8e2b, 0x5fa8c3423c052dd8}, + {0xcdb02555653131b6, 0x3792f412cb06794e}, {0x808e17555f3ebf11, 0xe2bbd88bbee40bd1}, + {0xa0b19d2ab70e6ed6, 0x5b6aceaeae9d0ec5}, {0xc8de047564d20a8b, 0xf245825a5a445276}, + {0xfb158592be068d2e, 0xeed6e2f0f0d56713}, {0x9ced737bb6c4183d, 0x55464dd69685606c}, + {0xc428d05aa4751e4c, 0xaa97e14c3c26b887}, {0xf53304714d9265df, 0xd53dd99f4b3066a9}, + {0x993fe2c6d07b7fab, 0xe546a8038efe402a}, {0xbf8fdb78849a5f96, 0xde98520472bdd034}, + {0xef73d256a5c0f77c, 0x963e66858f6d4441}, {0x95a8637627989aad, 0xdde7001379a44aa9}, + {0xbb127c53b17ec159, 0x5560c018580d5d53}, {0xe9d71b689dde71af, 0xaab8f01e6e10b4a7}, + {0x9226712162ab070d, 0xcab3961304ca70e9}, {0xb6b00d69bb55c8d1, 0x3d607b97c5fd0d23}, + {0xe45c10c42a2b3b05, 0x8cb89a7db77c506b}, {0x8eb98a7a9a5b04e3, 0x77f3608e92adb243}, + {0xb267ed1940f1c61c, 0x55f038b237591ed4}, {0xdf01e85f912e37a3, 0x6b6c46dec52f6689}, + {0x8b61313bbabce2c6, 0x2323ac4b3b3da016}, {0xae397d8aa96c1b77, 0xabec975e0a0d081b}, + {0xd9c7dced53c72255, 0x96e7bd358c904a22}, {0x881cea14545c7575, 0x7e50d64177da2e55}, + {0xaa242499697392d2, 0xdde50bd1d5d0b9ea}, {0xd4ad2dbfc3d07787, 0x955e4ec64b44e865}, + {0x84ec3c97da624ab4, 0xbd5af13bef0b113f}, {0xa6274bbdd0fadd61, 0xecb1ad8aeacdd58f}, + {0xcfb11ead453994ba, 0x67de18eda5814af3}, {0x81ceb32c4b43fcf4, 0x80eacf948770ced8}, + {0xa2425ff75e14fc31, 0xa1258379a94d028e}, {0xcad2f7f5359a3b3e, 0x096ee45813a04331}, + {0xfd87b5f28300ca0d, 0x8bca9d6e188853fd}, {0x9e74d1b791e07e48, 0x775ea264cf55347e}, + {0xc612062576589dda, 0x95364afe032a819e}, {0xf79687aed3eec551, 0x3a83ddbd83f52205}, + {0x9abe14cd44753b52, 0xc4926a9672793543}, {0xc16d9a0095928a27, 0x75b7053c0f178294}, + {0xf1c90080baf72cb1, 0x5324c68b12dd6339}, {0x971da05074da7bee, 0xd3f6fc16ebca5e04}, + {0xbce5086492111aea, 0x88f4bb1ca6bcf585}, {0xec1e4a7db69561a5, 0x2b31e9e3d06c32e6}, + {0x9392ee8e921d5d07, 0x3aff322e62439fd0}, {0xb877aa3236a4b449, 0x09befeb9fad487c3}, + {0xe69594bec44de15b, 0x4c2ebe687989a9b4}, {0x901d7cf73ab0acd9, 0x0f9d37014bf60a11}, + {0xb424dc35095cd80f, 0x538484c19ef38c95}, {0xe12e13424bb40e13, 0x2865a5f206b06fba}, + {0x8cbccc096f5088cb, 0xf93f87b7442e45d4}, {0xafebff0bcb24aafe, 0xf78f69a51539d749}, + {0xdbe6fecebdedd5be, 0xb573440e5a884d1c}, {0x89705f4136b4a597, 0x31680a88f8953031}, + {0xabcc77118461cefc, 0xfdc20d2b36ba7c3e}, {0xd6bf94d5e57a42bc, 0x3d32907604691b4d}, + {0x8637bd05af6c69b5, 0xa63f9a49c2c1b110}, {0xa7c5ac471b478423, 0x0fcf80dc33721d54}, + {0xd1b71758e219652b, 0xd3c36113404ea4a9}, {0x83126e978d4fdf3b, 0x645a1cac083126ea}, + {0xa3d70a3d70a3d70a, 0x3d70a3d70a3d70a4}, {0xcccccccccccccccc, 0xcccccccccccccccd}, + {0x8000000000000000, 0x0000000000000000}, {0xa000000000000000, 0x0000000000000000}, + {0xc800000000000000, 0x0000000000000000}, {0xfa00000000000000, 0x0000000000000000}, + {0x9c40000000000000, 0x0000000000000000}, {0xc350000000000000, 0x0000000000000000}, + {0xf424000000000000, 0x0000000000000000}, {0x9896800000000000, 0x0000000000000000}, + {0xbebc200000000000, 0x0000000000000000}, {0xee6b280000000000, 0x0000000000000000}, + {0x9502f90000000000, 0x0000000000000000}, {0xba43b74000000000, 0x0000000000000000}, + {0xe8d4a51000000000, 0x0000000000000000}, {0x9184e72a00000000, 0x0000000000000000}, + {0xb5e620f480000000, 0x0000000000000000}, {0xe35fa931a0000000, 0x0000000000000000}, + {0x8e1bc9bf04000000, 0x0000000000000000}, {0xb1a2bc2ec5000000, 0x0000000000000000}, + {0xde0b6b3a76400000, 0x0000000000000000}, {0x8ac7230489e80000, 0x0000000000000000}, + {0xad78ebc5ac620000, 0x0000000000000000}, {0xd8d726b7177a8000, 0x0000000000000000}, + {0x878678326eac9000, 0x0000000000000000}, {0xa968163f0a57b400, 0x0000000000000000}, + {0xd3c21bcecceda100, 0x0000000000000000}, {0x84595161401484a0, 0x0000000000000000}, + {0xa56fa5b99019a5c8, 0x0000000000000000}, {0xcecb8f27f4200f3a, 0x0000000000000000}, + {0x813f3978f8940984, 0x4000000000000000}, {0xa18f07d736b90be5, 0x5000000000000000}, + {0xc9f2c9cd04674ede, 0xa400000000000000}, {0xfc6f7c4045812296, 0x4d00000000000000}, + {0x9dc5ada82b70b59d, 0xf020000000000000}, {0xc5371912364ce305, 0x6c28000000000000}, + {0xf684df56c3e01bc6, 0xc732000000000000}, {0x9a130b963a6c115c, 0x3c7f400000000000}, + {0xc097ce7bc90715b3, 0x4b9f100000000000}, {0xf0bdc21abb48db20, 0x1e86d40000000000}, + {0x96769950b50d88f4, 0x1314448000000000}, {0xbc143fa4e250eb31, 0x17d955a000000000}, + {0xeb194f8e1ae525fd, 0x5dcfab0800000000}, {0x92efd1b8d0cf37be, 0x5aa1cae500000000}, + {0xb7abc627050305ad, 0xf14a3d9e40000000}, {0xe596b7b0c643c719, 0x6d9ccd05d0000000}, + {0x8f7e32ce7bea5c6f, 0xe4820023a2000000}, {0xb35dbf821ae4f38b, 0xdda2802c8a800000}, + {0xe0352f62a19e306e, 0xd50b2037ad200000}, {0x8c213d9da502de45, 0x4526f422cc340000}, + {0xaf298d050e4395d6, 0x9670b12b7f410000}, {0xdaf3f04651d47b4c, 0x3c0cdd765f114000}, + {0x88d8762bf324cd0f, 0xa5880a69fb6ac800}, {0xab0e93b6efee0053, 0x8eea0d047a457a00}, + {0xd5d238a4abe98068, 0x72a4904598d6d880}, {0x85a36366eb71f041, 0x47a6da2b7f864750}, + {0xa70c3c40a64e6c51, 0x999090b65f67d924}, {0xd0cf4b50cfe20765, 0xfff4b4e3f741cf6d}, + {0x82818f1281ed449f, 0xbff8f10e7a8921a5}, {0xa321f2d7226895c7, 0xaff72d52192b6a0e}, + {0xcbea6f8ceb02bb39, 0x9bf4f8a69f764491}, {0xfee50b7025c36a08, 0x02f236d04753d5b5}, + {0x9f4f2726179a2245, 0x01d762422c946591}, {0xc722f0ef9d80aad6, 0x424d3ad2b7b97ef6}, + {0xf8ebad2b84e0d58b, 0xd2e0898765a7deb3}, {0x9b934c3b330c8577, 0x63cc55f49f88eb30}, + {0xc2781f49ffcfa6d5, 0x3cbf6b71c76b25fc}, {0xf316271c7fc3908a, 0x8bef464e3945ef7b}, + {0x97edd871cfda3a56, 0x97758bf0e3cbb5ad}, {0xbde94e8e43d0c8ec, 0x3d52eeed1cbea318}, + {0xed63a231d4c4fb27, 0x4ca7aaa863ee4bde}, {0x945e455f24fb1cf8, 0x8fe8caa93e74ef6b}, + {0xb975d6b6ee39e436, 0xb3e2fd538e122b45}, {0xe7d34c64a9c85d44, 0x60dbbca87196b617}, + {0x90e40fbeea1d3a4a, 0xbc8955e946fe31ce}, {0xb51d13aea4a488dd, 0x6babab6398bdbe42}, + {0xe264589a4dcdab14, 0xc696963c7eed2dd2}, {0x8d7eb76070a08aec, 0xfc1e1de5cf543ca3}, + {0xb0de65388cc8ada8, 0x3b25a55f43294bcc}, {0xdd15fe86affad912, 0x49ef0eb713f39ebf}, + {0x8a2dbf142dfcc7ab, 0x6e3569326c784338}, {0xacb92ed9397bf996, 0x49c2c37f07965405}, + {0xd7e77a8f87daf7fb, 0xdc33745ec97be907}, {0x86f0ac99b4e8dafd, 0x69a028bb3ded71a4}, + {0xa8acd7c0222311bc, 0xc40832ea0d68ce0d}, {0xd2d80db02aabd62b, 0xf50a3fa490c30191}, + {0x83c7088e1aab65db, 0x792667c6da79e0fb}, {0xa4b8cab1a1563f52, 0x577001b891185939}, + {0xcde6fd5e09abcf26, 0xed4c0226b55e6f87}, {0x80b05e5ac60b6178, 0x544f8158315b05b5}, + {0xa0dc75f1778e39d6, 0x696361ae3db1c722}, {0xc913936dd571c84c, 0x03bc3a19cd1e38ea}, + {0xfb5878494ace3a5f, 0x04ab48a04065c724}, {0x9d174b2dcec0e47b, 0x62eb0d64283f9c77}, + {0xc45d1df942711d9a, 0x3ba5d0bd324f8395}, {0xf5746577930d6500, 0xca8f44ec7ee3647a}, + {0x9968bf6abbe85f20, 0x7e998b13cf4e1ecc}, {0xbfc2ef456ae276e8, 0x9e3fedd8c321a67f}, + {0xefb3ab16c59b14a2, 0xc5cfe94ef3ea101f}, {0x95d04aee3b80ece5, 0xbba1f1d158724a13}, + {0xbb445da9ca61281f, 0x2a8a6e45ae8edc98}, {0xea1575143cf97226, 0xf52d09d71a3293be}, + {0x924d692ca61be758, 0x593c2626705f9c57}, {0xb6e0c377cfa2e12e, 0x6f8b2fb00c77836d}, + {0xe498f455c38b997a, 0x0b6dfb9c0f956448}, {0x8edf98b59a373fec, 0x4724bd4189bd5ead}, + {0xb2977ee300c50fe7, 0x58edec91ec2cb658}, {0xdf3d5e9bc0f653e1, 0x2f2967b66737e3ee}, + {0x8b865b215899f46c, 0xbd79e0d20082ee75}, {0xae67f1e9aec07187, 0xecd8590680a3aa12}, + {0xda01ee641a708de9, 0xe80e6f4820cc9496}, {0x884134fe908658b2, 0x3109058d147fdcde}, + {0xaa51823e34a7eede, 0xbd4b46f0599fd416}, {0xd4e5e2cdc1d1ea96, 0x6c9e18ac7007c91b}, + {0x850fadc09923329e, 0x03e2cf6bc604ddb1}, {0xa6539930bf6bff45, 0x84db8346b786151d}, + {0xcfe87f7cef46ff16, 0xe612641865679a64}, {0x81f14fae158c5f6e, 0x4fcb7e8f3f60c07f}, + {0xa26da3999aef7749, 0xe3be5e330f38f09e}, {0xcb090c8001ab551c, 0x5cadf5bfd3072cc6}, + {0xfdcb4fa002162a63, 0x73d9732fc7c8f7f7}, {0x9e9f11c4014dda7e, 0x2867e7fddcdd9afb}, + {0xc646d63501a1511d, 0xb281e1fd541501b9}, {0xf7d88bc24209a565, 0x1f225a7ca91a4227}, + {0x9ae757596946075f, 0x3375788de9b06959}, {0xc1a12d2fc3978937, 0x0052d6b1641c83af}, + {0xf209787bb47d6b84, 0xc0678c5dbd23a49b}, {0x9745eb4d50ce6332, 0xf840b7ba963646e1}, + {0xbd176620a501fbff, 0xb650e5a93bc3d899}, {0xec5d3fa8ce427aff, 0xa3e51f138ab4cebf}, + {0x93ba47c980e98cdf, 0xc66f336c36b10138}, {0xb8a8d9bbe123f017, 0xb80b0047445d4185}, + {0xe6d3102ad96cec1d, 0xa60dc059157491e6}, {0x9043ea1ac7e41392, 0x87c89837ad68db30}, + {0xb454e4a179dd1877, 0x29babe4598c311fc}, {0xe16a1dc9d8545e94, 0xf4296dd6fef3d67b}, + {0x8ce2529e2734bb1d, 0x1899e4a65f58660d}, {0xb01ae745b101e9e4, 0x5ec05dcff72e7f90}, + {0xdc21a1171d42645d, 0x76707543f4fa1f74}, {0x899504ae72497eba, 0x6a06494a791c53a9}, + {0xabfa45da0edbde69, 0x0487db9d17636893}, {0xd6f8d7509292d603, 0x45a9d2845d3c42b7}, + {0x865b86925b9bc5c2, 0x0b8a2392ba45a9b3}, {0xa7f26836f282b732, 0x8e6cac7768d7141f}, + {0xd1ef0244af2364ff, 0x3207d795430cd927}, {0x8335616aed761f1f, 0x7f44e6bd49e807b9}, + {0xa402b9c5a8d3a6e7, 0x5f16206c9c6209a7}, {0xcd036837130890a1, 0x36dba887c37a8c10}, + {0x802221226be55a64, 0xc2494954da2c978a}, {0xa02aa96b06deb0fd, 0xf2db9baa10b7bd6d}, + {0xc83553c5c8965d3d, 0x6f92829494e5acc8}, {0xfa42a8b73abbf48c, 0xcb772339ba1f17fa}, + {0x9c69a97284b578d7, 0xff2a760414536efc}, {0xc38413cf25e2d70d, 0xfef5138519684abb}, + {0xf46518c2ef5b8cd1, 0x7eb258665fc25d6a}, {0x98bf2f79d5993802, 0xef2f773ffbd97a62}, + {0xbeeefb584aff8603, 0xaafb550ffacfd8fb}, {0xeeaaba2e5dbf6784, 0x95ba2a53f983cf39}, + {0x952ab45cfa97a0b2, 0xdd945a747bf26184}, {0xba756174393d88df, 0x94f971119aeef9e5}, + {0xe912b9d1478ceb17, 0x7a37cd5601aab85e}, {0x91abb422ccb812ee, 0xac62e055c10ab33b}, + {0xb616a12b7fe617aa, 0x577b986b314d600a}, {0xe39c49765fdf9d94, 0xed5a7e85fda0b80c}, + {0x8e41ade9fbebc27d, 0x14588f13be847308}, {0xb1d219647ae6b31c, 0x596eb2d8ae258fc9}, + {0xde469fbd99a05fe3, 0x6fca5f8ed9aef3bc}, {0x8aec23d680043bee, 0x25de7bb9480d5855}, + {0xada72ccc20054ae9, 0xaf561aa79a10ae6b}, {0xd910f7ff28069da4, 0x1b2ba1518094da05}, + {0x87aa9aff79042286, 0x90fb44d2f05d0843}, {0xa99541bf57452b28, 0x353a1607ac744a54}, + {0xd3fa922f2d1675f2, 0x42889b8997915ce9}, {0x847c9b5d7c2e09b7, 0x69956135febada12}, + {0xa59bc234db398c25, 0x43fab9837e699096}, {0xcf02b2c21207ef2e, 0x94f967e45e03f4bc}, + {0x8161afb94b44f57d, 0x1d1be0eebac278f6}, {0xa1ba1ba79e1632dc, 0x6462d92a69731733}, + {0xca28a291859bbf93, 0x7d7b8f7503cfdcff}, {0xfcb2cb35e702af78, 0x5cda735244c3d43f}, + {0x9defbf01b061adab, 0x3a0888136afa64a8}, {0xc56baec21c7a1916, 0x088aaa1845b8fdd1}, + {0xf6c69a72a3989f5b, 0x8aad549e57273d46}, {0x9a3c2087a63f6399, 0x36ac54e2f678864c}, + {0xc0cb28a98fcf3c7f, 0x84576a1bb416a7de}, {0xf0fdf2d3f3c30b9f, 0x656d44a2a11c51d6}, + {0x969eb7c47859e743, 0x9f644ae5a4b1b326}, {0xbc4665b596706114, 0x873d5d9f0dde1fef}, + {0xeb57ff22fc0c7959, 0xa90cb506d155a7eb}, {0x9316ff75dd87cbd8, 0x09a7f12442d588f3}, + {0xb7dcbf5354e9bece, 0x0c11ed6d538aeb30}, {0xe5d3ef282a242e81, 0x8f1668c8a86da5fb}, + {0x8fa475791a569d10, 0xf96e017d694487bd}, {0xb38d92d760ec4455, 0x37c981dcc395a9ad}, + {0xe070f78d3927556a, 0x85bbe253f47b1418}, {0x8c469ab843b89562, 0x93956d7478ccec8f}, + {0xaf58416654a6babb, 0x387ac8d1970027b3}, {0xdb2e51bfe9d0696a, 0x06997b05fcc0319f}, + {0x88fcf317f22241e2, 0x441fece3bdf81f04}, {0xab3c2fddeeaad25a, 0xd527e81cad7626c4}, + {0xd60b3bd56a5586f1, 0x8a71e223d8d3b075}, {0x85c7056562757456, 0xf6872d5667844e4a}, + {0xa738c6bebb12d16c, 0xb428f8ac016561dc}, {0xd106f86e69d785c7, 0xe13336d701beba53}, + {0x82a45b450226b39c, 0xecc0024661173474}, {0xa34d721642b06084, 0x27f002d7f95d0191}, + {0xcc20ce9bd35c78a5, 0x31ec038df7b441f5}, {0xff290242c83396ce, 0x7e67047175a15272}, + {0x9f79a169bd203e41, 0x0f0062c6e984d387}, {0xc75809c42c684dd1, 0x52c07b78a3e60869}, + {0xf92e0c3537826145, 0xa7709a56ccdf8a83}, {0x9bbcc7a142b17ccb, 0x88a66076400bb692}, + {0xc2abf989935ddbfe, 0x6acff893d00ea436}, {0xf356f7ebf83552fe, 0x0583f6b8c4124d44}, + {0x98165af37b2153de, 0xc3727a337a8b704b}, {0xbe1bf1b059e9a8d6, 0x744f18c0592e4c5d}, + {0xeda2ee1c7064130c, 0x1162def06f79df74}, {0x9485d4d1c63e8be7, 0x8addcb5645ac2ba9}, + {0xb9a74a0637ce2ee1, 0x6d953e2bd7173693}, {0xe8111c87c5c1ba99, 0xc8fa8db6ccdd0438}, + {0x910ab1d4db9914a0, 0x1d9c9892400a22a3}, {0xb54d5e4a127f59c8, 0x2503beb6d00cab4c}, + {0xe2a0b5dc971f303a, 0x2e44ae64840fd61e}, {0x8da471a9de737e24, 0x5ceaecfed289e5d3}, + {0xb10d8e1456105dad, 0x7425a83e872c5f48}, {0xdd50f1996b947518, 0xd12f124e28f7771a}, + {0x8a5296ffe33cc92f, 0x82bd6b70d99aaa70}, {0xace73cbfdc0bfb7b, 0x636cc64d1001550c}, + {0xd8210befd30efa5a, 0x3c47f7e05401aa4f}, {0x8714a775e3e95c78, 0x65acfaec34810a72}, + {0xa8d9d1535ce3b396, 0x7f1839a741a14d0e}, {0xd31045a8341ca07c, 0x1ede48111209a051}, + {0x83ea2b892091e44d, 0x934aed0aab460433}, {0xa4e4b66b68b65d60, 0xf81da84d56178540}, + {0xce1de40642e3f4b9, 0x36251260ab9d668f}, {0x80d2ae83e9ce78f3, 0xc1d72b7c6b42601a}, + {0xa1075a24e4421730, 0xb24cf65b8612f820}, {0xc94930ae1d529cfc, 0xdee033f26797b628}, + {0xfb9b7cd9a4a7443c, 0x169840ef017da3b2}, {0x9d412e0806e88aa5, 0x8e1f289560ee864f}, + {0xc491798a08a2ad4e, 0xf1a6f2bab92a27e3}, {0xf5b5d7ec8acb58a2, 0xae10af696774b1dc}, + {0x9991a6f3d6bf1765, 0xacca6da1e0a8ef2a}, {0xbff610b0cc6edd3f, 0x17fd090a58d32af4}, + {0xeff394dcff8a948e, 0xddfc4b4cef07f5b1}, {0x95f83d0a1fb69cd9, 0x4abdaf101564f98f}, + {0xbb764c4ca7a4440f, 0x9d6d1ad41abe37f2}, {0xea53df5fd18d5513, 0x84c86189216dc5ee}, + {0x92746b9be2f8552c, 0x32fd3cf5b4e49bb5}, {0xb7118682dbb66a77, 0x3fbc8c33221dc2a2}, + {0xe4d5e82392a40515, 0x0fabaf3feaa5334b}, {0x8f05b1163ba6832d, 0x29cb4d87f2a7400f}, + {0xb2c71d5bca9023f8, 0x743e20e9ef511013}, {0xdf78e4b2bd342cf6, 0x914da9246b255417}, + {0x8bab8eefb6409c1a, 0x1ad089b6c2f7548f}, {0xae9672aba3d0c320, 0xa184ac2473b529b2}, + {0xda3c0f568cc4f3e8, 0xc9e5d72d90a2741f}, {0x8865899617fb1871, 0x7e2fa67c7a658893}, + {0xaa7eebfb9df9de8d, 0xddbb901b98feeab8}, {0xd51ea6fa85785631, 0x552a74227f3ea566}, + {0x8533285c936b35de, 0xd53a88958f872760}, {0xa67ff273b8460356, 0x8a892abaf368f138}, + {0xd01fef10a657842c, 0x2d2b7569b0432d86}, {0x8213f56a67f6b29b, 0x9c3b29620e29fc74}, + {0xa298f2c501f45f42, 0x8349f3ba91b47b90}, {0xcb3f2f7642717713, 0x241c70a936219a74}, + {0xfe0efb53d30dd4d7, 0xed238cd383aa0111}, {0x9ec95d1463e8a506, 0xf4363804324a40ab}, + {0xc67bb4597ce2ce48, 0xb143c6053edcd0d6}, {0xf81aa16fdc1b81da, 0xdd94b7868e94050b}, + {0x9b10a4e5e9913128, 0xca7cf2b4191c8327}, {0xc1d4ce1f63f57d72, 0xfd1c2f611f63a3f1}, + {0xf24a01a73cf2dccf, 0xbc633b39673c8ced}, {0x976e41088617ca01, 0xd5be0503e085d814}, + {0xbd49d14aa79dbc82, 0x4b2d8644d8a74e19}, {0xec9c459d51852ba2, 0xddf8e7d60ed1219f}, + {0x93e1ab8252f33b45, 0xcabb90e5c942b504}, {0xb8da1662e7b00a17, 0x3d6a751f3b936244}, + {0xe7109bfba19c0c9d, 0x0cc512670a783ad5}, {0x906a617d450187e2, 0x27fb2b80668b24c6}, + {0xb484f9dc9641e9da, 0xb1f9f660802dedf7}, {0xe1a63853bbd26451, 0x5e7873f8a0396974}, + {0x8d07e33455637eb2, 0xdb0b487b6423e1e9}, {0xb049dc016abc5e5f, 0x91ce1a9a3d2cda63}, + {0xdc5c5301c56b75f7, 0x7641a140cc7810fc}, {0x89b9b3e11b6329ba, 0xa9e904c87fcb0a9e}, + {0xac2820d9623bf429, 0x546345fa9fbdcd45}, {0xd732290fbacaf133, 0xa97c177947ad4096}, + {0x867f59a9d4bed6c0, 0x49ed8eabcccc485e}, {0xa81f301449ee8c70, 0x5c68f256bfff5a75}, + {0xd226fc195c6a2f8c, 0x73832eec6fff3112}, {0x83585d8fd9c25db7, 0xc831fd53c5ff7eac}, + {0xa42e74f3d032f525, 0xba3e7ca8b77f5e56}, {0xcd3a1230c43fb26f, 0x28ce1bd2e55f35ec}, + {0x80444b5e7aa7cf85, 0x7980d163cf5b81b4}, {0xa0555e361951c366, 0xd7e105bcc3326220}, + {0xc86ab5c39fa63440, 0x8dd9472bf3fefaa8}, {0xfa856334878fc150, 0xb14f98f6f0feb952}, + {0x9c935e00d4b9d8d2, 0x6ed1bf9a569f33d4}, {0xc3b8358109e84f07, 0x0a862f80ec4700c9}, + {0xf4a642e14c6262c8, 0xcd27bb612758c0fb}, {0x98e7e9cccfbd7dbd, 0x8038d51cb897789d}, + {0xbf21e44003acdd2c, 0xe0470a63e6bd56c4}, {0xeeea5d5004981478, 0x1858ccfce06cac75}, + {0x95527a5202df0ccb, 0x0f37801e0c43ebc9}, {0xbaa718e68396cffd, 0xd30560258f54e6bb}, + {0xe950df20247c83fd, 0x47c6b82ef32a206a}, {0x91d28b7416cdd27e, 0x4cdc331d57fa5442}, + {0xb6472e511c81471d, 0xe0133fe4adf8e953}, {0xe3d8f9e563a198e5, 0x58180fddd97723a7}, + {0x8e679c2f5e44ff8f, 0x570f09eaa7ea7649}, {0xb201833b35d63f73, 0x2cd2cc6551e513db}, + {0xde81e40a034bcf4f, 0xf8077f7ea65e58d2}, {0x8b112e86420f6191, 0xfb04afaf27faf783}, + {0xadd57a27d29339f6, 0x79c5db9af1f9b564}, {0xd94ad8b1c7380874, 0x18375281ae7822bd}, + {0x87cec76f1c830548, 0x8f2293910d0b15b6}, {0xa9c2794ae3a3c69a, 0xb2eb3875504ddb23}, + {0xd433179d9c8cb841, 0x5fa60692a46151ec}, {0x849feec281d7f328, 0xdbc7c41ba6bcd334}, + {0xa5c7ea73224deff3, 0x12b9b522906c0801}, {0xcf39e50feae16bef, 0xd768226b34870a01}, + {0x81842f29f2cce375, 0xe6a1158300d46641}, {0xa1e53af46f801c53, 0x60495ae3c1097fd1}, + {0xca5e89b18b602368, 0x385bb19cb14bdfc5}, {0xfcf62c1dee382c42, 0x46729e03dd9ed7b6}, + {0x9e19db92b4e31ba9, 0x6c07a2c26a8346d2}, {0xc5a05277621be293, 0xc7098b7305241886}, + {0xf70867153aa2db38, 0xb8cbee4fc66d1ea8}}; +}; + +#if defined(BOOST_NO_CXX17_INLINE_VARIABLES) && (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) + +template constexpr int cache_holder_ieee754_binary64_impl::cache_bits; +template constexpr int cache_holder_ieee754_binary64_impl::min_k; +template constexpr int cache_holder_ieee754_binary64_impl::max_k; +template constexpr typename cache_holder_ieee754_binary64_impl::cache_entry_type cache_holder_ieee754_binary64_impl::cache[]; + +#endif + +#if (!defined(BOOST_MSVC) || BOOST_MSVC != 1900) +using cache_holder_ieee754_binary64 = cache_holder_ieee754_binary64_impl; +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// Policies. +//////////////////////////////////////////////////////////////////////////////////////// + +// Forward declare the implementation class. +template > +struct impl; + +namespace policy_impl { +// Sign policies. +namespace sign { + struct base {}; + + struct ignore : base + { + using sign_policy = ignore; + static constexpr bool return_has_sign = false; + + template + static BOOST_CXX14_CONSTEXPR void handle_sign(SignedSignificandBits, ReturnType&) noexcept {} + }; + + struct return_sign : base + { + using sign_policy = return_sign; + static constexpr bool return_has_sign = true; + + template + static BOOST_CXX14_CONSTEXPR void handle_sign(SignedSignificandBits s, ReturnType& r) noexcept + { + r.is_negative = s.is_negative(); + } + }; +} + +// Trailing zero policies. +namespace trailing_zero { + struct base {}; + + struct ignore : base + { + using trailing_zero_policy = ignore; + static constexpr bool report_trailing_zeros = false; + + template + static BOOST_CXX14_CONSTEXPR void on_trailing_zeros(ReturnType&) noexcept {} + + template + static BOOST_CXX14_CONSTEXPR void no_trailing_zeros(ReturnType&) noexcept {} + }; + + struct remove : base + { + using trailing_zero_policy = remove; + static constexpr bool report_trailing_zeros = false; + + template + BOOST_FORCEINLINE static void on_trailing_zeros(ReturnType& r) noexcept + { + r.exponent += Impl::remove_trailing_zeros(r.significand); + } + + template + static BOOST_CXX14_CONSTEXPR void no_trailing_zeros(ReturnType&) noexcept {} + }; + + struct report : base + { + using trailing_zero_policy = report; + static constexpr bool report_trailing_zeros = true; + + template + static BOOST_CXX14_CONSTEXPR void on_trailing_zeros(ReturnType& r) noexcept + { + r.may_have_trailing_zeros = true; + } + + template + static BOOST_CXX14_CONSTEXPR void no_trailing_zeros(ReturnType& r) noexcept + { + r.may_have_trailing_zeros = false; + } + }; +} + +// Decimal-to-binary rounding mode policies. +namespace decimal_to_binary_rounding { + struct base {}; + + enum class tag_t + { + to_nearest, + left_closed_directed, + right_closed_directed + }; + + namespace interval_type { + struct symmetric_boundary + { + static constexpr bool is_symmetric = true; + bool is_closed; + constexpr bool include_left_endpoint() const noexcept { return is_closed; } + constexpr bool include_right_endpoint() const noexcept { return is_closed; } + }; + + struct asymmetric_boundary + { + static constexpr bool is_symmetric = false; + bool is_left_closed; + constexpr bool include_left_endpoint() const noexcept { return is_left_closed; } + constexpr bool include_right_endpoint() const noexcept { return !is_left_closed; } + }; + + struct closed + { + static constexpr bool is_symmetric = true; + static constexpr bool include_left_endpoint() noexcept { return true; } + static constexpr bool include_right_endpoint() noexcept { return true; } + }; + + struct open + { + static constexpr bool is_symmetric = true; + static constexpr bool include_left_endpoint() noexcept { return false; } + static constexpr bool include_right_endpoint() noexcept { return false; } + }; + + struct left_closed_right_open + { + static constexpr bool is_symmetric = false; + static constexpr bool include_left_endpoint() noexcept { return true; } + static constexpr bool include_right_endpoint() noexcept { return false; } + }; + + struct right_closed_left_open + { + static constexpr bool is_symmetric = false; + static constexpr bool include_left_endpoint() noexcept { return false; } + static constexpr bool include_right_endpoint() noexcept { return true; } + }; + } + + template + struct return_type : return_type + {}; + + struct nearest_to_even : base + { + using decimal_to_binary_rounding_policy = nearest_to_even; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::symmetric_boundary; + using shorter_interval_type = interval_type::closed; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func f) noexcept + { + return f(nearest_to_even{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(s.has_even_significand_bits()); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_to_odd : base + { + using decimal_to_binary_rounding_policy = nearest_to_odd; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::symmetric_boundary; + using shorter_interval_type = interval_type::open; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(nearest_to_odd{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(!s.has_even_significand_bits()); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_toward_plus_infinity : base + { + using decimal_to_binary_rounding_policy = nearest_toward_plus_infinity; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::asymmetric_boundary; + using shorter_interval_type = interval_type::asymmetric_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(nearest_toward_plus_infinity{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(!s.is_negative()); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(!s.is_negative()); + } + }; + + struct nearest_toward_minus_infinity : base + { + using decimal_to_binary_rounding_policy = nearest_toward_minus_infinity; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::asymmetric_boundary; + using shorter_interval_type = interval_type::asymmetric_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(nearest_toward_minus_infinity{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(s.is_negative()); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits s, Func&& f) noexcept + { + return f(s.is_negative()); + } + }; + + struct nearest_toward_zero : base + { + using decimal_to_binary_rounding_policy = nearest_toward_zero; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::right_closed_left_open; + using shorter_interval_type = interval_type::right_closed_left_open; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(nearest_toward_zero{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_away_from_zero : base + { + using decimal_to_binary_rounding_policy = nearest_away_from_zero; + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::left_closed_right_open; + using shorter_interval_type = interval_type::left_closed_right_open; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(nearest_away_from_zero{}); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_always_closed + { + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::closed; + using shorter_interval_type = interval_type::closed; + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_always_open + { + static constexpr auto tag = tag_t::to_nearest; + using normal_interval_type = interval_type::open; + using shorter_interval_type = interval_type::open; + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_normal_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + + template + BOOST_FORCEINLINE static constexpr ReturnType + invoke_shorter_interval_case(SignedSignificandBits, Func&& f) noexcept + { + return f(); + } + }; + + struct nearest_to_even_static_boundary : base + { + using decimal_to_binary_rounding_policy = nearest_to_even_static_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.has_even_significand_bits()) + { + return f(nearest_always_closed{}); + } + else + { + return f(nearest_always_open{}); + } + } + }; + + struct nearest_to_odd_static_boundary : base + { + using decimal_to_binary_rounding_policy = nearest_to_odd_static_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.has_even_significand_bits()) + { + return f(nearest_always_open{}); + } + else + { + return f(nearest_always_closed{}); + } + } + }; + struct nearest_toward_plus_infinity_static_boundary : base + { + using decimal_to_binary_rounding_policy = nearest_toward_plus_infinity_static_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.is_negative()) + { + return f(nearest_toward_zero{}); + } + else + { + return f(nearest_away_from_zero{}); + } + } + }; + + struct nearest_toward_minus_infinity_static_boundary : base + { + using decimal_to_binary_rounding_policy = nearest_toward_minus_infinity_static_boundary; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.is_negative()) + { + return f(nearest_away_from_zero{}); + } + else + { + return f(nearest_toward_zero{}); + } + } + }; + + struct left_closed_directed + { + static constexpr auto tag = tag_t::left_closed_directed; + }; + struct right_closed_directed + { + static constexpr auto tag = tag_t::right_closed_directed; + }; + + struct toward_plus_infinity : base + { + using decimal_to_binary_rounding_policy = toward_plus_infinity; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.is_negative()) + { + return f(left_closed_directed{}); + } + else + { + return f(right_closed_directed{}); + } + } + }; + + struct toward_minus_infinity : base + { + using decimal_to_binary_rounding_policy = toward_minus_infinity; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits s, Func&& f) noexcept + { + if (s.is_negative()) + { + return f(right_closed_directed{}); + } + else + { + return f(left_closed_directed{}); + } + } + }; + + struct toward_zero : base + { + using decimal_to_binary_rounding_policy = toward_zero; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(left_closed_directed{}); + } + }; + + struct away_from_zero : base + { + using decimal_to_binary_rounding_policy = away_from_zero; + + template + BOOST_FORCEINLINE static ReturnType delegate(SignedSignificandBits, Func&& f) noexcept + { + return f(right_closed_directed{}); + } + }; +} + +// Binary-to-decimal rounding policies. +// (Always assumes nearest rounding modes.) +namespace binary_to_decimal_rounding { + struct base {}; + + enum class tag_t + { + do_not_care, + to_even, + to_odd, + away_from_zero, + toward_zero + }; + + struct do_not_care : base + { + using binary_to_decimal_rounding_policy = do_not_care; + static constexpr auto tag = tag_t::do_not_care; + + template + static constexpr bool prefer_round_down(ReturnType const&) noexcept + { + return false; + } + }; + + struct to_even : base + { + using binary_to_decimal_rounding_policy = to_even; + static constexpr auto tag = tag_t::to_even; + + template + static constexpr bool prefer_round_down(ReturnType const& r) noexcept + { + return r.significand % 2 != 0; + } + }; + + struct to_odd : base + { + using binary_to_decimal_rounding_policy = to_odd; + static constexpr auto tag = tag_t::to_odd; + + template + static constexpr bool prefer_round_down(ReturnType const& r) noexcept + { + return r.significand % 2 == 0; + } + }; + + struct away_from_zero : base + { + using binary_to_decimal_rounding_policy = away_from_zero; + static constexpr auto tag = tag_t::away_from_zero; + + template + static constexpr bool prefer_round_down(ReturnType const&) noexcept + { + return false; + } + }; + + struct toward_zero : base + { + using binary_to_decimal_rounding_policy = toward_zero; + static constexpr auto tag = tag_t::toward_zero; + + template + static constexpr bool prefer_round_down(ReturnType const&) noexcept + { + return true; + } + }; +} + +// Cache policies. +namespace cache { + struct base {}; + + struct full : base + { + using cache_policy = full; + + template ::value, + cache_holder_ieee754_binary32, + cache_holder_ieee754_binary64>::type> + static constexpr typename cache_format::cache_entry_type get_cache(int k) noexcept + { + return cache_format::cache[std::size_t(k - cache_format::min_k)]; + } + }; +} +} + +namespace policy { +namespace sign { + BOOST_INLINE_VARIABLE constexpr auto ignore = detail::policy_impl::sign::ignore{}; + BOOST_INLINE_VARIABLE constexpr auto return_sign = detail::policy_impl::sign::return_sign{}; +} + +namespace trailing_zero { + BOOST_INLINE_VARIABLE constexpr auto ignore = detail::policy_impl::trailing_zero::ignore{}; + BOOST_INLINE_VARIABLE constexpr auto remove = detail::policy_impl::trailing_zero::remove{}; + BOOST_INLINE_VARIABLE constexpr auto report = detail::policy_impl::trailing_zero::report{}; +} + +namespace decimal_to_binary_rounding { + BOOST_INLINE_VARIABLE constexpr auto nearest_to_even = + detail::policy_impl::decimal_to_binary_rounding::nearest_to_even{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_to_odd = + detail::policy_impl::decimal_to_binary_rounding::nearest_to_odd{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_toward_plus_infinity = + detail::policy_impl::decimal_to_binary_rounding::nearest_toward_plus_infinity{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_toward_minus_infinity = + detail::policy_impl::decimal_to_binary_rounding::nearest_toward_minus_infinity{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_toward_zero = + detail::policy_impl::decimal_to_binary_rounding::nearest_toward_zero{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_away_from_zero = + detail::policy_impl::decimal_to_binary_rounding::nearest_away_from_zero{}; + + BOOST_INLINE_VARIABLE constexpr auto nearest_to_even_static_boundary = + detail::policy_impl::decimal_to_binary_rounding::nearest_to_even_static_boundary{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_to_odd_static_boundary = + detail::policy_impl::decimal_to_binary_rounding::nearest_to_odd_static_boundary{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_toward_plus_infinity_static_boundary = + detail::policy_impl::decimal_to_binary_rounding:: + nearest_toward_plus_infinity_static_boundary{}; + BOOST_INLINE_VARIABLE constexpr auto nearest_toward_minus_infinity_static_boundary = + detail::policy_impl::decimal_to_binary_rounding:: + nearest_toward_minus_infinity_static_boundary{}; + + BOOST_INLINE_VARIABLE constexpr auto toward_plus_infinity = + detail::policy_impl::decimal_to_binary_rounding::toward_plus_infinity{}; + BOOST_INLINE_VARIABLE constexpr auto toward_minus_infinity = + detail::policy_impl::decimal_to_binary_rounding::toward_minus_infinity{}; + BOOST_INLINE_VARIABLE constexpr auto toward_zero = + detail::policy_impl::decimal_to_binary_rounding::toward_zero{}; + BOOST_INLINE_VARIABLE constexpr auto away_from_zero = + detail::policy_impl::decimal_to_binary_rounding::away_from_zero{}; +} + +namespace binary_to_decimal_rounding { + BOOST_INLINE_VARIABLE constexpr auto do_not_care = + detail::policy_impl::binary_to_decimal_rounding::do_not_care{}; + BOOST_INLINE_VARIABLE constexpr auto to_even = + detail::policy_impl::binary_to_decimal_rounding::to_even{}; + BOOST_INLINE_VARIABLE constexpr auto to_odd = + detail::policy_impl::binary_to_decimal_rounding::to_odd{}; + BOOST_INLINE_VARIABLE constexpr auto away_from_zero = + detail::policy_impl::binary_to_decimal_rounding::away_from_zero{}; + BOOST_INLINE_VARIABLE constexpr auto toward_zero = + detail::policy_impl::binary_to_decimal_rounding::toward_zero{}; +} + +namespace cache { + BOOST_INLINE_VARIABLE constexpr auto full = detail::policy_impl::cache::full{}; +} +} // Namespace Policy + +//////////////////////////////////////////////////////////////////////////////////////// +// The main algorithm. +//////////////////////////////////////////////////////////////////////////////////////// + +template +struct impl : private FloatTraits, private FloatTraits::format +{ + using format = typename FloatTraits::format; + using carrier_uint = typename FloatTraits::carrier_uint; + + using FloatTraits::carrier_bits; + using format::significand_bits; + using format::min_exponent; + using format::max_exponent; + using format::exponent_bias; + using format::decimal_digits; + + static constexpr int kappa = std::is_same::value ? 1 : 2; + static_assert(kappa >= 1, "Kappa must be >= 1"); + // static_assert(carrier_bits >= significand_bits + 2 + log::floor_log2_pow10(kappa + 1)); + + static constexpr int min_k_a = -log::floor_log10_pow2_minus_log10_4_over_3(int(max_exponent - significand_bits)); + static constexpr int min_k_b = -log::floor_log10_pow2(int(max_exponent - significand_bits)) + kappa; + static constexpr int min_k = min_k_a < min_k_b ? min_k_a : min_k_b; + // static_assert(min_k >= cache_holder::min_k, "Min k is not in the cache"); + + static constexpr int max_k_a = -log::floor_log10_pow2_minus_log10_4_over_3(int(min_exponent - significand_bits /*+ 1*/)); + static constexpr int max_k_b = -log::floor_log10_pow2(int(min_exponent - significand_bits)) + kappa; + static constexpr int max_k = max_k_a > max_k_b ? max_k_a : max_k_b; + + using cache_format = typename std::conditional::value, + cache_holder_ieee754_binary32, + cache_holder_ieee754_binary64>::type; + using cache_entry_type = typename cache_format::cache_entry_type; + static constexpr auto cache_bits = cache_format::cache_bits; + + static constexpr int case_shorter_interval_left_endpoint_lower_threshold = 2; + static BOOST_CXX14_CONSTEXPR const int case_shorter_interval_left_endpoint_upper_threshold = 3; + //2 + log::floor_log2(compute_power(10, count_factors<5>((carrier_uint(1) << (significand_bits + 2)) - 1) + 1) / 3); + + static constexpr int case_shorter_interval_right_endpoint_lower_threshold = 0; + static BOOST_CXX14_CONSTEXPR const int case_shorter_interval_right_endpoint_upper_threshold = 3; + //2 + log::floor_log2(compute_power(10, count_factors<5>((carrier_uint(1) << (significand_bits + 1)) + 1) + 1) / 3); + + static constexpr int shorter_interval_tie_lower_threshold = + -log::floor_log5_pow2_minus_log5_3(significand_bits + 4) - 2 - significand_bits; + static constexpr int shorter_interval_tie_upper_threshold = + -log::floor_log5_pow2(significand_bits + 2) - 2 - significand_bits; + + struct compute_mul_result + { + carrier_uint result; + bool is_integer; + }; + + struct compute_mul_parity_result + { + bool parity; + bool is_integer; + }; + + //// The main algorithm assumes the input is a normal/subnormal finite number + + #if defined(__GNUC__) && (__GNUC__ < 5) && !defined(__clang__) + # pragma GCC diagnostic push + # pragma GCC diagnostic ignored "-Wmissing-field-initializers" + #endif + + template + BOOST_JSON_SAFEBUFFERS static ReturnType compute_nearest_normal(carrier_uint const two_fc, const int exponent, + AdditionalArgs... additional_args) noexcept + { + ////////////////////////////////////////////////////////////////////// + // Step 1: Schubfach multiplier calculation + ////////////////////////////////////////////////////////////////////// + + ReturnType ret_value = {}; + IntervalType interval_type{additional_args...}; + + // Compute k and beta. + const int minus_k = log::floor_log10_pow2(exponent) - kappa; + const auto cache = CachePolicy::template get_cache(-minus_k); + const int beta = exponent + log::floor_log2_pow10(-minus_k); + + // Compute zi and deltai. + // 10^kappa <= deltai < 10^(kappa + 1) + const auto deltai = compute_delta(cache, beta); + // For the case of binary32, the result of integer check is not correct for + // 29711844 * 2^-82 + // = 6.1442653300000000008655037797566933477355632930994033813476... * 10^-18 + // and 29711844 * 2^-81 + // = 1.2288530660000000001731007559513386695471126586198806762695... * 10^-17, + // and they are the unique counterexamples. However, since 29711844 is even, + // this does not cause any problem for the endpoints calculations; it can only + // cause a problem when we need to perform integer check for the center. + // Fortunately, with these inputs, that branch is never executed, so we are fine. + //const auto [zi, is_z_integer] = compute_mul((two_fc | 1) << beta, cache); + const auto z_res = compute_mul((two_fc | 1) << beta, cache); + const auto zi = z_res.result; + const auto is_z_integer = z_res.is_integer; + + ////////////////////////////////////////////////////////////////////// + // Step 2: Try larger divisor; remove trailing zeros if necessary + ////////////////////////////////////////////////////////////////////// + + BOOST_CXX14_CONSTEXPR auto big_divisor = compute_power(std::uint32_t(10), kappa + 1); + BOOST_CXX14_CONSTEXPR auto small_divisor = compute_power(std::uint32_t(10), kappa); + + // Using an upper bound on zi, we might be able to optimize the division + // better than the compiler; we are computing zi / big_divisor here. + #ifdef BOOST_NO_CXX14_CONSTEXPR + ret_value.significand = div::divide_by_pow10(kappa + 1, (carrier_uint(1) << (significand_bits + 1)) * big_divisor - 1, zi); + #else + ret_value.significand = div::divide_by_pow10(zi); + #endif + + auto r = std::uint32_t(zi - big_divisor * ret_value.significand); + + if (r < deltai) + { + // Exclude the right endpoint if necessary. + if (r == 0 && (is_z_integer & !interval_type.include_right_endpoint())) + { + BOOST_IF_CONSTEXPR (BinaryToDecimalRoundingPolicy::tag == policy_impl::binary_to_decimal_rounding::tag_t::do_not_care) + { + ret_value.significand *= 10; + ret_value.exponent = minus_k + kappa; + --ret_value.significand; + TrailingZeroPolicy::template no_trailing_zeros(ret_value); + + return ret_value; + } + else + { + --ret_value.significand; + r = big_divisor; + + goto small_divisor_case_label; + } + } + } + else if (r > deltai) + { + goto small_divisor_case_label; + } + else + { + // r == deltai; compare fractional parts. + // const auto [xi_parity, x_is_integer] = + // compute_mul_parity(two_fc - 1, cache, beta); + const auto x_res = compute_mul_parity(two_fc - 1, cache, beta); + const auto xi_parity = x_res.parity; + const auto x_is_integer = x_res.is_integer; + + if (!(xi_parity | (x_is_integer & interval_type.include_left_endpoint()))) + { + goto small_divisor_case_label; + } + } + ret_value.exponent = minus_k + kappa + 1; + + // We may need to remove trailing zeros. + TrailingZeroPolicy::template on_trailing_zeros(ret_value); + return ret_value; + + + ////////////////////////////////////////////////////////////////////// + // Step 3: Find the significand with the smaller divisor + ////////////////////////////////////////////////////////////////////// + + small_divisor_case_label: + TrailingZeroPolicy::template no_trailing_zeros(ret_value); + ret_value.significand *= 10; + ret_value.exponent = minus_k + kappa; + + BOOST_IF_CONSTEXPR (BinaryToDecimalRoundingPolicy::tag == policy_impl::binary_to_decimal_rounding::tag_t::do_not_care) + { + // Normally, we want to compute + // ret_value.significand += r / small_divisor + // and return, but we need to take care of the case that the resulting + // value is exactly the right endpoint, while that is not included in the + // interval. + if (!interval_type.include_right_endpoint()) + { + // Is r divisible by 10^kappa? + if (is_z_integer && div::check_divisibility_and_divide_by_pow10(r)) + { + // This should be in the interval. + ret_value.significand += r - 1; + } + else + { + ret_value.significand += r; + } + } + else + { + ret_value.significand += div::small_division_by_pow10(r); + } + } + else + { + auto dist = r - (deltai / 2) + (small_divisor / 2); + const bool approx_y_parity = ((dist ^ (small_divisor / 2)) & 1) != 0; + + // Is dist divisible by 10^kappa? + const bool divisible_by_small_divisor = div::check_divisibility_and_divide_by_pow10(dist); + + // Add dist / 10^kappa to the significand. + ret_value.significand += dist; + + if (divisible_by_small_divisor) + { + // Check z^(f) >= epsilon^(f). + // We have either yi == zi - epsiloni or yi == (zi - epsiloni) - 1, + // where yi == zi - epsiloni if and only if z^(f) >= epsilon^(f). + // Since there are only 2 possibilities, we only need to care about the + // parity. Also, zi and r should have the same parity since the divisor is + // an even number. + //const auto [yi_parity, is_y_integer] = + // compute_mul_parity(two_fc, cache, beta); + const auto y_res = compute_mul_parity(two_fc, cache, beta); + const auto yi_parity = y_res.parity; + const auto is_y_integer = y_res.is_integer; + + if (yi_parity != approx_y_parity) + { + --ret_value.significand; + } + else + { + // If z^(f) >= epsilon^(f), we might have a tie + // when z^(f) == epsilon^(f), or equivalently, when y is an integer. + // For tie-to-up case, we can just choose the upper one. + if (BinaryToDecimalRoundingPolicy::prefer_round_down(ret_value) & is_y_integer) + { + --ret_value.significand; + } + } + } + } + + return ret_value; + } + + template + BOOST_JSON_SAFEBUFFERS static ReturnType compute_nearest_shorter(const int exponent, AdditionalArgs... additional_args) noexcept + { + ReturnType ret_value = {}; + IntervalType interval_type{additional_args...}; + + // Compute k and beta. + const int minus_k = log::floor_log10_pow2_minus_log10_4_over_3(exponent); + const int beta = exponent + log::floor_log2_pow10(-minus_k); + + // Compute xi and zi. + const auto cache = CachePolicy::template get_cache(-minus_k); + + auto xi = compute_left_endpoint_for_shorter_interval_case(cache, beta); + auto zi = compute_right_endpoint_for_shorter_interval_case(cache, beta); + + // If we don't accept the right endpoint and + // if the right endpoint is an integer, decrease it. + if (!interval_type.include_right_endpoint() && is_right_endpoint_integer_shorter_interval(exponent)) + { + --zi; + } + // If we don't accept the left endpoint or + // if the left endpoint is not an integer, increase it. + if (!interval_type.include_left_endpoint() || !is_left_endpoint_integer_shorter_interval(exponent)) + { + ++xi; + } + + // Try bigger divisor. + ret_value.significand = zi / 10; + + // If succeed, remove trailing zeros if necessary and return. + if (ret_value.significand * 10 >= xi) + { + ret_value.exponent = minus_k + 1; + TrailingZeroPolicy::template on_trailing_zeros(ret_value); + return ret_value; + } + + // Otherwise, compute the round-up of y. + TrailingZeroPolicy::template no_trailing_zeros(ret_value); + ret_value.significand = compute_round_up_for_shorter_interval_case(cache, beta); + ret_value.exponent = minus_k; + + // When tie occurs, choose one of them according to the rule. + if (BinaryToDecimalRoundingPolicy::prefer_round_down(ret_value) && + exponent >= shorter_interval_tie_lower_threshold && + exponent <= shorter_interval_tie_upper_threshold) + { + --ret_value.significand; + } + else if (ret_value.significand < xi) + { + ++ret_value.significand; + } + + return ret_value; + } + + #if defined(__GNUC__) && (__GNUC__ < 5) && !defined(__clang__) + # pragma GCC diagnostic pop + #endif + + template + BOOST_JSON_SAFEBUFFERS static ReturnType compute_left_closed_directed(carrier_uint const two_fc, int exponent) noexcept + { + ////////////////////////////////////////////////////////////////////// + // Step 1: Schubfach multiplier calculation + ////////////////////////////////////////////////////////////////////// + + ReturnType ret_value; + + // Compute k and beta. + const int minus_k = log::floor_log10_pow2(exponent) - kappa; + const auto cache = CachePolicy::template get_cache(-minus_k); + const int beta = exponent + log::floor_log2_pow10(-minus_k); + + // Compute xi and deltai. + // 10^kappa <= deltai < 10^(kappa + 1) + const auto deltai = compute_delta(cache, beta); + //auto [xi, is_x_integer] = compute_mul(two_fc << beta, cache); + const auto x_res = compute_mul(two_fc << beta, cache); + auto xi = x_res.result; + auto is_x_integer = x_res.is_integer; + + // Deal with the unique exceptional cases + // 29711844 * 2^-82 + // = 6.1442653300000000008655037797566933477355632930994033813476... * 10^-18 + // and 29711844 * 2^-81 + // = 1.2288530660000000001731007559513386695471126586198806762695... * 10^-17 + // for binary32. + BOOST_IF_CONSTEXPR (std::is_same::value) + { + if (exponent <= -80) + { + is_x_integer = false; + } + } + + if (!is_x_integer) + { + ++xi; + } + + ////////////////////////////////////////////////////////////////////// + // Step 2: Try larger divisor; remove trailing zeros if necessary + ////////////////////////////////////////////////////////////////////// + + BOOST_CXX14_CONSTEXPR auto big_divisor = compute_power(std::uint32_t(10), kappa + 1); + + // Using an upper bound on xi, we might be able to optimize the division + // better than the compiler; we are computing xi / big_divisor here. + + #ifdef BOOST_NO_CXX14_CONSTEXPR + ret_value.significand = div::divide_by_pow10(kappa + 1, (carrier_uint(1) << (significand_bits + 1)) * big_divisor - 1, xi); + #else + ret_value.significand = div::divide_by_pow10(xi); + #endif + + auto r = std::uint32_t(xi - big_divisor * ret_value.significand); + + if (r != 0) + { + ++ret_value.significand; + r = big_divisor - r; + } + + if (r > deltai) + { + goto small_divisor_case_label; + } + else if (r == deltai) + { + // Compare the fractional parts. + // This branch is never taken for the exceptional cases + // 2f_c = 29711482, e = -81 + // (6.1442649164096937243516663440523473127541365101933479309082... * 10^-18) + // and 2f_c = 29711482, e = -80 + // (1.2288529832819387448703332688104694625508273020386695861816... * 10^-17). + //const auto [zi_parity, is_z_integer] = + // compute_mul_parity(two_fc + 2, cache, beta); + const auto z_res = compute_mul_parity(two_fc + 2, cache, beta); + if (z_res.parity || z_res.is_integer) + { + goto small_divisor_case_label; + } + } + + // The ceiling is inside, so we are done. + ret_value.exponent = minus_k + kappa + 1; + TrailingZeroPolicy::template on_trailing_zeros(ret_value); + return ret_value; + + + ////////////////////////////////////////////////////////////////////// + // Step 3: Find the significand with the smaller divisor + ////////////////////////////////////////////////////////////////////// + + small_divisor_case_label: + ret_value.significand *= 10; + ret_value.significand -= div::small_division_by_pow10(r); + ret_value.exponent = minus_k + kappa; + TrailingZeroPolicy::template no_trailing_zeros(ret_value); + return ret_value; + } + + template + BOOST_JSON_SAFEBUFFERS static ReturnType compute_right_closed_directed(carrier_uint const two_fc, const int exponent, bool shorter_interval) noexcept + { + ////////////////////////////////////////////////////////////////////// + // Step 1: Schubfach multiplier calculation + ////////////////////////////////////////////////////////////////////// + + ReturnType ret_value; + + // Compute k and beta. + const int minus_k = log::floor_log10_pow2(exponent - (shorter_interval ? 1 : 0)) - kappa; + const auto cache = CachePolicy::template get_cache(-minus_k); + const int beta = exponent + log::floor_log2_pow10(-minus_k); + + // Compute zi and deltai. + // 10^kappa <= deltai < 10^(kappa + 1) + const auto deltai = shorter_interval ? compute_delta(cache, beta - 1) : compute_delta(cache, beta); + carrier_uint const zi = compute_mul(two_fc << beta, cache).result; + + + ////////////////////////////////////////////////////////////////////// + // Step 2: Try larger divisor; remove trailing zeros if necessary + ////////////////////////////////////////////////////////////////////// + + BOOST_CXX14_CONSTEXPR auto big_divisor = compute_power(std::uint32_t(10), kappa + 1); + + // Using an upper bound on zi, we might be able to optimize the division better than + // the compiler; we are computing zi / big_divisor here. + #ifdef BOOST_NO_CXX14_CONSTEXPR + ret_value.significand = div::divide_by_pow10(kappa + 1, (carrier_uint(1) << (significand_bits + 1)) * big_divisor - 1, zi); + #else + ret_value.significand = div::divide_by_pow10(zi); + #endif + + const auto r = std::uint32_t(zi - big_divisor * ret_value.significand); + + if (r > deltai) + { + goto small_divisor_case_label; + } + else if (r == deltai) + { + // Compare the fractional parts. + if (!compute_mul_parity(two_fc - (shorter_interval ? 1 : 2), cache, beta).parity) + { + goto small_divisor_case_label; + } + } + + // The floor is inside, so we are done. + ret_value.exponent = minus_k + kappa + 1; + TrailingZeroPolicy::template on_trailing_zeros(ret_value); + return ret_value; + + + ////////////////////////////////////////////////////////////////////// + // Step 3: Find the significand with the small divisor + ////////////////////////////////////////////////////////////////////// + + small_divisor_case_label: + ret_value.significand *= 10; + ret_value.significand += div::small_division_by_pow10(r); + ret_value.exponent = minus_k + kappa; + TrailingZeroPolicy::template no_trailing_zeros(ret_value); + + return ret_value; + } + + // Remove trailing zeros from n and return the number of zeros removed. + BOOST_FORCEINLINE static int remove_trailing_zeros(carrier_uint& n) noexcept + { + if (n == 0) + { + return 0; + } + + BOOST_IF_CONSTEXPR (std::is_same::value) + { + constexpr auto mod_inv_5 = UINT32_C(0xcccccccd); + constexpr auto mod_inv_25 = mod_inv_5 * mod_inv_5; + + int s = 0; + while (true) + { + auto q = boost::core::rotr(n * mod_inv_25, 2); + if (q <= (std::numeric_limits::max)() / 100) + { + n = q; + s += 2; + } + else + { + break; + } + } + auto q = boost::core::rotr(n * mod_inv_5, 1); + if (q <= (std::numeric_limits::max)() / 10) + { + n = q; + s |= 1; + } + + return s; + } + else + { + // Static assertion does not work unless if constexpr is supported + // static_assert(std::is_same::value, "Must be a double type"); + + // Divide by 10^8 and reduce to 32-bits if divisible. + // Since ret_value.significand <= (2^53 * 1000 - 1) / 1000 < 10^16, + // n is at most of 16 digits. + + // This magic number is ceil(2^90 / 10^8). + constexpr auto magic_number = UINT64_C(12379400392853802749); + auto nm = umul128(n, magic_number); + + // Is n is divisible by 10^8? + if ((nm.high & ((std::uint64_t(1) << (90 - 64)) - 1)) == 0 && + nm.low < magic_number) { + // If yes, work with the quotient. + auto n32 = static_cast(nm.high >> (90 - 64)); + + constexpr auto mod_inv_5 = UINT32_C(0xcccccccd); + constexpr auto mod_inv_25 = mod_inv_5 * mod_inv_5; + + int s = 8; + while (true) + { + auto q = boost::core::rotr(n32 * mod_inv_25, 2); + if (q <= (std::numeric_limits::max)() / 100) + { + n32 = q; + s += 2; + } + else + { + break; + } + } + + auto q = boost::core::rotr(n32 * mod_inv_5, 1); + if (q <= (std::numeric_limits::max)() / 10) + { + n32 = q; + s |= 1; + } + + n = n32; + return s; + } + + // If n is not divisible by 10^8, work with n itself. + constexpr auto mod_inv_5 = UINT64_C(0xcccccccccccccccd); + constexpr auto mod_inv_25 = mod_inv_5 * mod_inv_5; + + int s = 0; + while (true) + { + auto q = static_cast(boost::core::rotr(n * mod_inv_25, 2)); + if (q <= (std::numeric_limits::max)() / 100) + { + n = q; + s += 2; + } + else + { + break; + } + } + + auto q = static_cast(boost::core::rotr(n * mod_inv_5, 1)); + if (q <= (std::numeric_limits::max)() / 10) + { + n = q; + s |= 1; + } + + return s; + } + } + + template ::value, bool>::type = true> + static compute_mul_result compute_mul(carrier_uint u, cache_entry_type const& cache) noexcept + { + auto r = umul96_upper64(u, cache); + return {carrier_uint(r >> 32), carrier_uint(r) == 0}; + } + + template ::value, bool>::type = true> + static compute_mul_result compute_mul(carrier_uint u, cache_entry_type const& cache) noexcept + { + auto r = umul192_upper128(u, cache); + return {r.high, r.low == 0}; + } + + template ::value, bool>::type = true> + static constexpr std::uint32_t compute_delta(cache_entry_type const& cache, + int beta) noexcept + { + return std::uint32_t(cache >> (cache_bits - 1 - beta)); + } + + template ::value, bool>::type = true> + static constexpr std::uint32_t compute_delta(cache_entry_type const& cache, + int beta) noexcept + { + return std::uint32_t(cache.high >> (carrier_bits - 1 - beta)); + } + + template ::value, bool>::type = true> + static compute_mul_parity_result compute_mul_parity(carrier_uint two_f, + cache_entry_type const& cache, + int beta) noexcept + { + auto r = umul96_lower64(two_f, cache); + return {((r >> (64 - beta)) & 1) != 0, std::uint32_t(r >> (32 - beta)) == 0}; + } + + template ::value, bool>::type = true> + static compute_mul_parity_result compute_mul_parity(carrier_uint two_f, + cache_entry_type const& cache, + int beta) noexcept + { + auto r = umul192_lower128(two_f, cache); + return {((r.high >> (64 - beta)) & 1) != 0, ((r.high << beta) | (r.low >> (64 - beta))) == 0}; + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_left_endpoint_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return carrier_uint((cache - (cache >> (significand_bits + 2))) >> (cache_bits - significand_bits - 1 - beta)); + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_left_endpoint_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return (cache.high - (cache.high >> (significand_bits + 2))) >> (carrier_bits - significand_bits - 1 - beta); + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_right_endpoint_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return carrier_uint((cache + (cache >> (significand_bits + 1))) >> (cache_bits - significand_bits - 1 - beta)); + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_right_endpoint_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return (cache.high + (cache.high >> (significand_bits + 1))) >> (carrier_bits - significand_bits - 1 - beta); + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_round_up_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return (carrier_uint(cache >> (cache_bits - significand_bits - 2 - beta)) + 1) / 2; + } + + template ::value, bool>::type = true> + static constexpr carrier_uint compute_round_up_for_shorter_interval_case(cache_entry_type const& cache, int beta) noexcept + { + return ((cache.high >> (carrier_bits - significand_bits - 2 - beta)) + 1) / 2; + } + + static constexpr bool is_right_endpoint_integer_shorter_interval(int exponent) noexcept + { + return exponent >= case_shorter_interval_right_endpoint_lower_threshold && + exponent <= case_shorter_interval_right_endpoint_upper_threshold; + } + + static constexpr bool is_left_endpoint_integer_shorter_interval(int exponent) noexcept + { + return exponent >= case_shorter_interval_left_endpoint_lower_threshold && + exponent <= case_shorter_interval_left_endpoint_upper_threshold; + } +}; + + +//////////////////////////////////////////////////////////////////////////////////////// +// Policy holder. +//////////////////////////////////////////////////////////////////////////////////////// + +namespace policy_impl { + template + struct policy_holder : Policies... {}; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// The interface function. +//////////////////////////////////////////////////////////////////////////////////////// + +#ifdef BOOST_MSVC +# pragma warning(push) +# pragma warning(disable: 4100) // Unreferenced formal parameter (interval_type_provider) +# pragma warning(disable: 4189) // Local variable is initializaed but unused (tag) +#endif + +template > +BOOST_FORCEINLINE BOOST_JSON_SAFEBUFFERS auto +to_decimal(dragonbox_signed_significand_bits dragonbox_signed_significand_bits, + unsigned int exponent_bits) noexcept + -> decimal_fp +{ + // Build policy holder type. + using namespace policy_impl; + + using policy_holder = policy_holder; + + using return_type = decimal_fp; + + return_type ret = policy_holder::template delegate(dragonbox_signed_significand_bits, + [exponent_bits, dragonbox_signed_significand_bits](policy_impl::decimal_to_binary_rounding::nearest_to_even interval_type_provider) { + using format = typename FloatTraits::format; + constexpr auto tag = decltype(interval_type_provider)::tag; + + auto two_fc = dragonbox_signed_significand_bits.remove_sign_bit_and_shift(); + auto exponent = int(exponent_bits); + + BOOST_IF_CONSTEXPR (tag == decimal_to_binary_rounding::tag_t::to_nearest) { // NOLINT: if constexpr not always false + // Is the input a normal number? + if (exponent != 0) { + exponent += format::exponent_bias - format::significand_bits; + + // Shorter interval case; proceed like Schubfach. + // One might think this condition is wrong, since when exponent_bits == 1 + // and two_fc == 0, the interval is actually regular. However, it turns out + // that this seemingly wrong condition is actually fine, because the end + // result is anyway the same. + // + // [binary32] + // (fc-1/2) * 2^e = 1.175'494'28... * 10^-38 + // (fc-1/4) * 2^e = 1.175'494'31... * 10^-38 + // fc * 2^e = 1.175'494'35... * 10^-38 + // (fc+1/2) * 2^e = 1.175'494'42... * 10^-38 + // + // Hence, shorter_interval_case will return 1.175'494'4 * 10^-38. + // 1.175'494'3 * 10^-38 is also a correct shortest representation that will + // be rejected if we assume shorter interval, but 1.175'494'4 * 10^-38 is + // closer to the true value so it doesn't matter. + // + // [binary64] + // (fc-1/2) * 2^e = 2.225'073'858'507'201'13... * 10^-308 + // (fc-1/4) * 2^e = 2.225'073'858'507'201'25... * 10^-308 + // fc * 2^e = 2.225'073'858'507'201'38... * 10^-308 + // (fc+1/2) * 2^e = 2.225'073'858'507'201'63... * 10^-308 + // + // Hence, shorter_interval_case will return 2.225'073'858'507'201'4 * + // 10^-308. This is indeed of the shortest length, and it is the unique one + // closest to the true value among valid representations of the same length. + static_assert(std::is_same::value || + std::is_same::value, "Format must be IEEE754 binary 32 or 64"); + + if (two_fc == 0) { + return decltype(interval_type_provider)::template invoke_shorter_interval_case( + dragonbox_signed_significand_bits, [exponent]() { + return detail::impl:: + template compute_nearest_shorter< + return_type, + typename decltype(interval_type_provider):: + shorter_interval_type, + typename policy_holder::trailing_zero_policy, + typename policy_holder:: + binary_to_decimal_rounding_policy, + typename policy_holder::cache_policy>( + exponent); + }); + } + + two_fc |= (decltype(two_fc)(1) << (format::significand_bits + 1)); + } + // Is the input a subnormal number? + else { + exponent = format::min_exponent - format::significand_bits; + } + + return decltype(interval_type_provider)::template invoke_normal_interval_case( + dragonbox_signed_significand_bits, [two_fc, exponent](bool additional_args) { + return detail::impl:: + template compute_nearest_normal< + return_type, + typename decltype(interval_type_provider)::normal_interval_type, + typename policy_holder::trailing_zero_policy, + typename policy_holder::binary_to_decimal_rounding_policy, + typename policy_holder::cache_policy>(two_fc, exponent, additional_args); + }); + } + else BOOST_IF_CONSTEXPR (tag == decimal_to_binary_rounding::tag_t::left_closed_directed) // NOLINT: if constexpr not always false + { + // Is the input a normal number? + if (exponent != 0) { + exponent += format::exponent_bias - format::significand_bits; + two_fc |= (decltype(two_fc)(1) << (format::significand_bits + 1)); + } + // Is the input a subnormal number? + else { + exponent = format::min_exponent - format::significand_bits; + } + + return detail::impl::template compute_left_closed_directed< + return_type, typename policy_holder::trailing_zero_policy, + typename policy_holder::cache_policy>(two_fc, exponent); + } + else + { + // Assertion does not work unless if constexpr is defined + // static_assert(tag == decimal_to_binary_rounding::tag_t::right_closed_directed, "Tag should be right_closed_direction"); + + bool shorter_interval = false; + + // Is the input a normal number? + if (exponent != 0) { + if (two_fc == 0 && exponent != 1) { + shorter_interval = true; + } + exponent += format::exponent_bias - format::significand_bits; + two_fc |= (decltype(two_fc)(1) << (format::significand_bits + 1)); + } + // Is the input a subnormal number? + else { + exponent = format::min_exponent - format::significand_bits; + } + + return detail::impl::template compute_right_closed_directed< + return_type, typename policy_holder::trailing_zero_policy, + typename policy_holder::cache_policy>(two_fc, exponent, shorter_interval); + } + }); + + policy_holder::handle_sign(dragonbox_signed_significand_bits, ret); + return ret; +} + +#ifdef BOOST_MSVC +# pragma warning(pop) +#endif + +namespace to_chars_detail { + +std::size_t dragon_box_print_chars( + dragonbox_float_traits::carrier_uint significand, + int exponent, + char* first, + char* last) noexcept; + +} + +} // namespace detail +} // namespace json +} // namespace boost + +#ifdef BOOST_MSVC +# pragma warning(pop) + +#endif + +#endif // BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_HPP diff --git a/include/boost/json/detail/dragonbox/dragonbox_common.hpp b/include/boost/json/detail/dragonbox/dragonbox_common.hpp new file mode 100644 index 000000000..92281f0d6 --- /dev/null +++ b/include/boost/json/detail/dragonbox/dragonbox_common.hpp @@ -0,0 +1,450 @@ +// Copyright 2020-2022 Junekey Jeon +// +// The contents of this file may be used under the terms of +// the Apache License v2.0 with LLVM Exceptions. +// +// (See accompanying file LICENSE-Apache or copy at +// https://llvm.org/foundation/relicensing/LICENSE.txt) +// +// Alternatively, the contents of this file may be used under the terms of +// the Boost Software License, Version 1.0. +// (See accompanying file LICENSE-Boost or copy at +// https://www.boost.org/LICENSE_1_0.txt) +// +// Unless required by applicable law or agreed to in writing, this software +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. +// +// Some parts are copied from Dragonbox project. +// +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_COMMON_HPP +#define BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_COMMON_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace json { +namespace detail { + +template +struct physical_bits +{ + static constexpr std::size_t value = sizeof(T) * CHAR_BIT; +}; + +template +struct value_bits +{ + static constexpr std::size_t value = std::numeric_limits::value, T>::type>::digits; +}; + +#ifdef BOOST_NO_CXX17_INLINE_VARIABLES + +template constexpr std::size_t physical_bits::value; +template constexpr std::size_t value_bits::value; + +#endif + +// A floating-point traits class defines ways to interpret a bit pattern of given size as an +// encoding of floating-point number. This is a default implementation of such a traits class, +// supporting ways to interpret 32-bits into a binary32-encoded floating-point number and to +// interpret 64-bits into a binary64-encoded floating-point number. Users might specialize this +// class to change the default behavior for certain types. +template +struct default_float_traits +{ + // I don't know if there is a truly reliable way of detecting + // IEEE-754 binary32/binary64 formats; I just did my best here. + static_assert(std::numeric_limits::is_iec559 && std::numeric_limits::radix == 2 && + (detail::physical_bits::value == 32 || detail::physical_bits::value == 64), + "default_ieee754_traits only works for 32-bits or 64-bits types " + "supporting binary32 or binary64 formats!"); + + // The type that is being viewed. + using type = T; + + // Refers to the format specification class. + using format = + typename std::conditional::value == 32, detail::ieee754_binary32, detail::ieee754_binary64>::type; + + // Defines an unsignedeger type that is large enough to carry a variable of type T. + // Most of the operations will be done on this integer type. + using carrier_uint = typename std::conditional::value == 32, std::uint32_t, std::uint64_t>::type; + + static_assert(sizeof(carrier_uint) == sizeof(T), "carrier_uint must be T"); + + // Number of bits in the above unsignedeger type. + static constexpr int carrier_bits = static_cast(detail::physical_bits::value); + + // Convert from carrier_uint into the original type. + // Depending on the floating-point encoding format, this operation might not be possible for + // some specific bit patterns. However, the contract is that u always denotes a + // valid bit pattern, so this function must be assumed to be noexcept. + static T carrier_to_float(carrier_uint u) noexcept + { + T x; + std::memcpy(&x, &u, sizeof(carrier_uint)); + return x; + } + + // Same as above. + static carrier_uint float_to_carrier(T x) noexcept + { + carrier_uint u; + std::memcpy(&u, &x, sizeof(carrier_uint)); + return u; + } + + // Extract exponent bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. This function does not do bias adjustment. + static constexpr unsigned extract_exponent_bits(carrier_uint u) noexcept + { + return static_cast(u >> format::exponent_bits) & static_cast((1U << format::exponent_bits) - 1); + } + + // Extract significand bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. The result does not contain the implicit bit. + static constexpr carrier_uint extract_significand_bits(carrier_uint u) noexcept + { + return static_cast(u & static_cast((static_cast(1) << format::significand_bits) - 1)); + } + + // Remove the exponent bits and extract significand bits together with the sign bit. + static constexpr carrier_uint remove_exponent_bits(carrier_uint u, unsigned exponent_bits) noexcept + { + return u ^ (static_cast(exponent_bits) << format::significand_bits); + } + + // Shift the obtained signed significand bits to the left by 1 to remove the sign bit. + static constexpr carrier_uint remove_sign_bit_and_shift(carrier_uint u) noexcept + { + return static_cast(static_cast(u) << 1); + } + + // The actual value of exponent is obtained by adding this value to the extracted exponent bits + static constexpr int exponent_bias = 1 - (1 << (carrier_bits - format::significand_bits - 2)); + + // Obtain the actual value of the binary exponent from the extracted exponent bits. + static constexpr int binary_exponent(unsigned exponent_bits) noexcept + { + return exponent_bits == 0 ? format::min_exponent : static_cast(exponent_bits) + format::exponent_bias; + } + + // Obtain the actual value of the binary exponent from the extracted significand bits and + // exponent bits. + static constexpr carrier_uint binary_significand(carrier_uint significand_bits, unsigned exponent_bits) noexcept + { + return exponent_bits == 0 ? significand_bits : (significand_bits | (static_cast(1) << format::significand_bits)); + } + + + // Various boolean observer functions + + static constexpr bool is_nonzero(carrier_uint u) noexcept { return (u << 1) != 0; } + + static constexpr bool is_positive(carrier_uint u) noexcept + { + return u < static_cast(1) << (format::significand_bits + format::exponent_bits); + } + + static constexpr bool is_negative(carrier_uint u) noexcept { return !is_positive(u); } + + static constexpr bool is_finite(unsigned exponent_bits) noexcept + { + //constexpr unsigned exponent_bits_all_set = (1u << format::exponent_bits) - 1; + return exponent_bits != (1u << format::exponent_bits) - 1; + } + + static constexpr bool has_all_zero_significand_bits(carrier_uint u) noexcept + { + return (u << 1) == 0; + } + + static constexpr bool has_even_significand_bits(carrier_uint u) noexcept + { + return u % 2 == 0; + } +}; + +// Convenient wrappers for floating-point traits classes. +// In order to reduce the argument passing overhead, these classes should be as simple as +// possible (e.g., no inheritance, no private non-static data member, etc.; this is an +// unfortunate fact about common ABI convention). + +template > +struct float_bits; + +template > +struct signed_significand_bits; + +template +struct float_bits +{ + using type = T; + using traits_type = Traits; + using carrier_uint = typename traits_type::carrier_uint; + + carrier_uint u; + + float_bits() = default; + constexpr explicit float_bits(carrier_uint bit_pattern) noexcept : u{bit_pattern} {} + constexpr explicit float_bits(T float_value) noexcept : u{traits_type::float_to_carrier(float_value)} {} + + constexpr T to_float() const noexcept { return traits_type::carrier_to_float(u); } + + // Extract exponent bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. This function does not do bias adjustment. + constexpr unsigned extract_exponent_bits() const noexcept + { + return traits_type::extract_exponent_bits(u); + } + + // Extract significand bits from a bit pattern. + // The result must be aligned to the LSB so that there is no additional zero paddings + // on the right. The result does not contain the implicit bit. + constexpr carrier_uint extract_significand_bits() const noexcept + { + return traits_type::extract_significand_bits(u); + } + + // Remove the exponent bits and extract significand bits together with the sign bit. + constexpr signed_significand_bits remove_exponent_bits(unsigned exponent_bits) const noexcept + { + return signed_significand_bits(traits_type::remove_exponent_bits(u, exponent_bits)); + } + + // Obtain the actual value of the binary exponent from the extracted exponent bits. + static constexpr int binary_exponent(unsigned exponent_bits) noexcept + { + return traits_type::binary_exponent(exponent_bits); + } + + constexpr int binary_exponent() const noexcept + { + return binary_exponent(extract_exponent_bits()); + } + + // Obtain the actual value of the binary exponent from the extracted significand bits and + // exponent bits. + static constexpr carrier_uint binary_significand(carrier_uint significand_bits, unsigned exponent_bits) noexcept + { + return traits_type::binary_significand(significand_bits, exponent_bits); + } + + constexpr carrier_uint binary_significand() const noexcept + { + return binary_significand(extract_significand_bits(), extract_exponent_bits()); + } + + constexpr bool is_nonzero() const noexcept { return traits_type::is_nonzero(u); } + + constexpr bool is_positive() const noexcept { return traits_type::is_positive(u); } + + constexpr bool is_negative() const noexcept { return traits_type::is_negative(u); } + + constexpr bool is_finite(unsigned exponent_bits) const noexcept { return traits_type::is_finite(exponent_bits); } + + constexpr bool is_finite() const noexcept { return traits_type::is_finite(extract_exponent_bits()); } + + constexpr bool has_even_significand_bits() const noexcept { return traits_type::has_even_significand_bits(u); } +}; + +template +struct signed_significand_bits +{ + using type = T; + using traits_type = Traits; + using carrier_uint = typename traits_type::carrier_uint; + + carrier_uint u; + + signed_significand_bits() = default; + constexpr explicit signed_significand_bits(carrier_uint bit_pattern) noexcept + : u{bit_pattern} {} + + // Shift the obtained signed significand bits to the left by 1 to remove the sign bit. + constexpr carrier_uint remove_sign_bit_and_shift() const noexcept + { + return traits_type::remove_sign_bit_and_shift(u); + } + + constexpr bool is_positive() const noexcept { return traits_type::is_positive(u); } + + constexpr bool is_negative() const noexcept { return traits_type::is_negative(u); } + + constexpr bool has_all_zero_significand_bits() const noexcept + { + return traits_type::has_all_zero_significand_bits(u); + } + + constexpr bool has_even_significand_bits() const noexcept + { + return traits_type::has_even_significand_bits(u); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////// +// Some simple utilities for constexpr computation. +//////////////////////////////////////////////////////////////////////////////////////// + +template +BOOST_JSON_CXX14_CONSTEXPR Int compute_power(Int a, Int2 exp) noexcept +{ + BOOST_ASSERT(exp >= 0); + + Int res = 1; + while (exp > 0) + { + if (exp % 2 != 0) + { + res *= a; + } + + a *= a; + exp >>= 1; + } + return res; +} + +static constexpr std::uint64_t power_of_10[] = { + UINT64_C(1), UINT64_C(10), UINT64_C(100), UINT64_C(1000), UINT64_C(10000), + UINT64_C(100000), UINT64_C(1000000), UINT64_C(10000000), UINT64_C(100000000), + UINT64_C(1000000000), UINT64_C(10000000000), UINT64_C(100000000000), UINT64_C(1000000000000), + UINT64_C(10000000000000), UINT64_C(100000000000000), UINT64_C(1000000000000000), + UINT64_C(10000000000000000), UINT64_C(100000000000000000), UINT64_C(1000000000000000000), + UINT64_C(10000000000000000000) +}; + +static_assert(sizeof(power_of_10) == 20 * sizeof(std::uint64_t), "There should be the first 20 powers of 10"); + + +template +BOOST_JSON_CXX14_CONSTEXPR int count_factors(UInt n) noexcept +{ + int c = 0; + + while (n % a == 0) + { + n /= a; + ++c; + } + return c; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// Utilities for fast/constexpr log computation. +//////////////////////////////////////////////////////////////////////////////////////// + +namespace log { +static_assert((-1 >> 1) == -1, "right-shift for signed integers must be arithmetic"); + +// Compute floor(e * c - s). +enum class multiply : std::uint32_t {}; +enum class subtract : std::uint32_t {}; +enum class shift : std::size_t {}; +enum class min_exponent : std::int32_t {}; +enum class max_exponent : std::int32_t {}; + +template +constexpr int compute(int e) noexcept +{ + return static_cast((std::int32_t(e) * std::int32_t(m) - std::int32_t(f)) >> std::size_t(k)); +} + +// For constexpr computation. +// Returns -1 when n = 0. +template +BOOST_JSON_CXX14_CONSTEXPR int floor_log2(UInt n) noexcept +{ + int count = -1; + while (n != 0) + { + ++count; + n >>= 1; + } + + return count; +} + +static constexpr int floor_log10_pow2_min_exponent = -2620; + +static constexpr int floor_log10_pow2_max_exponent = 2620; + +constexpr int floor_log10_pow2(int e) noexcept +{ + using namespace log; + return compute(e); +} + +static constexpr int floor_log2_pow10_min_exponent = -1233; + +static constexpr int floor_log2_pow10_max_exponent = 1233; + +constexpr int floor_log2_pow10(int e) noexcept +{ + using namespace log; + return compute(e); +} + +static constexpr int floor_log10_pow2_minus_log10_4_over_3_min_exponent = -2985; + +static constexpr int floor_log10_pow2_minus_log10_4_over_3_max_exponent = 2936; + +constexpr int floor_log10_pow2_minus_log10_4_over_3(int e) noexcept +{ + using namespace log; + return compute(e); +} + +static constexpr int floor_log5_pow2_min_exponent = -1831; + +static constexpr int floor_log5_pow2_max_exponent = 1831; + +constexpr int floor_log5_pow2(int e) noexcept +{ + using namespace log; + return compute(e); +} + +static constexpr int floor_log5_pow2_minus_log5_3_min_exponent = -3543; + +static constexpr int floor_log5_pow2_minus_log5_3_max_exponent = 2427; + +constexpr int floor_log5_pow2_minus_log5_3(int e) noexcept +{ + using namespace log; + return compute(e); +} +} // Namespace log + +} // namespace detail +} // namespace json +} // namespace boost + +#endif // BOOST_JSON_DETAIL_DRAGONBOX_DRAGONBOX_COMMON_HPP diff --git a/include/boost/json/detail/dragonbox/emulated128.hpp b/include/boost/json/detail/dragonbox/emulated128.hpp new file mode 100644 index 000000000..574f48256 --- /dev/null +++ b/include/boost/json/detail/dragonbox/emulated128.hpp @@ -0,0 +1,1014 @@ +// Copyright 2020-2023 Daniel Lemire +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt +// +// If the architecture (e.g. Apple ARM) does not have __int128 we need to emulate it + +#ifndef BOOST_JSON_DETAIL_DRAGONBOX_EMULATED128_HPP +#define BOOST_JSON_DETAIL_DRAGONBOX_EMULATED128_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace json { +namespace detail { + +// Compilers might support built-in 128-bit integer types. However, it seems that +// emulating them with a pair of 64-bit integers actually produces a better code, +// so we avoid using those built-ins. That said, they are still useful for +// implementing 64-bit x 64-bit -> 128-bit multiplication. + +// Memcpy-able temp class for uint128 +template +struct trivial_uint128_impl +{ + std::uint64_t low; + std::uint64_t high; +}; + +template <> +struct trivial_uint128_impl +{ + std::uint64_t high; + std::uint64_t low; +}; + +using trivial_uint128 = trivial_uint128_impl; + +// Macro replacement lists can not be enclosed in parentheses +struct uint128 +{ + std::uint64_t high; + std::uint64_t low; + + // Constructors + constexpr uint128() noexcept : high {}, low {} {} + + constexpr uint128(const uint128& v) noexcept = default; + + constexpr uint128(uint128&& v) noexcept = default; + + constexpr uint128(std::uint64_t high_, std::uint64_t low_) noexcept : high {high_}, low {low_} {} + + constexpr uint128(const trivial_uint128& v) noexcept : high {v.high}, low {v.low} {} // NOLINT + + constexpr uint128(trivial_uint128&& v) noexcept : high {v.high}, low {v.low} {} // NOLINT + + #define SIGNED_CONSTRUCTOR(expr) constexpr uint128(expr v) noexcept : high {v < 0 ? UINT64_MAX : UINT64_C(0)}, low {static_cast(v)} {} // NOLINT + #define UNSIGNED_CONSTRUCTOR(expr) constexpr uint128(expr v) noexcept : high {}, low {static_cast(v)} {} // NOLINT + + SIGNED_CONSTRUCTOR(char) // NOLINT + SIGNED_CONSTRUCTOR(signed char) // NOLINT + SIGNED_CONSTRUCTOR(short) // NOLINT + SIGNED_CONSTRUCTOR(int) // NOLINT + SIGNED_CONSTRUCTOR(long) // NOLINT + SIGNED_CONSTRUCTOR(long long) // NOLINT + + UNSIGNED_CONSTRUCTOR(unsigned char) // NOLINT + UNSIGNED_CONSTRUCTOR(unsigned short) // NOLINT + UNSIGNED_CONSTRUCTOR(unsigned) // NOLINT + UNSIGNED_CONSTRUCTOR(unsigned long) // NOLINT + UNSIGNED_CONSTRUCTOR(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr uint128(boost::int128_type v) noexcept : // NOLINT : Allow implicit conversions + high {static_cast(v >> 64)}, + low {static_cast(static_cast(v) & ~UINT64_C(0))} {} + + constexpr uint128(boost::uint128_type v) noexcept : // NOLINT : Allow implicit conversions + high {static_cast(v >> 64)}, + low {static_cast(v & ~UINT64_C(0))} {} + #endif + + #undef SIGNED_CONSTRUCTOR + #undef UNSIGNED_CONSTRUCTOR + + // Assignment Operators + #define SIGNED_ASSIGNMENT_OPERATOR(expr) BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const expr& v) noexcept { high = v < 0 ? UINT64_MAX : UINT64_C(0); low = static_cast(v); return *this; } // NOLINT + #define UNSIGNED_ASSIGNMENT_OPERATOR(expr) BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const expr& v) noexcept { high = 0U; low = static_cast(v); return *this; } // NOLINT + + SIGNED_ASSIGNMENT_OPERATOR(char) // NOLINT + SIGNED_ASSIGNMENT_OPERATOR(signed char) // NOLINT + SIGNED_ASSIGNMENT_OPERATOR(short) // NOLINT + SIGNED_ASSIGNMENT_OPERATOR(int) // NOLINT + SIGNED_ASSIGNMENT_OPERATOR(long) // NOLINT + SIGNED_ASSIGNMENT_OPERATOR(long long) // NOLINT + + UNSIGNED_ASSIGNMENT_OPERATOR(unsigned char) // NOLINT + UNSIGNED_ASSIGNMENT_OPERATOR(unsigned short) // NOLINT + UNSIGNED_ASSIGNMENT_OPERATOR(unsigned) // NOLINT + UNSIGNED_ASSIGNMENT_OPERATOR(unsigned long) // NOLINT + UNSIGNED_ASSIGNMENT_OPERATOR(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const boost::int128_type& v) noexcept { *this = uint128(v); return *this; } + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const boost::uint128_type& v) noexcept { *this = uint128(v); return *this; } + #endif + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const trivial_uint128& v) noexcept { this->low = v.low; this->high = v.high; return *this; } + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator=(const uint128&) noexcept; + + #undef SIGNED_ASSIGNMENT_OPERATOR + #undef UNSIGNED_ASSIGNMENT_OPERATOR + + // Conversion Operators + #define INTEGER_CONVERSION_OPERATOR(expr) explicit constexpr operator expr() const noexcept { return static_cast(low); } // NOLINT + #define FLOAT_CONVERSION_OPERATOR(expr) explicit operator expr() const noexcept { return std::ldexp(static_cast(high), 64) + static_cast(low); } // NOLINT + + INTEGER_CONVERSION_OPERATOR(char) // NOLINT + INTEGER_CONVERSION_OPERATOR(signed char) // NOLINT + INTEGER_CONVERSION_OPERATOR(short) // NOLINT + INTEGER_CONVERSION_OPERATOR(int) // NOLINT + INTEGER_CONVERSION_OPERATOR(long) // NOLINT + INTEGER_CONVERSION_OPERATOR(long long) // NOLINT + INTEGER_CONVERSION_OPERATOR(unsigned char) // NOLINT + INTEGER_CONVERSION_OPERATOR(unsigned short) // NOLINT + INTEGER_CONVERSION_OPERATOR(unsigned) // NOLINT + INTEGER_CONVERSION_OPERATOR(unsigned long) // NOLINT + INTEGER_CONVERSION_OPERATOR(unsigned long long) // NOLINT + + explicit constexpr operator bool() const noexcept { return high || low; } + + #ifdef BOOST_HAS_INT128 + explicit constexpr operator boost::int128_type() const noexcept { return (static_cast(high) << 64) + low; } + explicit constexpr operator boost::uint128_type() const noexcept { return (static_cast(high) << 64) + low; } + #endif + + FLOAT_CONVERSION_OPERATOR(float) // NOLINT + FLOAT_CONVERSION_OPERATOR(double) // NOLINT + FLOAT_CONVERSION_OPERATOR(long double) // NOLINT + + #undef INTEGER_CONVERSION_OPERATOR + #undef FLOAT_CONVERSION_OPERATOR + + // Unary Operators + constexpr friend uint128 operator-(uint128 val) noexcept; + constexpr friend uint128 operator+(uint128 val) noexcept; + + // Comparison Operators + + // Equality + #define INTEGER_OPERATOR_EQUAL(expr) constexpr friend bool operator==(uint128 lhs, expr rhs) noexcept { return lhs.high == 0 && rhs >= 0 && lhs.low == static_cast(rhs); } // NOLINT + #define UNSIGNED_INTEGER_OPERATOR_EQUAL(expr) constexpr friend bool operator==(uint128 lhs, expr rhs) noexcept { return lhs.high == 0 && lhs.low == static_cast(rhs); } // NOLINT + + INTEGER_OPERATOR_EQUAL(char) // NOLINT + INTEGER_OPERATOR_EQUAL(signed char) // NOLINT + INTEGER_OPERATOR_EQUAL(short) // NOLINT + INTEGER_OPERATOR_EQUAL(int) // NOLINT + INTEGER_OPERATOR_EQUAL(long) // NOLINT + INTEGER_OPERATOR_EQUAL(long long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_EQUAL(unsigned char) // NOLINT + UNSIGNED_INTEGER_OPERATOR_EQUAL(unsigned short) // NOLINT + UNSIGNED_INTEGER_OPERATOR_EQUAL(unsigned) // NOLINT + UNSIGNED_INTEGER_OPERATOR_EQUAL(unsigned long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_EQUAL(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr friend bool operator==(uint128 lhs, boost::int128_type rhs) noexcept { return lhs == uint128(rhs); } + constexpr friend bool operator==(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs == uint128(rhs); } + #endif + + constexpr friend bool operator==(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_EQUAL + #undef UNSIGNED_INTEGER_OPERATOR_EQUAL + + // Inequality + #define INTEGER_OPERATOR_NOTEQUAL(expr) constexpr friend bool operator!=(uint128 lhs, expr rhs) noexcept { return !(lhs == rhs); } // NOLINT + + INTEGER_OPERATOR_NOTEQUAL(char) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(signed char) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(short) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(int) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(long) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(long long) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(unsigned char) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(unsigned short) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(unsigned) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(unsigned long) // NOLINT + INTEGER_OPERATOR_NOTEQUAL(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr friend bool operator!=(uint128 lhs, boost::int128_type rhs) noexcept { return !(lhs == rhs); } + constexpr friend bool operator!=(uint128 lhs, boost::uint128_type rhs) noexcept { return !(lhs == rhs); } + #endif + + constexpr friend bool operator!=(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_NOTEQUAL + + // Less than + #define INTEGER_OPERATOR_LESS_THAN(expr) constexpr friend bool operator<(uint128 lhs, expr rhs) noexcept { return lhs.high == 0U && rhs > 0 && lhs.low < static_cast(rhs); } // NOLINT + #define UNSIGNED_INTEGER_OPERATOR_LESS_THAN(expr) constexpr friend bool operator<(uint128 lhs, expr rhs) noexcept { return lhs.high == 0U && lhs.low < static_cast(rhs); } // NOLINT + + INTEGER_OPERATOR_LESS_THAN(char) // NOLINT + INTEGER_OPERATOR_LESS_THAN(signed char) // NOLINT + INTEGER_OPERATOR_LESS_THAN(short) // NOLINT + INTEGER_OPERATOR_LESS_THAN(int) // NOLINT + INTEGER_OPERATOR_LESS_THAN(long) // NOLINT + INTEGER_OPERATOR_LESS_THAN(long long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN(unsigned char) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN(unsigned short) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN(unsigned) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN(unsigned long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<(uint128 lhs, boost::int128_type rhs) noexcept { return lhs < uint128(rhs); } + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs < uint128(rhs); } + #endif + + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_LESS_THAN + #undef UNSIGNED_INTEGER_OPERATOR_LESS_THAN + + // Less than or equal to + #define INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(expr) constexpr friend bool operator<=(uint128 lhs, expr rhs) noexcept { return lhs.high == 0U && rhs >= 0 && lhs.low <= static_cast(rhs); } // NOLINT + #define UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(expr) constexpr friend bool operator<=(uint128 lhs, expr rhs) noexcept { return lhs.high == 0U && lhs.low <= static_cast(rhs); } // NOLINT + + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(char) // NOLINT + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(signed char) // NOLINT + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(short) // NOLINT + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(int) // NOLINT + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(long) // NOLINT + INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(long long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(unsigned char) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(unsigned short) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(unsigned) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(unsigned long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<=(uint128 lhs, boost::int128_type rhs) noexcept { return lhs <= uint128(rhs); } + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<=(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs <= uint128(rhs); } + #endif + + BOOST_JSON_CXX14_CONSTEXPR friend bool operator<=(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO + #undef UNSIGNED_INTEGER_OPERATOR_LESS_THAN_OR_EQUAL_TO + + // Greater than + #define INTEGER_OPERATOR_GREATER_THAN(expr) constexpr friend bool operator>(uint128 lhs, expr rhs) noexcept { return lhs.high > 0U || rhs < 0 || lhs.low > static_cast(rhs); } // NOLINT + #define UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(expr) constexpr friend bool operator>(uint128 lhs, expr rhs) noexcept { return lhs.high > 0U || lhs.low > static_cast(rhs); } // NOLINT + + INTEGER_OPERATOR_GREATER_THAN(char) // NOLINT + INTEGER_OPERATOR_GREATER_THAN(signed char) // NOLINT + INTEGER_OPERATOR_GREATER_THAN(short) // NOLINT + INTEGER_OPERATOR_GREATER_THAN(int) // NOLINT + INTEGER_OPERATOR_GREATER_THAN(long) // NOLINT + INTEGER_OPERATOR_GREATER_THAN(long long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(unsigned char) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(unsigned short) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(unsigned) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(unsigned long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>(uint128 lhs, boost::int128_type rhs) noexcept { return lhs > uint128(rhs); } + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs > uint128(rhs); } + #endif + + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_GREATER_THAN + #undef UNSIGNED_INTEGER_OPERATOR_GREATER_THAN + + // Greater than or equal to + #define INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(expr) constexpr friend bool operator>=(uint128 lhs, expr rhs) noexcept { return lhs.high > 0U || rhs < 0 || lhs.low >= static_cast(rhs); } // NOLINT + #define UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(expr) constexpr friend bool operator>=(uint128 lhs, expr rhs) noexcept { return lhs.high > 0U || lhs.low >= static_cast(rhs); } // NOLINT + + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(char) // NOLINT + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(signed char) // NOLINT + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(short) // NOLINT + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(int) // NOLINT + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(long) // NOLINT + INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(long long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(unsigned char) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(unsigned short) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(unsigned) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(unsigned long) // NOLINT + UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>=(uint128 lhs, boost::int128_type rhs) noexcept { return lhs >= uint128(rhs); } + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>=(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs >= uint128(rhs); } + #endif + + BOOST_JSON_CXX14_CONSTEXPR friend bool operator>=(uint128 lhs, uint128 rhs) noexcept; + + #undef INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO + #undef UNSIGNED_INTEGER_OPERATOR_GREATER_THAN_OR_EQUAL_TO + + // Binary Operators + + // Not + constexpr friend uint128 operator~(uint128 v) noexcept; + + // Or + #define INTEGER_BINARY_OPERATOR_OR(expr) constexpr friend uint128 operator|(uint128 lhs, expr rhs) noexcept { return {lhs.high, lhs.low | static_cast(rhs)}; } // NOLINT + + INTEGER_BINARY_OPERATOR_OR(char) // NOLINT + INTEGER_BINARY_OPERATOR_OR(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_OR(short) // NOLINT + INTEGER_BINARY_OPERATOR_OR(int) // NOLINT + INTEGER_BINARY_OPERATOR_OR(long) // NOLINT + INTEGER_BINARY_OPERATOR_OR(long long) // NOLINT + INTEGER_BINARY_OPERATOR_OR(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_OR(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_OR(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_OR(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_OR(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr friend uint128 operator|(uint128 lhs, boost::int128_type rhs) noexcept { return lhs | uint128(rhs); } + constexpr friend uint128 operator|(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs | uint128(rhs); } + #endif + + constexpr friend uint128 operator|(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator|=(uint128 v) noexcept; + + #undef INTEGER_BINARY_OPERATOR_OR + + // And + #define INTEGER_BINARY_OPERATOR_AND(expr) constexpr friend uint128 operator&(uint128 lhs, expr rhs) noexcept { return {lhs.high, lhs.low & static_cast(rhs)}; } // NOLINT + + INTEGER_BINARY_OPERATOR_AND(char) // NOLINT + INTEGER_BINARY_OPERATOR_AND(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_AND(short) // NOLINT + INTEGER_BINARY_OPERATOR_AND(int) // NOLINT + INTEGER_BINARY_OPERATOR_AND(long) // NOLINT + INTEGER_BINARY_OPERATOR_AND(long long) // NOLINT + INTEGER_BINARY_OPERATOR_AND(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_AND(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_AND(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_AND(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_AND(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr friend uint128 operator&(uint128 lhs, boost::int128_type rhs) noexcept { return lhs & uint128(rhs); } + constexpr friend uint128 operator&(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs & uint128(rhs); } + #endif + + constexpr friend uint128 operator&(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator&=(uint128 v) noexcept; + + #undef INTEGER_BINARY_OPERATOR_AND + + // Xor + #define INTEGER_BINARY_OPERATOR_XOR(expr) constexpr friend uint128 operator^(uint128 lhs, expr rhs) noexcept { return {lhs.high, lhs.low ^ static_cast(rhs)}; } // NOLINT + + INTEGER_BINARY_OPERATOR_XOR(char) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(short) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(int) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(long) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(long long) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_XOR(unsigned long long) // NOLINT + + #ifdef BOOST_HAS_INT128 + constexpr friend uint128 operator^(uint128 lhs, boost::int128_type rhs) noexcept { return lhs ^ uint128(rhs); } + constexpr friend uint128 operator^(uint128 lhs, boost::uint128_type rhs) noexcept { return lhs ^ uint128(rhs); } + #endif + + constexpr friend uint128 operator^(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator^=(uint128 v) noexcept; + + #undef INTEGER_BINARY_OPERATOR_XOR + + // Left shift + #define INTEGER_BINARY_OPERATOR_LEFT_SHIFT(expr) \ + BOOST_CXX14_CONSTEXPR friend uint128 operator<<(uint128 lhs, expr rhs) noexcept \ + { \ + if (rhs >= 64) \ + { \ + return {lhs.low << (rhs - 64), 0}; \ + } \ + else if (rhs == 0) \ + { \ + return lhs; \ + } \ + \ + return {(lhs.high << rhs) | (lhs.low >> (64 - rhs)), lhs.low << rhs}; \ + } // NOLINT + + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(char) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(short) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(int) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(long) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(long long) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_LEFT_SHIFT(unsigned long long) // NOLINT + + #define INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(expr) \ + BOOST_CXX14_CONSTEXPR uint128 &operator<<=(expr amount) noexcept \ + { \ + *this = *this << amount; \ + return *this; \ + } // NOLINT + + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(short) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(int) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(long long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT(unsigned long long) // NOLINT + + #undef INTEGER_BINARY_OPERATOR_LEFT_SHIFT + #undef INTEGER_BINARY_OPERATOR_EQUALS_LEFT_SHIFT + + // Right Shift + #define INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(expr) \ + BOOST_CXX14_CONSTEXPR friend uint128 operator>>(uint128 lhs, expr amount) noexcept \ + { \ + if (amount >= 64) \ + { \ + return {0, lhs.high >> (amount - 64)}; \ + } \ + else if (amount == 0) \ + { \ + return lhs; \ + } \ + \ + return {lhs.high >> amount, (lhs.low >> amount) | (lhs.high << (64 - amount))}; \ + } // NOLINT + + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(char) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(short) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(int) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(long) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(long long) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_RIGHT_SHIFT(unsigned long long) // NOLINT + + #define INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(expr) \ + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator>>=(expr amount) noexcept \ + { \ + *this = *this >> amount; \ + return *this; \ + } // NOLINT + + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(signed char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(short) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(int) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(long long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(unsigned char) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(unsigned short) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(unsigned) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(unsigned long) // NOLINT + INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT(unsigned long long) // NOLINT + + #undef INTEGER_BINARY_OPERATOR_RIGHT_SHIFT + #undef INTEGER_BINARY_OPERATOR_EQUALS_RIGHT_SHIFT + + // Arithmetic operators (Add, sub, mul, div, mod) + inline uint128 &operator+=(std::uint64_t n) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend uint128 operator+(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator+=(uint128 v) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator++() noexcept; + + BOOST_JSON_CXX14_CONSTEXPR const uint128 operator++(int) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend uint128 operator-(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator-=(uint128 v) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator--() noexcept; + + BOOST_JSON_CXX14_CONSTEXPR const uint128 operator--(int) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend uint128 operator*(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator*=(uint128 v) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend uint128 operator/(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator/=(uint128 v) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend uint128 operator%(uint128 lhs, uint128 rhs) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR uint128 &operator%=(uint128 v) noexcept; + +private: + BOOST_JSON_CXX14_CONSTEXPR friend int high_bit(uint128 v) noexcept; + + BOOST_JSON_CXX14_CONSTEXPR friend void + div_impl(uint128 lhs, uint128 rhs, uint128 "ient, uint128 &remainder) noexcept; +}; + +constexpr uint128 operator-(uint128 val) noexcept +{ + return {~val.high + static_cast(val.low == 0), ~val.low + 1}; +} + +constexpr uint128 operator+(uint128 val) noexcept +{ + return val; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator=(const uint128& v) noexcept // NOLINT : User defined for older compilers +{ + low = v.low; + high = v.high; + return *this; +} + +constexpr bool operator==(uint128 lhs, uint128 rhs) noexcept +{ + return lhs.high == rhs.high && lhs.low == rhs.low; +} + +constexpr bool operator!=(uint128 lhs, uint128 rhs) noexcept +{ + return !(lhs == rhs); +} + +BOOST_JSON_CXX14_CONSTEXPR bool operator<(uint128 lhs, uint128 rhs) noexcept +{ + if (lhs.high == rhs.high) + { + return lhs.low < rhs.low; + } + + return lhs.high < rhs.high; +} + +BOOST_JSON_CXX14_CONSTEXPR bool operator<=(uint128 lhs, uint128 rhs) noexcept +{ + return !(rhs < lhs); +} + +BOOST_JSON_CXX14_CONSTEXPR bool operator>(uint128 lhs, uint128 rhs) noexcept +{ + return rhs < lhs; +} + +BOOST_JSON_CXX14_CONSTEXPR bool operator>=(uint128 lhs, uint128 rhs) noexcept +{ + return !(lhs < rhs); +} + +constexpr uint128 operator~(uint128 v) noexcept +{ + return {~v.high, ~v.low}; +} + +constexpr uint128 operator|(uint128 lhs, uint128 rhs) noexcept +{ + return {lhs.high | rhs.high, lhs.low | rhs.low}; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator|=(uint128 v) noexcept +{ + *this = *this | v; + return *this; +} + +constexpr uint128 operator&(uint128 lhs, uint128 rhs) noexcept +{ + return {lhs.high & rhs.high, lhs.low & rhs.low}; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator&=(uint128 v) noexcept +{ + *this = *this & v; + return *this; +} + +constexpr uint128 operator^(uint128 lhs, uint128 rhs) noexcept +{ + return {lhs.high ^ rhs.high, lhs.low ^ rhs.low}; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator^=(uint128 v) noexcept +{ + *this = *this ^ v; + return *this; +} + +inline uint128 &uint128::operator+=(std::uint64_t n) noexcept +{ + #if BOOST_JSON_HAS_BUILTIN(__builtin_addcll) + + unsigned long long carry {}; + low = __builtin_addcll(low, n, 0, &carry); + high = __builtin_addcll(high, 0, carry, &carry); + + #elif BOOST_JSON_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) + + unsigned long long result {}; + auto carry = __builtin_ia32_addcarryx_u64(0, low, n, &result); + low = result; + __builtin_ia32_addcarryx_u64(carry, high, 0, &result); + high = result; + + #elif defined(BOOST_MSVC) && defined(_M_X64) + + auto carry = _addcarry_u64(0, low, n, &low); + _addcarry_u64(carry, high, 0, &high); + + #else + + auto sum = low + n; + high += (sum < low ? 1 : 0); + low = sum; + + #endif + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 operator+(uint128 lhs, uint128 rhs) noexcept +{ + const uint128 temp = {lhs.high + rhs.high, lhs.low + rhs.low}; + + // Need to carry a bit into rhs + if (temp.low < lhs.low) + { + return {temp.high + 1, temp.low}; + } + + return temp; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator+=(uint128 v) noexcept +{ + *this = *this + v; + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator++() noexcept +{ + if (this->low == UINT64_MAX) + { + this->low = 0; + ++this->high; + } + else + { + ++this->low; + } + + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR const uint128 uint128::operator++(int) noexcept +{ + return ++(*this); +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 operator-(uint128 lhs, uint128 rhs) noexcept +{ + const uint128 temp {lhs.high - rhs.high, lhs.low - rhs.low}; + + // Check for carry + if (lhs.low < rhs.low) + { + return {temp.high - 1, temp.low}; + } + + return temp; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator-=(uint128 v) noexcept +{ + *this = *this - v; + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator--() noexcept +{ + if (this->low == 0) + { + this->low = UINT64_MAX; + --this->high; + } + else // NOLINT + { + --this->low; + } + + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR const uint128 uint128::operator--(int) noexcept +{ + return --(*this); +} +BOOST_JSON_CXX14_CONSTEXPR uint128 operator*(uint128 lhs, uint128 rhs) noexcept +{ + const auto a = static_cast(lhs.low >> 32); + const auto b = static_cast(lhs.low & UINT32_MAX); + const auto c = static_cast(rhs.low >> 32); + const auto d = static_cast(rhs.low & UINT32_MAX); + + uint128 result { lhs.high * rhs.low + lhs.low * rhs.high + a * c, b * d }; + result += uint128(a * d) << 32; + result += uint128(b * c) << 32; + return result; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator*=(uint128 v) noexcept +{ + *this = *this * v; + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR int high_bit(uint128 v) noexcept +{ + if (v.high != 0) + { + return 127 - boost::core::countl_zero(v.high); + } + else if (v.low != 0) + { + return 63 - boost::core::countl_zero(v.low); + } + + return 0; +} + +// See: https://stackoverflow.com/questions/5386377/division-without-using +BOOST_JSON_CXX14_CONSTEXPR void div_impl(uint128 lhs, uint128 rhs, uint128& quotient, uint128& remainder) noexcept +{ + constexpr uint128 one {0, 1}; + + if (rhs > lhs) + { + quotient = 0U; + remainder = 0U; + } + else if (lhs == rhs) + { + quotient = 1U; + remainder = 0U; + } + + uint128 denom = rhs; + quotient = 0U; + + std::int32_t shift = high_bit(lhs) - high_bit(rhs); + if (shift < 0) + { + shift = 32 - shift; + } + denom <<= shift; + + for (int i = 0; i <= shift; ++i) + { + quotient <<= 1; + if (lhs >= denom) + { + lhs -= denom; + quotient |= one; + } + denom >>= 1; + } + + remainder = lhs; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 operator/(uint128 lhs, uint128 rhs) noexcept +{ + uint128 quotient {0, 0}; + uint128 remainder {0, 0}; + div_impl(lhs, rhs, quotient, remainder); + + return quotient; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator/=(uint128 v) noexcept +{ + *this = *this / v; + return *this; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 operator%(uint128 lhs, uint128 rhs) noexcept +{ + uint128 quotient {0, 0}; + uint128 remainder {0, 0}; + div_impl(lhs, rhs, quotient, remainder); + + return remainder; +} + +BOOST_JSON_CXX14_CONSTEXPR uint128 &uint128::operator%=(uint128 v) noexcept +{ + *this = *this % v; + return *this; +} + +static inline std::uint64_t umul64(std::uint32_t x, std::uint32_t y) noexcept +{ + // __emulu is not available on ARM https://learn.microsoft.com/en-us/cpp/intrinsics/emul-emulu?view=msvc-170 + #if defined(BOOST_JSON_HAS_MSVC_32BIT_INTRINSICS) && !defined(_M_ARM) + + return __emulu(x, y); + + #else + + return x * static_cast(y); + + #endif +} + +// Get 128-bit result of multiplication of two 64-bit unsigned integers. +BOOST_JSON_SAFEBUFFERS inline uint128 umul128(std::uint64_t x, std::uint64_t y) noexcept +{ + #if defined(BOOST_HAS_INT128) + + auto result = static_cast(x) * static_cast(y); + return {static_cast(result >> 64), static_cast(result)}; + + // _umul128 is x64 only https://learn.microsoft.com/en-us/cpp/intrinsics/umul128?view=msvc-170 + #elif defined(BOOST_JSON_HAS_MSVC_64BIT_INTRINSICS) && !defined(_M_ARM64) + + unsigned long long high; + std::uint64_t low = _umul128(x, y, &high); + return {static_cast(high), low}; + + // https://developer.arm.com/documentation/dui0802/a/A64-General-Instructions/UMULH + #elif defined(_M_ARM64) && !defined(__MINGW32__) + + std::uint64_t high = __umulh(x, y); + std::uint64_t low = x * y; + return {high, low}; + + #else + + auto a = static_cast(x >> 32); + auto b = static_cast(x); + auto c = static_cast(y >> 32); + auto d = static_cast(y); + + auto ac = umul64(a, c); + auto bc = umul64(b, c); + auto ad = umul64(a, d); + auto bd = umul64(b, d); + + auto intermediate = (bd >> 32) + static_cast(ad) + static_cast(bc); + + return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32), + (intermediate << 32) + static_cast(bd)}; + + #endif +} + +BOOST_JSON_SAFEBUFFERS inline std::uint64_t umul128_upper64(std::uint64_t x, std::uint64_t y) noexcept +{ + #if defined(BOOST_HAS_INT128) + + auto result = static_cast(x) * static_cast(y); + return static_cast(result >> 64); + + #elif defined(BOOST_JSON_HAS_MSVC_64BIT_INTRINSICS) + + return __umulh(x, y); + + #else + + auto a = static_cast(x >> 32); + auto b = static_cast(x); + auto c = static_cast(y >> 32); + auto d = static_cast(y); + + auto ac = umul64(a, c); + auto bc = umul64(b, c); + auto ad = umul64(a, d); + auto bd = umul64(b, d); + + auto intermediate = (bd >> 32) + static_cast(ad) + static_cast(bc); + + return ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32); + + #endif +} + +// Get upper 128-bits of multiplication of a 64-bit unsigned integer and a 128-bit +// unsigned integer. +BOOST_JSON_SAFEBUFFERS inline uint128 umul192_upper128(std::uint64_t x, uint128 y) noexcept +{ + auto r = umul128(x, y.high); + r += umul128_upper64(x, y.low); + return r; +} + +// Get upper 64-bits of multiplication of a 32-bit unsigned integer and a 64-bit +// unsigned integer. +inline std::uint64_t umul96_upper64(std::uint32_t x, std::uint64_t y) noexcept +{ + #if defined(BOOST_HAS_INT128) || defined(BOOST_JSON_HAS_MSVC_64BIT_INTRINSICS) + + return umul128_upper64(static_cast(x) << 32, y); + + #else + + auto yh = static_cast(y >> 32); + auto yl = static_cast(y); + + auto xyh = umul64(x, yh); + auto xyl = umul64(x, yl); + + return xyh + (xyl >> 32); + + #endif +} + +// Get lower 128-bits of multiplication of a 64-bit unsigned integer and a 128-bit +// unsigned integer. +BOOST_JSON_SAFEBUFFERS inline uint128 umul192_lower128(std::uint64_t x, uint128 y) noexcept +{ + auto high = x * y.high; + auto highlow = umul128(x, y.low); + return {high + highlow.high, highlow.low}; +} + +// Get lower 64-bits of multiplication of a 32-bit unsigned integer and a 64-bit +// unsigned integer. +inline std::uint64_t umul96_lower64(std::uint32_t x, std::uint64_t y) noexcept +{ + return x * y; +} + +} // namespace detail +} // namespace json +} // namespace boost + +// Non-standard libraries may add specializations for library-provided types +namespace std { + +template <> +struct numeric_limits +{ + // Member constants + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_specialized = true; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_signed = false; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_integer = true; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_exact = true; + BOOST_ATTRIBUTE_UNUSED static constexpr bool has_infinity = false; + BOOST_ATTRIBUTE_UNUSED static constexpr bool has_quiet_NaN = false; + BOOST_ATTRIBUTE_UNUSED static constexpr bool has_signaling_NaN = false; + BOOST_ATTRIBUTE_UNUSED static constexpr std::float_round_style round_style = std::round_toward_zero; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_iec559 = false; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_bounded = true; + BOOST_ATTRIBUTE_UNUSED static constexpr bool is_modulo = true; + BOOST_ATTRIBUTE_UNUSED static constexpr int digits = 128; + BOOST_ATTRIBUTE_UNUSED static constexpr int digits10 = 38; + BOOST_ATTRIBUTE_UNUSED static constexpr int max_digits10 = 0; + BOOST_ATTRIBUTE_UNUSED static constexpr int radix = 2; + BOOST_ATTRIBUTE_UNUSED static constexpr int min_exponent = 0; + BOOST_ATTRIBUTE_UNUSED static constexpr int min_exponent10 = 0; + BOOST_ATTRIBUTE_UNUSED static constexpr int max_exponent = 0; + BOOST_ATTRIBUTE_UNUSED static constexpr int max_exponent10 = 0; + BOOST_ATTRIBUTE_UNUSED static constexpr bool traps = std::numeric_limits::traps; + BOOST_ATTRIBUTE_UNUSED static constexpr bool tinyness_before = false; + + // Member functions + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 (min)() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 lowest() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 (max)() { return {UINT64_MAX, UINT64_MAX}; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 epsilon() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 round_error() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 infinity() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 quiet_NaN() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 signaling_NaN() { return 0; } + BOOST_ATTRIBUTE_UNUSED static constexpr boost::json::detail::uint128 (denorm_min)() { return 0; } +}; + +} // Namespace std + +#endif // BOOST_JSON_DETAIL_DRAGONBOX_EMULATED128_HPP diff --git a/include/boost/json/detail/dragonbox/impl/to_chars.ipp b/include/boost/json/detail/dragonbox/impl/to_chars.ipp new file mode 100644 index 000000000..65f7e6f08 --- /dev/null +++ b/include/boost/json/detail/dragonbox/impl/to_chars.ipp @@ -0,0 +1,500 @@ +// Copyright 2020-2023 Junekey Jeon +// Copyright 2022 Peter Dimov +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace json { +namespace detail { +namespace to_chars_detail { + +#ifdef BOOST_MSVC +# pragma warning(push) +# pragma warning(disable: 4127) // Conditional expression is constant (e.g. BOOST_IF_CONSTEXPR statements) +#endif + + // These "//"'s are to prevent clang-format to ruin this nice alignment. + // Thanks to reddit user u/mcmcc: + // https://www.reddit.com/r/cpp/comments/so3wx9/dragonbox_110_is_released_a_fast_floattostring/hw8z26r/?context=3 + static constexpr char radix_100_head_table[] = { + '0', '.', '1', '.', '2', '.', '3', '.', '4', '.', // + '5', '.', '6', '.', '7', '.', '8', '.', '9', '.', // + '1', '.', '1', '.', '1', '.', '1', '.', '1', '.', // + '1', '.', '1', '.', '1', '.', '1', '.', '1', '.', // + '2', '.', '2', '.', '2', '.', '2', '.', '2', '.', // + '2', '.', '2', '.', '2', '.', '2', '.', '2', '.', // + '3', '.', '3', '.', '3', '.', '3', '.', '3', '.', // + '3', '.', '3', '.', '3', '.', '3', '.', '3', '.', // + '4', '.', '4', '.', '4', '.', '4', '.', '4', '.', // + '4', '.', '4', '.', '4', '.', '4', '.', '4', '.', // + '5', '.', '5', '.', '5', '.', '5', '.', '5', '.', // + '5', '.', '5', '.', '5', '.', '5', '.', '5', '.', // + '6', '.', '6', '.', '6', '.', '6', '.', '6', '.', // + '6', '.', '6', '.', '6', '.', '6', '.', '6', '.', // + '7', '.', '7', '.', '7', '.', '7', '.', '7', '.', // + '7', '.', '7', '.', '7', '.', '7', '.', '7', '.', // + '8', '.', '8', '.', '8', '.', '8', '.', '8', '.', // + '8', '.', '8', '.', '8', '.', '8', '.', '8', '.', // + '9', '.', '9', '.', '9', '.', '9', '.', '9', '.', // + '9', '.', '9', '.', '9', '.', '9', '.', '9', '.' // + }; + + static void print_1_digit(std::uint32_t n, char* buffer) noexcept + { + *buffer = char('0' + n); + } + + static void print_2_digits(std::uint32_t n, char* buffer) noexcept + { + std::memcpy(buffer, radix_table + n * 2, 2); + } + + // These digit generation routines are inspired by James Anhalt's itoa algorithm: + // https://github.com/jeaiii/itoa + // The main idea is for given n, find y such that floor(10^k * y / 2^32) = n holds, + // where k is an appropriate integer depending on the length of n. + // For example, if n = 1234567, we set k = 6. In this case, we have + // floor(y / 2^32) = 1, + // floor(10^2 * ((10^0 * y) mod 2^32) / 2^32) = 23, + // floor(10^2 * ((10^2 * y) mod 2^32) / 2^32) = 45, and + // floor(10^2 * ((10^4 * y) mod 2^32) / 2^32) = 67. + // See https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/ for more explanation. + + BOOST_FORCEINLINE static void print_9_digits(std::uint32_t s32, int& exponent, + char*& buffer) noexcept + { + // -- IEEE-754 binary32 + // Since we do not cut trailing zeros in advance, s32 must be of 6~9 digits + // unless the original input was subnormal. + // In particular, when it is of 9 digits it shouldn't have any trailing zeros. + // -- IEEE-754 binary64 + // In this case, s32 must be of 7~9 digits unless the input is subnormal, + // and it shouldn't have any trailing zeros if it is of 9 digits. + if (s32 >= 100000000) + { + // 9 digits. + // 1441151882 = ceil(2^57 / 1'0000'0000) + 1 + auto prod = s32 * std::uint64_t(1441151882); + prod >>= 25; + std::memcpy(buffer, radix_100_head_table + std::uint32_t(prod >> 32) * 2, 2); + + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 6); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 8); + + exponent += 8; + buffer += 10; + } + else if (s32 >= 1000000) + { + // 7 or 8 digits. + // 281474978 = ceil(2^48 / 100'0000) + 1 + auto prod = s32 * std::uint64_t(281474978); + prod >>= 16; + const auto head_digits = std::uint32_t(prod >> 32); + // If s32 is of 8 digits, increase the exponent by 7. + // Otherwise, increase it by 6. + exponent += static_cast(6 + unsigned(head_digits >= 10)); + + // Write the first digit and the decimal point. + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + // This third character may be overwritten later, but we don't care. + buffer[2] = radix_table[head_digits * 2 + 1]; + + // Remaining 6 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 1000000)) + { + // The number of characters actually need to be written is: + // 1, if only the first digit is nonzero, which means that either s32 is of 7 + // digits or it is of 8 digits but the second digit is zero, or + // 3, otherwise. + // Note that buffer[2] is never '0' if s32 is of 7 digits, because the input is + // never zero. + buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2); + } + else + { + // At least one of the remaining 6 digits are nonzero. + // After this adjustment, now the first destination becomes buffer + 2. + buffer += unsigned(head_digits >= 10); + + // Obtain the next two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + + // Remaining 4 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000)) + { + buffer += (3 + unsigned(buffer[3] > '0')); + } + else + { + // At least one of the remaining 4 digits are nonzero. + + // Obtain the next two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + + // Remaining 2 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100)) + { + buffer += (5 + unsigned(buffer[5] > '0')); + } + else + { + // Obtain the last two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 6); + + buffer += (7 + unsigned(buffer[7] > '0')); + } + } + } + } + else if (s32 >= 10000) + { + // 5 or 6 digits. + // 429497 = ceil(2^32 / 1'0000) + auto prod = s32 * std::uint64_t(429497); + const auto head_digits = std::uint32_t(prod >> 32); + + // If s32 is of 6 digits, increase the exponent by 5. + // Otherwise, increase it by 4. + exponent += static_cast(4 + unsigned(head_digits >= 10)); + + // Write the first digit and the decimal point. + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + // This third character may be overwritten later but we don't care. + buffer[2] = radix_table[head_digits * 2 + 1]; + + // Remaining 4 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000)) + { + // The number of characters actually written is 1 or 3, similarly to the case of + // 7 or 8 digits. + buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2); + } + else + { + // At least one of the remaining 4 digits are nonzero. + // After this adjustment, now the first destination becomes buffer + 2. + buffer += unsigned(head_digits >= 10); + + // Obtain the next two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + + // Remaining 2 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100)) + { + buffer += (3 + unsigned(buffer[3] > '0')); + } + else + { + // Obtain the last two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + + buffer += (5 + unsigned(buffer[5] > '0')); + } + } + } + else if (s32 >= 100) + { + // 3 or 4 digits. + // 42949673 = ceil(2^32 / 100) + auto prod = s32 * std::uint64_t(42949673); + const auto head_digits = std::uint32_t(prod >> 32); + + // If s32 is of 4 digits, increase the exponent by 3. + // Otherwise, increase it by 2. + exponent += (2 + int(head_digits >= 10)); + + // Write the first digit and the decimal point. + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + // This third character may be overwritten later but we don't care. + buffer[2] = radix_table[head_digits * 2 + 1]; + + // Remaining 2 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100)) + { + // The number of characters actually written is 1 or 3, similarly to the case of + // 7 or 8 digits. + buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2); + } + else + { + // At least one of the remaining 2 digits are nonzero. + // After this adjustment, now the first destination becomes buffer + 2. + buffer += unsigned(head_digits >= 10); + + // Obtain the last two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + + buffer += (3 + unsigned(buffer[3] > '0')); + } + } + else + { + // 1 or 2 digits. + // If s32 is of 2 digits, increase the exponent by 1. + exponent += int(s32 >= 10); + + // Write the first digit and the decimal point. + std::memcpy(buffer, radix_100_head_table + s32 * 2, 2); + // This third character may be overwritten later but we don't care. + buffer[2] = radix_table[s32 * 2 + 1]; + + // The number of characters actually written is 1 or 3, similarly to the case of + // 7 or 8 digits. + buffer += (1 + (unsigned(s32 >= 10) & unsigned(buffer[2] > '0')) * 2); + } + } + + std::size_t dragon_box_print_chars(const std::uint64_t significand, int exponent, char* first, char* last) noexcept + { + BOOST_ASSERT( detail::max_number_chars >= std::size_t(last - first) ); + + auto buffer = first; + // Print significand by decomposing it into a 9-digit block and a 8-digit block. + std::uint32_t first_block; + std::uint32_t second_block {}; + bool no_second_block; + + if (significand >= 100000000) + { + first_block = std::uint32_t(significand / 100000000); + second_block = std::uint32_t(significand) - first_block * 100000000; + exponent += 8; + no_second_block = (second_block == 0); + } + else + { + first_block = std::uint32_t(significand); + no_second_block = true; + } + + if (no_second_block) + { + print_9_digits(first_block, exponent, buffer); + } + else + { + // We proceed similarly to print_9_digits(), but since we do not need to remove + // trailing zeros, the procedure is a bit simpler. + if (first_block >= 100000000) + { + // The input is of 17 digits, thus there should be no trailing zero at all. + // The first block is of 9 digits. + // 1441151882 = ceil(2^57 / 1'0000'0000) + 1 + auto prod = first_block * std::uint64_t(1441151882); + prod >>= 25; + std::memcpy(buffer, radix_100_head_table + std::uint32_t(prod >> 32) * 2, 2); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 6); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 8); + + // The second block is of 8 digits. + // 281474978 = ceil(2^48 / 100'0000) + 1 + prod = second_block * std::uint64_t(281474978); + prod >>= 16; + prod += 1; + print_2_digits(std::uint32_t(prod >> 32), buffer + 10); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 12); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 14); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 16); + + exponent += 8; + buffer += 18; + } + else + { + if (first_block >= 1000000) + { + // 7 or 8 digits. + // 281474978 = ceil(2^48 / 100'0000) + 1 + auto prod = first_block * std::uint64_t(281474978); + prod >>= 16; + const auto head_digits = std::uint32_t(prod >> 32); + + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + buffer[2] = radix_table[head_digits * 2 + 1]; + + exponent += static_cast(6 + unsigned(head_digits >= 10)); + buffer += unsigned(head_digits >= 10); + + // Print remaining 6 digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 6); + + buffer += 8; + } + else if (first_block >= 10000) + { + // 5 or 6 digits. + // 429497 = ceil(2^32 / 1'0000) + auto prod = first_block * std::uint64_t(429497); + const auto head_digits = std::uint32_t(prod >> 32); + + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + buffer[2] = radix_table[head_digits * 2 + 1]; + + exponent += static_cast(4 + unsigned(head_digits >= 10)); + buffer += unsigned(head_digits >= 10); + + // Print remaining 4 digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + + buffer += 6; + } + else if (first_block >= 100) + { + // 3 or 4 digits. + // 42949673 = ceil(2^32 / 100) + auto prod = first_block * std::uint64_t(42949673); + const auto head_digits = std::uint32_t(prod >> 32); + + std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2); + buffer[2] = radix_table[head_digits * 2 + 1]; + + exponent += static_cast(2 + unsigned(head_digits >= 10)); + buffer += unsigned(head_digits >= 10); + + // Print remaining 2 digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + + buffer += 4; + } + else + { + // 1 or 2 digits. + std::memcpy(buffer, radix_100_head_table + first_block * 2, 2); + buffer[2] = radix_table[first_block * 2 + 1]; + + exponent += (first_block >= 10); + buffer += (2 + unsigned(first_block >= 10)); + } + + // Next, print the second block. + // The second block is of 8 digits, but we may have trailing zeros. + // 281474978 = ceil(2^48 / 100'0000) + 1 + auto prod = second_block * std::uint64_t(281474978); + prod >>= 16; + prod += 1; + print_2_digits(std::uint32_t(prod >> 32), buffer); + + // Remaining 6 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 1000000)) + { + buffer += (1 + unsigned(buffer[1] > '0')); + } + else + { + // Obtain the next two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 2); + + // Remaining 4 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000)) + { + buffer += (3 + unsigned(buffer[3] > '0')); + } + else + { + // Obtain the next two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 4); + + // Remaining 2 digits are all zero? + if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100)) + { + buffer += (5 + unsigned(buffer[5] > '0')); + } + else + { + // Obtain the last two digits. + prod = std::uint32_t(prod) * std::uint64_t(100); + print_2_digits(std::uint32_t(prod >> 32), buffer + 6); + buffer += (7 + unsigned(buffer[7] > '0')); + } + } + } + } + } + if (exponent < 0) + { + std::memcpy(buffer, "E-", 2); + buffer += 2; + exponent = -exponent; + } + else if (exponent == 0) + { + std::memcpy(buffer, "E0", 2); + return 2 + (buffer - first); + } + else + { + std::memcpy(buffer, "E+", 2); + buffer += 2; + } + + if (exponent >= 100) + { + // d1 = exponent / 10; d2 = exponent % 10; + // 6554 = ceil(2^16 / 10) + auto prod = std::uint32_t(exponent) * std::uint32_t(6554); + auto d1 = prod >> 16; + prod = std::uint16_t(prod) * std::uint32_t(5); // * 10 + auto d2 = prod >> 15; // >> 16 + print_2_digits(d1, buffer); + print_1_digit(d2, buffer + 2); + buffer += 3; + } + else + { + print_2_digits(static_cast(exponent), buffer); + buffer += 2; + } + + return buffer - first; + } + +#ifdef BOOST_MSVC +# pragma warning(pop) +#endif + +} // namespace to_chars_detail +} // namespace detail +} // namespace json +} // namespace boost diff --git a/include/boost/json/detail/dragonbox/to_chars_integer_impl.hpp b/include/boost/json/detail/dragonbox/to_chars_integer_impl.hpp new file mode 100644 index 000000000..6836d2596 --- /dev/null +++ b/include/boost/json/detail/dragonbox/to_chars_integer_impl.hpp @@ -0,0 +1,50 @@ +// Copyright 2020-2023 Junekey Jeon +// Copyright 2022 Peter Dimov +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_JSON_DETAIL_DRAGONBOX_TO_CHARS_INTEGER_IMPL_HPP +#define BOOST_JSON_DETAIL_DRAGONBOX_TO_CHARS_INTEGER_IMPL_HPP + +#include + +namespace boost { +namespace json { +namespace detail { + +static constexpr char radix_table[] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' +}; + +static constexpr char digit_table[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z' +}; + +} // namespace detail +} // namespace json +} // namespace boost + +#endif // BOOST_JSON_DETAIL_DRAGONBOX_TO_CHARS_INTEGER_IMPL_HPP diff --git a/include/boost/json/detail/format.hpp b/include/boost/json/detail/format.hpp index 4931b5027..4e72622d8 100644 --- a/include/boost/json/detail/format.hpp +++ b/include/boost/json/detail/format.hpp @@ -10,11 +10,13 @@ #ifndef BOOST_JSON_DETAIL_FORMAT_HPP #define BOOST_JSON_DETAIL_FORMAT_HPP +#include + namespace boost { namespace json { namespace detail { -int constexpr max_number_chars = +constexpr std::size_t max_number_chars = 1 + // '-' 19 + // unsigned 64-bit mantissa 1 + // 'e' @@ -33,7 +35,7 @@ format_int64( char* dest, int64_t i) noexcept; BOOST_JSON_DECL -unsigned +std::size_t format_double( char* dest, double d, bool allow_infinity_and_nan = false) noexcept; diff --git a/include/boost/json/detail/impl/format.ipp b/include/boost/json/detail/impl/format.ipp index 66307bf69..adcf05163 100644 --- a/include/boost/json/detail/impl/format.ipp +++ b/include/boost/json/detail/impl/format.ipp @@ -11,7 +11,9 @@ #ifndef BOOST_JSON_DETAIL_IMPL_FORMAT_IPP #define BOOST_JSON_DETAIL_IMPL_FORMAT_IPP -#include +#include +#include +#include #include namespace boost { @@ -110,12 +112,81 @@ format_int64( return 1 + format_uint64(dest, ui); } -unsigned +std::size_t format_double( char* dest, double d, bool allow_infinity_and_nan) noexcept { - return static_cast( - ryu::d2s_buffered_n(d, dest, allow_infinity_and_nan)); + using Traits = dragonbox_float_traits; + + auto const bits = dragonbox_float_bits(d); + auto const exponent_bits = bits.extract_exponent_bits(); + auto const s = bits.remove_exponent_bits(exponent_bits); + + if( bits.is_finite(exponent_bits) ) + { + bool is_negative; + if(( is_negative = s.is_negative() )) + { + *dest = '-'; + ++dest; + } + + if( bits.is_nonzero() ) + { + auto const decimal = to_decimal( + s, exponent_bits); + std::size_t const size = to_chars_detail::dragon_box_print_chars( + decimal.significand, + decimal.exponent, + dest, + dest + detail::max_number_chars); + BOOST_ASSERT( size ); + return size + is_negative; + } + else + { + std::memcpy(dest, "0E0", 3); + return 3 + is_negative; + } + } + + + if( allow_infinity_and_nan ) + { + if( s.has_all_zero_significand_bits() ) + { + if( s.is_negative() ) + { + std::memcpy(dest, "-Infinity", 9); + return 9; + } + else + { + std::memcpy(dest, "Infinity", 8); + return 8; + } + } + + std::memcpy(dest, "NaN", 3); + return 3; + } + + if( s.has_all_zero_significand_bits() ) + { + if( s.is_negative() ) + { + std::memcpy(dest, "-1e99999", 8); + return 8; + } + else + { + std::memcpy(dest, "1e99999", 7); + return 7; + } + } + + std::memcpy(dest, "null", 4); + return 4; } } // detail diff --git a/include/boost/json/detail/ryu/detail/common.hpp b/include/boost/json/detail/ryu/detail/common.hpp deleted file mode 100644 index 196060a3f..000000000 --- a/include/boost/json/detail/ryu/detail/common.hpp +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_DETAIL_COMMON_HPP -#define BOOST_JSON_DETAIL_RYU_DETAIL_COMMON_HPP - -#include -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -constexpr int DOUBLE_MANTISSA_BITS = 52; -constexpr int DOUBLE_EXPONENT_BITS = 11; -constexpr int DOUBLE_BIAS = 1023; - -#if defined(_M_IX86) || defined(_M_ARM) -#define BOOST_JSON_RYU_32_BIT_PLATFORM -#endif - -inline uint32_t decimalLength9(const uint32_t v) { - // Function precondition: v is not a 10-digit number. - // (f2s: 9 digits are sufficient for round-tripping.) - // (d2fixed: We print 9-digit blocks.) - BOOST_ASSERT(v < 1000000000); - if (v >= 100000000) { return 9; } - if (v >= 10000000) { return 8; } - if (v >= 1000000) { return 7; } - if (v >= 100000) { return 6; } - if (v >= 10000) { return 5; } - if (v >= 1000) { return 4; } - if (v >= 100) { return 3; } - if (v >= 10) { return 2; } - return 1; -} - -// Returns e == 0 ? 1 : ceil(log_2(5^e)). -inline int32_t pow5bits(const int32_t e) { - // This approximation works up to the point that the multiplication overflows at e = 3529. - // If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater - // than 2^9297. - BOOST_ASSERT(e >= 0); - BOOST_ASSERT(e <= 3528); - return (int32_t) (((((uint32_t) e) * 1217359) >> 19) + 1); -} - -// Returns floor(log_10(2^e)). -inline uint32_t log10Pow2(const int32_t e) { - // The first value this approximation fails for is 2^1651 which is just greater than 10^297. - BOOST_ASSERT(e >= 0); - BOOST_ASSERT(e <= 1650); - return (((uint32_t) e) * 78913) >> 18; -} - -// Returns floor(log_10(5^e)). -inline uint32_t log10Pow5(const int32_t e) { - // The first value this approximation fails for is 5^2621 which is just greater than 10^1832. - BOOST_ASSERT(e >= 0); - BOOST_ASSERT(e <= 2620); - return (((uint32_t) e) * 732923) >> 20; -} - -inline int copy_special_str(char * const result, const bool sign, const bool exponent, const bool mantissa) { - if (mantissa) { - memcpy(result, "NaN", 3); - return 3; - } - if (sign) { - result[0] = '-'; - } - if (exponent) { - memcpy(result + sign, "Infinity", 8); - return sign + 8; - } - memcpy(result + sign, "0E0", 3); - return sign + 3; -} - -inline -int -copy_special_str_conforming( - char* const result, bool sign, bool exponent, bool mantissa) -{ - if (mantissa) - { - memcpy(result, "null", 4); - return 4; - } - - if (sign) - result[0] = '-'; - - if (exponent) - { - memcpy(result + sign, "1e99999", 7); - return sign + 7; - } - - memcpy(result + sign, "0E0", 3); - return sign + 3; -} - -inline uint32_t float_to_bits(const float f) { - uint32_t bits = 0; - memcpy(&bits, &f, sizeof(float)); - return bits; -} - -inline uint64_t double_to_bits(const double d) { - uint64_t bits = 0; - memcpy(&bits, &d, sizeof(double)); - return bits; -} - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/detail/d2s.hpp b/include/boost/json/detail/ryu/detail/d2s.hpp deleted file mode 100644 index 28f8587eb..000000000 --- a/include/boost/json/detail/ryu/detail/d2s.hpp +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_DETAIL_D2S_HPP -#define BOOST_JSON_DETAIL_RYU_DETAIL_D2S_HPP - -#include -#include - -// Only include the full table if we're not optimizing for size. -#if !defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) -#include -#endif -#if defined(BOOST_JSON_RYU_HAS_UINT128) -typedef __uint128_t uint128_t; -#else -#include -#endif - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -constexpr int DOUBLE_POW5_INV_BITCOUNT = 122; -constexpr int DOUBLE_POW5_BITCOUNT = 121; - -#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) - -constexpr int POW5_TABLE_SIZE = 26; - -inline -std::uint64_t const -(&DOUBLE_POW5_TABLE() noexcept)[POW5_TABLE_SIZE] -{ - static constexpr std::uint64_t arr[26] = { - 1ull, 5ull, 25ull, 125ull, 625ull, 3125ull, 15625ull, 78125ull, 390625ull, - 1953125ull, 9765625ull, 48828125ull, 244140625ull, 1220703125ull, 6103515625ull, - 30517578125ull, 152587890625ull, 762939453125ull, 3814697265625ull, - 19073486328125ull, 95367431640625ull, 476837158203125ull, - 2384185791015625ull, 11920928955078125ull, 59604644775390625ull, - 298023223876953125ull //, 1490116119384765625ull - }; - return arr; -} - -inline -std::uint64_t const -(&DOUBLE_POW5_SPLIT2() noexcept)[13][2] -{ - static constexpr std::uint64_t arr[13][2] = { - { 0u, 72057594037927936u }, - { 10376293541461622784u, 93132257461547851u }, - { 15052517733678820785u, 120370621524202240u }, - { 6258995034005762182u, 77787690973264271u }, - { 14893927168346708332u, 100538234169297439u }, - { 4272820386026678563u, 129942622070561240u }, - { 7330497575943398595u, 83973451344588609u }, - { 18377130505971182927u, 108533142064701048u }, - { 10038208235822497557u, 140275798336537794u }, - { 7017903361312433648u, 90651109995611182u }, - { 6366496589810271835u, 117163813585596168u }, - { 9264989777501460624u, 75715339914673581u }, - { 17074144231291089770u, 97859783203563123u }}; - return arr; -} - -// Unfortunately, the results are sometimes off by one. We use an additional -// lookup table to store those cases and adjust the result. -inline -std::uint32_t const -(&POW5_OFFSETS() noexcept)[13] -{ - static constexpr std::uint32_t arr[13] = { - 0x00000000, 0x00000000, 0x00000000, 0x033c55be, 0x03db77d8, 0x0265ffb2, - 0x00000800, 0x01a8ff56, 0x00000000, 0x0037a200, 0x00004000, 0x03fffffc, - 0x00003ffe}; - return arr; -} - -inline -std::uint64_t const -(&DOUBLE_POW5_INV_SPLIT2() noexcept)[13][2] -{ - static constexpr std::uint64_t arr[13][2] = { - { 1u, 288230376151711744u }, - { 7661987648932456967u, 223007451985306231u }, - { 12652048002903177473u, 172543658669764094u }, - { 5522544058086115566u, 266998379490113760u }, - { 3181575136763469022u, 206579990246952687u }, - { 4551508647133041040u, 159833525776178802u }, - { 1116074521063664381u, 247330401473104534u }, - { 17400360011128145022u, 191362629322552438u }, - { 9297997190148906106u, 148059663038321393u }, - { 11720143854957885429u, 229111231347799689u }, - { 15401709288678291155u, 177266229209635622u }, - { 3003071137298187333u, 274306203439684434u }, - { 17516772882021341108u, 212234145163966538u }}; - return arr; -} - -inline -std::uint32_t const -(&POW5_INV_OFFSETS() noexcept)[20] -{ - static constexpr std::uint32_t arr[20] = { - 0x51505404, 0x55054514, 0x45555545, 0x05511411, 0x00505010, 0x00000004, - 0x00000000, 0x00000000, 0x55555040, 0x00505051, 0x00050040, 0x55554000, - 0x51659559, 0x00001000, 0x15000010, 0x55455555, 0x41404051, 0x00001010, - 0x00000014, 0x00000000}; - return arr; -} - -#if defined(BOOST_JSON_RYU_HAS_UINT128) - -// Computes 5^i in the form required by Ryu, and stores it in the given pointer. -inline -void -double_computePow5( - const std::uint32_t i, - std::uint64_t* const result) -{ - const std::uint32_t base = i / POW5_TABLE_SIZE; - const std::uint32_t base2 = base * POW5_TABLE_SIZE; - const std::uint32_t offset = i - base2; - const std::uint64_t* const mul = DOUBLE_POW5_SPLIT2()[base]; - if (offset == 0) - { - result[0] = mul[0]; - result[1] = mul[1]; - return; - } - const std::uint64_t m = DOUBLE_POW5_TABLE()[offset]; - const uint128_t b0 = ((uint128_t)m) * mul[0]; - const uint128_t b2 = ((uint128_t)m) * mul[1]; - const std::uint32_t delta = pow5bits(i) - pow5bits(base2); - const uint128_t shiftedSum = (b0 >> delta) + (b2 << (64 - delta)) + ((POW5_OFFSETS()[base] >> offset) & 1); - result[0] = (std::uint64_t)shiftedSum; - result[1] = (std::uint64_t)(shiftedSum >> 64); -} - -// Computes 5^-i in the form required by Ryu, and stores it in the given pointer. -inline -void -double_computeInvPow5( - const std::uint32_t i, - std::uint64_t* const result) -{ - const std::uint32_t base = (i + POW5_TABLE_SIZE - 1) / POW5_TABLE_SIZE; - const std::uint32_t base2 = base * POW5_TABLE_SIZE; - const std::uint32_t offset = base2 - i; - const std::uint64_t* const mul = DOUBLE_POW5_INV_SPLIT2()[base]; // 1/5^base2 - if (offset == 0) - { - result[0] = mul[0]; - result[1] = mul[1]; - return; - } - const std::uint64_t m = DOUBLE_POW5_TABLE()[offset]; // 5^offset - const uint128_t b0 = ((uint128_t)m) * (mul[0] - 1); - const uint128_t b2 = ((uint128_t)m) * mul[1]; // 1/5^base2 * 5^offset = 1/5^(base2-offset) = 1/5^i - const std::uint32_t delta = pow5bits(base2) - pow5bits(i); - const uint128_t shiftedSum = - ((b0 >> delta) + (b2 << (64 - delta))) + 1 + ((POW5_INV_OFFSETS()[i / 16] >> ((i % 16) << 1)) & 3); - result[0] = (std::uint64_t)shiftedSum; - result[1] = (std::uint64_t)(shiftedSum >> 64); -} - -#else // defined(BOOST_JSON_RYU_HAS_UINT128) - -// Computes 5^i in the form required by Ryu, and stores it in the given pointer. -inline -void -double_computePow5( - const std::uint32_t i, - std::uint64_t* const result) -{ - const std::uint32_t base = i / POW5_TABLE_SIZE; - const std::uint32_t base2 = base * POW5_TABLE_SIZE; - const std::uint32_t offset = i - base2; - const std::uint64_t* const mul = DOUBLE_POW5_SPLIT2()[base]; - if (offset == 0) - { - result[0] = mul[0]; - result[1] = mul[1]; - return; - } - std::uint64_t const m = DOUBLE_POW5_TABLE()[offset]; - std::uint64_t high1; - std::uint64_t const low1 = umul128(m, mul[1], &high1); - std::uint64_t high0; - std::uint64_t const low0 = umul128(m, mul[0], &high0); - std::uint64_t const sum = high0 + low1; - if (sum < high0) - ++high1; // overflow into high1 - // high1 | sum | low0 - std::uint32_t const delta = pow5bits(i) - pow5bits(base2); - result[0] = shiftright128(low0, sum, delta) + ((POW5_OFFSETS()[base] >> offset) & 1); - result[1] = shiftright128(sum, high1, delta); -} - -// Computes 5^-i in the form required by Ryu, and stores it in the given pointer. -inline -void -double_computeInvPow5( - const std::uint32_t i, - std::uint64_t* const result) -{ - const std::uint32_t base = (i + POW5_TABLE_SIZE - 1) / POW5_TABLE_SIZE; - const std::uint32_t base2 = base * POW5_TABLE_SIZE; - const std::uint32_t offset = base2 - i; - const std::uint64_t* const mul = DOUBLE_POW5_INV_SPLIT2()[base]; // 1/5^base2 - if (offset == 0) - { - result[0] = mul[0]; - result[1] = mul[1]; - return; - } - std::uint64_t const m = DOUBLE_POW5_TABLE()[offset]; - std::uint64_t high1; - std::uint64_t const low1 = umul128(m, mul[1], &high1); - std::uint64_t high0; - std::uint64_t const low0 = umul128(m, mul[0] - 1, &high0); - std::uint64_t const sum = high0 + low1; - if (sum < high0) - ++high1; // overflow into high1 - // high1 | sum | low0 - std::uint32_t const delta = pow5bits(base2) - pow5bits(i); - result[0] = shiftright128(low0, sum, delta) + 1 + ((POW5_INV_OFFSETS()[i / 16] >> ((i % 16) << 1)) & 3); - result[1] = shiftright128(sum, high1, delta); -} - -#endif // defined(BOOST_JSON_RYU_HAS_UINT128) - -#endif // defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/detail/d2s_full_table.hpp b/include/boost/json/detail/ryu/detail/d2s_full_table.hpp deleted file mode 100644 index 4cbec2593..000000000 --- a/include/boost/json/detail/ryu/detail/d2s_full_table.hpp +++ /dev/null @@ -1,365 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_DETAIL_D2S_FULL_TABLE_HPP -#define BOOST_JSON_DETAIL_RYU_DETAIL_D2S_FULL_TABLE_HPP - -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { - -// These tables are generated by PrintDoubleLookupTable. -inline -std::uint64_t const -(&DOUBLE_POW5_INV_SPLIT() noexcept)[292][2] -{ - static constexpr std::uint64_t arr[292][2] = { - { 1u, 288230376151711744u }, { 3689348814741910324u, 230584300921369395u }, - { 2951479051793528259u, 184467440737095516u }, { 17118578500402463900u, 147573952589676412u }, - { 12632330341676300947u, 236118324143482260u }, { 10105864273341040758u, 188894659314785808u }, - { 15463389048156653253u, 151115727451828646u }, { 17362724847566824558u, 241785163922925834u }, - { 17579528692795369969u, 193428131138340667u }, { 6684925324752475329u, 154742504910672534u }, - { 18074578149087781173u, 247588007857076054u }, { 18149011334012135262u, 198070406285660843u }, - { 3451162622983977240u, 158456325028528675u }, { 5521860196774363583u, 253530120045645880u }, - { 4417488157419490867u, 202824096036516704u }, { 7223339340677503017u, 162259276829213363u }, - { 7867994130342094503u, 259614842926741381u }, { 2605046489531765280u, 207691874341393105u }, - { 2084037191625412224u, 166153499473114484u }, { 10713157136084480204u, 265845599156983174u }, - { 12259874523609494487u, 212676479325586539u }, { 13497248433629505913u, 170141183460469231u }, - { 14216899864323388813u, 272225893536750770u }, { 11373519891458711051u, 217780714829400616u }, - { 5409467098425058518u, 174224571863520493u }, { 4965798542738183305u, 278759314981632789u }, - { 7661987648932456967u, 223007451985306231u }, { 2440241304404055250u, 178405961588244985u }, - { 3904386087046488400u, 285449538541191976u }, { 17880904128604832013u, 228359630832953580u }, - { 14304723302883865611u, 182687704666362864u }, { 15133127457049002812u, 146150163733090291u }, - { 16834306301794583852u, 233840261972944466u }, { 9778096226693756759u, 187072209578355573u }, - { 15201174610838826053u, 149657767662684458u }, { 2185786488890659746u, 239452428260295134u }, - { 5437978005854438120u, 191561942608236107u }, { 15418428848909281466u, 153249554086588885u }, - { 6222742084545298729u, 245199286538542217u }, { 16046240111861969953u, 196159429230833773u }, - { 1768945645263844993u, 156927543384667019u }, { 10209010661905972635u, 251084069415467230u }, - { 8167208529524778108u, 200867255532373784u }, { 10223115638361732810u, 160693804425899027u }, - { 1599589762411131202u, 257110087081438444u }, { 4969020624670815285u, 205688069665150755u }, - { 3975216499736652228u, 164550455732120604u }, { 13739044029062464211u, 263280729171392966u }, - { 7301886408508061046u, 210624583337114373u }, { 13220206756290269483u, 168499666669691498u }, - { 17462981995322520850u, 269599466671506397u }, { 6591687966774196033u, 215679573337205118u }, - { 12652048002903177473u, 172543658669764094u }, { 9175230360419352987u, 276069853871622551u }, - { 3650835473593572067u, 220855883097298041u }, { 17678063637842498946u, 176684706477838432u }, - { 13527506561580357021u, 282695530364541492u }, { 3443307619780464970u, 226156424291633194u }, - { 6443994910566282300u, 180925139433306555u }, { 5155195928453025840u, 144740111546645244u }, - { 15627011115008661990u, 231584178474632390u }, { 12501608892006929592u, 185267342779705912u }, - { 2622589484121723027u, 148213874223764730u }, { 4196143174594756843u, 237142198758023568u }, - { 10735612169159626121u, 189713759006418854u }, { 12277838550069611220u, 151771007205135083u }, - { 15955192865369467629u, 242833611528216133u }, { 1696107848069843133u, 194266889222572907u }, - { 12424932722681605476u, 155413511378058325u }, { 1433148282581017146u, 248661618204893321u }, - { 15903913885032455010u, 198929294563914656u }, { 9033782293284053685u, 159143435651131725u }, - { 14454051669254485895u, 254629497041810760u }, { 11563241335403588716u, 203703597633448608u }, - { 16629290697806691620u, 162962878106758886u }, { 781423413297334329u, 260740604970814219u }, - { 4314487545379777786u, 208592483976651375u }, { 3451590036303822229u, 166873987181321100u }, - { 5522544058086115566u, 266998379490113760u }, { 4418035246468892453u, 213598703592091008u }, - { 10913125826658934609u, 170878962873672806u }, { 10082303693170474728u, 273406340597876490u }, - { 8065842954536379782u, 218725072478301192u }, { 17520720807854834795u, 174980057982640953u }, - { 5897060404116273733u, 279968092772225526u }, { 1028299508551108663u, 223974474217780421u }, - { 15580034865808528224u, 179179579374224336u }, { 17549358155809824511u, 286687326998758938u }, - { 2971440080422128639u, 229349861599007151u }, { 17134547323305344204u, 183479889279205720u }, - { 13707637858644275364u, 146783911423364576u }, { 14553522944347019935u, 234854258277383322u }, - { 4264120725993795302u, 187883406621906658u }, { 10789994210278856888u, 150306725297525326u }, - { 9885293106962350374u, 240490760476040522u }, { 529536856086059653u, 192392608380832418u }, - { 7802327114352668369u, 153914086704665934u }, { 1415676938738538420u, 246262538727465495u }, - { 1132541550990830736u, 197010030981972396u }, { 15663428499760305882u, 157608024785577916u }, - { 17682787970132668764u, 252172839656924666u }, { 10456881561364224688u, 201738271725539733u }, - { 15744202878575200397u, 161390617380431786u }, { 17812026976236499989u, 258224987808690858u }, - { 3181575136763469022u, 206579990246952687u }, { 13613306553636506187u, 165263992197562149u }, - { 10713244041592678929u, 264422387516099439u }, { 12259944048016053467u, 211537910012879551u }, - { 6118606423670932450u, 169230328010303641u }, { 2411072648389671274u, 270768524816485826u }, - { 16686253377679378312u, 216614819853188660u }, { 13349002702143502650u, 173291855882550928u }, - { 17669055508687693916u, 277266969412081485u }, { 14135244406950155133u, 221813575529665188u }, - { 240149081334393137u, 177450860423732151u }, { 11452284974360759988u, 283921376677971441u }, - { 5472479164746697667u, 227137101342377153u }, { 11756680961281178780u, 181709681073901722u }, - { 2026647139541122378u, 145367744859121378u }, { 18000030682233437097u, 232588391774594204u }, - { 18089373360528660001u, 186070713419675363u }, { 3403452244197197031u, 148856570735740291u }, - { 16513570034941246220u, 238170513177184465u }, { 13210856027952996976u, 190536410541747572u }, - { 3189987192878576934u, 152429128433398058u }, { 1414630693863812771u, 243886605493436893u }, - { 8510402184574870864u, 195109284394749514u }, { 10497670562401807014u, 156087427515799611u }, - { 9417575270359070576u, 249739884025279378u }, { 14912757845771077107u, 199791907220223502u }, - { 4551508647133041040u, 159833525776178802u }, { 10971762650154775986u, 255733641241886083u }, - { 16156107749607641435u, 204586912993508866u }, { 9235537384944202825u, 163669530394807093u }, - { 11087511001168814197u, 261871248631691349u }, { 12559357615676961681u, 209496998905353079u }, - { 13736834907283479668u, 167597599124282463u }, { 18289587036911657145u, 268156158598851941u }, - { 10942320814787415393u, 214524926879081553u }, { 16132554281313752961u, 171619941503265242u }, - { 11054691591134363444u, 274591906405224388u }, { 16222450902391311402u, 219673525124179510u }, - { 12977960721913049122u, 175738820099343608u }, { 17075388340318968271u, 281182112158949773u }, - { 2592264228029443648u, 224945689727159819u }, { 5763160197165465241u, 179956551781727855u }, - { 9221056315464744386u, 287930482850764568u }, { 14755542681855616155u, 230344386280611654u }, - { 15493782960226403247u, 184275509024489323u }, { 1326979923955391628u, 147420407219591459u }, - { 9501865507812447252u, 235872651551346334u }, { 11290841220991868125u, 188698121241077067u }, - { 1653975347309673853u, 150958496992861654u }, { 10025058185179298811u, 241533595188578646u }, - { 4330697733401528726u, 193226876150862917u }, { 14532604630946953951u, 154581500920690333u }, - { 1116074521063664381u, 247330401473104534u }, { 4582208431592841828u, 197864321178483627u }, - { 14733813189500004432u, 158291456942786901u }, { 16195403473716186445u, 253266331108459042u }, - { 5577625149489128510u, 202613064886767234u }, { 8151448934333213131u, 162090451909413787u }, - { 16731667109675051333u, 259344723055062059u }, { 17074682502481951390u, 207475778444049647u }, - { 6281048372501740465u, 165980622755239718u }, { 6360328581260874421u, 265568996408383549u }, - { 8777611679750609860u, 212455197126706839u }, { 10711438158542398211u, 169964157701365471u }, - { 9759603424184016492u, 271942652322184754u }, { 11497031554089123517u, 217554121857747803u }, - { 16576322872755119460u, 174043297486198242u }, { 11764721337440549842u, 278469275977917188u }, - { 16790474699436260520u, 222775420782333750u }, { 13432379759549008416u, 178220336625867000u }, - { 3045063541568861850u, 285152538601387201u }, { 17193446092222730773u, 228122030881109760u }, - { 13754756873778184618u, 182497624704887808u }, { 18382503128506368341u, 145998099763910246u }, - { 3586563302416817083u, 233596959622256395u }, { 2869250641933453667u, 186877567697805116u }, - { 17052795772514404226u, 149502054158244092u }, { 12527077977055405469u, 239203286653190548u }, - { 17400360011128145022u, 191362629322552438u }, { 2852241564676785048u, 153090103458041951u }, - { 15631632947708587046u, 244944165532867121u }, { 8815957543424959314u, 195955332426293697u }, - { 18120812478965698421u, 156764265941034957u }, { 14235904707377476180u, 250822825505655932u }, - { 4010026136418160298u, 200658260404524746u }, { 17965416168102169531u, 160526608323619796u }, - { 2919224165770098987u, 256842573317791675u }, { 2335379332616079190u, 205474058654233340u }, - { 1868303466092863352u, 164379246923386672u }, { 6678634360490491686u, 263006795077418675u }, - { 5342907488392393349u, 210405436061934940u }, { 4274325990713914679u, 168324348849547952u }, - { 10528270399884173809u, 269318958159276723u }, { 15801313949391159694u, 215455166527421378u }, - { 1573004715287196786u, 172364133221937103u }, { 17274202803427156150u, 275782613155099364u }, - { 17508711057483635243u, 220626090524079491u }, { 10317620031244997871u, 176500872419263593u }, - { 12818843235250086271u, 282401395870821749u }, { 13944423402941979340u, 225921116696657399u }, - { 14844887537095493795u, 180736893357325919u }, { 15565258844418305359u, 144589514685860735u }, - { 6457670077359736959u, 231343223497377177u }, { 16234182506113520537u, 185074578797901741u }, - { 9297997190148906106u, 148059663038321393u }, { 11187446689496339446u, 236895460861314229u }, - { 12639306166338981880u, 189516368689051383u }, { 17490142562555006151u, 151613094951241106u }, - { 2158786396894637579u, 242580951921985771u }, { 16484424376483351356u, 194064761537588616u }, - { 9498190686444770762u, 155251809230070893u }, { 11507756283569722895u, 248402894768113429u }, - { 12895553841597688639u, 198722315814490743u }, { 17695140702761971558u, 158977852651592594u }, - { 17244178680193423523u, 254364564242548151u }, { 10105994129412828495u, 203491651394038521u }, - { 4395446488788352473u, 162793321115230817u }, { 10722063196803274280u, 260469313784369307u }, - { 1198952927958798777u, 208375451027495446u }, { 15716557601334680315u, 166700360821996356u }, - { 17767794532651667857u, 266720577315194170u }, { 14214235626121334286u, 213376461852155336u }, - { 7682039686155157106u, 170701169481724269u }, { 1223217053622520399u, 273121871170758831u }, - { 15735968901865657612u, 218497496936607064u }, { 16278123936234436413u, 174797997549285651u }, - { 219556594781725998u, 279676796078857043u }, { 7554342905309201445u, 223741436863085634u }, - { 9732823138989271479u, 178993149490468507u }, { 815121763415193074u, 286389039184749612u }, - { 11720143854957885429u, 229111231347799689u }, { 13065463898708218666u, 183288985078239751u }, - { 6763022304224664610u, 146631188062591801u }, { 3442138057275642729u, 234609900900146882u }, - { 13821756890046245153u, 187687920720117505u }, { 11057405512036996122u, 150150336576094004u }, - { 6623802375033462826u, 240240538521750407u }, { 16367088344252501231u, 192192430817400325u }, - { 13093670675402000985u, 153753944653920260u }, { 2503129006933649959u, 246006311446272417u }, - { 13070549649772650937u, 196805049157017933u }, { 17835137349301941396u, 157444039325614346u }, - { 2710778055689733971u, 251910462920982955u }, { 2168622444551787177u, 201528370336786364u }, - { 5424246770383340065u, 161222696269429091u }, { 1300097203129523457u, 257956314031086546u }, - { 15797473021471260058u, 206365051224869236u }, { 8948629602435097724u, 165092040979895389u }, - { 3249760919670425388u, 264147265567832623u }, { 9978506365220160957u, 211317812454266098u }, - { 15361502721659949412u, 169054249963412878u }, { 2442311466204457120u, 270486799941460606u }, - { 16711244431931206989u, 216389439953168484u }, { 17058344360286875914u, 173111551962534787u }, - { 12535955717491360170u, 276978483140055660u }, { 10028764573993088136u, 221582786512044528u }, - { 15401709288678291155u, 177266229209635622u }, { 9885339602917624555u, 283625966735416996u }, - { 4218922867592189321u, 226900773388333597u }, { 14443184738299482427u, 181520618710666877u }, - { 4175850161155765295u, 145216494968533502u }, { 10370709072591134795u, 232346391949653603u }, - { 15675264887556728482u, 185877113559722882u }, { 5161514280561562140u, 148701690847778306u }, - { 879725219414678777u, 237922705356445290u }, { 703780175531743021u, 190338164285156232u }, - { 11631070584651125387u, 152270531428124985u }, { 162968861732249003u, 243632850284999977u }, - { 11198421533611530172u, 194906280227999981u }, { 5269388412147313814u, 155925024182399985u }, - { 8431021459435702103u, 249480038691839976u }, { 3055468352806651359u, 199584030953471981u }, - { 17201769941212962380u, 159667224762777584u }, { 16454785461715008838u, 255467559620444135u }, - { 13163828369372007071u, 204374047696355308u }, { 17909760324981426303u, 163499238157084246u }, - { 2830174816776909822u, 261598781051334795u }, { 2264139853421527858u, 209279024841067836u }, - { 16568707141704863579u, 167423219872854268u }, { 4373838538276319787u, 267877151796566830u }, - { 3499070830621055830u, 214301721437253464u }, { 6488605479238754987u, 171441377149802771u }, - { 3003071137298187333u, 274306203439684434u }, { 6091805724580460189u, 219444962751747547u }, - { 15941491023890099121u, 175555970201398037u }, { 10748990379256517301u, 280889552322236860u }, - { 8599192303405213841u, 224711641857789488u }, { 14258051472207991719u, 179769313486231590u }}; - return arr; -} - -inline -std::uint64_t const -(&DOUBLE_POW5_SPLIT() noexcept)[326][2] -{ - static constexpr std::uint64_t arr[326][2] = { - { 0u, 72057594037927936u }, { 0u, 90071992547409920u }, - { 0u, 112589990684262400u }, { 0u, 140737488355328000u }, - { 0u, 87960930222080000u }, { 0u, 109951162777600000u }, - { 0u, 137438953472000000u }, { 0u, 85899345920000000u }, - { 0u, 107374182400000000u }, { 0u, 134217728000000000u }, - { 0u, 83886080000000000u }, { 0u, 104857600000000000u }, - { 0u, 131072000000000000u }, { 0u, 81920000000000000u }, - { 0u, 102400000000000000u }, { 0u, 128000000000000000u }, - { 0u, 80000000000000000u }, { 0u, 100000000000000000u }, - { 0u, 125000000000000000u }, { 0u, 78125000000000000u }, - { 0u, 97656250000000000u }, { 0u, 122070312500000000u }, - { 0u, 76293945312500000u }, { 0u, 95367431640625000u }, - { 0u, 119209289550781250u }, { 4611686018427387904u, 74505805969238281u }, - { 10376293541461622784u, 93132257461547851u }, { 8358680908399640576u, 116415321826934814u }, - { 612489549322387456u, 72759576141834259u }, { 14600669991935148032u, 90949470177292823u }, - { 13639151471491547136u, 113686837721616029u }, { 3213881284082270208u, 142108547152020037u }, - { 4314518811765112832u, 88817841970012523u }, { 781462496279003136u, 111022302462515654u }, - { 10200200157203529728u, 138777878078144567u }, { 13292654125893287936u, 86736173798840354u }, - { 7392445620511834112u, 108420217248550443u }, { 4628871007212404736u, 135525271560688054u }, - { 16728102434789916672u, 84703294725430033u }, { 7075069988205232128u, 105879118406787542u }, - { 18067209522111315968u, 132348898008484427u }, { 8986162942105878528u, 82718061255302767u }, - { 6621017659204960256u, 103397576569128459u }, { 3664586055578812416u, 129246970711410574u }, - { 16125424340018921472u, 80779356694631608u }, { 1710036351314100224u, 100974195868289511u }, - { 15972603494424788992u, 126217744835361888u }, { 9982877184015493120u, 78886090522101180u }, - { 12478596480019366400u, 98607613152626475u }, { 10986559581596820096u, 123259516440783094u }, - { 2254913720070624656u, 77037197775489434u }, { 12042014186943056628u, 96296497219361792u }, - { 15052517733678820785u, 120370621524202240u }, { 9407823583549262990u, 75231638452626400u }, - { 11759779479436578738u, 94039548065783000u }, { 14699724349295723422u, 117549435082228750u }, - { 4575641699882439235u, 73468396926392969u }, { 10331238143280436948u, 91835496157991211u }, - { 8302361660673158281u, 114794370197489014u }, { 1154580038986672043u, 143492962746861268u }, - { 9944984561221445835u, 89683101716788292u }, { 12431230701526807293u, 112103877145985365u }, - { 1703980321626345405u, 140129846432481707u }, { 17205888765512323542u, 87581154020301066u }, - { 12283988920035628619u, 109476442525376333u }, { 1519928094762372062u, 136845553156720417u }, - { 12479170105294952299u, 85528470722950260u }, { 15598962631618690374u, 106910588403687825u }, - { 5663645234241199255u, 133638235504609782u }, { 17374836326682913246u, 83523897190381113u }, - { 7883487353071477846u, 104404871487976392u }, { 9854359191339347308u, 130506089359970490u }, - { 10770660513014479971u, 81566305849981556u }, { 13463325641268099964u, 101957882312476945u }, - { 2994098996302961243u, 127447352890596182u }, { 15706369927971514489u, 79654595556622613u }, - { 5797904354682229399u, 99568244445778267u }, { 2635694424925398845u, 124460305557222834u }, - { 6258995034005762182u, 77787690973264271u }, { 3212057774079814824u, 97234613716580339u }, - { 17850130272881932242u, 121543267145725423u }, { 18073860448192289507u, 75964541966078389u }, - { 8757267504958198172u, 94955677457597987u }, { 6334898362770359811u, 118694596821997484u }, - { 13182683513586250689u, 74184123013748427u }, { 11866668373555425458u, 92730153767185534u }, - { 5609963430089506015u, 115912692208981918u }, { 17341285199088104971u, 72445432630613698u }, - { 12453234462005355406u, 90556790788267123u }, { 10954857059079306353u, 113195988485333904u }, - { 13693571323849132942u, 141494985606667380u }, { 17781854114260483896u, 88434366004167112u }, - { 3780573569116053255u, 110542957505208891u }, { 114030942967678664u, 138178696881511114u }, - { 4682955357782187069u, 86361685550944446u }, { 15077066234082509644u, 107952106938680557u }, - { 5011274737320973344u, 134940133673350697u }, { 14661261756894078100u, 84337583545844185u }, - { 4491519140835433913u, 105421979432305232u }, { 5614398926044292391u, 131777474290381540u }, - { 12732371365632458552u, 82360921431488462u }, { 6692092170185797382u, 102951151789360578u }, - { 17588487249587022536u, 128688939736700722u }, { 15604490549419276989u, 80430587335437951u }, - { 14893927168346708332u, 100538234169297439u }, { 14005722942005997511u, 125672792711621799u }, - { 15671105866394830300u, 78545495444763624u }, { 1142138259283986260u, 98181869305954531u }, - { 15262730879387146537u, 122727336632443163u }, { 7233363790403272633u, 76704585395276977u }, - { 13653390756431478696u, 95880731744096221u }, { 3231680390257184658u, 119850914680120277u }, - { 4325643253124434363u, 74906821675075173u }, { 10018740084832930858u, 93633527093843966u }, - { 3300053069186387764u, 117041908867304958u }, { 15897591223523656064u, 73151193042065598u }, - { 10648616992549794273u, 91438991302581998u }, { 4087399203832467033u, 114298739128227498u }, - { 14332621041645359599u, 142873423910284372u }, { 18181260187883125557u, 89295889943927732u }, - { 4279831161144355331u, 111619862429909666u }, { 14573160988285219972u, 139524828037387082u }, - { 13719911636105650386u, 87203017523366926u }, { 7926517508277287175u, 109003771904208658u }, - { 684774848491833161u, 136254714880260823u }, { 7345513307948477581u, 85159196800163014u }, - { 18405263671790372785u, 106448996000203767u }, { 18394893571310578077u, 133061245000254709u }, - { 13802651491282805250u, 83163278125159193u }, { 3418256308821342851u, 103954097656448992u }, - { 4272820386026678563u, 129942622070561240u }, { 2670512741266674102u, 81214138794100775u }, - { 17173198981865506339u, 101517673492625968u }, { 3019754653622331308u, 126897091865782461u }, - { 4193189667727651020u, 79310682416114038u }, { 14464859121514339583u, 99138353020142547u }, - { 13469387883465536574u, 123922941275178184u }, { 8418367427165960359u, 77451838296986365u }, - { 15134645302384838353u, 96814797871232956u }, { 471562554271496325u, 121018497339041196u }, - { 9518098633274461011u, 75636560836900747u }, { 7285937273165688360u, 94545701046125934u }, - { 18330793628311886258u, 118182126307657417u }, { 4539216990053847055u, 73863828942285886u }, - { 14897393274422084627u, 92329786177857357u }, { 4786683537745442072u, 115412232722321697u }, - { 14520892257159371055u, 72132645451451060u }, { 18151115321449213818u, 90165806814313825u }, - { 8853836096529353561u, 112707258517892282u }, { 1843923083806916143u, 140884073147365353u }, - { 12681666973447792349u, 88052545717103345u }, { 2017025661527576725u, 110065682146379182u }, - { 11744654113764246714u, 137582102682973977u }, { 422879793461572340u, 85988814176858736u }, - { 528599741826965425u, 107486017721073420u }, { 660749677283706782u, 134357522151341775u }, - { 7330497575943398595u, 83973451344588609u }, { 13774807988356636147u, 104966814180735761u }, - { 3383451930163631472u, 131208517725919702u }, { 15949715511634433382u, 82005323578699813u }, - { 6102086334260878016u, 102506654473374767u }, { 3015921899398709616u, 128133318091718459u }, - { 18025852251620051174u, 80083323807324036u }, { 4085571240815512351u, 100104154759155046u }, - { 14330336087874166247u, 125130193448943807u }, { 15873989082562435760u, 78206370905589879u }, - { 15230800334775656796u, 97757963631987349u }, { 5203442363187407284u, 122197454539984187u }, - { 946308467778435600u, 76373409087490117u }, { 5794571603150432404u, 95466761359362646u }, - { 16466586540792816313u, 119333451699203307u }, { 7985773578781816244u, 74583407312002067u }, - { 5370530955049882401u, 93229259140002584u }, { 6713163693812353001u, 116536573925003230u }, - { 18030785363914884337u, 72835358703127018u }, { 13315109668038829614u, 91044198378908773u }, - { 2808829029766373305u, 113805247973635967u }, { 17346094342490130344u, 142256559967044958u }, - { 6229622945628943561u, 88910349979403099u }, { 3175342663608791547u, 111137937474253874u }, - { 13192550366365765242u, 138922421842817342u }, { 3633657960551215372u, 86826513651760839u }, - { 18377130505971182927u, 108533142064701048u }, { 4524669058754427043u, 135666427580876311u }, - { 9745447189362598758u, 84791517238047694u }, { 2958436949848472639u, 105989396547559618u }, - { 12921418224165366607u, 132486745684449522u }, { 12687572408530742033u, 82804216052780951u }, - { 11247779492236039638u, 103505270065976189u }, { 224666310012885835u, 129381587582470237u }, - { 2446259452971747599u, 80863492239043898u }, { 12281196353069460307u, 101079365298804872u }, - { 15351495441336825384u, 126349206623506090u }, { 14206370669262903769u, 78968254139691306u }, - { 8534591299723853903u, 98710317674614133u }, { 15279925143082205283u, 123387897093267666u }, - { 14161639232853766206u, 77117435683292291u }, { 13090363022639819853u, 96396794604115364u }, - { 16362953778299774816u, 120495993255144205u }, { 12532689120651053212u, 75309995784465128u }, - { 15665861400813816515u, 94137494730581410u }, { 10358954714162494836u, 117671868413226763u }, - { 4168503687137865320u, 73544917758266727u }, { 598943590494943747u, 91931147197833409u }, - { 5360365506546067587u, 114913933997291761u }, { 11312142901609972388u, 143642417496614701u }, - { 9375932322719926695u, 89776510935384188u }, { 11719915403399908368u, 112220638669230235u }, - { 10038208235822497557u, 140275798336537794u }, { 10885566165816448877u, 87672373960336121u }, - { 18218643725697949000u, 109590467450420151u }, { 18161618638695048346u, 136988084313025189u }, - { 13656854658398099168u, 85617552695640743u }, { 12459382304570236056u, 107021940869550929u }, - { 1739169825430631358u, 133777426086938662u }, { 14922039196176308311u, 83610891304336663u }, - { 14040862976792997485u, 104513614130420829u }, { 3716020665709083144u, 130642017663026037u }, - { 4628355925281870917u, 81651261039391273u }, { 10397130925029726550u, 102064076299239091u }, - { 8384727637859770284u, 127580095374048864u }, { 5240454773662356427u, 79737559608780540u }, - { 6550568467077945534u, 99671949510975675u }, { 3576524565420044014u, 124589936888719594u }, - { 6847013871814915412u, 77868710555449746u }, { 17782139376623420074u, 97335888194312182u }, - { 13004302183924499284u, 121669860242890228u }, { 17351060901807587860u, 76043662651806392u }, - { 3242082053549933210u, 95054578314757991u }, { 17887660622219580224u, 118818222893447488u }, - { 11179787888887237640u, 74261389308404680u }, { 13974734861109047050u, 92826736635505850u }, - { 8245046539531533005u, 116033420794382313u }, { 16682369133275677888u, 72520887996488945u }, - { 7017903361312433648u, 90651109995611182u }, { 17995751238495317868u, 113313887494513977u }, - { 8659630992836983623u, 141642359368142472u }, { 5412269370523114764u, 88526474605089045u }, - { 11377022731581281359u, 110658093256361306u }, { 4997906377621825891u, 138322616570451633u }, - { 14652906532082110942u, 86451635356532270u }, { 9092761128247862869u, 108064544195665338u }, - { 2142579373455052779u, 135080680244581673u }, { 12868327154477877747u, 84425425152863545u }, - { 2250350887815183471u, 105531781441079432u }, { 2812938609768979339u, 131914726801349290u }, - { 6369772649532999991u, 82446704250843306u }, { 17185587848771025797u, 103058380313554132u }, - { 3035240737254230630u, 128822975391942666u }, { 6508711479211282048u, 80514359619964166u }, - { 17359261385868878368u, 100642949524955207u }, { 17087390713908710056u, 125803686906194009u }, - { 3762090168551861929u, 78627304316371256u }, { 4702612710689827411u, 98284130395464070u }, - { 15101637925217060072u, 122855162994330087u }, { 16356052730901744401u, 76784476871456304u }, - { 1998321839917628885u, 95980596089320381u }, { 7109588318324424010u, 119975745111650476u }, - { 13666864735807540814u, 74984840694781547u }, { 12471894901332038114u, 93731050868476934u }, - { 6366496589810271835u, 117163813585596168u }, { 3979060368631419896u, 73227383490997605u }, - { 9585511479216662775u, 91534229363747006u }, { 2758517312166052660u, 114417786704683758u }, - { 12671518677062341634u, 143022233380854697u }, { 1002170145522881665u, 89388895863034186u }, - { 10476084718758377889u, 111736119828792732u }, { 13095105898447972362u, 139670149785990915u }, - { 5878598177316288774u, 87293843616244322u }, { 16571619758500136775u, 109117304520305402u }, - { 11491152661270395161u, 136396630650381753u }, { 264441385652915120u, 85247894156488596u }, - { 330551732066143900u, 106559867695610745u }, { 5024875683510067779u, 133199834619513431u }, - { 10058076329834874218u, 83249896637195894u }, { 3349223375438816964u, 104062370796494868u }, - { 4186529219298521205u, 130077963495618585u }, { 14145795808130045513u, 81298727184761615u }, - { 13070558741735168987u, 101623408980952019u }, { 11726512408741573330u, 127029261226190024u }, - { 7329070255463483331u, 79393288266368765u }, { 13773023837756742068u, 99241610332960956u }, - { 17216279797195927585u, 124052012916201195u }, { 8454331864033760789u, 77532508072625747u }, - { 5956228811614813082u, 96915635090782184u }, { 7445286014518516353u, 121144543863477730u }, - { 9264989777501460624u, 75715339914673581u }, { 16192923240304213684u, 94644174893341976u }, - { 1794409976670715490u, 118305218616677471u }, { 8039035263060279037u, 73940761635423419u }, - { 5437108060397960892u, 92425952044279274u }, { 16019757112352226923u, 115532440055349092u }, - { 788976158365366019u, 72207775034593183u }, { 14821278253238871236u, 90259718793241478u }, - { 9303225779693813237u, 112824648491551848u }, { 11629032224617266546u, 141030810614439810u }, - { 11879831158813179495u, 88144256634024881u }, { 1014730893234310657u, 110180320792531102u }, - { 10491785653397664129u, 137725400990663877u }, { 8863209042587234033u, 86078375619164923u }, - { 6467325284806654637u, 107597969523956154u }, { 17307528642863094104u, 134497461904945192u }, - { 10817205401789433815u, 84060913690590745u }, { 18133192770664180173u, 105076142113238431u }, - { 18054804944902837312u, 131345177641548039u }, { 18201782118205355176u, 82090736025967524u }, - { 4305483574047142354u, 102613420032459406u }, { 14605226504413703751u, 128266775040574257u }, - { 2210737537617482988u, 80166734400358911u }, { 16598479977304017447u, 100208418000448638u }, - { 11524727934775246001u, 125260522500560798u }, { 2591268940807140847u, 78287826562850499u }, - { 17074144231291089770u, 97859783203563123u }, { 16730994270686474309u, 122324729004453904u }, - { 10456871419179046443u, 76452955627783690u }, { 3847717237119032246u, 95566194534729613u }, - { 9421332564826178211u, 119457743168412016u }, { 5888332853016361382u, 74661089480257510u }, - { 16583788103125227536u, 93326361850321887u }, { 16118049110479146516u, 116657952312902359u }, - { 16991309721690548428u, 72911220195563974u }, { 12015765115258409727u, 91139025244454968u }, - { 15019706394073012159u, 113923781555568710u }, { 9551260955736489391u, 142404726944460888u }, - { 5969538097335305869u, 89002954340288055u }, { 2850236603241744433u, 111253692925360069u }}; - return arr; -} - -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/detail/d2s_intrinsics.hpp b/include/boost/json/detail/ryu/detail/d2s_intrinsics.hpp deleted file mode 100644 index 04eb0af87..000000000 --- a/include/boost/json/detail/ryu/detail/d2s_intrinsics.hpp +++ /dev/null @@ -1,231 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_DETAIL_D2S_INTRINSICS_HPP -#define BOOST_JSON_DETAIL_RYU_DETAIL_D2S_INTRINSICS_HPP - -#include - -// This sets BOOST_JSON_RYU_32_BIT_PLATFORM as a side effect if applicable. -#include - -#if defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS) -#include -#endif - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -#if defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS) - -inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* const productHi) { - return _umul128(a, b, productHi); -} - -inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, const uint32_t dist) { - // For the __shiftright128 intrinsic, the shift value is always - // modulo 64. - // In the current implementation of the double-precision version - // of Ryu, the shift value is always < 64. (In the case - // RYU_OPTIMIZE_SIZE == 0, the shift value is in the range [49, 58]. - // Otherwise in the range [2, 59].) - // Check this here in case a future change requires larger shift - // values. In this case this function needs to be adjusted. - BOOST_ASSERT(dist < 64); - return __shiftright128(lo, hi, (unsigned char) dist); -} - -#else // defined(HAS_64_BIT_INTRINSICS) - -inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* const productHi) { - // The casts here help MSVC to avoid calls to the __allmul library function. - const uint32_t aLo = (uint32_t)a; - const uint32_t aHi = (uint32_t)(a >> 32); - const uint32_t bLo = (uint32_t)b; - const uint32_t bHi = (uint32_t)(b >> 32); - - const uint64_t b00 = (uint64_t)aLo * bLo; - const uint64_t b01 = (uint64_t)aLo * bHi; - const uint64_t b10 = (uint64_t)aHi * bLo; - const uint64_t b11 = (uint64_t)aHi * bHi; - - const uint32_t b00Lo = (uint32_t)b00; - const uint32_t b00Hi = (uint32_t)(b00 >> 32); - - const uint64_t mid1 = b10 + b00Hi; - const uint32_t mid1Lo = (uint32_t)(mid1); - const uint32_t mid1Hi = (uint32_t)(mid1 >> 32); - - const uint64_t mid2 = b01 + mid1Lo; - const uint32_t mid2Lo = (uint32_t)(mid2); - const uint32_t mid2Hi = (uint32_t)(mid2 >> 32); - - const uint64_t pHi = b11 + mid1Hi + mid2Hi; - const uint64_t pLo = ((uint64_t)mid2Lo << 32) | b00Lo; - - *productHi = pHi; - return pLo; -} - -inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, const uint32_t dist) { - // We don't need to handle the case dist >= 64 here (see above). - BOOST_ASSERT(dist < 64); -#if defined(RYU_OPTIMIZE_SIZE) || !defined(RYU_32_BIT_PLATFORM) - BOOST_ASSERT(dist > 0); - return (hi << (64 - dist)) | (lo >> dist); -#else - // Avoid a 64-bit shift by taking advantage of the range of shift values. - BOOST_ASSERT(dist >= 32); - return (hi << (64 - dist)) | ((uint32_t)(lo >> 32) >> (dist - 32)); -#endif -} - -#endif // defined(HAS_64_BIT_INTRINSICS) - -#ifdef RYU_32_BIT_PLATFORM - -// Returns the high 64 bits of the 128-bit product of a and b. -inline uint64_t umulh(const uint64_t a, const uint64_t b) { - // Reuse the umul128 implementation. - // Optimizers will likely eliminate the instructions used to compute the - // low part of the product. - uint64_t hi; - umul128(a, b, &hi); - return hi; -} - -// On 32-bit platforms, compilers typically generate calls to library -// functions for 64-bit divisions, even if the divisor is a constant. -// -// E.g.: -// https://bugs.llvm.org/show_bug.cgi?id=37932 -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17958 -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37443 -// -// The functions here perform division-by-constant using multiplications -// in the same way as 64-bit compilers would do. -// -// NB: -// The multipliers and shift values are the ones generated by clang x64 -// for expressions like x/5, x/10, etc. - -inline uint64_t div5(const uint64_t x) { - return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 2; -} - -inline uint64_t div10(const uint64_t x) { - return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 3; -} - -inline uint64_t div100(const uint64_t x) { - return umulh(x >> 2, 0x28F5C28F5C28F5C3u) >> 2; -} - -inline uint64_t div1e8(const uint64_t x) { - return umulh(x, 0xABCC77118461CEFDu) >> 26; -} - -inline uint64_t div1e9(const uint64_t x) { - return umulh(x >> 9, 0x44B82FA09B5A53u) >> 11; -} - -inline uint32_t mod1e9(const uint64_t x) { - // Avoid 64-bit math as much as possible. - // Returning (uint32_t) (x - 1000000000 * div1e9(x)) would - // perform 32x64-bit multiplication and 64-bit subtraction. - // x and 1000000000 * div1e9(x) are guaranteed to differ by - // less than 10^9, so their highest 32 bits must be identical, - // so we can truncate both sides to uint32_t before subtracting. - // We can also simplify (uint32_t) (1000000000 * div1e9(x)). - // We can truncate before multiplying instead of after, as multiplying - // the highest 32 bits of div1e9(x) can't affect the lowest 32 bits. - return ((uint32_t) x) - 1000000000 * ((uint32_t) div1e9(x)); -} - -#else // RYU_32_BIT_PLATFORM - -inline uint64_t div5(const uint64_t x) { - return x / 5; -} - -inline uint64_t div10(const uint64_t x) { - return x / 10; -} - -inline uint64_t div100(const uint64_t x) { - return x / 100; -} - -inline uint64_t div1e8(const uint64_t x) { - return x / 100000000; -} - -inline uint64_t div1e9(const uint64_t x) { - return x / 1000000000; -} - -inline uint32_t mod1e9(const uint64_t x) { - return (uint32_t) (x - 1000000000 * div1e9(x)); -} - -#endif // RYU_32_BIT_PLATFORM - -inline uint32_t pow5Factor(uint64_t value) { - uint32_t count = 0; - for (;;) { - BOOST_ASSERT(value != 0); - const uint64_t q = div5(value); - const uint32_t r = ((uint32_t) value) - 5 * ((uint32_t) q); - if (r != 0) { - break; - } - value = q; - ++count; - } - return count; -} - -// Returns true if value is divisible by 5^p. -inline bool multipleOfPowerOf5(const uint64_t value, const uint32_t p) { - // I tried a case distinction on p, but there was no performance difference. - return pow5Factor(value) >= p; -} - -// Returns true if value is divisible by 2^p. -inline bool multipleOfPowerOf2(const uint64_t value, const uint32_t p) { - BOOST_ASSERT(value != 0); - // return __builtin_ctzll(value) >= p; - return (value & ((1ull << p) - 1)) == 0; -} - - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/detail/digit_table.hpp b/include/boost/json/detail/ryu/detail/digit_table.hpp deleted file mode 100644 index f1bb89ccc..000000000 --- a/include/boost/json/detail/ryu/detail/digit_table.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_DETAIL_DIGIT_TABLE_HPP -#define BOOST_JSON_DETAIL_RYU_DETAIL_DIGIT_TABLE_HPP - -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -// A table of all two-digit numbers. This is used to speed up decimal digit -// generation by copying pairs of digits into the final output. -inline -char const -(&DIGIT_TABLE() noexcept)[200] -{ - static constexpr char arr[200] = { - '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', - '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', - '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', - '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', - '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', - '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', - '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', - '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', - '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', - '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' }; - return arr; -} - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/impl/d2s.ipp b/include/boost/json/detail/ryu/impl/d2s.ipp deleted file mode 100644 index acf243bdf..000000000 --- a/include/boost/json/detail/ryu/impl/d2s.ipp +++ /dev/null @@ -1,732 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -// Runtime compiler options: -// -DRYU_DEBUG Generate verbose debugging output to stdout. -// -// -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower, -// depending on your compiler. -// -// -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every -// required power of 5, only store every 26th entry, and compute -// intermediate values with a multiplication. This reduces the lookup table -// size by about 10x (only one case, and only double) at the cost of some -// performance. Currently requires MSVC intrinsics. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP -#define BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP - -#include -#include -#include - -#ifdef RYU_DEBUG -#include -#endif - -// ABSL avoids uint128_t on Win32 even if __SIZEOF_INT128__ is defined. -// Let's do the same for now. -#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) -#define BOOST_JSON_RYU_HAS_UINT128 -#elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64) -#define BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS -#endif - -#include -#include -#include -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -// We need a 64x128-bit multiplication and a subsequent 128-bit shift. -// Multiplication: -// The 64-bit factor is variable and passed in, the 128-bit factor comes -// from a lookup table. We know that the 64-bit factor only has 55 -// significant bits (i.e., the 9 topmost bits are zeros). The 128-bit -// factor only has 124 significant bits (i.e., the 4 topmost bits are -// zeros). -// Shift: -// In principle, the multiplication result requires 55 + 124 = 179 bits to -// represent. However, we then shift this value to the right by j, which is -// at least j >= 115, so the result is guaranteed to fit into 179 - 115 = 64 -// bits. This means that we only need the topmost 64 significant bits of -// the 64x128-bit multiplication. -// -// There are several ways to do this: -// 1. Best case: the compiler exposes a 128-bit type. -// We perform two 64x64-bit multiplications, add the higher 64 bits of the -// lower result to the higher result, and shift by j - 64 bits. -// -// We explicitly cast from 64-bit to 128-bit, so the compiler can tell -// that these are only 64-bit inputs, and can map these to the best -// possible sequence of assembly instructions. -// x64 machines happen to have matching assembly instructions for -// 64x64-bit multiplications and 128-bit shifts. -// -// 2. Second best case: the compiler exposes intrinsics for the x64 assembly -// instructions mentioned in 1. -// -// 3. We only have 64x64 bit instructions that return the lower 64 bits of -// the result, i.e., we have to use plain C. -// Our inputs are less than the full width, so we have three options: -// a. Ignore this fact and just implement the intrinsics manually. -// b. Split both into 31-bit pieces, which guarantees no internal overflow, -// but requires extra work upfront (unless we change the lookup table). -// c. Split only the first factor into 31-bit pieces, which also guarantees -// no internal overflow, but requires extra work since the intermediate -// results are not perfectly aligned. -#if defined(BOOST_JSON_RYU_HAS_UINT128) - -// Best case: use 128-bit type. -inline -std::uint64_t - mulShift( - const std::uint64_t m, - const std::uint64_t* const mul, - const std::int32_t j) noexcept -{ - const uint128_t b0 = ((uint128_t) m) * mul[0]; - const uint128_t b2 = ((uint128_t) m) * mul[1]; - return (std::uint64_t) (((b0 >> 64) + b2) >> (j - 64)); -} - -inline -uint64_t -mulShiftAll( - const std::uint64_t m, - const std::uint64_t* const mul, - std::int32_t const j, - std::uint64_t* const vp, - std::uint64_t* const vm, - const std::uint32_t mmShift) noexcept -{ -// m <<= 2; -// uint128_t b0 = ((uint128_t) m) * mul[0]; // 0 -// uint128_t b2 = ((uint128_t) m) * mul[1]; // 64 -// -// uint128_t hi = (b0 >> 64) + b2; -// uint128_t lo = b0 & 0xffffffffffffffffull; -// uint128_t factor = (((uint128_t) mul[1]) << 64) + mul[0]; -// uint128_t vpLo = lo + (factor << 1); -// *vp = (std::uint64_t) ((hi + (vpLo >> 64)) >> (j - 64)); -// uint128_t vmLo = lo - (factor << mmShift); -// *vm = (std::uint64_t) ((hi + (vmLo >> 64) - (((uint128_t) 1ull) << 64)) >> (j - 64)); -// return (std::uint64_t) (hi >> (j - 64)); - *vp = mulShift(4 * m + 2, mul, j); - *vm = mulShift(4 * m - 1 - mmShift, mul, j); - return mulShift(4 * m, mul, j); -} - -#elif defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS) - -inline -std::uint64_t -mulShift( - const std::uint64_t m, - const std::uint64_t* const mul, - const std::int32_t j) noexcept -{ - // m is maximum 55 bits - std::uint64_t high1; // 128 - std::uint64_t const low1 = umul128(m, mul[1], &high1); // 64 - std::uint64_t high0; // 64 - umul128(m, mul[0], &high0); // 0 - std::uint64_t const sum = high0 + low1; - if (sum < high0) - ++high1; // overflow into high1 - return shiftright128(sum, high1, j - 64); -} - -inline -std::uint64_t -mulShiftAll( - const std::uint64_t m, - const std::uint64_t* const mul, - const std::int32_t j, - std::uint64_t* const vp, - std::uint64_t* const vm, - const std::uint32_t mmShift) noexcept -{ - *vp = mulShift(4 * m + 2, mul, j); - *vm = mulShift(4 * m - 1 - mmShift, mul, j); - return mulShift(4 * m, mul, j); -} - -#else // !defined(BOOST_JSON_RYU_HAS_UINT128) && !defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS) - -inline -std::uint64_t -mulShiftAll( - std::uint64_t m, - const std::uint64_t* const mul, - const std::int32_t j, - std::uint64_t* const vp, - std::uint64_t* const vm, - const std::uint32_t mmShift) -{ - m <<= 1; - // m is maximum 55 bits - std::uint64_t tmp; - std::uint64_t const lo = umul128(m, mul[0], &tmp); - std::uint64_t hi; - std::uint64_t const mid = tmp + umul128(m, mul[1], &hi); - hi += mid < tmp; // overflow into hi - - const std::uint64_t lo2 = lo + mul[0]; - const std::uint64_t mid2 = mid + mul[1] + (lo2 < lo); - const std::uint64_t hi2 = hi + (mid2 < mid); - *vp = shiftright128(mid2, hi2, (std::uint32_t)(j - 64 - 1)); - - if (mmShift == 1) - { - const std::uint64_t lo3 = lo - mul[0]; - const std::uint64_t mid3 = mid - mul[1] - (lo3 > lo); - const std::uint64_t hi3 = hi - (mid3 > mid); - *vm = shiftright128(mid3, hi3, (std::uint32_t)(j - 64 - 1)); - } - else - { - const std::uint64_t lo3 = lo + lo; - const std::uint64_t mid3 = mid + mid + (lo3 < lo); - const std::uint64_t hi3 = hi + hi + (mid3 < mid); - const std::uint64_t lo4 = lo3 - mul[0]; - const std::uint64_t mid4 = mid3 - mul[1] - (lo4 > lo3); - const std::uint64_t hi4 = hi3 - (mid4 > mid3); - *vm = shiftright128(mid4, hi4, (std::uint32_t)(j - 64)); - } - - return shiftright128(mid, hi, (std::uint32_t)(j - 64 - 1)); -} - -#endif // BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS - -inline -std::uint32_t -decimalLength17( - const std::uint64_t v) -{ - // This is slightly faster than a loop. - // The average output length is 16.38 digits, so we check high-to-low. - // Function precondition: v is not an 18, 19, or 20-digit number. - // (17 digits are sufficient for round-tripping.) - BOOST_ASSERT(v < 100000000000000000L); - if (v >= 10000000000000000L) { return 17; } - if (v >= 1000000000000000L) { return 16; } - if (v >= 100000000000000L) { return 15; } - if (v >= 10000000000000L) { return 14; } - if (v >= 1000000000000L) { return 13; } - if (v >= 100000000000L) { return 12; } - if (v >= 10000000000L) { return 11; } - if (v >= 1000000000L) { return 10; } - if (v >= 100000000L) { return 9; } - if (v >= 10000000L) { return 8; } - if (v >= 1000000L) { return 7; } - if (v >= 100000L) { return 6; } - if (v >= 10000L) { return 5; } - if (v >= 1000L) { return 4; } - if (v >= 100L) { return 3; } - if (v >= 10L) { return 2; } - return 1; -} - -// A floating decimal representing m * 10^e. -struct floating_decimal_64 -{ - std::uint64_t mantissa; - // Decimal exponent's range is -324 to 308 - // inclusive, and can fit in a short if needed. - std::int32_t exponent; -}; - -inline -floating_decimal_64 -d2d( - const std::uint64_t ieeeMantissa, - const std::uint32_t ieeeExponent) -{ - std::int32_t e2; - std::uint64_t m2; - if (ieeeExponent == 0) - { - // We subtract 2 so that the bounds computation has 2 additional bits. - e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2; - m2 = ieeeMantissa; - } - else - { - e2 = (std::int32_t)ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2; - m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa; - } - const bool even = (m2 & 1) == 0; - const bool acceptBounds = even; - -#ifdef RYU_DEBUG - printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2); -#endif - - // Step 2: Determine the interval of valid decimal representations. - const std::uint64_t mv = 4 * m2; - // Implicit bool -> int conversion. True is 1, false is 0. - const std::uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1; - // We would compute mp and mm like this: - // uint64_t mp = 4 * m2 + 2; - // uint64_t mm = mv - 1 - mmShift; - - // Step 3: Convert to a decimal power base using 128-bit arithmetic. - std::uint64_t vr, vp, vm; - std::int32_t e10; - bool vmIsTrailingZeros = false; - bool vrIsTrailingZeros = false; - if (e2 >= 0) { - // I tried special-casing q == 0, but there was no effect on performance. - // This expression is slightly faster than max(0, log10Pow2(e2) - 1). - const std::uint32_t q = log10Pow2(e2) - (e2 > 3); - e10 = (std::int32_t)q; - const std::int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t)q) - 1; - const std::int32_t i = -e2 + (std::int32_t)q + k; -#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) - uint64_t pow5[2]; - double_computeInvPow5(q, pow5); - vr = mulShiftAll(m2, pow5, i, &vp, &vm, mmShift); -#else - vr = mulShiftAll(m2, DOUBLE_POW5_INV_SPLIT()[q], i, &vp, &vm, mmShift); -#endif -#ifdef RYU_DEBUG - printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q); - printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); -#endif - if (q <= 21) - { - // This should use q <= 22, but I think 21 is also safe. Smaller values - // may still be safe, but it's more difficult to reason about them. - // Only one of mp, mv, and mm can be a multiple of 5, if any. - const std::uint32_t mvMod5 = ((std::uint32_t)mv) - 5 * ((std::uint32_t)div5(mv)); - if (mvMod5 == 0) - { - vrIsTrailingZeros = multipleOfPowerOf5(mv, q); - } - else if (acceptBounds) - { - // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q - // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q - // <=> true && pow5Factor(mm) >= q, since e2 >= q. - vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q); - } - else - { - // Same as min(e2 + 1, pow5Factor(mp)) >= q. - vp -= multipleOfPowerOf5(mv + 2, q); - } - } - } - else - { - // This expression is slightly faster than max(0, log10Pow5(-e2) - 1). - const std::uint32_t q = log10Pow5(-e2) - (-e2 > 1); - e10 = (std::int32_t)q + e2; - const std::int32_t i = -e2 - (std::int32_t)q; - const std::int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT; - const std::int32_t j = (std::int32_t)q - k; -#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) - std::uint64_t pow5[2]; - double_computePow5(i, pow5); - vr = mulShiftAll(m2, pow5, j, &vp, &vm, mmShift); -#else - vr = mulShiftAll(m2, DOUBLE_POW5_SPLIT()[i], j, &vp, &vm, mmShift); -#endif -#ifdef RYU_DEBUG - printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q); - printf("%u %d %d %d\n", q, i, k, j); - printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); -#endif - if (q <= 1) - { - // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. - // mv = 4 * m2, so it always has at least two trailing 0 bits. - vrIsTrailingZeros = true; - if (acceptBounds) - { - // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1. - vmIsTrailingZeros = mmShift == 1; - } - else - { - // mp = mv + 2, so it always has at least one trailing 0 bit. - --vp; - } - } - else if (q < 63) - { - // TODO(ulfjack): Use a tighter bound here. - // We want to know if the full product has at least q trailing zeros. - // We need to compute min(p2(mv), p5(mv) - e2) >= q - // <=> p2(mv) >= q && p5(mv) - e2 >= q - // <=> p2(mv) >= q (because -e2 >= q) - vrIsTrailingZeros = multipleOfPowerOf2(mv, q); -#ifdef RYU_DEBUG - printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false"); -#endif - } - } -#ifdef RYU_DEBUG - printf("e10=%d\n", e10); - printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); - printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false"); - printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false"); -#endif - - // Step 4: Find the shortest decimal representation in the interval of valid representations. - std::int32_t removed = 0; - std::uint8_t lastRemovedDigit = 0; - std::uint64_t output; - // On average, we remove ~2 digits. - if (vmIsTrailingZeros || vrIsTrailingZeros) - { - // General case, which happens rarely (~0.7%). - for (;;) - { - const std::uint64_t vpDiv10 = div10(vp); - const std::uint64_t vmDiv10 = div10(vm); - if (vpDiv10 <= vmDiv10) - break; - const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10); - const std::uint64_t vrDiv10 = div10(vr); - const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10); - vmIsTrailingZeros &= vmMod10 == 0; - vrIsTrailingZeros &= lastRemovedDigit == 0; - lastRemovedDigit = (uint8_t)vrMod10; - vr = vrDiv10; - vp = vpDiv10; - vm = vmDiv10; - ++removed; - } -#ifdef RYU_DEBUG - printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); - printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false"); -#endif - if (vmIsTrailingZeros) - { - for (;;) - { - const std::uint64_t vmDiv10 = div10(vm); - const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10); - if (vmMod10 != 0) - break; - const std::uint64_t vpDiv10 = div10(vp); - const std::uint64_t vrDiv10 = div10(vr); - const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10); - vrIsTrailingZeros &= lastRemovedDigit == 0; - lastRemovedDigit = (uint8_t)vrMod10; - vr = vrDiv10; - vp = vpDiv10; - vm = vmDiv10; - ++removed; - } - } -#ifdef RYU_DEBUG - printf("%" PRIu64 " %d\n", vr, lastRemovedDigit); - printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false"); -#endif - if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) - { - // Round even if the exact number is .....50..0. - lastRemovedDigit = 4; - } - // We need to take vr + 1 if vr is outside bounds or we need to round up. - output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5); - } - else - { - // Specialized for the common case (~99.3%). Percentages below are relative to this. - bool roundUp = false; - const std::uint64_t vpDiv100 = div100(vp); - const std::uint64_t vmDiv100 = div100(vm); - if (vpDiv100 > vmDiv100) - { - // Optimization: remove two digits at a time (~86.2%). - const std::uint64_t vrDiv100 = div100(vr); - const std::uint32_t vrMod100 = ((std::uint32_t)vr) - 100 * ((std::uint32_t)vrDiv100); - roundUp = vrMod100 >= 50; - vr = vrDiv100; - vp = vpDiv100; - vm = vmDiv100; - removed += 2; - } - // Loop iterations below (approximately), without optimization above: - // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02% - // Loop iterations below (approximately), with optimization above: - // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02% - for (;;) - { - const std::uint64_t vpDiv10 = div10(vp); - const std::uint64_t vmDiv10 = div10(vm); - if (vpDiv10 <= vmDiv10) - break; - const std::uint64_t vrDiv10 = div10(vr); - const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10); - roundUp = vrMod10 >= 5; - vr = vrDiv10; - vp = vpDiv10; - vm = vmDiv10; - ++removed; - } -#ifdef RYU_DEBUG - printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false"); - printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false"); -#endif - // We need to take vr + 1 if vr is outside bounds or we need to round up. - output = vr + (vr == vm || roundUp); - } - const std::int32_t exp = e10 + removed; - -#ifdef RYU_DEBUG - printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); - printf("O=%" PRIu64 "\n", output); - printf("EXP=%d\n", exp); -#endif - - floating_decimal_64 fd; - fd.exponent = exp; - fd.mantissa = output; - return fd; -} - -inline -int -to_chars( - const floating_decimal_64 v, - const bool sign, - char* const result) -{ - // Step 5: Print the decimal representation. - int index = 0; - if (sign) - result[index++] = '-'; - - std::uint64_t output = v.mantissa; - std::uint32_t const olength = decimalLength17(output); - -#ifdef RYU_DEBUG - printf("DIGITS=%" PRIu64 "\n", v.mantissa); - printf("OLEN=%u\n", olength); - printf("EXP=%u\n", v.exponent + olength); -#endif - - // Print the decimal digits. - // The following code is equivalent to: - // for (uint32_t i = 0; i < olength - 1; ++i) { - // const uint32_t c = output % 10; output /= 10; - // result[index + olength - i] = (char) ('0' + c); - // } - // result[index] = '0' + output % 10; - - std::uint32_t i = 0; - // We prefer 32-bit operations, even on 64-bit platforms. - // We have at most 17 digits, and uint32_t can store 9 digits. - // If output doesn't fit into uint32_t, we cut off 8 digits, - // so the rest will fit into uint32_t. - if ((output >> 32) != 0) - { - // Expensive 64-bit division. - std::uint64_t const q = div1e8(output); - std::uint32_t output2 = ((std::uint32_t)output) - 100000000 * ((std::uint32_t)q); - output = q; - - const std::uint32_t c = output2 % 10000; - output2 /= 10000; - const std::uint32_t d = output2 % 10000; - const std::uint32_t c0 = (c % 100) << 1; - const std::uint32_t c1 = (c / 100) << 1; - const std::uint32_t d0 = (d % 100) << 1; - const std::uint32_t d1 = (d / 100) << 1; - std::memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2); - std::memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2); - std::memcpy(result + index + olength - i - 5, DIGIT_TABLE() + d0, 2); - std::memcpy(result + index + olength - i - 7, DIGIT_TABLE() + d1, 2); - i += 8; - } - uint32_t output2 = (std::uint32_t)output; - while (output2 >= 10000) - { -#ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217 - const uint32_t c = output2 - 10000 * (output2 / 10000); -#else - const uint32_t c = output2 % 10000; -#endif - output2 /= 10000; - const uint32_t c0 = (c % 100) << 1; - const uint32_t c1 = (c / 100) << 1; - memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2); - memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2); - i += 4; - } - if (output2 >= 100) { - const uint32_t c = (output2 % 100) << 1; - output2 /= 100; - memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c, 2); - i += 2; - } - if (output2 >= 10) { - const uint32_t c = output2 << 1; - // We can't use memcpy here: the decimal dot goes between these two digits. - result[index + olength - i] = DIGIT_TABLE()[c + 1]; - result[index] = DIGIT_TABLE()[c]; - } - else { - result[index] = (char)('0' + output2); - } - - // Print decimal point if needed. - if (olength > 1) { - result[index + 1] = '.'; - index += olength + 1; - } - else { - ++index; - } - - // Print the exponent. - result[index++] = 'E'; - int32_t exp = v.exponent + (int32_t)olength - 1; - if (exp < 0) { - result[index++] = '-'; - exp = -exp; - } - - if (exp >= 100) { - const int32_t c = exp % 10; - memcpy(result + index, DIGIT_TABLE() + 2 * (exp / 10), 2); - result[index + 2] = (char)('0' + c); - index += 3; - } - else if (exp >= 10) { - memcpy(result + index, DIGIT_TABLE() + 2 * exp, 2); - index += 2; - } - else { - result[index++] = (char)('0' + exp); - } - - return index; -} - -static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent, - floating_decimal_64* const v) { - const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa; - const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS; - - if (e2 > 0) { - // f = m2 * 2^e2 >= 2^53 is an integer. - // Ignore this case for now. - return false; - } - - if (e2 < -52) { - // f < 1. - return false; - } - - // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53. - // Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0. - const uint64_t mask = (1ull << -e2) - 1; - const uint64_t fraction = m2 & mask; - if (fraction != 0) { - return false; - } - - // f is an integer in the range [1, 2^53). - // Note: mantissa might contain trailing (decimal) 0's. - // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17(). - v->mantissa = m2 >> -e2; - v->exponent = 0; - return true; -} - -} // detail - -int -d2s_buffered_n( - double f, - char* result, - bool allow_infinity_and_nan) noexcept -{ - using namespace detail; - // Step 1: Decode the floating-point number, and unify normalized and subnormal cases. - std::uint64_t const bits = double_to_bits(f); - -#ifdef RYU_DEBUG - printf("IN="); - for (std::int32_t bit = 63; bit >= 0; --bit) { - printf("%d", (int)((bits >> bit) & 1)); - } - printf("\n"); -#endif - - // Decode bits into sign, mantissa, and exponent. - const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0; - const std::uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1); - const std::uint32_t ieeeExponent = (std::uint32_t)((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1)); - // Case distinction; exit early for the easy cases. - if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) { - // We changed how special numbers are output by default - if (allow_infinity_and_nan) - return copy_special_str(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0); - else - return copy_special_str_conforming(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0); - - } - - floating_decimal_64 v; - const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v); - if (isSmallInt) { - // For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros. - // For scientific notation we need to move these zeros into the exponent. - // (This is not needed for fixed-point notation, so it might be beneficial to trim - // trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.) - for (;;) { - std::uint64_t const q = div10(v.mantissa); - std::uint32_t const r = ((std::uint32_t) v.mantissa) - 10 * ((std::uint32_t) q); - if (r != 0) - break; - v.mantissa = q; - ++v.exponent; - } - } - else { - v = d2d(ieeeMantissa, ieeeExponent); - } - - return to_chars(v, ieeeSign, result); -} - -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/detail/ryu/ryu.hpp b/include/boost/json/detail/ryu/ryu.hpp deleted file mode 100644 index 5560de309..000000000 --- a/include/boost/json/detail/ryu/ryu.hpp +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#ifndef BOOST_JSON_DETAIL_RYU_HPP -#define BOOST_JSON_DETAIL_RYU_HPP - -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { - -BOOST_JSON_DECL -int d2s_buffered_n( - double f, char* result, bool allow_infinity_and_nan = true) noexcept; - -} // ryu - -} // detail -} // namespace json -} // namespace boost - -#endif diff --git a/include/boost/json/src.hpp b/include/boost/json/src.hpp index e28eb61a6..c1e3e4f20 100644 --- a/include/boost/json/src.hpp +++ b/include/boost/json/src.hpp @@ -54,7 +54,7 @@ in a translation unit of the program. #include #include -#include #include +#include #endif diff --git a/test/Jamfile b/test/Jamfile index 37aec9643..47755f778 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -52,9 +52,6 @@ local SOURCES = value_ref.cpp utf8.cpp visit.cpp - ryu/d2s_intrinsics_test.cpp - ryu/d2s_table_test.cpp - ryu/d2s_test.cpp ; project diff --git a/test/ryu/d2s_intrinsics_test.cpp b/test/ryu/d2s_intrinsics_test.cpp deleted file mode 100644 index fc94b6846..000000000 --- a/test/ryu/d2s_intrinsics_test.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#include -#include "gtest.hpp" -#include - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -TEST(D2sIntrinsicsTest, mod1e9) { - ASSERT_EQ(0, mod1e9(0)); - ASSERT_EQ(1, mod1e9(1)); - ASSERT_EQ(2, mod1e9(2)); - ASSERT_EQ(10, mod1e9(10)); - ASSERT_EQ(100, mod1e9(100)); - ASSERT_EQ(1000, mod1e9(1000)); - ASSERT_EQ(10000, mod1e9(10000)); - ASSERT_EQ(100000, mod1e9(100000)); - ASSERT_EQ(1000000, mod1e9(1000000)); - ASSERT_EQ(10000000, mod1e9(10000000)); - ASSERT_EQ(100000000, mod1e9(100000000)); - ASSERT_EQ(0, mod1e9(1000000000)); - ASSERT_EQ(0, mod1e9(2000000000)); - ASSERT_EQ(1, mod1e9(1000000001)); - ASSERT_EQ(1234, mod1e9(1000001234)); - ASSERT_EQ(123456789, mod1e9(12345123456789ull)); - ASSERT_EQ(123456789, mod1e9(123456789123456789ull)); -} - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost diff --git a/test/ryu/d2s_table_test.cpp b/test/ryu/d2s_table_test.cpp deleted file mode 100644 index 67d669a43..000000000 --- a/test/ryu/d2s_table_test.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) -#define BOOST_JSON_RYU_HAS_UINT128 -#elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64) \ - && !defined(__clang__) // https://bugs.llvm.org/show_bug.cgi?id=37755 -#define BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS -#endif - -// We want to test the size-optimized case here. -#if !defined(BOOST_JSON_RYU_OPTIMIZE_SIZE) -#define BOOST_JSON_RYU_OPTIMIZE_SIZE -#endif -#include -#include -#include -#include -#include -#include "gtest.hpp" - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { -namespace detail { - -TEST(D2sTableTest, double_computePow5) { - for (int i = 0; i < 326; i++) { - uint64_t m[2]; - double_computePow5(i, m); - ASSERT_EQ(m[0], DOUBLE_POW5_SPLIT()[i][0]); - ASSERT_EQ(m[1], DOUBLE_POW5_SPLIT()[i][1]); - } -} - -TEST(D2sTableTest, compute_offsets_for_double_computePow5) { - uint32_t totalErrors = 0; - uint32_t offsets[13] = {0}; - for (int i = 0; i < 326; i++) { - uint64_t m[2]; - double_computePow5(i, m); - if (m[0] != DOUBLE_POW5_SPLIT()[i][0]) { - offsets[i / POW5_TABLE_SIZE] |= 1 << (i % POW5_TABLE_SIZE); - totalErrors++; - } - } - if (totalErrors != 0) { - for (int i = 0; i < 13; i++) { - printf("0x%08x,\n", offsets[i]); - } - } - ASSERT_EQ(totalErrors, 0); -} - -TEST(D2sTableTest, double_computeInvPow5) { - for (int i = 0; i < 292; i++) { - uint64_t m[2]; - double_computeInvPow5(i, m); - ASSERT_EQ(m[0], DOUBLE_POW5_INV_SPLIT()[i][0]); - ASSERT_EQ(m[1], DOUBLE_POW5_INV_SPLIT()[i][1]); - } -} - -TEST(D2sTableTest, compute_offsets_for_double_computeInvPow5) { - uint32_t totalErrors = 0; - uint32_t offsets[20] = {0}; - for (int i = 0; i < 292; i++) { - uint64_t m[2]; - double_computeInvPow5(i, m); - if (m[0] != DOUBLE_POW5_INV_SPLIT()[i][0]) { - offsets[i / 16] |= ((DOUBLE_POW5_INV_SPLIT()[i][0] - m[0]) & 3) << ((i % 16) << 1); - totalErrors++; - } - } - if (totalErrors != 0) { - for (int i = 0; i < 20; i++) { - printf("0x%08x,\n", offsets[i]); - } - } - ASSERT_EQ(totalErrors, 0); -} - -} // detail -} // ryu - -} // detail -} // namespace json -} // namespace boost diff --git a/test/ryu/d2s_test.cpp b/test/ryu/d2s_test.cpp deleted file mode 100644 index e6f1c27de..000000000 --- a/test/ryu/d2s_test.cpp +++ /dev/null @@ -1,285 +0,0 @@ -// Copyright 2018 Ulf Adams -// -// The contents of this file may be used under the terms of the Apache License, -// Version 2.0. -// -// (See accompanying file LICENSE-Apache or copy at -// http://www.apache.org/licenses/LICENSE-2.0) -// -// Alternatively, the contents of this file may be used under the terms of -// the Boost Software License, Version 1.0. -// (See accompanying file LICENSE-Boost or copy at -// https://www.boost.org/LICENSE_1_0.txt) -// -// Unless required by applicable law or agreed to in writing, this software -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. - -/* - This is a derivative work -*/ - -#include -#include -#include -#include -#include "gtest.hpp" - -namespace boost { -namespace json { -namespace detail { - -namespace ryu { - -static double int64Bits2Double(uint64_t bits) { - double f; - memcpy(&f, &bits, sizeof(double)); - return f; -} - -static double ieeeParts2Double(const bool sign, const uint32_t ieeeExponent, const uint64_t ieeeMantissa) { - BOOST_ASSERT(ieeeExponent <= 2047); - BOOST_ASSERT(ieeeMantissa <= ((std::uint64_t)1 << 53) - 1); - return int64Bits2Double(((std::uint64_t)sign << 63) | ((std::uint64_t)ieeeExponent << 52) | ieeeMantissa); -} - -char const* d2s_test_helper(char* result, double f) { - int index = d2s_buffered_n(f, result); - result[index] = '\0'; - return result; -} - -TEST(D2sTest, Basic) { - std::array buffer; - ASSERT_STREQ("0E0", d2s_test_helper(buffer.data(), 0.0)); - ASSERT_STREQ("-0E0", d2s_test_helper(buffer.data(), -0.0)); - ASSERT_STREQ("1E0", d2s_test_helper(buffer.data(), 1.0)); - ASSERT_STREQ("-1E0", d2s_test_helper(buffer.data(), -1.0)); - ASSERT_STREQ( - "NaN", - d2s_test_helper(buffer.data(), std::numeric_limits::quiet_NaN())); - using Limits = std::numeric_limits; - ASSERT_STREQ( - "Infinity", d2s_test_helper(buffer.data(), Limits::infinity())); - ASSERT_STREQ( - "-Infinity", d2s_test_helper(buffer.data(), -Limits::infinity())); -} - -TEST(D2sTest, SwitchToSubnormal) { - std::array buffer; - ASSERT_STREQ("2.2250738585072014E-308", d2s_test_helper(buffer.data(), 2.2250738585072014E-308)); -} - -TEST(D2sTest, MinAndMax) { - std::array buffer; - ASSERT_STREQ("1.7976931348623157E308", d2s_test_helper(buffer.data(), int64Bits2Double(0x7fefffffffffffff))); - ASSERT_STREQ("5E-324", d2s_test_helper(buffer.data(), int64Bits2Double(1))); -} - -TEST(D2sTest, LotsOfTrailingZeros) { - std::array buffer; - ASSERT_STREQ("2.9802322387695312E-8", d2s_test_helper(buffer.data(), 2.98023223876953125E-8)); -} - -TEST(D2sTest, Regression) { - std::array buffer; - ASSERT_STREQ("-2.109808898695963E16", d2s_test_helper(buffer.data(), -2.109808898695963E16)); - ASSERT_STREQ("4.940656E-318", d2s_test_helper(buffer.data(), 4.940656E-318)); - ASSERT_STREQ("1.18575755E-316", d2s_test_helper(buffer.data(), 1.18575755E-316)); - ASSERT_STREQ("2.989102097996E-312", d2s_test_helper(buffer.data(), 2.989102097996E-312)); - ASSERT_STREQ("9.0608011534336E15", d2s_test_helper(buffer.data(), 9.0608011534336E15)); - ASSERT_STREQ("4.708356024711512E18", d2s_test_helper(buffer.data(), 4.708356024711512E18)); - ASSERT_STREQ("9.409340012568248E18", d2s_test_helper(buffer.data(), 9.409340012568248E18)); - ASSERT_STREQ("1.2345678E0", d2s_test_helper(buffer.data(), 1.2345678)); -} - -TEST(D2sTest, LooksLikePow5) { - std::array buffer; - // These numbers have a mantissa that is a multiple of the largest power of 5 that fits, - // and an exponent that causes the computation for q to result in 22, which is a corner - // case for Ryu. - ASSERT_STREQ("5.764607523034235E39", d2s_test_helper(buffer.data(), int64Bits2Double(0x4830F0CF064DD592))); - ASSERT_STREQ("1.152921504606847E40", d2s_test_helper(buffer.data(), int64Bits2Double(0x4840F0CF064DD592))); - ASSERT_STREQ("2.305843009213694E40", d2s_test_helper(buffer.data(), int64Bits2Double(0x4850F0CF064DD592))); -} - -TEST(D2sTest, OutputLength) { - std::array buffer; - ASSERT_STREQ("1E0", d2s_test_helper(buffer.data(), 1)); // already tested in Basic - ASSERT_STREQ("1.2E0", d2s_test_helper(buffer.data(), 1.2)); - ASSERT_STREQ("1.23E0", d2s_test_helper(buffer.data(), 1.23)); - ASSERT_STREQ("1.234E0", d2s_test_helper(buffer.data(), 1.234)); - ASSERT_STREQ("1.2345E0", d2s_test_helper(buffer.data(), 1.2345)); - ASSERT_STREQ("1.23456E0", d2s_test_helper(buffer.data(), 1.23456)); - ASSERT_STREQ("1.234567E0", d2s_test_helper(buffer.data(), 1.234567)); - ASSERT_STREQ("1.2345678E0", d2s_test_helper(buffer.data(), 1.2345678)); // already tested in Regression - ASSERT_STREQ("1.23456789E0", d2s_test_helper(buffer.data(), 1.23456789)); - ASSERT_STREQ("1.234567895E0", d2s_test_helper(buffer.data(), 1.234567895)); // 1.234567890 would be trimmed - ASSERT_STREQ("1.2345678901E0", d2s_test_helper(buffer.data(), 1.2345678901)); - ASSERT_STREQ("1.23456789012E0", d2s_test_helper(buffer.data(), 1.23456789012)); - ASSERT_STREQ("1.234567890123E0", d2s_test_helper(buffer.data(), 1.234567890123)); - ASSERT_STREQ("1.2345678901234E0", d2s_test_helper(buffer.data(), 1.2345678901234)); - ASSERT_STREQ("1.23456789012345E0", d2s_test_helper(buffer.data(), 1.23456789012345)); - ASSERT_STREQ("1.234567890123456E0", d2s_test_helper(buffer.data(), 1.234567890123456)); - ASSERT_STREQ("1.2345678901234567E0", d2s_test_helper(buffer.data(), 1.2345678901234567)); - - // Test 32-bit chunking - ASSERT_STREQ("4.294967294E0", d2s_test_helper(buffer.data(), 4.294967294)); // 2^32 - 2 - ASSERT_STREQ("4.294967295E0", d2s_test_helper(buffer.data(), 4.294967295)); // 2^32 - 1 - ASSERT_STREQ("4.294967296E0", d2s_test_helper(buffer.data(), 4.294967296)); // 2^32 - ASSERT_STREQ("4.294967297E0", d2s_test_helper(buffer.data(), 4.294967297)); // 2^32 + 1 - ASSERT_STREQ("4.294967298E0", d2s_test_helper(buffer.data(), 4.294967298)); // 2^32 + 2 -} - -// Test min, max shift values in shiftright128 -TEST(D2sTest, MinMaxShift) { - std::array buffer; - const uint64_t maxMantissa = ((std::uint64_t)1 << 53) - 1; - - // 32-bit opt-size=0: 49 <= dist <= 50 - // 32-bit opt-size=1: 30 <= dist <= 50 - // 64-bit opt-size=0: 50 <= dist <= 50 - // 64-bit opt-size=1: 30 <= dist <= 50 - ASSERT_STREQ("1.7800590868057611E-307", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 4, 0))); - // 32-bit opt-size=0: 49 <= dist <= 49 - // 32-bit opt-size=1: 28 <= dist <= 49 - // 64-bit opt-size=0: 50 <= dist <= 50 - // 64-bit opt-size=1: 28 <= dist <= 50 - ASSERT_STREQ("2.8480945388892175E-306", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 6, maxMantissa))); - // 32-bit opt-size=0: 52 <= dist <= 53 - // 32-bit opt-size=1: 2 <= dist <= 53 - // 64-bit opt-size=0: 53 <= dist <= 53 - // 64-bit opt-size=1: 2 <= dist <= 53 - ASSERT_STREQ("2.446494580089078E-296", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 41, 0))); - // 32-bit opt-size=0: 52 <= dist <= 52 - // 32-bit opt-size=1: 2 <= dist <= 52 - // 64-bit opt-size=0: 53 <= dist <= 53 - // 64-bit opt-size=1: 2 <= dist <= 53 - ASSERT_STREQ("4.8929891601781557E-296", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 40, maxMantissa))); - - // 32-bit opt-size=0: 57 <= dist <= 58 - // 32-bit opt-size=1: 57 <= dist <= 58 - // 64-bit opt-size=0: 58 <= dist <= 58 - // 64-bit opt-size=1: 58 <= dist <= 58 - ASSERT_STREQ("1.8014398509481984E16", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 1077, 0))); - // 32-bit opt-size=0: 57 <= dist <= 57 - // 32-bit opt-size=1: 57 <= dist <= 57 - // 64-bit opt-size=0: 58 <= dist <= 58 - // 64-bit opt-size=1: 58 <= dist <= 58 - ASSERT_STREQ("3.6028797018963964E16", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 1076, maxMantissa))); - // 32-bit opt-size=0: 51 <= dist <= 52 - // 32-bit opt-size=1: 51 <= dist <= 59 - // 64-bit opt-size=0: 52 <= dist <= 52 - // 64-bit opt-size=1: 52 <= dist <= 59 - ASSERT_STREQ("2.900835519859558E-216", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 307, 0))); - // 32-bit opt-size=0: 51 <= dist <= 51 - // 32-bit opt-size=1: 51 <= dist <= 59 - // 64-bit opt-size=0: 52 <= dist <= 52 - // 64-bit opt-size=1: 52 <= dist <= 59 - ASSERT_STREQ("5.801671039719115E-216", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 306, maxMantissa))); - - // https://github.com/ulfjack/ryu/commit/19e44d16d80236f5de25800f56d82606d1be00b9#commitcomment-30146483 - // 32-bit opt-size=0: 49 <= dist <= 49 - // 32-bit opt-size=1: 44 <= dist <= 49 - // 64-bit opt-size=0: 50 <= dist <= 50 - // 64-bit opt-size=1: 44 <= dist <= 50 - ASSERT_STREQ("3.196104012172126E-27", d2s_test_helper(buffer.data(), ieeeParts2Double(false, 934, 0x000FA7161A4D6E0Cu))); -} - -TEST(D2sTest, SmallIntegers) { - std::array buffer; - ASSERT_STREQ("9.007199254740991E15", d2s_test_helper(buffer.data(), 9007199254740991.0)); // 2^53-1 - ASSERT_STREQ("9.007199254740992E15", d2s_test_helper(buffer.data(), 9007199254740992.0)); // 2^53 - - ASSERT_STREQ("1E0", d2s_test_helper(buffer.data(), 1.0e+0)); - ASSERT_STREQ("1.2E1", d2s_test_helper(buffer.data(), 1.2e+1)); - ASSERT_STREQ("1.23E2", d2s_test_helper(buffer.data(), 1.23e+2)); - ASSERT_STREQ("1.234E3", d2s_test_helper(buffer.data(), 1.234e+3)); - ASSERT_STREQ("1.2345E4", d2s_test_helper(buffer.data(), 1.2345e+4)); - ASSERT_STREQ("1.23456E5", d2s_test_helper(buffer.data(), 1.23456e+5)); - ASSERT_STREQ("1.234567E6", d2s_test_helper(buffer.data(), 1.234567e+6)); - ASSERT_STREQ("1.2345678E7", d2s_test_helper(buffer.data(), 1.2345678e+7)); - ASSERT_STREQ("1.23456789E8", d2s_test_helper(buffer.data(), 1.23456789e+8)); - ASSERT_STREQ("1.23456789E9", d2s_test_helper(buffer.data(), 1.23456789e+9)); - ASSERT_STREQ("1.234567895E9", d2s_test_helper(buffer.data(), 1.234567895e+9)); - ASSERT_STREQ("1.2345678901E10", d2s_test_helper(buffer.data(), 1.2345678901e+10)); - ASSERT_STREQ("1.23456789012E11", d2s_test_helper(buffer.data(), 1.23456789012e+11)); - ASSERT_STREQ("1.234567890123E12", d2s_test_helper(buffer.data(), 1.234567890123e+12)); - ASSERT_STREQ("1.2345678901234E13", d2s_test_helper(buffer.data(), 1.2345678901234e+13)); - ASSERT_STREQ("1.23456789012345E14", d2s_test_helper(buffer.data(), 1.23456789012345e+14)); - ASSERT_STREQ("1.234567890123456E15", d2s_test_helper(buffer.data(), 1.234567890123456e+15)); - - // 10^i - ASSERT_STREQ("1E0", d2s_test_helper(buffer.data(), 1.0e+0)); - ASSERT_STREQ("1E1", d2s_test_helper(buffer.data(), 1.0e+1)); - ASSERT_STREQ("1E2", d2s_test_helper(buffer.data(), 1.0e+2)); - ASSERT_STREQ("1E3", d2s_test_helper(buffer.data(), 1.0e+3)); - ASSERT_STREQ("1E4", d2s_test_helper(buffer.data(), 1.0e+4)); - ASSERT_STREQ("1E5", d2s_test_helper(buffer.data(), 1.0e+5)); - ASSERT_STREQ("1E6", d2s_test_helper(buffer.data(), 1.0e+6)); - ASSERT_STREQ("1E7", d2s_test_helper(buffer.data(), 1.0e+7)); - ASSERT_STREQ("1E8", d2s_test_helper(buffer.data(), 1.0e+8)); - ASSERT_STREQ("1E9", d2s_test_helper(buffer.data(), 1.0e+9)); - ASSERT_STREQ("1E10", d2s_test_helper(buffer.data(), 1.0e+10)); - ASSERT_STREQ("1E11", d2s_test_helper(buffer.data(), 1.0e+11)); - ASSERT_STREQ("1E12", d2s_test_helper(buffer.data(), 1.0e+12)); - ASSERT_STREQ("1E13", d2s_test_helper(buffer.data(), 1.0e+13)); - ASSERT_STREQ("1E14", d2s_test_helper(buffer.data(), 1.0e+14)); - ASSERT_STREQ("1E15", d2s_test_helper(buffer.data(), 1.0e+15)); - - // 10^15 + 10^i - ASSERT_STREQ("1.000000000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+0)); - ASSERT_STREQ("1.00000000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+1)); - ASSERT_STREQ("1.0000000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+2)); - ASSERT_STREQ("1.000000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+3)); - ASSERT_STREQ("1.00000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+4)); - ASSERT_STREQ("1.0000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+5)); - ASSERT_STREQ("1.000000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+6)); - ASSERT_STREQ("1.00000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+7)); - ASSERT_STREQ("1.0000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+8)); - ASSERT_STREQ("1.000001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+9)); - ASSERT_STREQ("1.00001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+10)); - ASSERT_STREQ("1.0001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+11)); - ASSERT_STREQ("1.001E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+12)); - ASSERT_STREQ("1.01E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+13)); - ASSERT_STREQ("1.1E15", d2s_test_helper(buffer.data(), 1.0e+15 + 1.0e+14)); - - // Largest power of 2 <= 10^(i+1) - ASSERT_STREQ("8E0", d2s_test_helper(buffer.data(), 8.0)); - ASSERT_STREQ("6.4E1", d2s_test_helper(buffer.data(), 64.0)); - ASSERT_STREQ("5.12E2", d2s_test_helper(buffer.data(), 512.0)); - ASSERT_STREQ("8.192E3", d2s_test_helper(buffer.data(), 8192.0)); - ASSERT_STREQ("6.5536E4", d2s_test_helper(buffer.data(), 65536.0)); - ASSERT_STREQ("5.24288E5", d2s_test_helper(buffer.data(), 524288.0)); - ASSERT_STREQ("8.388608E6", d2s_test_helper(buffer.data(), 8388608.0)); - ASSERT_STREQ("6.7108864E7", d2s_test_helper(buffer.data(), 67108864.0)); - ASSERT_STREQ("5.36870912E8", d2s_test_helper(buffer.data(), 536870912.0)); - ASSERT_STREQ("8.589934592E9", d2s_test_helper(buffer.data(), 8589934592.0)); - ASSERT_STREQ("6.8719476736E10", d2s_test_helper(buffer.data(), 68719476736.0)); - ASSERT_STREQ("5.49755813888E11", d2s_test_helper(buffer.data(), 549755813888.0)); - ASSERT_STREQ("8.796093022208E12", d2s_test_helper(buffer.data(), 8796093022208.0)); - ASSERT_STREQ("7.0368744177664E13", d2s_test_helper(buffer.data(), 70368744177664.0)); - ASSERT_STREQ("5.62949953421312E14", d2s_test_helper(buffer.data(), 562949953421312.0)); - ASSERT_STREQ("9.007199254740992E15", d2s_test_helper(buffer.data(), 9007199254740992.0)); - - // 1000 * (Largest power of 2 <= 10^(i+1)) - ASSERT_STREQ("8E3", d2s_test_helper(buffer.data(), 8.0e+3)); - ASSERT_STREQ("6.4E4", d2s_test_helper(buffer.data(), 64.0e+3)); - ASSERT_STREQ("5.12E5", d2s_test_helper(buffer.data(), 512.0e+3)); - ASSERT_STREQ("8.192E6", d2s_test_helper(buffer.data(), 8192.0e+3)); - ASSERT_STREQ("6.5536E7", d2s_test_helper(buffer.data(), 65536.0e+3)); - ASSERT_STREQ("5.24288E8", d2s_test_helper(buffer.data(), 524288.0e+3)); - ASSERT_STREQ("8.388608E9", d2s_test_helper(buffer.data(), 8388608.0e+3)); - ASSERT_STREQ("6.7108864E10", d2s_test_helper(buffer.data(), 67108864.0e+3)); - ASSERT_STREQ("5.36870912E11", d2s_test_helper(buffer.data(), 536870912.0e+3)); - ASSERT_STREQ("8.589934592E12", d2s_test_helper(buffer.data(), 8589934592.0e+3)); - ASSERT_STREQ("6.8719476736E13", d2s_test_helper(buffer.data(), 68719476736.0e+3)); - ASSERT_STREQ("5.49755813888E14", d2s_test_helper(buffer.data(), 549755813888.0e+3)); - ASSERT_STREQ("8.796093022208E15", d2s_test_helper(buffer.data(), 8796093022208.0e+3)); -} - -} // ryu - -} // detail -} // namespace json -} // namespace boost diff --git a/test/ryu/gtest.hpp b/test/ryu/gtest.hpp deleted file mode 100644 index 582cebe21..000000000 --- a/test/ryu/gtest.hpp +++ /dev/null @@ -1,34 +0,0 @@ -// -// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// Official repository: https://github.com/boostorg/json -// - -#ifndef GTEST_HPP -#define GTEST_HPP - -#include -#include - -#include "test_suite.hpp" - -#define TEST(s1,s2) \ -struct s1 ## _ ## s2 ## _test; \ -TEST_SUITE(s1 ## _ ## s2 ## _test, "boost.Ryu." #s1 "." #s2); \ -struct s1 ## _ ## s2 ## _test { \ - void run(); }; void s1 ## _ ## s2 ## _test::run() - -#define EXPECT_STREQ(s1, s2) \ - BOOST_TEST(::boost::json::string_view(s1) == \ - ::boost::json::string_view(s2)) - -#define ASSERT_STREQ(s1, s2) \ - { auto const s1_ = (s1); auto const s2_ = (s2); \ - EXPECT_STREQ(s1_, s2_); } - -#define ASSERT_EQ(e1, e2) BOOST_TEST((e1)==(e2)) - -#endif diff --git a/test/serializer.cpp b/test/serializer.cpp index d2f82a9e1..a36f7c72d 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -838,7 +838,7 @@ class serializer_test #ifdef BOOST_DESCRIBE_CXX14 { serializer_test_ns::my_struct s{"some string", 1424, 12.4}; - check_udt(s, R"({"s":"some string","n":1424,"d":1.24E1})"); + check_udt(s, R"({"s":"some string","n":1424,"d":1.24E+01})"); } { check_udt( @@ -853,7 +853,7 @@ class serializer_test check_udt( v, "112" ); v = 0.5; - check_udt( v, "5E-1" ); + check_udt( v, "5E-01" ); v = "this is a string"; check_udt(v, R"("this is a string")");