Skip to content

Commit

Permalink
Workaround a bug in gcc10 (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 10, 2020
1 parent 16985fd commit 6fb7c6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
7 changes: 0 additions & 7 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,6 @@ const typename basic_data<T>::digit_pair basic_data<T>::digits[] = {
template <typename T>
const char basic_data<T>::hex_digits[] = "0123456789abcdef";

template <typename T>
const uint16_t basic_data<T>::bsr2log10[] = {
1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};

#define FMT_POWERS_OF_10(factor) \
factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
(factor)*1000000, (factor)*10000000, (factor)*100000000, \
Expand Down
17 changes: 13 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,6 @@ using uint32_or_64_or_128_t =

// Static data is placed in this class template for the header-only config.
template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
// Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
static const uint16_t bsr2log10[];
static const uint64_t powers_of_10_64[];
static const uint32_t zero_or_powers_of_10_32[];
static const uint64_t zero_or_powers_of_10_64[];
Expand All @@ -797,6 +795,17 @@ template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
static const char right_padding_shifts[5];
};

// Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
// This is a function instead of an array to workaround a bug in GCC10 (#1810).
FMT_INLINE uint16_t bsr2log10(int bsr) {
constexpr uint16_t data[] = {
1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
return data[bsr];
}

#ifndef FMT_EXPORTED
FMT_EXTERN template struct basic_data<void>;
#endif
Expand All @@ -809,7 +818,7 @@ struct data : basic_data<> {};
// except for n == 0 in which case count_digits returns 1.
inline int count_digits(uint64_t n) {
// https://github.com/fmtlib/format-benchmark/blob/master/digits10
auto t = data::bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63];
auto t = bsr2log10(FMT_BUILTIN_CLZLL(n | 1) ^ 63);
return t - (n < data::zero_or_powers_of_10_64[t]);
}
#else
Expand Down Expand Up @@ -867,7 +876,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n);
#ifdef FMT_BUILTIN_CLZ
// Optional version of count_digits for better performance on 32-bit platforms.
inline int count_digits(uint32_t n) {
auto t = data::bsr2log10[FMT_BUILTIN_CLZ(n | 1) ^ 31];
auto t = bsr2log10(FMT_BUILTIN_CLZ(n | 1) ^ 31);
return t - (n < data::zero_or_powers_of_10_32[t]);
}
#endif
Expand Down

0 comments on commit 6fb7c6f

Please sign in to comment.