Skip to content

Conversation

@a74nh
Copy link
Contributor

@a74nh a74nh commented Jan 7, 2026

Fixes #122728

Copilot AI review requested due to automatic review settings January 7, 2026 16:20
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 7, 2026
@a74nh
Copy link
Contributor Author

a74nh commented Jan 7, 2026

@dotnet/arm64-contrib

@dotnet-policy-service
Copy link
Contributor

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

Copy link
Contributor

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 refactors overflow detection and saturating arithmetic helper methods in the ARM hardware intrinsics test helpers. It replaces multiple type-specific implementations with generic methods using IBinaryInteger<T> and BigInteger for accurate overflow detection.

Key changes:

  • Consolidates duplicate overflow detection logic into generic methods
  • Fixes overflow detection bugs by using BigInteger for precise range checking
  • Reduces code duplication across sbyte, short, int, and long types

Comment on lines +3073 to +3074
byte result;
bool addOvf = TryAddUnsigned(op1, rndCns, out result);
Copy link
Member

Choose a reason for hiding this comment

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

nit: No need to fix this, but just as FYI you can do this, which is a bit more idiomatic in modern C#:

Suggested change
byte result;
bool addOvf = TryAddUnsigned(op1, rndCns, out result);
bool addOvf = TryAddUnsigned(op1, rndCns, out byte result);

Comment on lines +3118 to +3119
result = unchecked(left + TSigned.CreateChecked(right));
return result < left;
Copy link
Member

Choose a reason for hiding this comment

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

Is this one correct?

Presumably this is for SUQADD (signed saturating addition of unsigned value), in which case the implementation is converting right from unsigned to signed which will throw if it is greater than TSigned.MaxValue.

That seems incorrect because I'd expect, for example, that -128 + 128 was valid and would produce 0

bool ovf;

if (op2 < 0)
if (right < TSigned.Zero)
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
if (right < TSigned.Zero)
if (TSigned.IsNegative(right))

{
ovf = (result < op1);
var mag = TUnsigned.CreateChecked(-right);
Copy link
Member

Choose a reason for hiding this comment

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

This seems "wrong" since it will fail for TSigned.MinValue.

I believe you rather just want TUnsigned.CreateTruncating(-right), since all other values will have become positive while right and the two's complement representation is the same between MinValue and (MaxValue + 1).

-- Noting this is only sensible if TSigned and TUnsigned are the same width.

ovf = (result < op1);
var mag = TUnsigned.CreateChecked(-right);
result = unchecked(left - mag);
return left < mag;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be checking result > left?

That is, we're subtracting two unsigned values, so we overflow if the new result is greater than the original input.

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

Labels

area-System.Runtime.Intrinsics community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: _Sve_ro::JIT.HardwareIntrinsics.Arm._Sve.Program.Sve_AddSaturate_short()

2 participants