Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/coreclr/jit/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ TBase EvaluateUnaryScalarSpecialized(genTreeOps oper, TBase arg0)
{
switch (oper)
{
case GT_NEG:
{
return static_cast<TBase>(0) - arg0;
}

case GT_NOT:
{
return ~arg0;
Expand Down Expand Up @@ -399,6 +404,11 @@ TBase EvaluateUnaryScalarSpecialized(genTreeOps oper, TBase arg0)
template <>
inline float EvaluateUnaryScalarSpecialized<float>(genTreeOps oper, float arg0)
{
if (oper == GT_NEG)
{
return -arg0;
}

uint32_t arg0Bits = BitOperations::SingleToUInt32Bits(arg0);
uint32_t resultBits = EvaluateUnaryScalarSpecialized<uint32_t>(oper, arg0Bits);
return BitOperations::UInt32BitsToSingle(resultBits);
Expand All @@ -407,6 +417,11 @@ inline float EvaluateUnaryScalarSpecialized<float>(genTreeOps oper, float arg0)
template <>
inline double EvaluateUnaryScalarSpecialized<double>(genTreeOps oper, double arg0)
{
if (oper == GT_NEG)
{
return -arg0;
}

uint64_t arg0Bits = BitOperations::DoubleToUInt64Bits(arg0);
uint64_t resultBits = EvaluateUnaryScalarSpecialized<uint64_t>(oper, arg0Bits);
return BitOperations::UInt64BitsToDouble(resultBits);
Expand All @@ -415,18 +430,7 @@ inline double EvaluateUnaryScalarSpecialized<double>(genTreeOps oper, double arg
template <typename TBase>
TBase EvaluateUnaryScalar(genTreeOps oper, TBase arg0)
Copy link
Member

@EgorBo EgorBo Jul 29, 2024

Choose a reason for hiding this comment

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

nit: is EvaluateUnaryScalar method needed then? callers can call EvaluateUnaryScalarSpecialized directly

Copy link
Member Author

Choose a reason for hiding this comment

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

It might not be needed, but it's consistent with the setup of other APIs.

If we were to change this, I'd probably make it EvaluateUnaryScalar and just specialize that directly instead. That way there isn't two differently named entry points for Unary vs Binary vs ...

{
switch (oper)
{
case GT_NEG:
{
return static_cast<TBase>(0) - arg0;
}

default:
{
return EvaluateUnaryScalarSpecialized<TBase>(oper, arg0);
}
}
return EvaluateUnaryScalarSpecialized<TBase>(oper, arg0);
}

#if defined(FEATURE_MASKED_HW_INTRINSICS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,22 @@ public static Vector<T> operator >>(Vector<T> value, int shiftCount)
/// <param name="value">The vector to negate.</param>
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="value" />.</returns>
[Intrinsic]
public static Vector<T> operator -(Vector<T> value) => Zero - value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector<T> operator -(Vector<T> value)
{
if (typeof(T) == typeof(float))
{
return value ^ Vector.Create(-0.0f).As<float, T>();
}
else if (typeof(T) == typeof(double))
{
return value ^ Vector.Create(-0.0).As<double, T>();
}
else
{
return Zero - value;
}
}

/// <summary>Returns a given vector unchanged.</summary>
/// <param name="value">The vector.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,22 @@ public static Vector128<T> operator >>(Vector128<T> value, int shiftCount)
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
[Intrinsic]
public static Vector128<T> operator -(Vector128<T> vector) => Zero - vector;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<T> operator -(Vector128<T> vector)
{
if (typeof(T) == typeof(float))
{
return vector ^ Vector128.Create(-0.0f).As<float, T>();
}
else if (typeof(T) == typeof(double))
{
return vector ^ Vector128.Create(-0.0).As<double, T>();
}
else
{
return Zero - vector;
}
}

/// <summary>Returns a given vector unchanged.</summary>
/// <param name="value">The vector.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,22 @@ public static Vector256<T> operator >>(Vector256<T> value, int shiftCount)
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
[Intrinsic]
public static Vector256<T> operator -(Vector256<T> vector) => Zero - vector;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<T> operator -(Vector256<T> vector)
{
if (typeof(T) == typeof(float))
{
return vector ^ Vector256.Create(-0.0f).As<float, T>();
}
else if (typeof(T) == typeof(double))
{
return vector ^ Vector256.Create(-0.0).As<double, T>();
}
else
{
return Zero - vector;
}
}

/// <summary>Returns a given vector unchanged.</summary>
/// <param name="value">The vector.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,22 @@ public static Vector512<T> operator >>(Vector512<T> value, int shiftCount)
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
[Intrinsic]
public static Vector512<T> operator -(Vector512<T> vector) => Zero - vector;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<T> operator -(Vector512<T> vector)
{
if (typeof(T) == typeof(float))
{
return vector ^ Vector512.Create(-0.0f).As<float, T>();
}
else if (typeof(T) == typeof(double))
{
return vector ^ Vector512.Create(-0.0).As<double, T>();
}
else
{
return Zero - vector;
}
}

/// <summary>Returns a given vector unchanged.</summary>
/// <param name="value">The vector.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,22 @@ public static Vector64<T> operator >>(Vector64<T> value, int shiftCount)
/// <returns>A vector whose elements are the unary negation of the corresponding elements in <paramref name="vector" />.</returns>
/// <exception cref="NotSupportedException">The type of the vector (<typeparamref name="T" />) is not supported.</exception>
[Intrinsic]
public static Vector64<T> operator -(Vector64<T> vector) => Zero - vector;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector64<T> operator -(Vector64<T> vector)
{
if (typeof(T) == typeof(float))
{
return vector ^ Vector64.Create(-0.0f).As<float, T>();
}
else if (typeof(T) == typeof(double))
{
return vector ^ Vector64.Create(-0.0).As<double, T>();
}
else
{
return Zero - vector;
}
}

/// <summary>Returns a given vector unchanged.</summary>
/// <param name="value">The vector.</param>
Expand Down
36 changes: 36 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_105627/Runtime_105627.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using Xunit;

// Run on Arm64 Linux
// Seed: 10489942769529190437-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
// Reduced from 58.9 KiB to 0.4 KiB in 00:00:28
// Debug: Outputs [9223372036854775808, 9223372036854775808]
// Release: Outputs [0, 0]

public class Runtime_105627
{
public static Vector128<double> s_9;

[Fact]
public static void TestEntyPoint()
{
var vr1 = Vector128.CreateScalar(0d);

if (AdvSimd.IsSupported)
{
s_9 = AdvSimd.Arm64.Negate(vr1);
}
else
{
s_9 = -vr1;
}
Assert.Equal(Vector128.Create<ulong>(0x80000000_00000000UL), s_9.AsUInt64());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>