Repository navigation
Fix exponent/significand cohort comparison for differing byte lengths - #131205
Merged
tannergooding merged 1 commit intoJul 23, 2026
Merged
tannergooding merged 1 commit into
tannergooding merged 1 commit into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 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 fixes correctness in TotalOrderIeee754Comparer<T>’s fully-generic comparison logic when comparing exponents/significands whose big-endian encodings can legally differ in byte length (notably for custom IFloatingPointIeee754<T> implementations). It updates the generic exponent/significand comparison to be length-robust and extends test coverage to exercise these cases, including driving real float/double values through the generic path via wrapper types.
Changes:
- Add a shared
CompareMagnitudeBigEndian(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y, byte fill)helper to correctly compare differing-length big-endian encodings via appropriate sign/zero extension. - Update
CompareExponentto handle differing exponent byte lengths and add shortest-bit-length fast paths (including a zero-bit-length early decision). - Update
CompareSignificand’s equal-bit-length branch to use the new helper, and expand tests to cover the new behaviors (including wrapper-driven generic-path validation for binary32/64).
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Numerics/TotalOrderIeee754Comparer.cs | Makes generic exponent/significand comparisons correct when operand byte lengths differ by introducing CompareMagnitudeBigEndian and integrating bit-length fast paths in CompareExponent. |
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/TotalOrderIeee754ComparerTests.cs | Adds targeted regression coverage for differing byte-length scenarios (exponent cohort + significand equal-bit-length cases) and validates generic-path behavior against real float/double via wrapper types. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
CompareExponent's MSB-flip + SequenceCompareTo trick only produces the correct signed two's-complement order when both spans are the same length; the same latent issue existed in CompareSignificand's equal-bit-length fallback since byte count isn't guaranteed to match bit length. Add a shared CompareMagnitudeBigEndian helper that determines sign from the top bit (for the signed exponent case) and compares differing-length big-endian magnitudes correctly by treating the shorter span as sign/zero-extended. Also add two bit-length-driven fast paths to CompareExponent: - For a fixed sign, the shortest two's complement bit length is monotonic in magnitude, so a mismatch settles same-sign comparisons without walking the full magnitude (mirrors CompareSignificand). - A bit length of 0 uniquely identifies an exponent value of exactly 0 (there is no negative-zero encoding), so a zero operand settles the comparison, or lets the non-zero operand's sign alone decide, without reading either operand's raw exponent bytes. Add SingleWrapper/DoubleWrapper test types that wrap a real float and double to drive the fully generic CompareGeneric path (which the built-in types themselves never reach, since Compare fast-paths them) and assert its result matches the real binary32/64 totalOrder semantics, in addition to the existing synthetic byte-array stubs. Co-authored-by: Copilot App <[email protected]>
tannergooding
force-pushed
the
tannergooding-fix-total-order-exponent-compare
branch
from
July 22, 2026 17:35
bde795f to
aad1bb1
Compare
PranavSenthilnathan
approved these changes
Jul 22, 2026
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.
Follow-up to #131087 (discussion here). @PranavSenthilnathan noted a correctness gap in
TotalOrderIeee754Comparer<T>.CompareExponent.Problem
CompareExponent's MSB-flip +SequenceCompareTotrick only produces the correct signed two's-complement order when both operands' exponent spans are the same length. For a customIFloatingPointIeee754<T>whoseGetExponentByteCount()returns a minimal, value-dependent two's complement byte count, the two exponents can have different byte lengths, and the naive comparison is wrong in that case (a 1-byte encoding of+5can compare greater than a 2-byte encoding of+260).The same latent issue existed in
CompareSignificand's equal-bit-length fallback: byte count isn't guaranteed to match bit length, so two equal-bit-length significands can still be encoded with differing byte counts.This only affects custom
IFloatingPointIeee754<T>implementations reaching the generic path -- every built-in type (float,double,Half,BFloat16,NFloat,Decimal32/64/128) is fast-pathed earlier inCompareand is unaffected.Fix
Add a shared
CompareMagnitudeBigEndian(x, y, fill)helper that correctly compares differing-length big-endian magnitudes by treating the shorter span as sign/zero-extended withfill(0x00for unsigned/non-negative,0xFFfor negative). BothCompareExponentandCompareSignificand's equal-bit-length branch now use it.Also added two bit-length-driven fast paths to
CompareExponent, sinceGetExponentShortestBitLength()is cheap to query:CompareSignificandpattern).0uniquely identifies an exponent value of exactly0(there's no negative-zero two's complement encoding), so a zero operand settles the comparison immediately, or lets a zero-vs-nonzero pair decide from just the nonzero operand's sign -- without ever reading the zero operand's raw bytes.CompareMagnitudeBigEndian's leading-byte scan usesIndexOfAnyExceptinstead of a manual loop.Tests
StubFloatingPointIeee754Cohortexercising same-length, differing-length, cross-sign, negation, and the new zero-bit-length fast path for the exponent cohort branch, plus a differing-byte-count case for the significand branch.SingleWrapper/DoubleWrappertest types that wrap a realfloat/doubleto driveTotalOrderIeee754Comparer<T>through the fully generic path (which built-in types never reach, sinceComparefast-paths them) and assert the result matches the real binary32/64 totalOrder semantics.Note
This PR description and the commit were drafted with GitHub Copilot assistance.