Skip to content

Commit d4a0101

Browse files
committed
Ensure the managed fallback for negate handles float properly
1 parent 34cfeb7 commit d4a0101

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

src/libraries/System.Private.CoreLib/src/System/Numerics/Vector_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,22 @@ public static Vector<T> operator >>(Vector<T> value, int shiftCount)
576576
/// <param name="value">The vector to negate.</param>
577577
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="value" />.</returns>
578578
[Intrinsic]
579-
public static Vector<T> operator -(Vector<T> value) => Zero - value;
579+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
580+
public static Vector<T> operator -(Vector<T> value)
581+
{
582+
if (typeof(T) == typeof(float))
583+
{
584+
return value ^ Vector.Create(-0.0f).As<float, T>();
585+
}
586+
else if (typeof(T) == typeof(double))
587+
{
588+
return value ^ Vector.Create(-0.0).As<double, T>();
589+
}
590+
else
591+
{
592+
return Zero - value;
593+
}
594+
}
580595

581596
/// <summary>Returns a given vector unchanged.</summary>
582597
/// <param name="value">The vector.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,22 @@ public static Vector128<T> operator >>(Vector128<T> value, int shiftCount)
337337
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
338338
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
339339
[Intrinsic]
340-
public static Vector128<T> operator -(Vector128<T> vector) => Zero - vector;
340+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
341+
public static Vector128<T> operator -(Vector128<T> vector)
342+
{
343+
if (typeof(T) == typeof(float))
344+
{
345+
return vector ^ Vector128.Create(-0.0f).As<float, T>();
346+
}
347+
else if (typeof(T) == typeof(double))
348+
{
349+
return vector ^ Vector128.Create(-0.0).As<double, T>();
350+
}
351+
else
352+
{
353+
return Zero - vector;
354+
}
355+
}
341356

342357
/// <summary>Returns a given vector unchanged.</summary>
343358
/// <param name="value">The vector.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,22 @@ public static Vector256<T> operator >>(Vector256<T> value, int shiftCount)
335335
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
336336
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
337337
[Intrinsic]
338-
public static Vector256<T> operator -(Vector256<T> vector) => Zero - vector;
338+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
339+
public static Vector256<T> operator -(Vector256<T> vector)
340+
{
341+
if (typeof(T) == typeof(float))
342+
{
343+
return vector ^ Vector256.Create(-0.0f).As<float, T>();
344+
}
345+
else if (typeof(T) == typeof(double))
346+
{
347+
return vector ^ Vector256.Create(-0.0).As<double, T>();
348+
}
349+
else
350+
{
351+
return Zero - vector;
352+
}
353+
}
339354

340355
/// <summary>Returns a given vector unchanged.</summary>
341356
/// <param name="value">The vector.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,22 @@ public static Vector512<T> operator >>(Vector512<T> value, int shiftCount)
335335
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
336336
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
337337
[Intrinsic]
338-
public static Vector512<T> operator -(Vector512<T> vector) => Zero - vector;
338+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
339+
public static Vector512<T> operator -(Vector512<T> vector)
340+
{
341+
if (typeof(T) == typeof(float))
342+
{
343+
return vector ^ Vector512.Create(-0.0f).As<float, T>();
344+
}
345+
else if (typeof(T) == typeof(double))
346+
{
347+
return vector ^ Vector512.Create(-0.0).As<double, T>();
348+
}
349+
else
350+
{
351+
return Zero - vector;
352+
}
353+
}
339354

340355
/// <summary>Returns a given vector unchanged.</summary>
341356
/// <param name="value">The vector.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector64_1.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,22 @@ public static Vector64<T> operator >>(Vector64<T> value, int shiftCount)
389389
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
390390
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
391391
[Intrinsic]
392-
public static Vector64<T> operator -(Vector64<T> vector) => Zero - vector;
392+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
393+
public static Vector64<T> operator -(Vector64<T> vector)
394+
{
395+
if (typeof(T) == typeof(float))
396+
{
397+
return vector ^ Vector64.Create(-0.0f).As<float, T>();
398+
}
399+
else if (typeof(T) == typeof(double))
400+
{
401+
return vector ^ Vector64.Create(-0.0).As<double, T>();
402+
}
403+
else
404+
{
405+
return Zero - vector;
406+
}
407+
}
393408

394409
/// <summary>Returns a given vector unchanged.</summary>
395410
/// <param name="value">The vector.</param>

0 commit comments

Comments
 (0)