Skip to content

Commit

Permalink
Implement a fallback uint128_t
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 17, 2022
1 parent 6a13464 commit ce93a66
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
33 changes: 31 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,35 @@ template <typename T> inline auto convert_for_visit(T value) -> T {
#endif
#if !FMT_USE_INT128
enum class int128_t {};
enum class uint128_t {};
class uint128_t {
private:
uint64_t lo_, hi_;
constexpr uint128_t(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}

public:
constexpr uint128_t(uint64_t value = 0) : hi_(0), lo_(value) {}
explicit operator int() const { return lo_; }
explicit operator uint64_t() const { return lo_; }
friend auto operator==(const uint128_t& lhs, const uint128_t& rhs) -> bool {
return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_;
}
friend auto operator&(const uint128_t& lhs, const uint128_t& rhs)
-> uint128_t {
return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_};
}
friend auto operator-(const uint128_t& lhs, uint64_t rhs) -> uint128_t {
FMT_ASSERT(lhs.lo_ >= rhs, "");
return {lhs.hi_, lhs.lo_ - rhs};
}
auto operator>>(int shift) const -> uint128_t {
if (shift == 64) return {0, hi_};
return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)};
}
auto operator<<(int shift) const -> uint128_t {
if (shift == 64) return {lo_, 0};
return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)};
}
};
// Reduce template instantiations.
template <typename T> inline auto convert_for_visit(T) -> monostate {
return {};
Expand Down Expand Up @@ -1431,7 +1459,8 @@ template <typename Context> struct arg_mapper {
!std::is_const<remove_reference_t<T>>::value ||
has_fallback_formatter<U, char_type>::value> {};

#if (FMT_MSC_VER != 0 && FMT_MSC_VER < 1910) || FMT_ICC_VERSION != 0 || FMT_NVCC != 0
#if (FMT_MSC_VER != 0 && FMT_MSC_VER < 1910) || FMT_ICC_VERSION != 0 || \
FMT_NVCC != 0
// Workaround a bug in MSVC and Intel (Issue 2746).
template <typename T> FMT_CONSTEXPR FMT_INLINE auto do_map(T&& val) -> T& {
return val;
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ struct fp {
using carrier_uint = typename dragonbox::float_info<Float>::carrier_uint;
const carrier_uint significand_mask = implicit_bit - 1;
auto u = bit_cast<carrier_uint>(n);
f = u & significand_mask;
f = static_cast<uint64_t>(u & significand_mask);
int biased_e =
static_cast<int>((u & exponent_mask<Float>()) >>
dragonbox::float_info<Float>::significand_bits);
Expand Down
27 changes: 27 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ using testing::Return;
# error core-test includes format.h
#endif

TEST(uint128_test, ctor) {
using fmt::detail::uint128_t;
auto n = uint128_t();
EXPECT_EQ(n, 0);
n = uint128_t(42);
EXPECT_EQ(n, 42);
EXPECT_EQ(static_cast<uint64_t>(n), 42);
}

TEST(uint128_test, shift) {
auto n = fmt::detail::uint128_t(42);
n = n << 64;
EXPECT_EQ(static_cast<uint64_t>(n), 0);
n = n >> 64;
EXPECT_EQ(static_cast<uint64_t>(n), 42);
n = n << 62;
EXPECT_EQ(static_cast<uint64_t>(n >> 64), 0xa);
EXPECT_EQ(static_cast<uint64_t>(n), 0x8000000000000000);
n = n >> 62;
EXPECT_EQ(static_cast<uint64_t>(n), 42);
}

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

TEST(string_view_test, value_type) {
static_assert(std::is_same<string_view::value_type, char>::value, "");
}
Expand Down

0 comments on commit ce93a66

Please sign in to comment.