Skip to content

Commit 6d35e39

Browse files
github-actions[bot]pedrobsailastephentoub
authored
[release/8.0-staging] UInt64.CreateSaturating<Int128> truncates instead of saturates (#97047)
* UInt64.CreateSaturating<Int128> truncates instead of saturates * Update src/libraries/System.Private.CoreLib/src/System/Int128.cs --------- Co-authored-by: pedrobsaila <badrebsaila@outlook.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 96b0a55 commit 6d35e39

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,8 @@ static bool INumberBase<Int128>.TryConvertToSaturating<TOther>(Int128 value, [Ma
18991899
}
19001900
else if (typeof(TOther) == typeof(ulong))
19011901
{
1902-
ulong actualResult = (value <= 0) ? ulong.MinValue : (ulong)value;
1902+
ulong actualResult = (value >= ulong.MaxValue) ? ulong.MaxValue :
1903+
(value <= ulong.MinValue) ? ulong.MinValue : (ulong)value;
19031904
result = (TOther)(object)actualResult;
19041905
return true;
19051906
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,7 @@ public static void CreateSaturatingFromInt128Test()
16421642
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MaxValue));
16431643
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MinValue));
16441644
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.NegativeOne));
1645+
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>((Int128)ulong.MaxValue + (Int128)10L));
16451646
}
16461647

16471648
[Fact]

0 commit comments

Comments
 (0)