← Back to Pull requests

Divide by the largest power of the base in to_string() #390

Closed
unknown closed this pull request without merging on Oct 13, 2026 13:01 UTC
View on GitHub
unknown unknown commented on Oct 12, 2026 22:15 UTC
Author

Convert numbers to strings by dividing them by the largest power of the base fitting in a word (10^19 for decimal, 16^15 for hex), which produces a chunk of digits per division, instead of a full udivrem() per digit. The division stops when the rest fits in a word: its digits, and a number fitting in a word from the start, are converted directly with 64-bit arithmetic, without computing the power. The digits are written from the end of a buffer on the stack, so the string is constructed once (no push_back() and reverse()).

The change is local to to_string() and uses the existing udivrem(). Faster conversions for the bases 10 and 16 (compile-time constants and a precomputed reciprocal) follow in separate PRs.

Tests: zero, which had no test, and numbers of multiple chunks of digits in the bases 2 and 16, including chunks of zeros, for every type; exact strings for uint256 (2^64, 10^19, 10^38 + 1 and the max value in decimal, the max value in the base 36). The output is also identical to main for all bases 2..36 over 389,592 conversions (powers of 2 and their neighbors, 2^64 ± 1, 10^19 ± 1 and random values of uint128, uint256 and uint512).

Instructions per conversion

Average dynamic instruction counts per call for 2048 random numbers of 193 to 255 bits (4 words), below 2^63 (1 word) or from 1 to 15 (4 bits), clang 23 and GCC 16 at -O2, -O3 and -O2 -march=x86-64-v3, compared with main:

| case | main | this PR | change |
|---|---|---|---|
| decimal, 4 words | 15.7 … 17.3K | 1.44 … 1.59K | −90 … −92% |
| hex, 4 words | 12.9 … 14.2K | 1.40 … 1.51K | −89 … −90% |
| decimal, 1 word | 1.94 … 2.22K | 209 … 250 | −87 … −91% |
| base 36, 4 words | 10.1 … 11.1K | 1.27 … 1.35K | −87 … −88% |
| base 2, 4 words | 51.0 … 56.3K | 2.85 … 3.33K | −94 … −95% |
| decimal, 4 bits | 216 … 292 | 102 … 109 | −52 … −64% |
| hex, 4 bits | 134 … 201 | 88 … 95 | −34 … −55% |
| base 2, 4 bits | 585 … 704 | 130 … 139 | −76 … −81% |

Cycles

The same cases, perf stat cycles per call, median of 9 interleaved rounds pinned to one core of a Ryzen 9 5950X:

| case | main | this PR | change |
|---|---|---|---|
| decimal, 4 words | 6.5 … 9.0K | 925 … 1085 | −85 … −89% |
| hex, 4 words | 5.3 … 7.2K | 784 … 877 | −85 … −88% |
| decimal, 1 word | 787 … 1066 | 99 … 122 | −85 … −90% |
| base 36, 4 words | 4.2 … 7.3K | 666 … 982 | −84 … −88% |
| base 2, 4 words | 20.9 … 27.3K | 2.90 … 3.09K | −85 … −89% |
| decimal, 4 bits | 83 … 112 | 29 … 32 | −62 … −74% |
| hex, 4 bits | 42 … 77 | 26 … 29 | −43 … −67% |
| base 2, 4 bits | 243 … 310 | 35 … 45 | −84 … −88% |

With GCC the multi-word timings also depend on the stack position: over 64 positions, the decimal 4-word conversion of this PR takes 1028 … 1445 cycles at -O2 (clang 897 … 1314).

Code size

A TU with to_string() (the default and a run-time base) and hex() for uint256: clang −108 … +155 bytes, GCC −0.6 … −3.1 KB. With GCC, to_string() shrinks from 1.6 to 1.0 KB (no push_back() growth path and no reverse()), and at -O3 it is no longer inlined into each of the three callers. udivrem() is called out of line before and after.

Checks

The changed lines have 100% coverage with GCC -fcondition-coverage (also in the gcovr 7.2 report, as in the CI) and clang -fcoverage-mcdc for every type. clang-tidy 20 with the repository configuration, the unit tests with the sanitizers of the CI (address, undefined, implicit-conversion, nullability), and MSVC 19.44 and latest with /W4 /WX (with and without /O2) are clean.

🤖 Generated with Claude Code

Diff: 450 lines (+337 / -113) across 5 files Reviewer: chfast Review duration: 885m
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 450 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.366
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #390"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.4% (MEDIUM) · Higher = larger blast radius
45.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 17.9% · Safe to merge
17.9%
Repository