mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-16 14:42:25 +00:00
Optimize format_decimal
This commit is contained in:
parent
ecdc7ec1f1
commit
58fcf0c0da
13
format.h
13
format.h
@ -712,24 +712,23 @@ inline unsigned count_digits(uint32_t n) {
|
|||||||
// Formats a decimal unsigned integer value writing into buffer.
|
// Formats a decimal unsigned integer value writing into buffer.
|
||||||
template <typename UInt, typename Char>
|
template <typename UInt, typename Char>
|
||||||
inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
|
inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
|
||||||
--num_digits;
|
buffer += num_digits;
|
||||||
while (value >= 100) {
|
while (value >= 100) {
|
||||||
// Integer division is slow so do it for a group of two digits instead
|
// Integer division is slow so do it for a group of two digits instead
|
||||||
// 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.
|
||||||
unsigned index = (value % 100) * 2;
|
unsigned index = (value % 100) * 2;
|
||||||
value /= 100;
|
value /= 100;
|
||||||
buffer[num_digits] = Data::DIGITS[index + 1];
|
*--buffer = Data::DIGITS[index + 1];
|
||||||
buffer[num_digits - 1] = Data::DIGITS[index];
|
*--buffer = Data::DIGITS[index];
|
||||||
num_digits -= 2;
|
|
||||||
}
|
}
|
||||||
if (value < 10) {
|
if (value < 10) {
|
||||||
*buffer = static_cast<char>('0' + value);
|
*--buffer = static_cast<char>('0' + value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
unsigned index = static_cast<unsigned>(value * 2);
|
unsigned index = static_cast<unsigned>(value * 2);
|
||||||
buffer[1] = Data::DIGITS[index + 1];
|
*--buffer = Data::DIGITS[index + 1];
|
||||||
buffer[0] = Data::DIGITS[index];
|
*--buffer = Data::DIGITS[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
Loading…
x
Reference in New Issue
Block a user