Skip to content

Commit

Permalink
Simplify TensorPrimitive's AbsoluteOperator (dotnet#92577)
Browse files Browse the repository at this point in the history
Vector{128/256/512} all provide Abs; no need to do this manually.
  • Loading branch information
stephentoub authored and michaelgsharp committed Oct 20, 2023
1 parent bd57689 commit 4afeb64
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1256,28 +1256,10 @@ public static float Invoke(Vector512<float> x)
private readonly struct AbsoluteOperator : IUnaryOperator
{
public static float Invoke(float x) => MathF.Abs(x);

public static Vector128<float> Invoke(Vector128<float> x)
{
Vector128<uint> raw = x.AsUInt32();
Vector128<uint> mask = Vector128.Create((uint)0x7FFFFFFF);
return (raw & mask).AsSingle();
}

public static Vector256<float> Invoke(Vector256<float> x)
{
Vector256<uint> raw = x.AsUInt32();
Vector256<uint> mask = Vector256.Create((uint)0x7FFFFFFF);
return (raw & mask).AsSingle();
}

public static Vector128<float> Invoke(Vector128<float> x) => Vector128.Abs(x);
public static Vector256<float> Invoke(Vector256<float> x) => Vector256.Abs(x);
#if NET8_0_OR_GREATER
public static Vector512<float> Invoke(Vector512<float> x)
{
Vector512<uint> raw = x.AsUInt32();
Vector512<uint> mask = Vector512.Create((uint)0x7FFFFFFF);
return (raw & mask).AsSingle();
}
public static Vector512<float> Invoke(Vector512<float> x) => Vector512.Abs(x);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,7 @@ public Vector<float> Invoke(Vector<float> x, Vector<float> y)
private readonly struct AbsoluteOperator : IUnaryOperator
{
public float Invoke(float x) => MathF.Abs(x);

public Vector<float> Invoke(Vector<float> x)
{
Vector<uint> raw = Vector.AsVectorUInt32(x);
Vector<uint> mask = new Vector<uint>(0x7FFFFFFF);
return Vector.AsVectorSingle(raw & mask);
}
public Vector<float> Invoke(Vector<float> x) => Vector.Abs(x);
}

private interface IUnaryOperator
Expand Down

0 comments on commit 4afeb64

Please sign in to comment.