From 7a50b43361692906680d99985e08a9e9884ce317 Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Tue, 20 Aug 2024 18:51:37 +0000 Subject: [PATCH] JIT: Cast UInt64 to Single directly during const folding (#106419) --- src/coreclr/jit/gentree.cpp | 2 +- src/coreclr/jit/utils.cpp | 3 +- .../JitBlue/Runtime_106338/Runtime_106338.cs | 36 +++++++++++++++++++ .../Runtime_106338/Runtime_106338.csproj | 8 +++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.csproj diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index f687f9139f598..50adf99687d37 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -15607,7 +15607,7 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree) case TYP_FLOAT: { -#if defined(TARGET_64BIT) +#ifdef TARGET_64BIT if (tree->IsUnsigned() && (lval1 < 0)) { f1 = FloatingPointUtils::convertUInt64ToFloat((uint64_t)lval1); diff --git a/src/coreclr/jit/utils.cpp b/src/coreclr/jit/utils.cpp index 209b23ac6f46a..cbee0be015818 100644 --- a/src/coreclr/jit/utils.cpp +++ b/src/coreclr/jit/utils.cpp @@ -2272,8 +2272,7 @@ double FloatingPointUtils::convertUInt64ToDouble(uint64_t uIntVal) float FloatingPointUtils::convertUInt64ToFloat(uint64_t u64) { - double d = convertUInt64ToDouble(u64); - return (float)d; + return (float)u64; } uint64_t FloatingPointUtils::convertDoubleToUInt64(double d) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.cs b/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.cs new file mode 100644 index 0000000000000..5cc1b75d42fda --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Generated by Fuzzlyn v2.2 on 2024-08-13 00:04:04 +// Run on Arm64 MacOS +// Seed: 13207615092246842583-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256 +// Reduced from 226.8 KiB to 0.4 KiB in 00:02:12 +// Debug: Outputs 1600094603 +// Release: Outputs 1600094604 +using System; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.X86; +using Xunit; + +public class Runtime_106338 +{ + [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/100368", TestPlatforms.Any)] + public static void TestEntryPoint() + { + ulong vr10 = 16105307123914158031UL; + float vr11 = 4294967295U | vr10; + uint result = BitConverter.SingleToUInt32Bits(vr11); + + if ((RuntimeInformation.ProcessArchitecture == Architecture.Arm64) || ((RuntimeInformation.ProcessArchitecture == Architecture.X64) && Avx512F.IsSupported)) + { + // Expected to cast ulong -> float directly + Assert.Equal(1600094603U, result); + } + else + { + // Expected to cast ulong -> double -> float + Assert.Equal(1600094604U, result); + } + } +} \ No newline at end of file diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.csproj new file mode 100644 index 0000000000000..de6d5e08882e8 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_106338/Runtime_106338.csproj @@ -0,0 +1,8 @@ + + + True + + + + +