Skip to content

Commit 06b2038

Browse files
committed
Optimize fractional_part_rounding_thresholds
1 parent 649f246 commit 06b2038

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

include/fmt/format.h

+6-12
Original file line numberDiff line numberDiff line change
@@ -3259,21 +3259,15 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
32593259
format_hexfloat(static_cast<double>(value), precision, specs, buf);
32603260
}
32613261

3262-
FMT_CONSTEXPR inline uint32_t fractional_part_rounding_thresholds(int index) {
3262+
constexpr uint32_t fractional_part_rounding_thresholds(int index) {
32633263
// For checking rounding thresholds.
32643264
// The kth entry is chosen to be the smallest integer such that the
32653265
// upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
3266-
constexpr uint32_t thresholds[8] = {
3267-
2576980378U, // ceil(2^31 + 2^32/10^1)
3268-
2190433321U, // ceil(2^31 + 2^32/10^2)
3269-
2151778616U, // ceil(2^31 + 2^32/10^3)
3270-
2147913145U, // ceil(2^31 + 2^32/10^4)
3271-
2147526598U, // ceil(2^31 + 2^32/10^5)
3272-
2147487943U, // ceil(2^31 + 2^32/10^6)
3273-
2147484078U, // ceil(2^31 + 2^32/10^7)
3274-
2147483691U // ceil(2^31 + 2^32/10^8)
3275-
};
3276-
return thresholds[index];
3266+
// It is equal to ceil(2^31 + 2^32/10^(k + 1)).
3267+
// These are stored in a string literal because we cannot have static arrays
3268+
// in constexpr functions and non-static ones are poorly optimized.
3269+
return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7"
3270+
U"\x800001ae\x8000002b"[index];
32773271
}
32783272

32793273
template <typename Float>

0 commit comments

Comments
 (0)