-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Use division by 100 in to_string for integers
#5691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 29 commits into
microsoft:main
from
AlexGuteniev:integers
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
86958ab
benchmark
AlexGuteniev 8b6f863
100 branch in to_string impl
AlexGuteniev 84b3d1a
sort better
AlexGuteniev 08b83da
hack around some linker issue in C++14
AlexGuteniev 068234b
Merge branch 'microsoft:main' into integers
AlexGuteniev 5de9ae1
Merge branch 'microsoft:main' into integers
AlexGuteniev 74815fc
Merge branch 'microsoft:main' into integers
AlexGuteniev 986ba2a
Merge branch 'microsoft:main' into integers
AlexGuteniev 6966faf
Merge branch 'main' into integers
AlexGuteniev 1ffa338
unshare table
AlexGuteniev f422b45
unrevert merge
AlexGuteniev b39a620
format
AlexGuteniev 77d37ff
eliminate the tail loop, we need at most one iteration
AlexGuteniev c1efce4
size
AlexGuteniev ed71c09
Generate table
AlexGuteniev 9f3e9bc
Let's consistently change 32-bit path
AlexGuteniev ad8e94f
Merge branch 'microsoft:main' into integers
AlexGuteniev edc2042
Use a multi-dim array with a constructor.
StephanTLavavej 3708bbf
Transform control flow to be simpler. No additional branches.
StephanTLavavej 6c29273
Use mt19937_64.
StephanTLavavej 333e3f6
Use 20 chars, comment why.
StephanTLavavej 395c8f6
Fix major bug: `_UIntegral_to_buff` takes the END of the buffer.
StephanTLavavej 65138c1
Adjust header inclusions.
StephanTLavavej 33fef86
Benchmark wchar_t.
StephanTLavavej 4b706e5
Add additional correctness tests for every length.
StephanTLavavej 5bf3370
For x86, print 8 digits at a time.
StephanTLavavej 141449f
Refine x86 optimization to avoid final division.
StephanTLavavej 77ab0a7
You want me to give him the CLAMPS, boss?
StephanTLavavej 74064c9
Clamp a while. Clamp forever!
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <benchmark/benchmark.h> | ||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <random> | ||
| #include <string> | ||
| #include <type_traits> | ||
|
|
||
| using namespace std; | ||
|
|
||
| template <class T, double M, double S> | ||
| auto generate_array() { | ||
| array<T, 2000> a; | ||
|
|
||
| mt19937_64 gen; | ||
| lognormal_distribution<double> dis(M, S); | ||
| auto get_clamped_value = [&] { | ||
| for (;;) { | ||
| const double dbl = floor(dis(gen)); | ||
| constexpr auto max_val = static_cast<double>(numeric_limits<T>::max()); | ||
| if (dbl <= max_val) { | ||
| return static_cast<T>(dbl); | ||
| } | ||
| } | ||
| }; | ||
| ranges::generate(a, get_clamped_value); | ||
|
|
||
| if constexpr (is_signed_v<T>) { | ||
| bernoulli_distribution b(0.5); | ||
| ranges::for_each(a, [&](T& v) { v *= (b(gen) ? -1 : 1); }); | ||
| } | ||
|
|
||
| return a; | ||
| } | ||
|
|
||
| template <class CharT, class T, double M, double S> | ||
| void internal_integer_to_buff(benchmark::State& state) { | ||
| auto a = generate_array<T, M, S>(); | ||
|
|
||
| CharT buff[20]; // can hold -2^63 and 2^64 - 1 | ||
| auto buff_end = end(buff); | ||
|
|
||
| auto it = a.begin(); | ||
| for (auto _ : state) { | ||
| auto i = *it; | ||
| benchmark::DoNotOptimize(i); | ||
| auto s = std::_UIntegral_to_buff(buff_end, i); | ||
| benchmark::DoNotOptimize(s); | ||
|
|
||
| ++it; | ||
| if (it == a.end()) { | ||
| it = a.begin(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <class T, double M, double S> | ||
| void integer_to_string(benchmark::State& state) { | ||
| auto a = generate_array<T, M, S>(); | ||
|
|
||
| auto it = a.begin(); | ||
| for (auto _ : state) { | ||
| auto i = *it; | ||
| benchmark::DoNotOptimize(i); | ||
| auto s = to_string(i); | ||
|
StephanTLavavej marked this conversation as resolved.
|
||
| benchmark::DoNotOptimize(s); | ||
|
|
||
| ++it; | ||
| if (it == a.end()) { | ||
| it = a.begin(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(internal_integer_to_buff<char, uint8_t, 2.5, 1.5>); | ||
| BENCHMARK(internal_integer_to_buff<char, uint16_t, 5.0, 3.0>); | ||
| BENCHMARK(internal_integer_to_buff<char, uint32_t, 10.0, 6.0>); | ||
| BENCHMARK(internal_integer_to_buff<char, uint64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK(internal_integer_to_buff<wchar_t, uint8_t, 2.5, 1.5>); | ||
| BENCHMARK(internal_integer_to_buff<wchar_t, uint16_t, 5.0, 3.0>); | ||
| BENCHMARK(internal_integer_to_buff<wchar_t, uint32_t, 10.0, 6.0>); | ||
| BENCHMARK(internal_integer_to_buff<wchar_t, uint64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK(integer_to_string<uint8_t, 2.5, 1.5>); | ||
| BENCHMARK(integer_to_string<uint16_t, 5.0, 3.0>); | ||
| BENCHMARK(integer_to_string<uint32_t, 10.0, 6.0>); | ||
| BENCHMARK(integer_to_string<uint64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK(integer_to_string<int8_t, 2.5, 1.5>); | ||
| BENCHMARK(integer_to_string<int16_t, 5.0, 3.0>); | ||
| BENCHMARK(integer_to_string<int32_t, 10.0, 6.0>); | ||
| BENCHMARK(integer_to_string<int64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK_MAIN(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As pointed out by statementreply on Discord, for$2^{64}$ , so to be rigorous you would need to either special-case that or make the test
uint_64this rounds todbl < max_val.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I can fix this in a followup.