Skip to content

Commit

Permalink
UInt64.CreateSaturating<Int128> truncates instead of saturates (dotne…
Browse files Browse the repository at this point in the history
…t#96369)

* UInt64.CreateSaturating<Int128> truncates instead of saturates

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

---------

Co-authored-by: Stephen Toub <[email protected]>
Co-authored-by: Tanner Gooding <[email protected]>
  • Loading branch information
3 people authored and tmds committed Jan 23, 2024
1 parent 7f93f72 commit 6160277
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ static bool INumberBase<Int128>.TryConvertToSaturating<TOther>(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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ public static void CreateSaturatingFromInt128Test()
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MaxValue));
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MinValue));
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.NegativeOne));
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>((Int128)ulong.MaxValue + (Int128)10L));
}

[Fact]
Expand Down

0 comments on commit 6160277

Please sign in to comment.