Skip to content

Enable vectorization of TensorPrimitives.ConvertTruncating for float/double #116895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public static void ConvertTruncating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Spa
/// <summary>(float)int</summary>
private readonly struct ConvertSingleToInt32 : IUnaryOperator<float, int>
{
public static bool Vectorizable => false; // TODO https://github.com/dotnet/runtime/pull/97529: make this true once vectorized behavior matches scalar
public static bool Vectorizable =>
#if NET9_0_OR_GREATER
true; // .NET 9+ includes https://github.com/dotnet/runtime/pull/97529
#else
false;
#endif

public static int Invoke(float x) => int.CreateTruncating(x);
public static Vector128<int> Invoke(Vector128<float> x) => Vector128.ConvertToInt32(x);
Expand All @@ -114,7 +119,12 @@ public static void ConvertTruncating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Spa
/// <summary>(float)uint</summary>
private readonly struct ConvertSingleToUInt32 : IUnaryOperator<float, uint>
{
public static bool Vectorizable => false; // TODO https://github.com/dotnet/runtime/pull/97529: make this true once vectorized behavior matches scalar
public static bool Vectorizable =>
#if NET9_0_OR_GREATER
true; // .NET 9+ includes https://github.com/dotnet/runtime/pull/97529
#else
false;
#endif

public static uint Invoke(float x) => uint.CreateTruncating(x);
public static Vector128<uint> Invoke(Vector128<float> x) => Vector128.ConvertToUInt32(x);
Expand All @@ -125,7 +135,12 @@ public static void ConvertTruncating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Spa
/// <summary>(ulong)double</summary>
private readonly struct ConvertDoubleToUInt64 : IUnaryOperator<double, ulong>
{
public static bool Vectorizable => false; // TODO https://github.com/dotnet/runtime/pull/97529: make this true once vectorized behavior matches scalar
public static bool Vectorizable =>
#if NET9_0_OR_GREATER
true; // .NET 9+ includes https://github.com/dotnet/runtime/pull/97529
#else
false;
#endif

public static ulong Invoke(double x) => ulong.CreateTruncating(x);
public static Vector128<ulong> Invoke(Vector128<double> x) => Vector128.ConvertToUInt64(x);
Expand All @@ -136,7 +151,12 @@ public static void ConvertTruncating<TFrom, TTo>(ReadOnlySpan<TFrom> source, Spa
/// <summary>(long)double</summary>
private readonly struct ConvertDoubleToInt64 : IUnaryOperator<double, long>
{
public static bool Vectorizable => false; // TODO https://github.com/dotnet/runtime/pull/97529: make this true once vectorized behavior matches scalar
public static bool Vectorizable =>
#if NET9_0_OR_GREATER
true; // .NET 9+ includes https://github.com/dotnet/runtime/pull/97529
#else
false;
#endif

public static long Invoke(double x) => long.CreateTruncating(x);
public static Vector128<long> Invoke(Vector128<double> x) => Vector128.ConvertToInt64(x);
Expand Down
Loading