Skip to content

Commit

Permalink
Implement 128-bit operator+= for uint128_fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 27, 2022
1 parent b41890c commit 9693016
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 6 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,12 @@ class uint128_fallback {
FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& {
return *this = *this >> shift;
}
FMT_CONSTEXPR void operator+=(uint64_t n) {
lo_ += n;
if (lo_ < n) ++hi_;
FMT_CONSTEXPR void operator+=(uint128_fallback n) {
uint64_t new_lo = lo_ + n.lo_;
uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0);
FMT_ASSERT(new_hi >= hi_, "");
lo_ = new_lo;
hi_ = new_hi;
}
};

Expand Down
15 changes: 12 additions & 3 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ using fmt::memory_buffer;
using fmt::runtime;
using fmt::string_view;
using fmt::detail::max_value;
using fmt::detail::uint128_fallback;

using testing::Return;
using testing::StrictMock;

enum { buffer_size = 256 };

TEST(uint128_test, ctor) {
using fmt::detail::uint128_fallback;
auto n = uint128_fallback();
EXPECT_EQ(n, 0);
n = uint128_fallback(42);
Expand All @@ -49,7 +49,7 @@ TEST(uint128_test, ctor) {
}

TEST(uint128_test, shift) {
auto n = fmt::detail::uint128_fallback(42);
auto n = uint128_fallback(42);
n = n << 64;
EXPECT_EQ(static_cast<uint64_t>(n), 0);
n = n >> 64;
Expand All @@ -62,10 +62,19 @@ TEST(uint128_test, shift) {
}

TEST(uint128_test, minus) {
auto n = fmt::detail::uint128_fallback(42);
auto n = uint128_fallback(42);
EXPECT_EQ(n - 2, 40);
}

TEST(uint128_test, plus_assign) {
auto n = uint128_fallback(32);
n += uint128_fallback(10);
EXPECT_EQ(n, 42);
n = uint128_fallback(max_value<uint64_t>());
n += uint128_fallback(1);
EXPECT_EQ(n, uint128_fallback(1) << 64);
}

template <typename Float> void check_isfinite() {
using fmt::detail::isfinite;
EXPECT_TRUE(isfinite(Float(0.0)));
Expand Down

0 comments on commit 9693016

Please sign in to comment.