Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Minor speedup.
Before (using https://github.com/fmtlib/format-benchmark):
After:
The idea is to avoid the modulo (which gets compiled to a
imuland asub) by looking at the 7 bits after the decimal point ofvalue / 100.This adds 256+1 bytes worth of lookup table (the old lookup table still need to stay there, unfortunately). Although technically the null terminator and the last 2 spaces are unused.
Correctness is shown by exhaustively search through the whole range of 32-bit integers and ensure that for all
i,(i * ((1ull<<39)/100+1)) >> (39 - 7) & ((1<<7) - 1)uniquely determines the value ofi % 100and((i * ((1ull<<39)/100+1)) >> 39) + ((i>=(100u<<25))<<25)is exactly equal toi / 100.The check
sizeof(UInt) == 4implicitly assumesCHAR_BIT == 8(is it worth being spelled out?)Source code of brute force checker
Future work:
write_significand__int128).note:
digits2_iis not constexpr (before C++20)write2digits_iis not constexpr either, so there's no need for thestd::is_constant_evaluatedmemcpyifFMT_OPTIMIZE_SIZEis true, butwrite2digitsdo that.hofman_funwill always be slower.