From 6d35e39f4f249c627c11586f4769361bde0ed62d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 16 Jan 2024 17:14:57 -0800 Subject: [PATCH] [release/8.0-staging] UInt64.CreateSaturating truncates instead of saturates (#97047) * UInt64.CreateSaturating truncates instead of saturates * Update src/libraries/System.Private.CoreLib/src/System/Int128.cs --------- Co-authored-by: pedrobsaila Co-authored-by: Stephen Toub --- src/libraries/System.Private.CoreLib/src/System/Int128.cs | 3 ++- .../System.Runtime/tests/System/UInt64Tests.GenericMath.cs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Int128.cs b/src/libraries/System.Private.CoreLib/src/System/Int128.cs index 50dce177e17135..fae7c1b441457b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int128.cs @@ -1899,7 +1899,8 @@ static bool INumberBase.TryConvertToSaturating(Int128 value, [Ma } else if (typeof(TOther) == typeof(ulong)) { - ulong actualResult = (value <= 0) ? ulong.MinValue : (ulong)value; + ulong actualResult = (value >= ulong.MaxValue) ? ulong.MaxValue : + (value <= ulong.MinValue) ? ulong.MinValue : (ulong)value; result = (TOther)(object)actualResult; return true; } diff --git a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs index 89639038fd4eeb..614dd54b10255e 100644 --- a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs @@ -1642,6 +1642,7 @@ public static void CreateSaturatingFromInt128Test() Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper.CreateSaturating(Int128.MaxValue)); Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper.CreateSaturating(Int128.MinValue)); Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper.CreateSaturating(Int128.NegativeOne)); + Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper.CreateSaturating((Int128)ulong.MaxValue + (Int128)10L)); } [Fact]