Skip to content

Simplify floating-point integer classification - #133934

Open
tannergooding wants to merge 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-floating-point-parity
Open

tannergooding wants to merge 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-floating-point-parity

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #133916.

Replace finite checks, floating-point remainder, and exponent/significand parity tests with truncation-based residual comparisons for float, double, and SIMD vectors. Nonfinite inputs produce NaN residuals and compare false. Even-integer classification subtracts from the original input so halving a subnormal cannot incorrectly classify it as zero.

  • Share the vector even/odd algorithms across element types and widths, removing the obsolete variable-shift helpers. Keep direct dispatch and rely on existing vector primitives for software fallbacks.
  • Apply the residual IsInteger comparison in the JIT lowering as well as the managed implementations.
  • Use integer parity checks for the integer root in Single.RootN and Double.RootN. This also fixes negative inputs with odd roots that round to an even number when converted to float, such as float.RootN(-1, 16777217).
  • Replace the combined special-value classification masks in vector Log/Log2 with an unsigned encoding-range comparison, preserving special results and subnormal handling.

Coverage focuses on representable neighbors of integer and precision boundaries, independent mixed-lane classification masks, four Single.RootN regressions, and mixed-lane logarithm special cases.

Performance

Representative paired BenchmarkDotNet measurements on Windows 11 / Ryzen 9 7950X, using Release runtime builds against baseline e273d4d8b14 (ns per element):

Operation Input distribution Before After
double.IsEvenInteger Mixed small values 2.69 0.72
double.IsOddInteger Mixed small values 3.66 0.84
float.IsOddInteger Mixed small values 6.75 0.74
double.IsEvenInteger Large integers 28.18 0.69
double.IsOddInteger Large integers 29.31 0.65

Representative accelerated IsInteger wrappers shrink from 51 to 31 bytes for Vector128<float> and from 54 to 34 bytes for Vector256<double>. The final dispatch simplification retained accelerated throughput within measurement noise.

Validation

Built CoreCLR/CoreLib in Checked and Release configurations. Ran scalar classification and RootN tests, and fixed-width/portable vector coverage. After the final simplification, 1,488 fixed-width and 507 portable classification tests passed in each of the normal, no-AVX2, and hardware-intrinsics-disabled configurations. The retained RootN regressions fail on the baseline. Windows x64 JIT formatting passed.

Validation is local to Windows x64; ARM64, Mono, and WASM were not run. The full no-AVX2 intrinsic suite encounters a pre-existing Vector256.IsPositive<double> JIT assertion; the focused tests for these changes pass.

Note

This PR description was drafted with GitHub Copilot.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 15, 2026 06:24
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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.

Copilot review overview

🔵 Needs a closer look

Moderate mixed-lane coverage gaps and the range-mask comment correction remain unresolved.

Review tier: Lite
Findings: None

What changed in this PR

This PR simplifies floating-point integer classification across scalar, SIMD, and JIT implementations, fixes RootN parity handling, and updates vector logarithm special-value handling.

Changes:

  • Uses truncation residuals and shared SIMD algorithms.
  • Simplifies JIT lowering and logarithm masks.
  • Adds boundary, mixed-lane, and regression coverage.

Review notes: two moderate findings request mixed-lane Log/Log2 coverage for each applicable vector width (one vote each); one nit requests correcting the range-mask comment (one vote).

File Reviewed changes
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​SingleTests.GenericMath.cs Adds float classification tests.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​SingleTests.cs Adds RootN regressions.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​DoubleTests.GenericMath.cs Adds double classification tests.
src/​libraries/​System.Runtime.Intrinsics/​tests/​Vectors/​Vector64Tests.cs Tests Vector64 classification.
src/​libraries/​System.Runtime.Intrinsics/​tests/​Vectors/​Vector512Tests.cs Tests Vector512 classification.
src/​libraries/​System.Runtime.Intrinsics/​tests/​Vectors/​Vector256Tests.cs Tests Vector256 classification.
src/​libraries/​System.Runtime.Intrinsics/​tests/​Vectors/​Vector128Tests.cs Tests Vector128 classification and mixed-lane logs.
src/​libraries/​System.Private.CoreLib/​src/​System/​Single.cs Updates float classification and RootN.
src/​libraries/​System.Private.CoreLib/​src/​System/​Runtime/​Intrinsics/​VectorMath.cs Shares SIMD algorithms and revises logarithm masks.
src/​libraries/​System.Private.CoreLib/​src/​System/​Runtime/​Intrinsics/​Vector64.cs Updates Vector64 dispatch and classification.
src/​libraries/​System.Private.CoreLib/​src/​System/​Runtime/​Intrinsics/​Vector512.cs Updates Vector512 dispatch and classification.
src/​libraries/​System.Private.CoreLib/​src/​System/​Runtime/​Intrinsics/​Vector256.cs Updates Vector256 dispatch and classification.
src/​libraries/​System.Private.CoreLib/​src/​System/​Runtime/​Intrinsics/​Vector128.cs Updates Vector128 dispatch and classification.
src/​libraries/​System.Private.CoreLib/​src/​System/​Numerics/​Vector.cs Updates portable vector classification.
src/​libraries/​System.Private.CoreLib/​src/​System/​Double.cs Updates double classification and RootN.
src/​libraries/​System.Numerics.Vectors/​tests/​GenericVectorTests.cs Tests portable vector classification.
src/​libraries/​Common/​tests/​System/​GenericMathTestMemberData.cs Adds classification boundary data.
src/​coreclr/​jit/​hwintrinsic.cpp Updates floating-point classification dispatch.
src/​coreclr/​jit/​gentree.cpp Updates SIMD integer classification lowering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

double.IsEvenInteger / double.IsOddInteger ~20× slower than double.IsInteger

2 participants