diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb6b1a8..5ae3c69 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1027,10 +1027,11 @@ jobs: strategy: fail-fast: false - runs-on: ubuntu-latest + # NVIDIA's ubuntu2604 repository does not carry cuda-nvcc-12-8 + runs-on: ubuntu-24.04 steps: - - uses: Jimver/cuda-toolkit@v0.2.30 + - uses: Jimver/cuda-toolkit@v0.2.35 id: cuda-toolkit with: cuda: '12.8.0' diff --git a/include/boost/int256/charconv.hpp b/include/boost/int256/charconv.hpp index 542ee45..27e734c 100644 --- a/include/boost/int256/charconv.hpp +++ b/include/boost/int256/charconv.hpp @@ -281,6 +281,98 @@ BOOST_INT256_HOST_DEVICE constexpr int num_digits(const int256::uint256& x) noex return estimated_digits; } +// Parses [first, last) into a 256-bit Integer with the contract of Boost.Charconv's +// from_chars_integer_impl. That function assigns BOOST_CHARCONV_INT128_MAX to uint256 +// in a branch that is a plain if before C++17, and uint256 has no builtin 128-bit +// operands without BOOST_INT256_HAS_INT128, so its loop is mirrored here for that case. +template +BOOST_INT256_HOST_DEVICE BOOST_CXX14_CONSTEXPR from_chars_result from_chars_256_impl(const char* first, const char* last, Integer& value, const int base) noexcept +{ + #if defined(BOOST_CHARCONV_HAS_INT128) && !defined(BOOST_INT256_HAS_INT128) + + if (!((first <= last) && (base >= 2 && base <= 36))) + { + return {first, std::errc::invalid_argument}; + } + + auto is_negative {false}; + auto next {first}; + + if (next != last) + { + if (std::numeric_limits::is_signed && *next == '-') + { + is_negative = true; + ++next; + } + else if (*next == '-' || *next == '+' || *next == ' ') + { + return {first, std::errc::invalid_argument}; + } + } + + // The magnitude of the most negative value is one more than the maximum + int256::uint256 max_value {static_cast((std::numeric_limits::max)())}; + if (is_negative) + { + ++max_value; + } + + const int256::uint256 unsigned_base {static_cast(base)}; + const int256::uint256 overflow_value {max_value / unsigned_base}; + const int256::uint256 max_digit {max_value % unsigned_base}; + + if (next == last) + { + return {first, std::errc::invalid_argument}; + } + + const auto first_digit {digit_from_char(*next)}; + if (first_digit >= base) + { + return {first, std::errc::invalid_argument}; + } + + int256::uint256 result {first_digit}; + auto overflowed {false}; + + // Every digit is checked against the bound, which avoids relying on Charconv's + // log_2_table (Boost 1.86 and later) for an unchecked leading window + for (++next; next != last; ++next) + { + const auto current_digit {digit_from_char(*next)}; + if (current_digit >= base) + { + break; + } + + if (result < overflow_value || (result == overflow_value && current_digit <= max_digit)) + { + result = result * unsigned_base + current_digit; + } + else + { + // Keep consuming digits so the returned pointer is past the whole number + overflowed = true; + } + } + + if (overflowed) + { + return {next, std::errc::result_out_of_range}; + } + + value = is_negative ? static_cast(-result) : static_cast(result); + + return {next, std::errc()}; + + #else + + return from_chars_integer_impl(first, last, value, base); + + #endif +} + } // namespace detail // Every base, base 10 included, goes through the base-parameterized four argument @@ -303,12 +395,12 @@ BOOST_INT256_HOST_DEVICE BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars(char* BOOST_INT256_HOST_DEVICE BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, int256::uint256& value, const int base = 10) noexcept { - return detail::from_chars_integer_impl(first, last, value, base); + return detail::from_chars_256_impl(first, last, value, base); } BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(core::string_view sv, int256::uint256& value, const int base = 10) noexcept { - return detail::from_chars_integer_impl(sv.data(), sv.data() + sv.size(), value, base); + return detail::from_chars_256_impl(sv.data(), sv.data() + sv.size(), value, base); } // The signed twins. Same note as above about to_chars_integer_impl's buffer size: this @@ -325,12 +417,12 @@ BOOST_INT256_HOST_DEVICE BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars(char* BOOST_INT256_HOST_DEVICE BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, int256::int256& value, const int base = 10) noexcept { - return detail::from_chars_integer_impl(first, last, value, base); + return detail::from_chars_256_impl(first, last, value, base); } BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(core::string_view sv, int256::int256& value, const int base = 10) noexcept { - return detail::from_chars_integer_impl(sv.data(), sv.data() + sv.size(), value, base); + return detail::from_chars_256_impl(sv.data(), sv.data() + sv.size(), value, base); } } // namespace charconv diff --git a/test/test_charconv.cpp b/test/test_charconv.cpp index fc1af22..d14ae3e 100644 --- a/test/test_charconv.cpp +++ b/test/test_charconv.cpp @@ -246,6 +246,44 @@ void test_from_chars_maximum_and_overflow() } } +// One past each extreme in every base, the tightest overflow: the smallest +// multiple of base above the unsigned maximum, 2^255, and -(2^255 + 1). These +// must fail with the pointer past the whole number and the value untouched. +void test_from_chars_tight_bounds() +{ + const uint256 max {uint256_max()}; + const uint256 signed_max_magnitude {max >> 1}; + + for (int base {2}; base <= 36; ++base) + { + bool ok {false}; + const auto past_unsigned_max {to_string_in_base(max / static_cast(base) + 1U, base, &ok) + "0"}; + BOOST_TEST(ok); + + { + uint256 parsed {42U}; + const auto r {boost::charconv::from_chars(past_unsigned_max.data(), past_unsigned_max.data() + past_unsigned_max.size(), parsed, base)}; + BOOST_TEST(r.ec == std::errc::result_out_of_range); + BOOST_TEST_EQ(r.ptr, past_unsigned_max.data() + past_unsigned_max.size()); + BOOST_TEST_EQ(parsed, uint256{42U}); + } + + const auto past_signed_max {to_string_in_base(signed_max_magnitude + 1U, base, &ok)}; + BOOST_TEST(ok); + const auto past_signed_min {"-" + to_string_in_base(signed_max_magnitude + 2U, base, &ok)}; + BOOST_TEST(ok); + + for (const auto& text : {past_signed_max, past_signed_min}) + { + int256 parsed {42}; + const auto r {boost::charconv::from_chars(text.data(), text.data() + text.size(), parsed, base)}; + BOOST_TEST(r.ec == std::errc::result_out_of_range); + BOOST_TEST_EQ(r.ptr, text.data() + text.size()); + BOOST_TEST(parsed == int256{42}); + } + } +} + void test_from_chars_invalid() { // A sign, in either direction, is not part of an unsigned value. @@ -526,6 +564,7 @@ int main() test_from_chars_partial(); test_string_view_overload(); test_from_chars_maximum_and_overflow(); + test_from_chars_tight_bounds(); test_to_chars_every_base(); test_from_chars_round_trips(); test_random_round_trips(); diff --git a/test/test_container_hash.cpp b/test/test_container_hash.cpp index 5a3f419..af49f10 100644 --- a/test/test_container_hash.cpp +++ b/test/test_container_hash.cpp @@ -18,6 +18,7 @@ # pragma clang diagnostic ignored "-Wsign-conversion" # pragma clang diagnostic ignored "-Wundef" # pragma clang diagnostic ignored "-Wshadow" +# pragma clang diagnostic ignored "-Wdouble-promotion" #elif defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wold-style-cast" @@ -26,6 +27,7 @@ # pragma GCC diagnostic ignored "-Wundef" # pragma GCC diagnostic ignored "-Wshadow" # pragma GCC diagnostic ignored "-Wuseless-cast" +# pragma GCC diagnostic ignored "-Wdouble-promotion" #elif defined(_MSC_VER) # pragma warning(push) // Padding warnings propagate out of . diff --git a/test/test_i256.cpp b/test/test_i256.cpp index a9e6c35..5748848 100644 --- a/test/test_i256.cpp +++ b/test/test_i256.cpp @@ -93,6 +93,10 @@ smp widen_i(boost::int256::detail::builtin_u128 v, std::false_type) return smp{smp{hi} << 64} | smp{lo}; } +// builtin_i128 is only in the operand list on the __int128 path; MSVC's +// std::_Signed128 is left out, so the overload would be unused there. +#if defined(BOOST_INT256_HAS_INT128) + smp widen_i(boost::int256::detail::builtin_i128 v, std::false_type) { using builtin_u128 = boost::int256::detail::builtin_u128; @@ -110,6 +114,8 @@ smp widen_i(boost::int256::detail::builtin_i128 v, std::false_type) return -magnitude; } +#endif // BOOST_INT256_HAS_INT128 + #endif template @@ -541,17 +547,22 @@ T directed_min(std::true_type /* narrow */) #if defined(BOOST_INT256_HAS_INT128) || defined(BOOST_INT256_HAS_MSVC_INT128) -boost::int256::detail::builtin_u128 directed_max_impl(std::true_type) +// Dispatched on is_signed_operand: true_type selects the signed maximum. +boost::int256::detail::builtin_u128 directed_max_impl(std::false_type) { return ~static_cast(0); } -boost::int256::detail::builtin_i128 directed_max_impl(std::false_type) +#if defined(BOOST_INT256_HAS_INT128) + +boost::int256::detail::builtin_i128 directed_max_impl(std::true_type) { return static_cast( (static_cast(1) << 127) - 1); } +#endif // BOOST_INT256_HAS_INT128 + template T directed_max(std::false_type /* wide */) {