mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-02 11:28:20 +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.
|
||||
template <typename UInt, typename Char>
|
||||
inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
|
||||
--num_digits;
|
||||
buffer += num_digits;
|
||||
while (value >= 100) {
|
||||
// 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
|
||||
// "Three Optimization Tips for C++". See speed-test for a comparison.
|
||||
unsigned index = (value % 100) * 2;
|
||||
value /= 100;
|
||||
buffer[num_digits] = Data::DIGITS[index + 1];
|
||||
buffer[num_digits - 1] = Data::DIGITS[index];
|
||||
num_digits -= 2;
|
||||
*--buffer = Data::DIGITS[index + 1];
|
||||
*--buffer = Data::DIGITS[index];
|
||||
}
|
||||
if (value < 10) {
|
||||
*buffer = static_cast<char>('0' + value);
|
||||
*--buffer = static_cast<char>('0' + value);
|
||||
return;
|
||||
}
|
||||
unsigned index = static_cast<unsigned>(value * 2);
|
||||
buffer[1] = Data::DIGITS[index + 1];
|
||||
buffer[0] = Data::DIGITS[index];
|
||||
*--buffer = Data::DIGITS[index + 1];
|
||||
*--buffer = Data::DIGITS[index];
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
|
Loading…
Reference in New Issue
Block a user