Skip to content

Commit 3c4b628

Browse files
pedrobsailastephentoubtannergooding
authored
UInt64.CreateSaturating<Int128> truncates instead of saturates (#96369)
* UInt64.CreateSaturating<Int128> truncates instead of saturates * Update src/libraries/System.Private.CoreLib/src/System/Int128.cs --------- Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: Tanner Gooding <tagoo@outlook.com>
1 parent 7de8f09 commit 3c4b628

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/libraries/System.Private.CoreLib/src/System/Int128.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,8 @@ static bool INumberBase<Int128>.TryConvertToSaturating<TOther>(Int128 value, [Ma
19041904
}
19051905
else if (typeof(TOther) == typeof(ulong))
19061906
{
1907-
ulong actualResult = (value <= 0) ? ulong.MinValue : (ulong)value;
1907+
ulong actualResult = (value >= ulong.MaxValue) ? ulong.MaxValue :
1908+
(value <= ulong.MinValue) ? ulong.MinValue : (ulong)value;
19081909
result = (TOther)(object)actualResult;
19091910
return true;
19101911
}

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,7 @@ public static void CreateSaturatingFromInt128Test()
16461646
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MaxValue));
16471647
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MinValue));
16481648
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.NegativeOne));
1649+
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>((Int128)ulong.MaxValue + (Int128)10L));
16491650
}
16501651

16511652
[Fact]

0 commit comments

Comments
 (0)