apply requested changes, apply code style

This commit is contained in:
Alexey Ochapov 2020-11-26 22:09:23 +03:00
parent 706555bf3c
commit c9a0032a75
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
3 changed files with 29 additions and 57 deletions

View File

@ -98,15 +98,10 @@
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
# define FMT_CONSTEXPR20 constexpr # define FMT_CONSTEXPR20 constexpr
# define FMT_HAS_IS_CONSTANT_EVALUATED 1
#else #else
# define FMT_CONSTEXPR20 # define FMT_CONSTEXPR20
#endif #endif
#ifndef FMT_HAS_IS_CONSTANT_EVALUATED
# define FMT_HAS_IS_CONSTANT_EVALUATED 0
#endif
#ifndef FMT_OVERRIDE #ifndef FMT_OVERRIDE
# if FMT_HAS_FEATURE(cxx_override_control) || \ # if FMT_HAS_FEATURE(cxx_override_control) || \
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
@ -291,7 +286,7 @@ struct monostate {};
namespace detail { namespace detail {
constexpr bool is_constant_evaluated() FMT_DETECTED_NOEXCEPT { constexpr bool is_constant_evaluated() FMT_DETECTED_NOEXCEPT {
#if FMT_HAS_IS_CONSTANT_EVALUATED #ifdef __cpp_lib_is_constant_evaluated
return std::is_constant_evaluated(); return std::is_constant_evaluated();
#else #else
return false; return false;

View File

@ -228,9 +228,6 @@ template <> FMT_FUNC int count_digits<4>(detail::fallback_uintptr n) {
return i >= 0 ? i * char_digits + count_digits<4, unsigned>(n.value[i]) : 1; return i >= 0 ? i * char_digits + count_digits<4, unsigned>(n.value[i]) : 1;
} }
template <typename T>
const data_digit_pair basic_data<T>::digits[] = FMT_DATA_DIGITS;
template <typename T> template <typename T>
const char basic_data<T>::hex_digits[] = "0123456789abcdef"; const char basic_data<T>::hex_digits[] = "0123456789abcdef";

View File

@ -278,39 +278,31 @@ FMT_END_NAMESPACE
# define FMT_DEPRECATED_NUMERIC_ALIGN 0 # define FMT_DEPRECATED_NUMERIC_ALIGN 0
#endif #endif
#define FMT_DATA_DIGITS \
{ \
{'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'}, \
{'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, \
{'1', '1'}, {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, \
{'1', '6'}, {'1', '7'}, {'1', '8'}, {'1', '9'}, {'2', '0'}, \
{'2', '1'}, {'2', '2'}, {'2', '3'}, {'2', '4'}, {'2', '5'}, \
{'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'}, {'3', '0'}, \
{'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'}, \
{'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, \
{'4', '1'}, {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, \
{'4', '6'}, {'4', '7'}, {'4', '8'}, {'4', '9'}, {'5', '0'}, \
{'5', '1'}, {'5', '2'}, {'5', '3'}, {'5', '4'}, {'5', '5'}, \
{'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'}, {'6', '0'}, \
{'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'}, \
{'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, \
{'7', '1'}, {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, \
{'7', '6'}, {'7', '7'}, {'7', '8'}, {'7', '9'}, {'8', '0'}, \
{'8', '1'}, {'8', '2'}, {'8', '3'}, {'8', '4'}, {'8', '5'}, \
{'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'}, {'9', '0'}, \
{'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'}, \
{'9', '6'}, {'9', '7'}, {'9', '8'}, { \
'9', '9' \
} \
}
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
// GCC generates slightly better code for pairs than chars.
using data_digit_pair = char[2]; using data_digit_pair = char[2];
namespace compile_time_formatting_data { namespace formatting_data {
constexpr data_digit_pair digits[] = FMT_DATA_DIGITS; constexpr data_digit_pair digits[] = {
{'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
{'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
{'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
{'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
{'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
{'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
{'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
{'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
{'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
{'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
{'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
{'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
{'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
{'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
{'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
{'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
{'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
} }
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't have // An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't have
@ -945,9 +937,6 @@ template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
static const uint64_t powers_of_5_64[]; static const uint64_t powers_of_5_64[];
static const uint32_t dragonbox_pow10_recovery_errors[]; static const uint32_t dragonbox_pow10_recovery_errors[];
#endif #endif
// GCC generates slightly better code for pairs than chars.
using digit_pair = char[2];
static const digit_pair digits[];
static const char hex_digits[]; static const char hex_digits[];
static const char foreground_color[]; static const char foreground_color[];
static const char background_color[]; static const char background_color[];
@ -995,7 +984,7 @@ template <typename T> FMT_CONSTEXPR int count_digits_trivial(T n) {
} }
} }
#if defined(FMT_BUILTIN_CLZLL) #ifdef FMT_BUILTIN_CLZLL
// Returns the number of decimal digits in n. Leading zeros are not counted // Returns the number of decimal digits in n. Leading zeros are not counted
// except for n == 0 in which case count_digits returns 1. // except for n == 0 in which case count_digits returns 1.
inline FMT_CONSTEXPR20 int count_digits(uint64_t n) { inline FMT_CONSTEXPR20 int count_digits(uint64_t n) {
@ -1008,9 +997,7 @@ inline FMT_CONSTEXPR20 int count_digits(uint64_t n) {
} }
#else #else
// Fallback version of count_digits used when __builtin_clz is not available. // Fallback version of count_digits used when __builtin_clz is not available.
FMT_CONSTEXPR int count_digits(uint64_t n) { FMT_CONSTEXPR int count_digits(uint64_t n) { return count_digits_trivial(n); }
return count_digits_trivial(n);
}
#endif #endif
#if FMT_USE_INT128 #if FMT_USE_INT128
@ -1031,8 +1018,7 @@ FMT_CONSTEXPR int count_digits(uint128_t n) {
#endif #endif
// Counts the number of digits in n. BITS = log2(radix). // Counts the number of digits in n. BITS = log2(radix).
template <unsigned BITS, typename UInt> template <unsigned BITS, typename UInt> FMT_CONSTEXPR int count_digits(UInt n) {
FMT_CONSTEXPR int count_digits(UInt n) {
int num_digits = 0; int num_digits = 0;
do { do {
++num_digits; ++num_digits;
@ -1050,7 +1036,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n);
# define FMT_ALWAYS_INLINE inline # define FMT_ALWAYS_INLINE inline
#endif #endif
#if defined(FMT_BUILTIN_CLZ) #ifdef FMT_BUILTIN_CLZ
// Optional version of count_digits for better performance on 32-bit platforms. // Optional version of count_digits for better performance on 32-bit platforms.
inline FMT_CONSTEXPR20 int count_digits(uint32_t n) { inline FMT_CONSTEXPR20 int count_digits(uint32_t n) {
if (is_constant_evaluated()) { if (is_constant_evaluated()) {
@ -1129,12 +1115,6 @@ inline FMT_CONSTEXPR20 format_decimal_result<Char*> format_decimal(Char* out,
UInt value, UInt value,
int size) { int size) {
FMT_ASSERT(size >= count_digits(value), "invalid digit count"); FMT_ASSERT(size >= count_digits(value), "invalid digit count");
const data_digit_pair* digits;
if (is_constant_evaluated()) {
digits = compile_time_formatting_data::digits;
} else {
digits = data::digits;
}
out += size; out += size;
Char* end = out; Char* end = out;
while (value >= 100) { while (value >= 100) {
@ -1142,7 +1122,7 @@ inline FMT_CONSTEXPR20 format_decimal_result<Char*> format_decimal(Char* out,
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
out -= 2; out -= 2;
copy2(out, digits[value % 100]); copy2(out, formatting_data::digits[value % 100]);
value /= 100; value /= 100;
} }
if (value < 10) { if (value < 10) {
@ -1150,7 +1130,7 @@ inline FMT_CONSTEXPR20 format_decimal_result<Char*> format_decimal(Char* out,
return {out, end}; return {out, end};
} }
out -= 2; out -= 2;
copy2(out, digits[value]); copy2(out, formatting_data::digits[value]);
return {out, end}; return {out, end};
} }
@ -1399,12 +1379,12 @@ template <typename Char, typename It> It write_exponent(int exp, It it) {
*it++ = static_cast<Char>('+'); *it++ = static_cast<Char>('+');
} }
if (exp >= 100) { if (exp >= 100) {
const char* top = data::digits[exp / 100]; const char* top = formatting_data::digits[exp / 100];
if (exp >= 1000) *it++ = static_cast<Char>(top[0]); if (exp >= 1000) *it++ = static_cast<Char>(top[0]);
*it++ = static_cast<Char>(top[1]); *it++ = static_cast<Char>(top[1]);
exp %= 100; exp %= 100;
} }
const char* d = data::digits[exp]; const char* d = formatting_data::digits[exp];
*it++ = static_cast<Char>(d[0]); *it++ = static_cast<Char>(d[0]);
*it++ = static_cast<Char>(d[1]); *it++ = static_cast<Char>(d[1]);
return it; return it;