-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Ensure that constant folding for long->float is handled correctly #90325
Changes from all commits
8fb6436
01ec621
a366e7a
8d33072
a79b11c
1040717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using Xunit; | ||
|
||
public class Runtime_90323 | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static float ConvertToSingle(long value) => (float)value; | ||
|
||
// 32-bit currently performs a 2-step conversion which causes a different result to be produced | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "currently" is a problematic word for a long-lived test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current plan of record is to fix the places that are doing incorrect conversions early in .NET 9. I can certainly remove "currently" in the interim if that helps? |
||
[ConditionalFact(typeof(TestLibrary.PlatformDetection), nameof(TestLibrary.PlatformDetection.Is64BitProcess))] | ||
public static int TestEntryPoint() | ||
{ | ||
bool passed = true; | ||
|
||
long value = 0x4000_0040_0000_0001L; | ||
|
||
if (ConvertToSingle(value) != (float)(value)) | ||
{ | ||
Console.WriteLine($"Mismatch between codegen and constant folding: {ConvertToSingle(value)} != {(float)(value)}"); | ||
passed = false; | ||
} | ||
|
||
if (BitConverter.SingleToUInt32Bits((float)(value)) != 0x5E80_0001) // 4.6116866E+18f | ||
{ | ||
Console.WriteLine($"Mismatch between constant folding and expected value: {(float)(value)} != 4.6116866E+18f; 0x{BitConverter.SingleToUInt32Bits((float)(value)):X8} != 0x5E800001"); | ||
passed = false; | ||
} | ||
|
||
return passed ? 100 : 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Optimize>True</Optimize> | ||
<!-- Needed for CLRTestEnvironmentVariable --> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="1" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests using this also require (something like):
|
||
<ProjectReference Include="$(TestLibraryProjectPath)" /> | ||
</ItemGroup> | ||
</Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the problem we were converting UINT32 to double when calling
forceCastToFloat
, before converting tofloat
in that function?Seems like we should get rid of
forceCastToFloat
andforceCastToUInt32
(their comments refer to ancient build compilers). But maybe all casts to/from floats should be explicit function calls toFloatingPointUtils
functions. E.g., here maybe addFloatingPointUtils::convertUInt32ToFloat()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the problem was that it was doing
uint->double->float
which can produce a different result fromuint->float
in some edge cases.64-bit currently does
uint->long->float
; where-as 32-bit currently doesuint->long->double->float
, so this ensures the constant folding matches that behavior.Longer term it would be beneficial to use helpers for these conversions, but that is a much bigger and more involved change. I tried to keep this as minimal as possible for .NET 8 to avoid churn/risk.