Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
100 changes: 96 additions & 4 deletions include/boost/int256/charconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Integer>
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<Integer>::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<int256::uint256>((std::numeric_limits<Integer>::max)())};
if (is_negative)
{
++max_value;
}

const int256::uint256 unsigned_base {static_cast<int256::uint256>(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<Integer>(-result) : static_cast<Integer>(result);

return {next, std::errc()};

#else

return from_chars_integer_impl<Integer, int256::uint256>(first, last, value, base);

#endif
}

} // namespace detail

// Every base, base 10 included, goes through the base-parameterized four argument
Expand All @@ -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<int256::uint256, int256::uint256>(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<int256::uint256, int256::uint256>(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
Expand All @@ -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<int256::int256, int256::uint256>(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<int256::int256, int256::uint256>(sv.data(), sv.data() + sv.size(), value, base);
return detail::from_chars_256_impl(sv.data(), sv.data() + sv.size(), value, base);
}

} // namespace charconv
Expand Down
39 changes: 39 additions & 0 deletions test/test_charconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint256>(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.
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions test/test_container_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 <utility>.
Expand Down
15 changes: 13 additions & 2 deletions test/test_i256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <typename T>
Expand Down Expand Up @@ -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<T>: true_type selects the signed maximum.
boost::int256::detail::builtin_u128 directed_max_impl(std::false_type)
{
return ~static_cast<boost::int256::detail::builtin_u128>(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<boost::int256::detail::builtin_i128>(
(static_cast<boost::int256::detail::builtin_u128>(1) << 127) - 1);
}

#endif // BOOST_INT256_HAS_INT128

template <typename T>
T directed_max(std::false_type /* wide */)
{
Expand Down
Loading