Skip to content

Simplify generic floating point->integer conversions #116896

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

Floating point to integer conversions for 32-bit and 64-bit integers have predictable truncating behavior on all platforms now. The manual truncation should be unnecessary.

@Copilot Copilot AI review requested due to automatic review settings June 22, 2025 03:13
@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 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

@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 generic floating point to integer conversions by removing manual saturation logic and replacing it with direct casts that now rely on predictable truncation behavior.

  • The conversion methods in Single.cs, IntPtr.cs, Int64.cs, Int32.cs, and Double.cs have been simplified by removing conditional checks for saturation.
  • All conversion paths now perform a straightforward cast, expecting consistent truncation behavior across platforms.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
System/Single.cs Removed saturation conditions for uint, ulong, and nuint conversions in favor of direct casts.
System/IntPtr.cs Simplified both saturating and truncating conversions from double and float to nint using direct casts.
System/Int64.cs Replaced saturating logic with direct casts in conversion methods from double and float to long.
System/Int32.cs Removed conditional saturation logic for both double and float conversions to int.
System/Double.cs Updated uint, ulong, and nuint conversion paths to use direct casts instead of manual saturation.
Comments suppressed due to low confidence (17)

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

  • The direct cast simplifies the conversion as expected; please ensure that the updated behavior for out-of-range values is covered by tests.
                uint actualResult = (uint)value;

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

  • Replacing the saturation logic with a direct cast is consistent with the new truncation guarantees; confirm this behavior in edge cases.
                ulong actualResult = (ulong)value;

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

  • Ensure that using a direct cast for nuint conversion meets the expected behavior on both 32-bit and 64-bit platforms.
                nuint actualResult = (nuint)value;

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

  • The direct cast from double to nint is appropriate now; please verify that all edge-case behaviors align with the expected truncation semantics.
                result = (nint)actualValue;

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

  • Using a direct cast for float to nint conversion is clear; double-check that extreme float values are handled as intended.
                result = (nint)actualValue;

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

  • Simplifying the double to nint conversion to a direct cast is logical; ensure that this change is validated against all expected truncation behaviors.
                result = (nint)actualValue;

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

  • The direct cast in the float conversion within the truncating method is concise; please confirm that tests validate the correct handling of boundary values.
                result = (nint)actualValue;

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

  • Directly casting double to long simplifies the logic; verify that the behavior for values outside the long range is acceptable given the new truncation rules.
                result = (long)actualValue;

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

  • Replacing conditional checks with a direct cast for float to long conversion is straightforward; ensure that regression tests cover any potential boundary conditions.
                result = (long)actualValue;

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

  • Using a direct cast in the double conversion for truncation clarifies the logic; please double-check that it behaves as specified with extreme inputs.
                result = (long)actualValue;

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

  • This direct cast for float to long conversion is concise; ensure that truncation behavior remains consistent across all relevant scenarios.
                result = (long)actualValue;

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

  • Simplifying the double to int conversion with a direct cast is clear; please confirm that this change meets the updated truncation behavior requirements.
                result = (int)actualValue;

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

  • The direct cast in the double conversion for truncation is appropriate; ensure that test cases validate handling of extreme values.
                result = (int)actualValue;

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

  • Direct casting for float to int conversion streamlines the logic; please verify that boundary cases are adequately tested.
                result = (int)actualValue;

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

  • The use of a direct cast for converting double to uint meets the new truncation expectations; ensure documentation reflects this change.
                uint actualResult = (uint)value;

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

  • Simplifying the ulong conversion by removing saturation conditions is in line with updated behavior; confirm that this change is covered by appropriate tests.
                ulong actualResult = (ulong)value;

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

  • Directly casting to nuint is a clean simplification; please ensure that the resulting behavior for extreme values is validated.
                nuint actualResult = (nuint)value;

Copy link
Contributor

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

@jkotas jkotas force-pushed the simplify-convert branch 2 times, most recently from 39ad2f4 to 9967441 Compare June 22, 2025 04:58
@dotnet dotnet deleted a comment from Copilot AI Jun 22, 2025
Floating point to integer conversions for 32-bit and 64-bit integers have predictable truncating behavior on all platforms now. The manual truncation should be unnecessary.

Keep the existing behavior for Mono since Mono has not been fixed to have the predictable truncating behavior.
@jkotas jkotas merged commit 56a3e2d into dotnet:main Jun 22, 2025
136 of 142 checks passed
@jkotas jkotas deleted the simplify-convert branch June 22, 2025 14:20
Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

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

LGTM. Just noting the same simplification should be possible for System.Half, Int128, and UInt128 as well.

@jkotas
Copy link
Member Author

jkotas commented Jun 22, 2025

Just noting the same simplification should be possible for System.Half, Int128, and UInt128 as well.

#116902

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