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

[RISC-V] Floating NaN overflow test fix #94168

Closed
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
25 changes: 17 additions & 8 deletions src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public class ByteTests_GenericMath
// IAdditionOperators
//

private static byte NaNConversionOverflowValue()
{
if(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == System.Runtime.InteropServices.Architecture.RiscV64)
{
return (byte)0xFF;
}
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

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

We're working on moving towards deterministic behavior across platforms: #61885

This is going to necessitate, longer term, that saturation occurs as it is what most newer platforms do and what many specs (like WASM) require. This is also going to necessitate standardizing the behavior for NaN.

For x64, it currently returns a sentinel value 0x8000_0000 (int) or 0x8000_0000_0000_0000 (long) when the output type would overflow or when NaN is given. byte and short therefore currently expect 0 because its truncating the 32 or 64-bit result to 8 or 16-bits.

For Arm64, it saturates and converts NaN to zero. For WASM, it requires saturation and leaves NaN conversion undefined.

We plan on matching Arm64 for the cross platform deterministic behavior here as it is simple and straightforward. It matches the general IEEE 754 guidance that you compute the result as if to infinite precision and unbounded range, and then round to the nearest representable. NaN then logically has no equivalent, so it becoming zero "makes sense" and avoids confusion in other regards.

Copy link
Member

Choose a reason for hiding this comment

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

As such, it would be better if we could ensure the RISC-V implementation adjusts and matches this behavior. The underlying platform specific behavior would then be available in the future for the APIs proposed in the linked issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your comment, I will try to find the proper solution.

return (byte)0x00;
}

[Fact]
public static void op_AdditionTest()
{
Expand Down Expand Up @@ -1571,7 +1580,7 @@ public static void CreateSaturatingFromDoubleTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateSaturating<double>(double.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<double>(double.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateSaturating<double>(double.NaN));
}

[Fact]
Expand All @@ -1595,7 +1604,7 @@ public static void CreateSaturatingFromHalfTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateSaturating<Half>(Half.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<Half>(Half.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateSaturating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1680,7 +1689,7 @@ public static void CreateSaturatingFromNFloatTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateSaturating<NFloat>(NFloat.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<NFloat>(NFloat.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateSaturating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1714,7 +1723,7 @@ public static void CreateSaturatingFromSingleTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateSaturating<float>(float.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<float>(float.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateSaturating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateSaturating<float>(float.NaN));
}

[Fact]
Expand Down Expand Up @@ -1831,7 +1840,7 @@ public static void CreateTruncatingFromDoubleTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateTruncating<double>(double.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<double>(double.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateTruncating<double>(double.NaN));
}

[Fact]
Expand All @@ -1855,7 +1864,7 @@ public static void CreateTruncatingFromHalfTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateTruncating<Half>(Half.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<Half>(Half.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateTruncating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1940,7 +1949,7 @@ public static void CreateTruncatingFromNFloatTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateTruncating<NFloat>(NFloat.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<NFloat>(NFloat.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateTruncating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1974,7 +1983,7 @@ public static void CreateTruncatingFromSingleTest()
Assert.Equal((byte)0xFF, NumberBaseHelper<byte>.CreateTruncating<float>(float.MaxValue));
Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<float>(float.MinValue));

Assert.Equal((byte)0x00, NumberBaseHelper<byte>.CreateTruncating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<byte>.CreateTruncating<float>(float.NaN));
}

[Fact]
Expand Down
25 changes: 17 additions & 8 deletions src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ public class CharTests_GenericMath
// IAdditionOperators
//

private static char NaNConversionOverflowValue()
{
if(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == System.Runtime.InteropServices.Architecture.RiscV64)
{
return (char)0xFFFF;
}
return (char)0x0000;
}

[Fact]
public static void op_AdditionTest()
{
Expand Down Expand Up @@ -1569,7 +1578,7 @@ public static void CreateSaturatingFromDoubleTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateSaturating<double>(double.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<double>(double.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateSaturating<double>(double.NaN));
}

[Fact]
Expand All @@ -1590,7 +1599,7 @@ public static void CreateSaturatingFromHalfTest()
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<Half>(Half.NegativeInfinity));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<Half>(Half.MinValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateSaturating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1675,7 +1684,7 @@ public static void CreateSaturatingFromNFloatTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateSaturating<NFloat>(NFloat.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<NFloat>(NFloat.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateSaturating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1709,7 +1718,7 @@ public static void CreateSaturatingFromSingleTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateSaturating<float>(float.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<float>(float.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateSaturating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateSaturating<float>(float.NaN));
}

[Fact]
Expand Down Expand Up @@ -1827,7 +1836,7 @@ public static void CreateTruncatingFromDoubleTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateTruncating<double>(double.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<double>(double.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateTruncating<double>(double.NaN));
}

[Fact]
Expand All @@ -1848,7 +1857,7 @@ public static void CreateTruncatingFromHalfTest()
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<Half>(Half.NegativeInfinity));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<Half>(Half.MinValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateTruncating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1933,7 +1942,7 @@ public static void CreateTruncatingFromNFloatTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateTruncating<NFloat>(NFloat.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<NFloat>(NFloat.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateTruncating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1967,7 +1976,7 @@ public static void CreateTruncatingFromSingleTest()
Assert.Equal((char)0xFFFF, NumberBaseHelper<char>.CreateTruncating<float>(float.MaxValue));
Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<float>(float.MinValue));

Assert.Equal((char)0x0000, NumberBaseHelper<char>.CreateTruncating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<char>.CreateTruncating<float>(float.NaN));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public class UInt16Tests_GenericMath
// IAdditionOperators
//

private static ushort NaNConversionOverflowValue()
{
if(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == System.Runtime.InteropServices.Architecture.RiscV64)
{
return (ushort)0xFFFF;
}
return (ushort)0x0000;
}

[Fact]
public static void op_AdditionTest()
{
Expand Down Expand Up @@ -1571,7 +1580,7 @@ public static void CreateSaturatingFromDoubleTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateSaturating<double>(double.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<double>(double.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateSaturating<double>(double.NaN));
}

[Fact]
Expand Down Expand Up @@ -1677,7 +1686,7 @@ public static void CreateSaturatingFromNFloatTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateSaturating<NFloat>(NFloat.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<NFloat>(NFloat.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateSaturating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1711,7 +1720,7 @@ public static void CreateSaturatingFromSingleTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateSaturating<float>(float.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<float>(float.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateSaturating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateSaturating<float>(float.NaN));
}

[Fact]
Expand Down Expand Up @@ -1829,7 +1838,7 @@ public static void CreateTruncatingFromDoubleTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateTruncating<double>(double.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<double>(double.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateTruncating<double>(double.NaN));
}

[Fact]
Expand All @@ -1850,7 +1859,7 @@ public static void CreateTruncatingFromHalfTest()
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<Half>(Half.NegativeInfinity));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<Half>(Half.MinValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateTruncating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1935,7 +1944,7 @@ public static void CreateTruncatingFromNFloatTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateTruncating<NFloat>(NFloat.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<NFloat>(NFloat.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateTruncating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1969,7 +1978,7 @@ public static void CreateTruncatingFromSingleTest()
Assert.Equal((ushort)0xFFFF, NumberBaseHelper<ushort>.CreateTruncating<float>(float.MaxValue));
Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<float>(float.MinValue));

Assert.Equal((ushort)0x0000, NumberBaseHelper<ushort>.CreateTruncating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<ushort>.CreateTruncating<float>(float.NaN));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public class UInt32Tests_GenericMath
// IAdditionOperators
//

private static uint NaNConversionOverflowValue()
{
if(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == System.Runtime.InteropServices.Architecture.RiscV64)
{
return (uint)0xFFFF_FFFF;
}
return (uint)0x0000_0000;
}

[Fact]
public static void op_AdditionTest()
{
Expand Down Expand Up @@ -1583,7 +1592,7 @@ public static void CreateSaturatingFromDoubleTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateSaturating<double>(double.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<double>(double.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateSaturating<double>(double.NaN));
}

[Fact]
Expand All @@ -1604,7 +1613,7 @@ public static void CreateSaturatingFromHalfTest()
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<Half>(Half.NegativeInfinity));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<Half>(Half.MinValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateSaturating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1700,7 +1709,7 @@ public static void CreateSaturatingFromNFloatTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateSaturating<NFloat>(NFloat.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<NFloat>(NFloat.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateSaturating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -1734,7 +1743,7 @@ public static void CreateSaturatingFromSingleTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateSaturating<float>(float.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<float>(float.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateSaturating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateSaturating<float>(float.NaN));
}

[Fact]
Expand Down Expand Up @@ -1852,7 +1861,7 @@ public static void CreateTruncatingFromDoubleTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateTruncating<double>(double.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<double>(double.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<double>(double.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateTruncating<double>(double.NaN));
}

[Fact]
Expand All @@ -1873,7 +1882,7 @@ public static void CreateTruncatingFromHalfTest()
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<Half>(Half.NegativeInfinity));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<Half>(Half.MinValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<Half>(Half.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateTruncating<Half>(Half.NaN));
}

[Fact]
Expand Down Expand Up @@ -1969,7 +1978,7 @@ public static void CreateTruncatingFromNFloatTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateTruncating<NFloat>(NFloat.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<NFloat>(NFloat.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<NFloat>(NFloat.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateTruncating<NFloat>(NFloat.NaN));
}

[Fact]
Expand Down Expand Up @@ -2003,7 +2012,7 @@ public static void CreateTruncatingFromSingleTest()
Assert.Equal((uint)0xFFFF_FFFF, NumberBaseHelper<uint>.CreateTruncating<float>(float.MaxValue));
Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<float>(float.MinValue));

Assert.Equal((uint)0x0000_0000, NumberBaseHelper<uint>.CreateTruncating<float>(float.NaN));
Assert.Equal(NaNConversionOverflowValue(), NumberBaseHelper<uint>.CreateTruncating<float>(float.NaN));
}

[Fact]
Expand Down
Loading