diff --git a/include/fmt/format.h b/include/fmt/format.h index 0bd2fdb1..6516975e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -366,10 +366,12 @@ class uint128_fallback { } FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback { if (shift == 64) return {0, hi_}; + if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64); return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)}; } FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback { if (shift == 64) return {lo_, 0}; + if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64); return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)}; } FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& { diff --git a/test/format-test.cc b/test/format-test.cc index 45a92624..8c1c305f 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -59,6 +59,8 @@ TEST(uint128_test, shift) { EXPECT_EQ(static_cast(n), 0x8000000000000000); n = n >> 62; EXPECT_EQ(static_cast(n), 42); + EXPECT_EQ(uint128_fallback(1) << 112, uint128_fallback(0x1000000000000, 0)); + EXPECT_EQ(uint128_fallback(0x1000000000000, 0) >> 112, uint128_fallback(1)); } TEST(uint128_test, minus) { @@ -234,7 +236,7 @@ TEST(util_test, format_system_error) { throws_on_alloc = true; } if (!throws_on_alloc) { - fmt::print("warning: std::allocator allocates {} chars", max_size); + fmt::print("warning: std::allocator allocates {} chars\n", max_size); return; } }