Skip to content

Commit b98926b

Browse files
committed
uint128_fallback -> uint128
1 parent 6322cf0 commit b98926b

File tree

4 files changed

+65
-78
lines changed

4 files changed

+65
-78
lines changed

include/fmt/base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ inline auto map(native_uint128 x) -> native_uint128 { return x; }
404404
# define FMT_USE_INT128 0
405405
#endif
406406
#if !FMT_USE_INT128
407-
enum class native_int128 {}; // A fallback to reduce conditional compilation.
407+
// Fallbacks to reduce conditional compilation and SFINAE.
408+
enum class native_int128 {};
408409
enum class native_uint128 {};
409-
// Reduce template instantiations.
410410
inline auto map(native_int128) -> monostate { return {}; }
411411
inline auto map(native_uint128) -> monostate { return {}; }
412412
#endif

include/fmt/format-inl.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ inline auto umul96_upper64(uint32_t x, uint64_t y) noexcept -> uint64_t {
209209

210210
// Computes lower 128 bits of multiplication of a 64-bit unsigned integer and a
211211
// 128-bit unsigned integer.
212-
inline auto umul192_lower128(uint64_t x, uint128_fallback y) noexcept
213-
-> uint128_fallback {
212+
inline auto umul192_lower128(uint64_t x, uint128 y) noexcept -> uint128 {
214213
uint64_t high = x * y.high();
215-
uint128_fallback high_low = umul128(x, y.low());
214+
uint128 high_low = umul128(x, y.low());
216215
return {high + high_low.high(), high_low.low()};
217216
}
218217

@@ -380,13 +379,13 @@ template <> struct cache_accessor<float> {
380379

381380
template <> struct cache_accessor<double> {
382381
using carrier_uint = float_info<double>::carrier_uint;
383-
using cache_entry_type = uint128_fallback;
382+
using cache_entry_type = uint128;
384383

385-
static auto get_cached_power(int k) noexcept -> uint128_fallback {
384+
static auto get_cached_power(int k) noexcept -> uint128 {
386385
FMT_ASSERT(k >= float_info<double>::min_k && k <= float_info<double>::max_k,
387386
"k is out of range");
388387

389-
static constexpr uint128_fallback pow10_significands[] = {
388+
static constexpr uint128 pow10_significands[] = {
390389
#if FMT_USE_FULL_CACHE_DRAGONBOX
391390
{0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b},
392391
{0x9faacf3df73609b1, 0x77b191618c54e9ad},
@@ -1072,7 +1071,7 @@ template <> struct cache_accessor<double> {
10721071
int offset = k - kb;
10731072

10741073
// Get base cache.
1075-
uint128_fallback base_cache = pow10_significands[cache_index];
1074+
uint128 base_cache = pow10_significands[cache_index];
10761075
if (offset == 0) return base_cache;
10771076

10781077
// Compute the required amount of bit-shift.
@@ -1081,17 +1080,16 @@ template <> struct cache_accessor<double> {
10811080

10821081
// Try to recover the real cache.
10831082
uint64_t pow5 = powers_of_5_64[offset];
1084-
uint128_fallback recovered_cache = umul128(base_cache.high(), pow5);
1085-
uint128_fallback middle_low = umul128(base_cache.low(), pow5);
1083+
uint128 recovered_cache = umul128(base_cache.high(), pow5);
1084+
uint128 middle_low = umul128(base_cache.low(), pow5);
10861085

10871086
recovered_cache += middle_low.high();
10881087

10891088
uint64_t high_to_middle = recovered_cache.high() << (64 - alpha);
10901089
uint64_t middle_to_low = recovered_cache.low() << (64 - alpha);
10911090

1092-
recovered_cache =
1093-
uint128_fallback{(recovered_cache.low() >> alpha) | high_to_middle,
1094-
((middle_low.low() >> alpha) | middle_to_low)};
1091+
recovered_cache = uint128{(recovered_cache.low() >> alpha) | high_to_middle,
1092+
((middle_low.low() >> alpha) | middle_to_low)};
10951093
FMT_ASSERT(recovered_cache.low() + 1 != 0, "");
10961094
return {recovered_cache.high(), recovered_cache.low() + 1};
10971095
#endif
@@ -1152,7 +1150,7 @@ template <> struct cache_accessor<double> {
11521150
}
11531151
};
11541152

1155-
FMT_FUNC auto get_cached_power(int k) noexcept -> uint128_fallback {
1153+
FMT_FUNC auto get_cached_power(int k) noexcept -> uint128 {
11561154
return cache_accessor<double>::get_cached_power(k);
11571155
}
11581156

include/fmt/format.h

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ inline auto is_big_endian() -> bool {
291291
#endif
292292
}
293293

294-
class uint128_fallback {
294+
class uint128 {
295295
private:
296296
uint64_t lo_, hi_;
297297

298298
public:
299-
constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}
300-
constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {}
299+
constexpr uint128(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}
300+
constexpr uint128(uint64_t value = 0) : lo_(value), hi_(0) {}
301301

302302
constexpr auto high() const noexcept -> uint64_t { return hi_; }
303303
constexpr auto low() const noexcept -> uint64_t { return lo_; }
@@ -307,77 +307,69 @@ class uint128_fallback {
307307
return static_cast<T>(lo_);
308308
}
309309

310-
friend constexpr auto operator==(const uint128_fallback& lhs,
311-
const uint128_fallback& rhs) -> bool {
310+
friend constexpr auto operator==(const uint128& lhs, const uint128& rhs)
311+
-> bool {
312312
return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_;
313313
}
314-
friend constexpr auto operator!=(const uint128_fallback& lhs,
315-
const uint128_fallback& rhs) -> bool {
314+
friend constexpr auto operator!=(const uint128& lhs, const uint128& rhs)
315+
-> bool {
316316
return !(lhs == rhs);
317317
}
318-
friend constexpr auto operator>(const uint128_fallback& lhs,
319-
const uint128_fallback& rhs) -> bool {
318+
friend constexpr auto operator>(const uint128& lhs, const uint128& rhs)
319+
-> bool {
320320
return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_;
321321
}
322-
friend constexpr auto operator|(const uint128_fallback& lhs,
323-
const uint128_fallback& rhs)
324-
-> uint128_fallback {
322+
friend constexpr auto operator|(const uint128& lhs, const uint128& rhs)
323+
-> uint128 {
325324
return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_};
326325
}
327-
friend constexpr auto operator&(const uint128_fallback& lhs,
328-
const uint128_fallback& rhs)
329-
-> uint128_fallback {
326+
friend constexpr auto operator&(const uint128& lhs, const uint128& rhs)
327+
-> uint128 {
330328
return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_};
331329
}
332-
friend constexpr auto operator~(const uint128_fallback& n)
333-
-> uint128_fallback {
334-
return {~n.hi_, ~n.lo_};
335-
}
336-
friend FMT_CONSTEXPR auto operator+(const uint128_fallback& lhs,
337-
const uint128_fallback& rhs)
338-
-> uint128_fallback {
339-
auto result = uint128_fallback(lhs);
330+
friend FMT_CONSTEXPR auto operator+(const uint128& lhs, const uint128& rhs)
331+
-> uint128 {
332+
auto result = uint128(lhs);
340333
result += rhs;
341334
return result;
342335
}
343-
friend FMT_CONSTEXPR auto operator*(const uint128_fallback& lhs, uint32_t rhs)
344-
-> uint128_fallback {
336+
friend FMT_CONSTEXPR auto operator*(const uint128& lhs, uint32_t rhs)
337+
-> uint128 {
345338
FMT_ASSERT(lhs.hi_ == 0, "");
346339
uint64_t hi = (lhs.lo_ >> 32) * rhs;
347340
uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs;
348341
uint64_t new_lo = (hi << 32) + lo;
349342
return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo};
350343
}
351-
friend constexpr auto operator-(const uint128_fallback& lhs, uint64_t rhs)
352-
-> uint128_fallback {
344+
friend constexpr auto operator-(const uint128& lhs, uint64_t rhs) -> uint128 {
353345
return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs};
354346
}
355-
FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback {
347+
FMT_CONSTEXPR auto operator>>(int shift) const -> uint128 {
356348
if (shift == 64) return {0, hi_};
357-
if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64);
349+
if (shift > 64) return uint128(0, hi_) >> (shift - 64);
358350
return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)};
359351
}
360-
FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback {
352+
FMT_CONSTEXPR auto operator<<(int shift) const -> uint128 {
361353
if (shift == 64) return {lo_, 0};
362-
if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64);
354+
if (shift > 64) return uint128(lo_, 0) << (shift - 64);
363355
return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)};
364356
}
365-
FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& {
357+
FMT_CONSTEXPR auto operator>>=(int shift) -> uint128& {
366358
return *this = *this >> shift;
367359
}
368-
FMT_CONSTEXPR void operator+=(uint128_fallback n) {
360+
FMT_CONSTEXPR void operator+=(uint128 n) {
369361
uint64_t new_lo = lo_ + n.lo_;
370362
uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0);
371363
FMT_ASSERT(new_hi >= hi_, "");
372364
lo_ = new_lo;
373365
hi_ = new_hi;
374366
}
375-
FMT_CONSTEXPR void operator&=(uint128_fallback n) {
367+
FMT_CONSTEXPR void operator&=(uint128 n) {
376368
lo_ &= n.lo_;
377369
hi_ &= n.hi_;
378370
}
379371

380-
FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128_fallback& {
372+
FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128& {
381373
if (is_constant_evaluated()) {
382374
lo_ += n;
383375
hi_ += (lo_ < n ? 1 : 0);
@@ -403,8 +395,7 @@ class uint128_fallback {
403395
}
404396
};
405397

406-
using uint128_t =
407-
conditional_t<FMT_USE_INT128, native_uint128, uint128_fallback>;
398+
using uint128_t = conditional_t<FMT_USE_INT128, native_uint128, uint128>;
408399

409400
#ifdef UINTPTR_MAX
410401
using uintptr_t = ::uintptr_t;
@@ -423,10 +414,10 @@ template <typename T> constexpr auto num_bits() -> int {
423414
// std::numeric_limits<T>::digits may return 0 for 128-bit ints.
424415
template <> constexpr auto num_bits<native_int128>() -> int { return 128; }
425416
template <> constexpr auto num_bits<native_uint128>() -> int { return 128; }
426-
template <> constexpr auto num_bits<uint128_fallback>() -> int { return 128; }
417+
template <> constexpr auto num_bits<uint128>() -> int { return 128; }
427418

428419
// A heterogeneous bit_cast used for converting 96-bit long double to uint128_t
429-
// and 128-bit pointers to uint128_fallback.
420+
// and 128-bit pointers to uint128.
430421
template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) > sizeof(From))>
431422
inline auto bit_cast(const From& from) -> To {
432423
constexpr auto size = static_cast<int>(sizeof(From) / sizeof(unsigned short));
@@ -1472,7 +1463,7 @@ template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
14721463
};
14731464

14741465
// Computes 128-bit result of multiplication of two 64-bit unsigned integers.
1475-
FMT_INLINE auto umul128(uint64_t x, uint64_t y) noexcept -> uint128_fallback {
1466+
FMT_INLINE auto umul128(uint64_t x, uint64_t y) noexcept -> uint128 {
14761467
#if FMT_USE_INT128
14771468
auto p = static_cast<native_uint128>(x) * static_cast<native_uint128>(y);
14781469
return {static_cast<uint64_t>(p >> 64), static_cast<uint64_t>(p)};
@@ -1528,14 +1519,13 @@ inline auto umul128_upper64(uint64_t x, uint64_t y) noexcept -> uint64_t {
15281519

15291520
// Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a
15301521
// 128-bit unsigned integer.
1531-
inline auto umul192_upper128(uint64_t x, uint128_fallback y) noexcept
1532-
-> uint128_fallback {
1533-
uint128_fallback r = umul128(x, y.high());
1522+
inline auto umul192_upper128(uint64_t x, uint128 y) noexcept -> uint128 {
1523+
uint128 r = umul128(x, y.high());
15341524
r += umul128_upper64(x, y.low());
15351525
return r;
15361526
}
15371527

1538-
FMT_API auto get_cached_power(int k) noexcept -> uint128_fallback;
1528+
FMT_API auto get_cached_power(int k) noexcept -> uint128;
15391529

15401530
// Type-specific information that Dragonbox uses.
15411531
template <typename T, typename Enable = void> struct float_info;

test/format-test.cc

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ using fmt::memory_buffer;
4343
using fmt::runtime;
4444
using fmt::string_view;
4545
using fmt::detail::max_value;
46-
using fmt::detail::uint128_fallback;
46+
using fmt::detail::uint128;
4747

4848
using testing::Return;
4949
using testing::StrictMock;
@@ -55,15 +55,15 @@ static_assert(std::output_iterator<fmt::appender, char>);
5555
enum { buffer_size = 256 };
5656

5757
TEST(uint128_test, ctor) {
58-
auto n = uint128_fallback();
58+
auto n = uint128();
5959
EXPECT_EQ(n, 0);
60-
n = uint128_fallback(42);
60+
n = uint128(42);
6161
EXPECT_EQ(n, 42);
6262
EXPECT_EQ(static_cast<uint64_t>(n), 42);
6363
}
6464

6565
TEST(uint128_test, shift) {
66-
auto n = uint128_fallback(42);
66+
auto n = uint128(42);
6767
n = n << 64;
6868
EXPECT_EQ(static_cast<uint64_t>(n), 0);
6969
n = n >> 64;
@@ -73,26 +73,26 @@ TEST(uint128_test, shift) {
7373
EXPECT_EQ(static_cast<uint64_t>(n), 0x8000000000000000);
7474
n = n >> 62;
7575
EXPECT_EQ(static_cast<uint64_t>(n), 42);
76-
EXPECT_EQ(uint128_fallback(1) << 112, uint128_fallback(0x1000000000000, 0));
77-
EXPECT_EQ(uint128_fallback(0x1000000000000, 0) >> 112, uint128_fallback(1));
76+
EXPECT_EQ(uint128(1) << 112, uint128(0x1000000000000, 0));
77+
EXPECT_EQ(uint128(0x1000000000000, 0) >> 112, uint128(1));
7878
}
7979

8080
TEST(uint128_test, minus) {
81-
auto n = uint128_fallback(42);
81+
auto n = uint128(42);
8282
EXPECT_EQ(n - 2, 40);
8383
}
8484

8585
TEST(uint128_test, plus_assign) {
86-
auto n = uint128_fallback(32);
87-
n += uint128_fallback(10);
86+
auto n = uint128(32);
87+
n += uint128(10);
8888
EXPECT_EQ(n, 42);
89-
n = uint128_fallback(max_value<uint64_t>());
90-
n += uint128_fallback(1);
91-
EXPECT_EQ(n, uint128_fallback(1) << 64);
89+
n = uint128(max_value<uint64_t>());
90+
n += uint128(1);
91+
EXPECT_EQ(n, uint128(1) << 64);
9292
}
9393

9494
TEST(uint128_test, multiply) {
95-
auto n = uint128_fallback(2251799813685247);
95+
auto n = uint128(2251799813685247);
9696
n = n * 3611864890;
9797
EXPECT_EQ(static_cast<uint64_t>(n >> 64), 440901);
9898
}
@@ -1723,14 +1723,13 @@ TEST(format_test, format_pointer) {
17231723
}
17241724

17251725
TEST(format_test, write_uintptr_fallback) {
1726-
// Test that formatting a pointer by converting it to uint128_fallback works.
1726+
// Test that formatting a pointer by converting it to uint128 works.
17271727
// This is needed to support systems without uintptr_t.
17281728
auto s = std::string();
1729-
fmt::detail::write_ptr<char>(
1730-
std::back_inserter(s),
1731-
fmt::detail::bit_cast<fmt::detail::uint128_fallback>(
1732-
reinterpret_cast<void*>(0xface)),
1733-
nullptr);
1729+
fmt::detail::write_ptr<char>(std::back_inserter(s),
1730+
fmt::detail::bit_cast<fmt::detail::uint128>(
1731+
reinterpret_cast<void*>(0xface)),
1732+
nullptr);
17341733
EXPECT_EQ(s, "0xface");
17351734
}
17361735

0 commit comments

Comments
 (0)