diff --git a/src/libraries/System.Private.CoreLib/src/System/Int128.cs b/src/libraries/System.Private.CoreLib/src/System/Int128.cs index 01a54edd8029a..12cc6c7dec562 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int128.cs @@ -1904,7 +1904,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.Runtime.Tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs index f93a032e68d59..c2dd6490190d4 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs @@ -1646,6 +1646,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]