Skip to content

Commit 64ba05c

Browse files
authored
Fix TensorPrimitives.Abs for 0 for signed integers (#97820)
It was erroneously throwing an exception intended only for the minimum value of a signed integer.
1 parent 102ae78 commit 64ba05c

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.netcore.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12073,13 +12073,11 @@ public static Vector128<T> Invoke(Vector128<T> x)
1207312073
// Handle signed integers specially, in order to throw if any attempt is made to
1207412074
// take the absolute value of the minimum value of the type, which doesn't have
1207512075
// a positive absolute value representation.
12076-
Vector128<T> negated = -x;
12077-
if (Vector128.Equals(x, negated) != Vector128<T>.Zero)
12076+
Vector128<T> abs = Vector128.ConditionalSelect(Vector128.LessThan(x, Vector128<T>.Zero), -x, x);
12077+
if (Vector128.LessThan(abs, Vector128<T>.Zero) != Vector128<T>.Zero)
1207812078
{
1207912079
ThrowNegateTwosCompOverflow();
1208012080
}
12081-
12082-
return Vector128.ConditionalSelect(Vector128.LessThan(x, Vector128<T>.Zero), negated, x);
1208312081
}
1208412082

1208512083
return Vector128.Abs(x);
@@ -12097,13 +12095,11 @@ public static Vector256<T> Invoke(Vector256<T> x)
1209712095
// Handle signed integers specially, in order to throw if any attempt is made to
1209812096
// take the absolute value of the minimum value of the type, which doesn't have
1209912097
// a positive absolute value representation.
12100-
Vector256<T> negated = -x;
12101-
if (Vector256.Equals(x, negated) != Vector256<T>.Zero)
12098+
Vector256<T> abs = Vector256.ConditionalSelect(Vector256.LessThan(x, Vector256<T>.Zero), -x, x);
12099+
if (Vector256.LessThan(abs, Vector256<T>.Zero) != Vector256<T>.Zero)
1210212100
{
1210312101
ThrowNegateTwosCompOverflow();
1210412102
}
12105-
12106-
return Vector256.ConditionalSelect(Vector256.LessThan(x, Vector256<T>.Zero), negated, x);
1210712103
}
1210812104

1210912105
return Vector256.Abs(x);
@@ -12121,13 +12117,11 @@ public static Vector512<T> Invoke(Vector512<T> x)
1212112117
// Handle signed integers specially, in order to throw if any attempt is made to
1212212118
// take the absolute value of the minimum value of the type, which doesn't have
1212312119
// a positive absolute value representation.
12124-
Vector512<T> negated = -x;
12125-
if (Vector512.Equals(x, negated) != Vector512<T>.Zero)
12120+
Vector512<T> abs = Vector512.ConditionalSelect(Vector512.LessThan(x, Vector512<T>.Zero), -x, x);
12121+
if (Vector512.LessThan(abs, Vector512<T>.Zero) != Vector512<T>.Zero)
1212612122
{
1212712123
ThrowNegateTwosCompOverflow();
1212812124
}
12129-
12130-
return Vector512.ConditionalSelect(Vector512.LessThan(x, Vector512<T>.Zero), negated, x);
1213112125
}
1213212126

1213312127
return Vector512.Abs(x);

0 commit comments

Comments
 (0)