Skip to content

Implement AddSaturate, SubtractSaturate and NarrowWithSaturation on the Vector types #115525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 14, 2025

Conversation

tannergooding
Copy link
Member

No description provided.

@tannergooding tannergooding marked this pull request as ready for review May 13, 2025 20:19
@Copilot Copilot AI review requested due to automatic review settings May 13, 2025 20:19
@tannergooding
Copy link
Member Author

CC. @dotnet/jit-contrib, @EgorBo, @kunalspathak to review the JIT side changes

CC. @michaelgsharp for the libraries side changes

Copy link
Contributor

@Copilot Copilot AI left a comment

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 adds saturating add/subtract operations and narrowing-with-saturation to various vector types and their scalar fallbacks, updates the intrinsic reference surfaces, and extends JIT and tests to cover these new APIs.

  • Introduce AddSaturate and SubtractSaturate methods in Vector64/128/256/512 and generic Vector<T> and their scalar implementations.
  • Add NarrowWithSaturation overloads for all vector sizes and wire them through intrinsics lists and JIT import logic.
  • Extend tests in all VectorXXTests.cs and GenericVectorTests.cs to exercise the new saturating and narrowing behaviours.

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs Implements AddSaturate, SubtractSaturate, NarrowWithSaturation, fixes unsafe patterns
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs Adds scalar AddSaturate/SubtractSaturate implementations
src/libraries/System.Runtime.Intrinsics/ref/System.Runtime.Intrinsics.cs Adds ref API entries for new saturating and narrowing methods
src/coreclr/jit/hwintrinsiclistxarch.h Registers new intrinsics in x86/x64 list
src/coreclr/jit/hwintrinsiclistarm64.h Registers new intrinsics in ARM64 list
src/coreclr/jit/hwintrinsicarm64.cpp Implements ARM64 import logic for saturating add/sub/narrowing
src/libraries/System.Runtime.Intrinsics/tests/Vectors/VectorXXTests.cs Adds unit tests for saturating and narrowing operations
src/libraries/System.Numerics.Vectors/src/* Adds AddSaturate/SubtractSaturate to generic Vector<T>
src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs Adds generic tests for new Vector<T> saturating ops
Comments suppressed due to low confidence (2)

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs:4

  • The ShiftLeft implementation uses vector._lower for both halves. The upper half should be shifted from vector._upper, i.e., use Vector256.ShiftLeft(vector._upper, shiftCount._upper).
Vector256.ShiftLeft(vector._lower, shiftCount._upper)

src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector512Tests.cs:7799

  • Consider adding a test for NarrowWithSaturation from double to float (e.g. Vector512<double>Vector512<float>) to validate floating-point narrowing behavior.
public void NarrowWithSaturationUInt64Test()

@kunalspathak
Copy link
Member

/azp run Fuzzlyn, Antigen

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

JIT changes LGTM

@tannergooding
Copy link
Member Author

/azp run runtime-coreclr jitstress-isas-x86, Fuzzlyn, Antigen

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@tannergooding
Copy link
Member Author

Fuzzlyn, JitStress ISA, and Antigen failures are unrelated and tracked by existing issues.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

LGTM

@tannergooding tannergooding merged commit 0b64041 into dotnet:main May 14, 2025
167 of 182 checks passed
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