You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
INumber.MinNumber and INumber.MaxNumber return NaN in Release configuration #98068
When running a program in Release configuration, INumber.MinNumber and INumber.MaxNumber return NaN if one of the numbers is not NaN. In Debug configuration these methods work as expected.
Reproduction Steps
Create a new console project, specify .NET 8 as the target platform.
Here is the result of the program in Release configuration:
Numbers: 9999
NaN: 90001
Regression?
The bug could not be reproduced in .NET 7. The program works as expected in both configurations.
Known Workarounds
You can use INumberBase.MinMagnitudeNumber and INumberBase.MaxMagnitudeNumber if you intend to work with only positive numbers.
Configuration
.NET:
Microsoft .NET SDK 8.0.101 (x64) from Visual Studio (8.1.123.58017)
Microsoft .NET SDK 8.0.200-preview.23624.5 (x64) from Visual Studio (8.2.23.62405)
OS:
Windows 11 Pro 23H2 22631.3085
Windows 11 Pro 22H2 22621.3085
Architecture:
x64
x64
Other information
The bug is not reproduced if you replace the first argument of MaxNumber with one of the following:
Looks like a codegen bug - when jit compiles float.MaxNumber(x, 1f) it imports it as MAXSS(1f, x) while it should be MAXSS(x, 1f). Because if the 1st argument of MAXSS is NaN - it returns the 2nd argument (1f). If the 2nd argument is NaN - it returns NaN.
We setup op1 = cnsNode and op2 = otherNode as required for Max and Min, we then need to swap them if isNumber is true, but because we do std::swap(op1, op2) and then inverse the assignments to retNode, we're just assigning back the original values.
Removing that std::swap(op1, op2) will cause the right thing to happen and fix it 🤦
Activity
MichalPetryka commentedon Feb 7, 2024
With TieredCompilation=0 this gives 100000 NaNs, looks like this is a bug in optimized code.
EgorBo commentedon Feb 7, 2024
Looks like a codegen bug - when jit compiles
float.MaxNumber(x, 1f)it imports it asMAXSS(1f, x)while it should beMAXSS(x, 1f). Because if the 1st argument of MAXSS is NaN - it returns the 2nd argument (1f). If the 2nd argument is NaN - it returns NaN.tannergooding commentedon Feb 7, 2024
We should also validate this works correctly for MinNumber.
We're supposed to have tests validating the configs here, so I wonder if they aren't getting run in the
TieredCompilation=0outerloop?tannergooding commentedon Feb 7, 2024
The issue is this line here: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/importercalls.cpp#L8520
We setup
op1 = cnsNodeandop2 = otherNodeas required forMaxandMin, we then need to swap them ifisNumberis true, but because we dostd::swap(op1, op2)and then inverse the assignments toretNode, we're just assigning back the original values.Removing that
std::swap(op1, op2)will cause the right thing to happen and fix it 🤦MichalPetryka commentedon Feb 7, 2024
Worth noting there's a codegen quality regression here too where the isNaN was folded to constant true on .net 7 but isn't anymore since .net 8.
tannergooding commentedon Feb 7, 2024
Code quality changes won't meet the backport bar. It's something we can ensure is re-handled for .NET 9.
It likely happened due to using
x != xinstead ofBitConverter, which is ultimately better for the more common non-constant scenarios.MichalPetryka commentedon Feb 7, 2024
Yeah, just wanted to note there's further work here after fixing the invalid codegen.