Skip to content
Open
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
Expand Up @@ -72,7 +72,9 @@ public static Vector128<T> Compare(Vector128<T> x, Vector128<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector128.AndNot(Vector128.GreaterThan(xMag, yMag) | IsNegative(xMag), IsNegative(yMag));
Vector128<T> equalResult = Vector128.IsPositive(x) & Vector128.IsNegative(y);
Vector128<T> nonOverflowResult = Vector128.GreaterThan(xMag, yMag) | (Vector128.Equals(xMag, yMag) & equalResult);
return Vector128.AndNot(nonOverflowResult | IsNegative(xMag), IsNegative(yMag));
}
else
{
Expand All @@ -96,7 +98,9 @@ public static Vector256<T> Compare(Vector256<T> x, Vector256<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector256.AndNot(Vector256.GreaterThan(xMag, yMag) | IsNegative(xMag), IsNegative(yMag));
Vector256<T> equalResult = Vector256.IsPositive(x) & Vector256.IsNegative(y);
Vector256<T> nonOverflowResult = Vector256.GreaterThan(xMag, yMag) | (Vector256.Equals(xMag, yMag) & equalResult);
return Vector256.AndNot(nonOverflowResult | Vector256.IsNegative(xMag), Vector256.IsNegative(yMag));
}
else
{
Expand All @@ -120,7 +124,9 @@ public static Vector512<T> Compare(Vector512<T> x, Vector512<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector512.AndNot(Vector512.GreaterThan(xMag, yMag) | IsNegative(xMag), IsNegative(yMag));
Vector512<T> equalResult = Vector512.IsPositive(x) & Vector512.IsNegative(y);
Vector512<T> nonOverflowResult = Vector512.GreaterThan(xMag, yMag) | (Vector512.Equals(xMag, yMag) & equalResult);
return Vector512.AndNot(nonOverflowResult | Vector512.IsNegative(xMag), Vector512.IsNegative(yMag));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public static Vector128<T> Compare(Vector128<T> x, Vector128<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector128.AndNot(Vector128.LessThan(xMag, yMag) | IsNegative(yMag), IsNegative(xMag));
Vector128<T> equalResult = Vector128.IsNegative(x) & Vector128.IsPositive(y);
Vector128<T> nonOverflowResult = Vector128.LessThan(xMag, yMag) | (Vector128.Equals(xMag, yMag) & equalResult);
return Vector128.AndNot(nonOverflowResult | Vector128.IsNegative(yMag), Vector128.IsNegative(xMag));
}
else
{
Expand All @@ -96,7 +98,9 @@ public static Vector256<T> Compare(Vector256<T> x, Vector256<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector256.AndNot(Vector256.LessThan(xMag, yMag) | IsNegative(yMag), IsNegative(xMag));
Vector256<T> equalResult = Vector256.IsNegative(x) & Vector256.IsPositive(y);
Vector256<T> nonOverflowResult = Vector256.LessThan(xMag, yMag) | (Vector256.Equals(xMag, yMag) & equalResult);
return Vector256.AndNot(nonOverflowResult | Vector256.IsNegative(yMag), Vector256.IsNegative(xMag));
}
else
{
Expand All @@ -120,7 +124,9 @@ public static Vector512<T> Compare(Vector512<T> x, Vector512<T> y)
|| typeof(T) == typeof(nint))
{
// Consider overflows (when IsNegative(Abs(x))) from Abs(MinValue) which implies maximum magnitude.
return Vector512.AndNot(Vector512.LessThan(xMag, yMag) | IsNegative(yMag), IsNegative(xMag));
Vector512<T> equalResult = Vector512.IsNegative(x) & Vector512.IsPositive(y);
Vector512<T> nonOverflowResult = Vector512.LessThan(xMag, yMag) | (Vector512.Equals(xMag, yMag) & equalResult);
return Vector512.AndNot(nonOverflowResult | Vector512.IsNegative(yMag), Vector512.IsNegative(xMag));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2725,7 +2725,8 @@ public unsafe abstract class GenericNumberTensorPrimitivesTests<T> : TensorPrimi
protected override T SumOfSquares(ReadOnlySpan<T> x) => TensorPrimitives.SumOfSquares(x);

protected override T ConvertFromSingle(float f) => T.CreateTruncating(f);
protected override bool IsFloatingPoint => typeof(T) == typeof(Half) || base.IsFloatingPoint;
protected override bool IsFloatingPoint => typeof(T) == typeof(NFloat) || typeof(T) == typeof(Half) || base.IsFloatingPoint;
protected override bool IsUnsignedInteger => typeof(T) == typeof(UInt128) || typeof(T) == typeof(nuint) || base.IsUnsignedInteger;

protected override T NextRandom()
{
Expand Down
113 changes: 95 additions & 18 deletions src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public abstract class TensorPrimitivesTests<T> where T : unmanaged, IEquatable<T
public delegate bool SpanIsAllAnyDelegate(ReadOnlySpan<T> x);

protected virtual bool IsFloatingPoint => typeof(T) == typeof(float) || typeof(T) == typeof(double);
protected virtual bool IsUnsignedInteger =>
typeof(T) == typeof(byte) || typeof(T) == typeof(ushort) || typeof(T) == typeof(uint) ||
typeof(T) == typeof(ulong) || typeof(T) == typeof(char);

protected virtual int? IndexOfSizeExceedingMaxValue() =>
(typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte)) ? Helpers.SizeGreaterThanByte :
Expand Down Expand Up @@ -1138,6 +1141,18 @@ public void IndexOfMax_Negative0LesserThanPositive0()
Assert.Equal(0, IndexOfMax([ConvertFromSingle(+0f), ConvertFromSingle(-0f)]));
Assert.Equal(1, IndexOfMax([ConvertFromSingle(-1), ConvertFromSingle(-0f)]));
Assert.Equal(2, IndexOfMax([ConvertFromSingle(-1), ConvertFromSingle(-0f), ConvertFromSingle(1f)]));

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(NegativeZero);
x[expected] = Zero;
x[tensorLength - 1] = Zero;
Assert.Equal(expected, IndexOfMax(x.Span));
}
});
}

[Fact]
Expand Down Expand Up @@ -1167,17 +1182,12 @@ public void IndexOfMaxMagnitude_AllLengths()
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
FillTensor(x, MinValue);
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);

T max = x[0];
for (int i = 0; i < x.Length; i++)
{
int compared = Comparer<T>.Default.Compare(Abs(x[i]), Abs(max));
if (compared > 0 || (compared == 0 && EqualityComparer<T>.Default.Equals(x[i], max)))
{
max = x[i];
}
max = MaxMagnitude(max, x[i]);
}
x[expected] = max;

Expand All @@ -1188,11 +1198,11 @@ public void IndexOfMaxMagnitude_AllLengths()
Assert.True(actual < expected || Comparer<T>.Default.Compare(x[actual], x[expected]) > 0, $"{tensorLength} {actual} {expected} {string.Join(",", MemoryMarshal.ToEnumerable<T>(x.Memory))}");
if (IsFloatingPoint)
{
AssertEqualTolerance(Abs(x[expected]), Abs(x[actual]));
AssertEqualTolerance(x[expected], x[actual], Zero);
}
else
{
Assert.Equal(Abs(x[expected]), Abs(x[actual]));
Assert.Equal(x[expected], x[actual]);
}
}
}
Expand All @@ -1216,6 +1226,24 @@ public void IndexOfMaxMagnitude_FirstNaNReturned()
});
}

[Fact]
public void IndexOfMaxMagnitude_Negative1LesserThanPositive1()
{
if (IsUnsignedInteger) return;

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(NegativeOne);
x[expected] = One;
x[tensorLength - 1] = One;
Assert.Equal(expected, IndexOfMaxMagnitude(x.Span));
}
});
}

[Fact]
public void IndexOfMaxMagnitude_Negative0LesserThanPositive0()
{
Expand All @@ -1227,6 +1255,18 @@ public void IndexOfMaxMagnitude_Negative0LesserThanPositive0()
Assert.Equal(0, IndexOfMaxMagnitude([ConvertFromSingle(+0f), ConvertFromSingle(-0f)]));
Assert.Equal(0, IndexOfMaxMagnitude([ConvertFromSingle(-1), ConvertFromSingle(-0f)]));
Assert.Equal(2, IndexOfMaxMagnitude([ConvertFromSingle(-1), ConvertFromSingle(-0f), ConvertFromSingle(1f)]));

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(NegativeZero);
x[expected] = Zero;
x[tensorLength - 1] = Zero;
Assert.Equal(expected, IndexOfMaxMagnitude(x.Span));
}
});
}

[Fact]
Expand Down Expand Up @@ -1291,6 +1331,18 @@ public void IndexOfMin_Negative0LesserThanPositive0()
Assert.Equal(1, IndexOfMin([ConvertFromSingle(+0f), ConvertFromSingle(-0f), ConvertFromSingle(-0f), ConvertFromSingle(-0f), ConvertFromSingle(-0f)]));
Assert.Equal(0, IndexOfMin([ConvertFromSingle(-1), ConvertFromSingle(-0f)]));
Assert.Equal(0, IndexOfMin([ConvertFromSingle(-1), ConvertFromSingle(-0f), ConvertFromSingle(1f)]));

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(Zero);
x[expected] = NegativeZero;
x[tensorLength - 1] = NegativeZero;
Assert.Equal(expected, IndexOfMin(x.Span));
}
});
}

[Fact]
Expand Down Expand Up @@ -1320,17 +1372,12 @@ public void IndexOfMinMagnitude_AllLengths()
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
FillTensor(x, MinValue);
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);

T min = x[0];
for (int i = 0; i < x.Length; i++)
{
int compared = Comparer<T>.Default.Compare(Abs(x[i]), Abs(min));
if (compared < 0 || (compared == 0 && Comparer<T>.Default.Compare(x[i], min) < 0))
{
min = x[i];
}
min = MinMagnitude(min, x[i]);
}

x[expected] = min;
Expand All @@ -1341,11 +1388,11 @@ public void IndexOfMinMagnitude_AllLengths()
Assert.True(actual < expected || Comparer<T>.Default.Compare(x[actual], x[expected]) < 0, $"{tensorLength} {actual} {expected} {string.Join(",", MemoryMarshal.ToEnumerable<T>(x.Memory))}");
if (IsFloatingPoint)
{
AssertEqualTolerance(Abs(x[expected]), Abs(x[actual]));
AssertEqualTolerance(x[expected], x[actual], Zero);
}
else
{
Assert.Equal(Abs(x[expected]), Abs(x[actual]));
Assert.Equal(x[expected], x[actual]);
}
}
}
Expand All @@ -1369,6 +1416,24 @@ public void IndexOfMinMagnitude_FirstNaNReturned()
});
}

[Fact]
public void IndexOfMinMagnitude_Negative1LesserThanPositive1()
{
if (IsUnsignedInteger) return;

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(One);
x[expected] = NegativeOne;
x[tensorLength - 1] = NegativeOne;
Assert.Equal(expected, IndexOfMinMagnitude(x.Span));
}
});
}

[Fact]
public void IndexOfMinMagnitude_Negative0LesserThanPositive0()
{
Expand All @@ -1380,6 +1445,18 @@ public void IndexOfMinMagnitude_Negative0LesserThanPositive0()
Assert.Equal(1, IndexOfMinMagnitude([ConvertFromSingle(+0f), ConvertFromSingle(-0f), ConvertFromSingle(-0f), ConvertFromSingle(-0f)]));
Assert.Equal(1, IndexOfMinMagnitude([ConvertFromSingle(-1), ConvertFromSingle(-0f)]));
Assert.Equal(1, IndexOfMinMagnitude([ConvertFromSingle(-1), ConvertFromSingle(-0f), ConvertFromSingle(1f)]));

Assert.All(Helpers.TensorLengths, tensorLength =>
{
foreach (int expected in new[] { 0, tensorLength / 2, tensorLength - 1 })
{
using BoundedMemory<T> x = CreateTensor(tensorLength);
x.Span.Fill(Zero);
x[expected] = NegativeZero;
x[tensorLength - 1] = NegativeZero;
Assert.Equal(expected, IndexOfMinMagnitude(x.Span));
}
});
}

[Fact]
Expand Down
Loading