int128_t -> int128_opt

This commit is contained in:
Victor Zverovich
2022-02-17 19:05:25 -08:00
parent 532a69a639
commit 15c2a3bacc
5 changed files with 86 additions and 78 deletions

View File

@@ -339,6 +339,36 @@ inline auto to_uintptr(const void* p) -> fallback_uintptr {
}
#endif
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) : lo_(value), hi_(0) {}
explicit operator int() const { return static_cast<int>(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)};
}
};
// Returns the largest possible value for type T. Same as
// std::numeric_limits<T>::max() but shorter and not affected by the max macro.
template <typename T> constexpr auto max_value() -> T {
@@ -348,7 +378,7 @@ template <typename T> constexpr auto num_bits() -> int {
return std::numeric_limits<T>::digits;
}
// std::numeric_limits<T>::digits may return 0 for 128-bit ints.
template <> constexpr auto num_bits<int128_t>() -> int { return 128; }
template <> constexpr auto num_bits<int128_opt>() -> int { return 128; }
template <> constexpr auto num_bits<uint128_t>() -> int { return 128; }
template <> constexpr auto num_bits<fallback_uintptr>() -> int {
return static_cast<int>(sizeof(void*) *
@@ -876,13 +906,13 @@ constexpr auto compile_string_to_view(detail::std_string_view<Char> s)
FMT_BEGIN_DETAIL_NAMESPACE
template <typename T> struct is_integral : std::is_integral<T> {};
template <> struct is_integral<int128_t> : std::true_type {};
template <> struct is_integral<int128_opt> : std::true_type {};
template <> struct is_integral<uint128_t> : std::true_type {};
template <typename T>
using is_signed =
std::integral_constant<bool, std::numeric_limits<T>::is_signed ||
std::is_same<T, int128_t>::value>;
std::is_same<T, int128_opt>::value>;
// Returns true if value is negative, false otherwise.
// Same as `value < 0` but doesn't produce warnings if T is an unsigned type.
@@ -908,9 +938,10 @@ template <typename T>
using uint32_or_64_or_128_t =
conditional_t<num_bits<T>() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS,
uint32_t,
conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>>;
conditional_t<num_bits<T>() <= 64, uint64_t, uint128_opt>>;
template <typename T>
using uint64_or_128_t = conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>;
using uint64_or_128_t =
conditional_t<num_bits<T>() <= 64, uint64_t, uint128_opt>;
#define FMT_POWERS_OF_10(factor) \
factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
@@ -950,7 +981,7 @@ template <typename T> FMT_CONSTEXPR auto count_digits_fallback(T n) -> int {
}
}
#if FMT_USE_INT128
FMT_CONSTEXPR inline auto count_digits(uint128_t n) -> int {
FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int {
return count_digits_fallback(n);
}
#endif
@@ -1044,7 +1075,7 @@ FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int {
template <typename Int> constexpr auto digits10() noexcept -> int {
return std::numeric_limits<Int>::digits10;
}
template <> constexpr auto digits10<int128_t>() noexcept -> int { return 38; }
template <> constexpr auto digits10<int128_opt>() noexcept -> int { return 38; }
template <> constexpr auto digits10<uint128_t>() noexcept -> int { return 38; }
template <typename Char> struct thousands_sep_result {
@@ -1250,7 +1281,7 @@ template <> struct float_info<double> {
template <typename T>
struct float_info<T, enable_if_t<std::is_same<T, long double>::value &&
std::numeric_limits<T>::digits == 64>> {
using carrier_uint = detail::uint128_t;
using carrier_uint = detail::uint128_opt;
static const int significand_bits = 64;
static const int exponent_bits = 15;
};