Optimize Half comparison operators and CompareTo#131297
Open
tannergooding wants to merge 1 commit into
Open
Conversation
Replace the operator-chained CompareTo with a single NaN check plus a monotonic sign-magnitude ordering key, and add an AVX2 fast path to CompareTo, operator <, and operator <= that compares via (float)Half, which lowers to a hardware vcvtph2ps conversion. The relational operators funnel > and >= through the fast path as well, while the equality operators stay on the cheaper bit logic. Equality is left unchanged since converting to float measured slower there. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
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
There was a problem hiding this comment.
Pull request overview
This PR optimizes System.Half comparison behavior by (1) rewriting Half.CompareTo(Half) to avoid operator chaining and redundant NaN/sign/zero checks and (2) adding an AVX2-gated fast path for <, <=, and CompareTo that compares via lossless (float)Half conversion.
Changes:
- Update
Half.CompareTo(Half)to handle NaN up-front and otherwise compare via a monotonic, bit-derived ordering key (collapsing+0/-0). - Add AVX2 fast paths for
operator <,operator <=, andCompareTo(Half)that compare throughfloaton supported targets. - Extend
HalfTestsCompareTo_TestDatawith additional signed-zero, subnormal-sign, and NaN/infinity ordering cases.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Half.cs | Reworks CompareTo(Half) to avoid operator chaining and adds AVX2-gated float-based fast paths for <, <=, and CompareTo. |
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/HalfTests.cs | Adds extra CompareTo test vectors covering signed zero, subnormal sign ordering, and NaN vs infinity ordering. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
This was referenced Jul 24, 2026
Open
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #43117.
Half.CompareTo(Half)previously delegated tothis < other,this > other, andthis == other. Since those operators were rewritten to handle signed values and zeros, each is now a full managed method with its own NaN/sign/zero handling -- so the common non-NaN path ran roughly sixIsNaNchecks plus redundant sign/zero work before falling into a bit comparison it could have done directly.This rewrites
CompareToto handle NaN once and then compare a monotonic sign-magnitude ordering key derived directly from the bits (both zeros collapse to the same key, preserving+0 == -0).It also adds an AVX2 fast path to
CompareTo,operator <, andoperator <=that compares via(float)Half. OnTARGET_XARCH+ AVX2 theHalf->floatconversion lowers to a hardwarevcvtph2ps, so comparing asfloatis materially cheaper.operator >/operator >=are thinright < left/right <= leftwrappers and inherit the fast path automatically, and the existingMin/Max/*Magnitude*helpers already delegate throughfloat/relational operators, so they benefit as well.Avx2.IsSupportedis a JIT-time constant, so the non-taken branch is eliminated and there's no overhead on other targets; the bit-based logic remains as the hardware-independent fallback.The equality operators (
==,!=,Equals) are intentionally left on the bit logic -- converting tofloatthere measured slower, since the bit equality check is nearly free and doesn't justify two conversions.Half->floatis lossless and order-preserving (NaN and signed-zero semantics preserved), so all fast paths match the existing bit semantics exactly.Measurements (local, AVX2 machine; 4096 comparisons/op; in-process BenchmarkDotNet)
CompareTo, mixed data:
operator <: 4.76 us -> 2.41 us (~2x).With AVX2 disabled the change is still a win over the old code (
CompareTo12.25 -> 7.06 us via the bit-key fallback), and the equality path measured slower asfloat(2.16 -> 3.20 us), which is why it was left alone.Tests: extended
HalfTests.CompareTo_TestDatawith signed-zero, subnormal-sign, and NaN/infinity ordering cases. FullHalfTests(1629 cases) pass both with AVX2 enabled and withDOTNET_EnableAVX2=0exercising the fallback.Note
This PR description and the code changes were drafted with the assistance of GitHub Copilot.