Skip to content
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

Vectorizes IndexOfMin/Max/Magnitude #93469

Merged
merged 20 commits into from
Oct 20, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
M:System.Numerics.Tensors.TensorPrimitives.ConvertToHalf(System.ReadOnlySpan{System.Single},System.Span{System.Half})
M:System.Numerics.Tensors.TensorPrimitives.ConvertToSingle(System.ReadOnlySpan{System.Half},System.Span{System.Single})
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Once this package has shipped a stable version, the following line
should be removed in order to re-enable validation. -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
<GenAPIExcludeApiList>ReferenceAssemblyExclusions.txt</GenAPIExcludeApiList>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,39 +316,12 @@ public static void Exp(ReadOnlySpan<float> x, Span<float> destination) =>
/// </remarks>
public static int IndexOfMax(ReadOnlySpan<float> x)
{
int result = -1;

if (!x.IsEmpty)
if (x.IsEmpty)
{
result = 0;
float max = float.NegativeInfinity;

for (int i = 0; i < x.Length; i++)
{
float current = x[i];

if (current != max)
{
if (float.IsNaN(current))
{
return i;
}

if (max < current)
{
result = i;
max = current;
}
}
else if (IsNegative(max) && !IsNegative(current))
{
result = i;
max = current;
}
}
return -1;
}

return result;
return IndexOfMinMaxCore<IndexOfMaxOperator>(x);
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>Searches for the index of the single-precision floating-point number with the largest magnitude in the specified tensor.</summary>
Expand All @@ -367,43 +340,12 @@ public static int IndexOfMax(ReadOnlySpan<float> x)
/// </remarks>
public static int IndexOfMaxMagnitude(ReadOnlySpan<float> x)
{
int result = -1;

if (!x.IsEmpty)
if (x.IsEmpty)
{
result = 0;
float max = float.NegativeInfinity;
float maxMag = float.NegativeInfinity;

for (int i = 0; i < x.Length; i++)
{
float current = x[i];
float currentMag = Math.Abs(current);

if (currentMag != maxMag)
{
if (float.IsNaN(currentMag))
{
return i;
}

if (maxMag < currentMag)
{
result = i;
max = current;
maxMag = currentMag;
}
}
else if (IsNegative(max) && !IsNegative(current))
{
result = i;
max = current;
maxMag = currentMag;
}
}
return -1;
}

return result;
return IndexOfMinMaxCore<IndexOfMaxMagnitudeOperator>(x);
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>Searches for the index of the smallest single-precision floating-point number in the specified tensor.</summary>
Expand All @@ -421,39 +363,12 @@ public static int IndexOfMaxMagnitude(ReadOnlySpan<float> x)
/// </remarks>
public static int IndexOfMin(ReadOnlySpan<float> x)
{
int result = -1;

if (!x.IsEmpty)
if (x.IsEmpty)
{
result = 0;
float min = float.PositiveInfinity;

for (int i = 0; i < x.Length; i++)
{
float current = x[i];

if (current != min)
{
if (float.IsNaN(current))
{
return i;
}

if (current < min)
{
result = i;
min = current;
}
}
else if (IsNegative(current) && !IsNegative(min))
{
result = i;
min = current;
}
}
return -1;
}

return result;
return IndexOfMinMaxCore<IndexOfMinOperator>(x);
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>Searches for the index of the single-precision floating-point number with the smallest magnitude in the specified tensor.</summary>
Expand All @@ -472,43 +387,12 @@ public static int IndexOfMin(ReadOnlySpan<float> x)
/// </remarks>
public static int IndexOfMinMagnitude(ReadOnlySpan<float> x)
{
int result = -1;

if (!x.IsEmpty)
if (x.IsEmpty)
{
result = 0;
float min = float.PositiveInfinity;
float minMag = float.PositiveInfinity;

for (int i = 0; i < x.Length; i++)
{
float current = x[i];
float currentMag = Math.Abs(current);

if (currentMag != minMag)
{
if (float.IsNaN(currentMag))
{
return i;
}

if (currentMag < minMag)
{
result = i;
min = current;
minMag = currentMag;
}
}
else if (IsNegative(current) && !IsNegative(min))
{
result = i;
min = current;
minMag = currentMag;
}
}
return -1;
}

return result;
return IndexOfMinMaxCore<IndexOfMinMagnitudeOperator>(x);
michaelgsharp marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>Computes the element-wise natural (base <c>e</c>) logarithm of single-precision floating-point numbers in the specified tensor.</summary>
Expand Down
Loading
Loading