From 2a1b3ac629bfec51ce70d3c0ebaf28e706754e19 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 10 Jul 2022 08:14:18 -0700 Subject: [PATCH 1/2] Fix large shift in uint128_fallback --- include/fmt/format.h | 2 ++ test/format-test.cc | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) 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; } } -- 2.36.1