Skip to content

Simplify TryConvert for Half, Int128 and UInt128 #116902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Jun 22, 2025

No description provided.

@Copilot Copilot AI review requested due to automatic review settings June 22, 2025 16:49
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 22, 2025
@jkotas jkotas requested a review from tannergooding June 22, 2025 16:49
@jkotas jkotas added area-System.Numerics and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 22, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR simplifies type conversions by removing explicit saturating logic for various numeric types and defaulting to direct casts, with conditional MONO-specific behavior added in some cases.

  • Eliminate manual bounds checks for UInt128 in Single and Double conversions
  • Introduce #if MONO blocks around HalfIntPtr, Int32, and Int64 conversions, defaulting to simple casts otherwise
  • Remove saturating logic for Int128 conversions in both saturating and truncating methods

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Single.cs Simplified UInt128 conversion to direct cast
src/libraries/System.Private.CoreLib/src/System/IntPtr.cs Added #if MONO for Half conversion, defaulted to simple cast
src/libraries/System.Private.CoreLib/src/System/Int64.cs Added #if MONO for Half conversion, defaulted to simple cast
src/libraries/System.Private.CoreLib/src/System/Int32.cs Added #if MONO for Half conversion, defaulted to simple cast
src/libraries/System.Private.CoreLib/src/System/Int128.cs Removed saturating bounds checks for double, Half, and float conversions
src/libraries/System.Private.CoreLib/src/System/Half.cs Added #if MONO for uint, ulong, and nuint conversions, defaulted to simple casts
src/libraries/System.Private.CoreLib/src/System/Double.cs Simplified UInt128 conversion to direct cast
Comments suppressed due to low confidence (17)

src/libraries/System.Private.CoreLib/src/System/Single.cs:1465

  • The change removes saturating behavior for PositiveInfinity and negative values, so casting alone may produce incorrect values. Consider reintroducing saturating logic to match previous behavior or ensure this change aligns with the intended specification.
                UInt128 actualResult = (UInt128)value;

src/libraries/System.Private.CoreLib/src/System/IntPtr.cs:1012

  • On non-MONO builds this drops the previous saturating handling for infinities; direct casting may underflow or overflow. Consider restoring saturating logic or verifying that unchecked casts meet the specification.
                result = (nint)actualValue;

src/libraries/System.Private.CoreLib/src/System/IntPtr.cs:1101

  • In the truncating path, infinities no longer saturate on non-MONO builds; direct cast may produce unexpected wrap‐around. Reintroduce saturating logic or confirm behavior change is acceptable.
                result = (nint)actualValue;

src/libraries/System.Private.CoreLib/src/System/Int64.cs:1017

  • The manual saturation for infinities is removed on non-MONO builds; a plain cast may overflow. Consider preserving saturating behavior to avoid incorrect boundary values.
                result = (long)actualValue;

src/libraries/System.Private.CoreLib/src/System/Int64.cs:1105

  • In the truncating conversion, infinities no longer saturate by default; direct casting can wrap. Verify that this change is intended or restore saturation logic.
                result = (long)actualValue;

src/libraries/System.Private.CoreLib/src/System/Int32.cs:1020

  • Dropped saturation for infinities on non-MONO builds could lead to incorrect overflow behavior. Re-add saturating logic or confirm cast behavior aligns with spec.
                result = (int)actualValue;

src/libraries/System.Private.CoreLib/src/System/Int32.cs:1110

  • The truncating path no longer saturates infinities by default; direct cast may wrap around. Ensure this change is intended or restore the previous saturating logic.
                result = (int)actualValue;

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

  • Removal of saturating checks for double inputs allows unbounded casts that can overflow. Consider restoring bound checks or ensure spec now expects wrap behavior.
                result = (Int128)actualValue;

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

  • Saturating logic for Half.PositiveInfinity and NegativeInfinity was removed. A straight cast may not match intended overflow behavior; re-evaluate this change.
                result = (Int128)actualValue;

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

  • Float conversion no longer saturates on extreme values; casting directly can overflow or wrap unexpectedly. Confirm this aligns with intended semantics.
                result = (Int128)actualValue;

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

  • In the truncating path for double, explicit saturation was removed; direct cast may wrap. Verify or restore saturating checks.
                result = (Int128)actualValue;

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

  • Truncating conversion for Half no longer saturates infinities; simple cast may yield incorrect boundary behavior. Consider reintroducing saturation.
                result = (Int128)actualValue;

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

  • The float truncating path lost its saturating logic; direct cast can overflow or wrap. Confirm intended behavior or restore the original checks.
                result = (Int128)actualValue;

src/libraries/System.Private.CoreLib/src/System/Half.cs:2129

  • By default, Halfuint conversion now skips saturating logic for infinities and NaN. A direct cast may not match desired behavior; consider preserving saturation.
                uint actualResult = (uint)value;

src/libraries/System.Private.CoreLib/src/System/Half.cs:2141

  • Removed default saturating behavior for Halfulong conversion; direct cast may overflow or mishandle NaN. Verify or reintroduce bounds checks.
                ulong actualResult = (ulong)value;

src/libraries/System.Private.CoreLib/src/System/Half.cs:2159

  • Default path skips saturation for Halfnuint, which may lead to incorrect wrapping on extreme values. Consider restoring saturating logic for consistency.
                nuint actualResult = (nuint)value;

src/libraries/System.Private.CoreLib/src/System/Double.cs:1446

  • Saturating logic for large double values and zero/negative checks was removed; direct cast may not handle infinities correctly. Reinstate saturation or confirm behavior change.
                UInt128 actualResult = (UInt128)value;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants