Skip to content
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

Simplify and optimize Math(F).Round #98186

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Change AdvSimd check
  • Loading branch information
MichalPetryka committed Feb 8, 2024
commit c82d47d1cfc6b8fb1904e67a83fc831edf3812a4
8 changes: 3 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,12 +1302,10 @@ public static double Round(double value, MidpointRounding mode)
{
// Rounds to the nearest value; if the number falls midway,
// it is rounded to the nearest value above (for positive numbers) or below (for negative numbers)
#pragma warning disable IntrinsicsInSystemPrivateCoreLib
case MidpointRounding.AwayFromZero when AdvSimd.IsSupported:
// For ARM/ARM64 we can lower it down to a single instruction FRINTA
return AdvSimd.RoundAwayFromZeroScalar(Vector64.CreateScalarUnsafe(value)).ToScalar();
#pragma warning restore IntrinsicsInSystemPrivateCoreLib
case MidpointRounding.AwayFromZero:
// For ARM/ARM64 we can lower it down to a single instruction FRINTA
if (AdvSimd.IsSupported)
return AdvSimd.RoundAwayFromZeroScalar(Vector64.CreateScalarUnsafe(value)).ToScalar();
// For other platforms we use a fast managed implementation
// manually fold BitDecrement(0.5)
return Truncate(value + CopySign(0.49999999999999994, value));
Expand Down
8 changes: 3 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/MathF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,10 @@ public static float Round(float x, MidpointRounding mode)
{
// Rounds to the nearest value; if the number falls midway,
// it is rounded to the nearest value above (for positive numbers) or below (for negative numbers)
#pragma warning disable IntrinsicsInSystemPrivateCoreLib
case MidpointRounding.AwayFromZero when AdvSimd.IsSupported:
// For ARM/ARM64 we can lower it down to a single instruction FRINTA
return AdvSimd.RoundAwayFromZeroScalar(Vector64.CreateScalarUnsafe(x)).ToScalar();
#pragma warning restore IntrinsicsInSystemPrivateCoreLib
case MidpointRounding.AwayFromZero:
// For ARM/ARM64 we can lower it down to a single instruction FRINTA
if (AdvSimd.IsSupported)
return AdvSimd.RoundAwayFromZeroScalar(Vector64.CreateScalarUnsafe(x)).ToScalar();
// For other platforms we use a fast managed implementation
// manually fold BitDecrement(0.5)
return Truncate(x + CopySign(0.49999997f, x));
Expand Down
Loading