Skip to content

Speed up BigInteger conversions to floating-point types - #130721

Merged
tannergooding merged 4 commits into
dotnet:mainfrom
tannergooding:tannergooding-biginteger-fp-fast-path
Jul 16, 2026
Merged

tannergooding merged 4 commits into
dotnet:mainfrom
tannergooding:tannergooding-biginteger-fp-fast-path

Conversation

@tannergooding

Copy link
Copy Markdown
Member

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 vs double.Parse/float.Parse (0 mismatches) plus the existing rounding tests.

ulong hardware fast path

Magnitudes that fit in 64 bits are by far the common case. Rather than run them through the general limb scan, operator double, operator float, and operator BFloat16 now reconstruct a ulong (ToUInt64) and use the correctly rounded (T)(ulong) conversion the hardware already provides. (double)(ulong), (float)(ulong), and BFloat16's direct integer rounding are all correctly single-rounded, so this is exact. Half has no fast path because every bits[]-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, ConvertToDouble now takes the target precision (53 for double, 24 for float, 11 for Half, 8 for BFloat16) and rounds a single time at that width using round-to-nearest, ties-to-even. The resulting double already 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[]-backed BigInteger magnitude 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. ConvertToDouble now scans the lower limbs (an O(n) walk) only in that case, instead of always. Since Half/float/BFloat16 now use the same round-to-nearest gate as double, they benefit too.

Numbers

Local A/B (Release corerun, identical CoreLib/JIT, base = current main, this = PR). ns/op:

Case (magnitude) double base -> PR float base -> PR
Small (<= 64-bit) 4.23 -> 1.57 (0.37x) 4.40 -> 1.57 (0.36x)
65-bit / ~128-bit ~neutral ~neutral
Large (~1024-bit) 7.15 -> 4.09 (0.57x) 7.22 -> 4.08 (0.57x)

The common <= 64-bit case is ~2.7x faster for both, and large-magnitude float is now at parity with double (was 7.2 vs 4.1ns).

Tests

Added wide (> 64-bit) rounding cases for double, float, and BFloat16 to cast_from.cs so 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.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a ulong magnitude and using the built-in integer-to-FP rounding.
  • Generalize ConvertToDouble to 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.cs coverage 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]>
Copilot AI review requested due to automatic review settings July 14, 2026 21:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 14, 2026 21:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new

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]>
Copilot AI review requested due to automatic review settings July 15, 2026 15:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When making

@tannergooding
tannergooding merged commit 2602173 into dotnet:main Jul 16, 2026
89 checks passed
@tannergooding
tannergooding deleted the tannergooding-biginteger-fp-fast-path branch July 16, 2026 07:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants