Repository navigation
Speed up BigInteger conversions to floating-point types - #130721
Merged
tannergooding merged 4 commits intoJul 16, 2026
Merged
tannergooding merged 4 commits into
tannergooding merged 4 commits into
Conversation
Add a hardware fast path to the `double`, `float`, and `BFloat16` operators: magnitudes that fit in 64 bits reconstruct a `ulong` and go through the correctly rounded `(T)(ulong)` conversion the hardware provides, rather than the general limb scan. This is the common case and is roughly 2.7x faster (small double 4.2 -> 1.6ns, small float 4.4 -> 1.6ns). Round the general limb scan directly at the target significand width instead of rounding to odd at 53 bits and re-rounding. `ConvertToDouble` now takes the target precision (53, 24, 11, or 8) and rounds once with round-to-nearest, ties-to-even, producing a double already on the narrow type's grid so the narrowing cast is exact. This is valid because every bits[]-backed magnitude is at least 2^31, so a target subnormal is never produced. It also lets the narrow types share the same lazy sticky scan as `double`. Only examine the lower limbs for a sticky bit when the rounding decision actually depends on them (at the halfway point), skipping the O(n) scan otherwise. Together these bring large-magnitude `float` to parity with `double` (both ~4.1ns, down from ~7.2ns). Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates System.Numerics.BigInteger explicit conversions to floating-point types to reduce overhead in the common “fits in 64 bits” case and to avoid unnecessary work in the general limb-scan rounding path, while keeping the conversion results unchanged.
Changes:
- Add a <= 64-bit magnitude fast path for
(double),(float), and(BFloat16)conversions by reconstructing aulongmagnitude and using the built-in integer-to-FP rounding. - Generalize
ConvertToDoubleto round directly to a specified target precision (53/24/11/8 bits) and perform the lower-limb “sticky” scan lazily only when needed for ties. - Extend
cast_from.cscoverage with >64-bit rounding tests to ensure the general limb-scan path stays exercised now that many values take the fast path.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs | Adds <=64-bit ulong fast paths for FP conversions; changes ConvertToDouble to precision-based rounding and lazy sticky scanning. |
| src/libraries/System.Runtime.Numerics/tests/BigInteger/cast_from.cs | Adds wide (>64-bit) rounding tests for double, float, and BFloat16 to keep the non-fast-path logic covered. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
Every bits[]-backed magnitude is at least 2^31, well outside Half's finite range, so the conversion is always an infinity with the value's sign. Return it directly instead of routing through ConvertToDouble and scanning the limbs. Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
The 64-bit fast path used `(float)ToUInt64(bits)`, which relies on the hardware `ulong -> float` conversion being correctly rounded. Mono (and Mono-based wasm) route it through `double`, double rounding and producing the wrong result. Gate the fast path behind `!MONO` so CoreCLR keeps the cheap direct cast while Mono rounds directly at float's 24-bit significand, which yields a double already on float's grid so the narrowing cast is exact. Co-authored-by: Copilot App <[email protected]>
This was referenced Jul 15, 2026
tannergooding
enabled auto-merge (squash)
July 16, 2026 05:39
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
A performance follow-up to #130565 (which fixed the correctness of
BigInteger-> floating-point conversions). No behavioral change -- the results are identical, this only makes them faster. Validated against a 2M-value oracle sweep vsdouble.Parse/float.Parse(0 mismatches) plus the existing rounding tests.ulonghardware fast pathMagnitudes that fit in 64 bits are by far the common case. Rather than run them through the general limb scan,
operator double,operator float, andoperator BFloat16now reconstruct aulong(ToUInt64) and use the correctly rounded(T)(ulong)conversion the hardware already provides.(double)(ulong),(float)(ulong), andBFloat16's direct integer rounding are all correctly single-rounded, so this is exact.Halfhas no fast path because everybits[]-backed magnitude (>= 2^31) overflows it to infinity anyway.Round directly at the target width
The narrow-type operators previously rounded to odd at 53 bits and then re-rounded via the narrowing cast, to dodge double rounding. Instead,
ConvertToDoublenow takes the targetprecision(53 fordouble, 24 forfloat, 11 forHalf, 8 forBFloat16) and rounds a single time at that width using round-to-nearest, ties-to-even. The resultingdoublealready lies exactly on the target type's grid, so the narrowing cast is exact and no double rounding is possible. Round-to-odd is gone entirely.This is valid precisely because every
bits[]-backedBigIntegermagnitude is at least 2^31, comfortably in the normal range of all four targets, so a target subnormal (the only case that would need extra intermediate precision) never occurs.A side benefit: the narrow types now share
double's sticky-scan gate.Lazy sticky scan
The lower limbs only affect the result when the top bits sit exactly on the halfway point (the round bit is set) and nothing above already made the value sticky.
ConvertToDoublenow scans the lower limbs (an O(n) walk) only in that case, instead of always. SinceHalf/float/BFloat16now use the same round-to-nearest gate asdouble, they benefit too.Numbers
Local A/B (Release corerun, identical CoreLib/JIT, base = current main, this = PR). ns/op:
The common <= 64-bit case is ~2.7x faster for both, and large-magnitude
floatis now at parity withdouble(was 7.2 vs 4.1ns).Tests
Added wide (> 64-bit) rounding cases for
double,float, andBFloat16tocast_from.csso the general limb-scan path stays covered now that the <= 64-bit killer values hit the fast path.Note
This PR description was drafted with the assistance of GitHub Copilot.