From 1882b9687ba650f50f8d90f0dc7a0b9e30bd4066 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 5 Sep 2019 17:43:40 -0700 Subject: [PATCH] Reduce the numer of ifdefs with an empty (u)int128_t fallback --- include/fmt/core.h | 50 +++++++++++++++++++++++--------------------- include/fmt/format.h | 37 +++++++++----------------------- test/format-test.cc | 24 ++++++++++----------- 3 files changed, 48 insertions(+), 63 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index f1200681..b86e8713 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -195,14 +195,6 @@ # define FMT_USE_EXPERIMENTAL_STRING_VIEW #endif -#ifdef FMT_USE_INT128 -// Do nothing. -#elif defined(__SIZEOF_INT128__) -# define FMT_USE_INT128 1 -#else -# define FMT_USE_INT128 0 -#endif - FMT_BEGIN_NAMESPACE // Implementations of enable_if_t and other types for pre-C++14 systems. @@ -239,6 +231,20 @@ using std_string_view = std::experimental::basic_string_view; template struct std_string_view {}; #endif +#ifdef FMT_USE_INT128 +// Do nothing. +#elif defined(__SIZEOF_INT128__) +# define FMT_USE_INT128 1 +using int128_t = __int128_t; +using uint128_t = __uint128_t; +#else +# define FMT_USE_INT128 0 +#endif +#if !FMT_USE_INT128 +struct int128_t {}; +struct uint128_t {}; +#endif + // Casts nonnegative integer to unsigned. template FMT_CONSTEXPR typename std::make_unsigned::type to_unsigned(Int value) { @@ -673,10 +679,8 @@ FMT_TYPE_CONSTANT(int, int_type); FMT_TYPE_CONSTANT(unsigned, uint_type); FMT_TYPE_CONSTANT(long long, long_long_type); FMT_TYPE_CONSTANT(unsigned long long, ulong_long_type); -#if FMT_USE_INT128 -FMT_TYPE_CONSTANT(__int128_t, int128_type); -FMT_TYPE_CONSTANT(__uint128_t, uint128_type); -#endif +FMT_TYPE_CONSTANT(int128_t, int128_type); +FMT_TYPE_CONSTANT(uint128_t, uint128_type); FMT_TYPE_CONSTANT(bool, bool_type); FMT_TYPE_CONSTANT(Char, char_type); FMT_TYPE_CONSTANT(double, double_type); @@ -716,10 +720,8 @@ template class value { unsigned uint_value; long long long_long_value; unsigned long long ulong_long_value; -#if FMT_USE_INT128 - __int128_t int128_value; - __uint128_t uint128_value; -#endif + int128_t int128_value; + uint128_t uint128_value; bool bool_value; char_type char_value; double double_value; @@ -734,10 +736,8 @@ template class value { FMT_CONSTEXPR value(unsigned val) : uint_value(val) {} value(long long val) : long_long_value(val) {} value(unsigned long long val) : ulong_long_value(val) {} -#if FMT_USE_INT128 - value(__int128_t val) : int128_value(val) {} - value(__uint128_t val) : uint128_value(val) {} -#endif + value(int128_t val) : int128_value(val) {} + value(uint128_t val) : uint128_value(val) {} value(double val) : double_value(val) {} value(long double val) : long_double_value(val) {} value(bool val) : bool_value(val) {} @@ -797,10 +797,8 @@ template struct arg_mapper { FMT_CONSTEXPR ulong_type map(unsigned long val) { return val; } FMT_CONSTEXPR long long map(long long val) { return val; } FMT_CONSTEXPR unsigned long long map(unsigned long long val) { return val; } -#if FMT_USE_INT128 - FMT_CONSTEXPR __int128_t map(__int128_t val) { return val; } - FMT_CONSTEXPR __uint128_t map(__uint128_t val) { return val; } -#endif + FMT_CONSTEXPR int128_t map(int128_t val) { return val; } + FMT_CONSTEXPR uint128_t map(uint128_t val) { return val; } FMT_CONSTEXPR bool map(bool val) { return val; } template ::value)> @@ -966,6 +964,10 @@ FMT_CONSTEXPR auto visit_format_arg(Visitor&& vis, return vis(arg.value_.int128_value); case internal::uint128_type: return vis(arg.value_.uint128_value); +#else + case internal::int128_type: + case internal::uint128_type: + break; #endif case internal::bool_type: return vis(arg.value_.bool_value); diff --git a/include/fmt/format.h b/include/fmt/format.h index b8309a03..dc0f6d99 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -636,20 +636,12 @@ FMT_CONSTEXPR bool is_negative(T) { return false; } -#if FMT_USE_INT128 -// Smallest of uint32_t, uint64_t, unsigned __int128 that is large enough to +// Smallest of uint32_t, uint64_t, uint128_t that is large enough to // represent all values of T. template using uint32_or_64_or_128_t = conditional_t< std::numeric_limits::digits <= 32, uint32_t, - conditional_t::digits <= 64, uint64_t, __uint128_t>>; -#else -// Smallest of uint32_t and uint64_t that is large enough to represent all -// values of T. -template -using uint32_or_64_or_128_t = - conditional_t::digits <= 32, uint32_t, uint64_t>; -#endif + conditional_t::digits <= 64, uint64_t, uint128_t>>; // Static data is placed in this class template for the header-only config. template struct FMT_EXTERN_TEMPLATE_API basic_data { @@ -699,7 +691,7 @@ inline int count_digits(uint64_t n) { #endif #if FMT_USE_INT128 -inline int count_digits(__uint128_t n) { +inline int count_digits(uint128_t n) { int count = 1; for (;;) { // Integer division is slow so do it for a group of four digits instead @@ -847,12 +839,8 @@ inline Char* format_decimal(Char* buffer, UInt value, int num_digits, template constexpr int digits10() noexcept { return std::numeric_limits::digits10; } - -#if FMT_USE_INT128 -template <> constexpr int digits10<__int128_t>() noexcept { return 38; } - -template <> constexpr int digits10<__uint128_t>() noexcept { return 38; } -#endif +template <> constexpr int digits10() noexcept { return 38; } +template <> constexpr int digits10() noexcept { return 38; } template inline Iterator format_decimal(Iterator out, UInt value, int num_digits, @@ -1644,15 +1632,14 @@ template class basic_writer { void write(int value) { write_decimal(value); } void write(long value) { write_decimal(value); } void write(long long value) { write_decimal(value); } -#if FMT_USE_INT128 - void write(__int128_t value) { write_decimal(value); } -#endif void write(unsigned value) { write_decimal(value); } void write(unsigned long value) { write_decimal(value); } void write(unsigned long long value) { write_decimal(value); } + #if FMT_USE_INT128 - void write(__uint128_t value) { write_decimal(value); } + void write(int128_t value) { write_decimal(value); } + void write(uint128_t value) { write_decimal(value); } #endif // Writes a formatted integer. @@ -1737,12 +1724,8 @@ template class basic_writer { using writer = basic_writer>; template struct is_integral : std::is_integral {}; - -#if FMT_USE_INT128 -template <> struct is_integral<__int128_t> : std::true_type {}; - -template <> struct is_integral<__uint128_t> : std::true_type {}; -#endif +template <> struct is_integral : std::true_type {}; +template <> struct is_integral : std::true_type {}; template class arg_formatter_base { diff --git a/test/format-test.cc b/test/format-test.cc index 235eccab..1577137e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1334,11 +1334,11 @@ TEST(FormatterTest, FormatBin) { } #if FMT_USE_INT128 -constexpr auto INT128_MAX = static_cast<__int128_t>( +constexpr auto int128_max = static_cast<__int128_t>( (static_cast<__uint128_t>(1) << ((__SIZEOF_INT128__ * CHAR_BIT) - 1)) - 1); -constexpr auto INT128_MIN = -INT128_MAX - 1; +constexpr auto int128_min = -int128_max - 1; -constexpr auto UINT128_MAX = ~static_cast<__uint128_t>(0); +constexpr auto uint128_max = ~static_cast<__uint128_t>(0); #endif TEST(FormatterTest, FormatDec) { @@ -1359,11 +1359,11 @@ TEST(FormatterTest, FormatDec) { EXPECT_EQ("18446744073709551616", format("{0}", static_cast<__int128_t>(UINT64_MAX) + 1)); EXPECT_EQ("170141183460469231731687303715884105727", - format("{0}", INT128_MAX)); + format("{0}", int128_max)); EXPECT_EQ("-170141183460469231731687303715884105728", - format("{0}", INT128_MIN)); + format("{0}", int128_min)); EXPECT_EQ("340282366920938463463374607431768211455", - format("{0}", UINT128_MAX)); + format("{0}", uint128_max)); #endif char buffer[BUFFER_SIZE]; @@ -1399,9 +1399,9 @@ TEST(FormatterTest, FormatHex) { format("{0:x}", static_cast<__int128_t>(INT64_MIN) - 1)); EXPECT_EQ("10000000000000000", format("{0:x}", static_cast<__int128_t>(UINT64_MAX) + 1)); - EXPECT_EQ("7fffffffffffffffffffffffffffffff", format("{0:x}", INT128_MAX)); - EXPECT_EQ("-80000000000000000000000000000000", format("{0:x}", INT128_MIN)); - EXPECT_EQ("ffffffffffffffffffffffffffffffff", format("{0:x}", UINT128_MAX)); + EXPECT_EQ("7fffffffffffffffffffffffffffffff", format("{0:x}", int128_max)); + EXPECT_EQ("-80000000000000000000000000000000", format("{0:x}", int128_min)); + EXPECT_EQ("ffffffffffffffffffffffffffffffff", format("{0:x}", uint128_max)); #endif char buffer[BUFFER_SIZE]; @@ -1435,11 +1435,11 @@ TEST(FormatterTest, FormatOct) { EXPECT_EQ("2000000000000000000000", format("{0:o}", static_cast<__int128_t>(UINT64_MAX) + 1)); EXPECT_EQ("1777777777777777777777777777777777777777777", - format("{0:o}", INT128_MAX)); + format("{0:o}", int128_max)); EXPECT_EQ("-2000000000000000000000000000000000000000000", - format("{0:o}", INT128_MIN)); + format("{0:o}", int128_min)); EXPECT_EQ("3777777777777777777777777777777777777777777", - format("{0:o}", UINT128_MAX)); + format("{0:o}", uint128_max)); #endif char buffer[BUFFER_SIZE];