Skip to content
Merged
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
1 change: 1 addition & 0 deletions include/boost/int128/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/int128/int128.hpp>
#include <string>
#include <format>
#include <tuple>

#define BOOST_INT128_HAS_FORMAT

Expand Down
14 changes: 4 additions & 10 deletions include/boost/int128/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,11 @@ constexpr uint128_t gcd(uint128_t a, uint128_t b) noexcept
}

b -= a;
} while (b != 0U && (a.high | b.high) > 0U);

// Stop doing 128-bit math as soon as we can
if (a.high == 0U && b.high == 0U)
{
const auto g {detail::gcd64(a.low, b.low)};
return uint128_t{0, g} << shift;
}

} while (b != 0U);

return a << shift; // LCOV_EXCL_LINE : Should be unreachable, but this is also the correct answer
// Stop doing 128-bit math as soon as we can
const auto g {detail::gcd64(a.low, b.low)};
return uint128_t{0, g} << shift;
}

constexpr int128_t gcd(const int128_t a, const int128_t b) noexcept
Expand Down
4 changes: 2 additions & 2 deletions test/limits_link_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
void test_odr_use( int const* );
void test_odr_use( std::size_t const* );

template<typename T> void test()
template<typename T> void test() // LCOV_EXCL_LINE
{
test_odr_use( &std::numeric_limits<T>::digits10 );
test_odr_use( &std::numeric_limits<T>::digits10 ); // LCOV_EXCL_LINE
}

void f2()
Expand Down
2 changes: 1 addition & 1 deletion test/limits_link_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ void test_odr_use( int const* )
{
}

void test_odr_use ( std::size_t const* )
void test_odr_use ( std::size_t const* ) // LCOV_EXCL_LINE
{
}
66 changes: 66 additions & 0 deletions test/test_num_digits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,51 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#ifndef BOOST_INT128_ALLOW_SIGN_CONVERSION
# define BOOST_INT128_ALLOW_SIGN_CONVERSION
#endif

#include <boost/int128.hpp>
#include <boost/int128/charconv.hpp>
#include <boost/core/lightweight_test.hpp>
#include <random>
#include <array>

#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wold-style-cast"
#endif

#include <boost/random/uniform_int_distribution.hpp>

#ifdef __clang__
# pragma clang diagnostic pop
#endif

static std::mt19937_64 rng{42};
boost::random::uniform_int_distribution<boost::int128::uint128_t> dist(std::numeric_limits<boost::int128::uint128_t>::min(),
std::numeric_limits<boost::int128::uint128_t>::max());

constexpr std::array<boost::int128::uint128_t, 5> edge_cases {{
boost::int128::uint128_t{1, 0},
boost::int128::uint128_t{1, 1},
boost::int128::uint128_t{2, 0},
boost::int128::uint128_t{2, UINT64_MAX - 1U},
boost::int128::uint128_t{4, 0},
}};

constexpr int trivial_num_digits(boost::int128::uint128_t x) noexcept
{
int digits = 0;

while (x)
{
x /= UINT64_C(10);
++digits;
}

return digits;
}

void test()
{
Expand All @@ -14,6 +56,30 @@ void test()
const auto current_val_digits {boost::charconv::detail::num_digits(current_val)};
BOOST_TEST_EQ(current_val_digits, i + 1);
}

for (int i {1}; i < 39; ++i)
{
const auto current_val {boost::charconv::detail::int128_pow10[static_cast<std::size_t>(i)] - 1U};
const auto current_val_digits {boost::charconv::detail::num_digits(current_val)};
BOOST_TEST_EQ(current_val_digits, i);
}

for (int i {}; i < 1024; ++i)
{
const auto value {dist(rng)};
const auto current_val_digits {boost::charconv::detail::num_digits(value)};
const auto trivial_digits {trivial_num_digits(value)};

BOOST_TEST_EQ(current_val_digits, trivial_digits);
}

for (const auto value : edge_cases)
{
const auto current_val_digits {boost::charconv::detail::num_digits(value)};
const auto trivial_digits {trivial_num_digits(value)};

BOOST_TEST_EQ(current_val_digits, trivial_digits);
}
}

int main()
Expand Down