This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
Updating Math.Round and MathF.Round to be IEEE compliant so that the intrinsic and managed form are deterministic. #25901
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// =================================================================================================== | ||
// Portions of the code implemented below are based on the 'Berkeley SoftFloat Release 3e' algorithms. | ||
// =================================================================================================== | ||
|
||
/*============================================================ | ||
** | ||
** Purpose: Some single-precision floating-point math operations | ||
|
@@ -10,6 +14,7 @@ | |
|
||
//This class contains only static members and doesn't require serialization. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System | ||
|
@@ -245,29 +250,61 @@ public static float MinMagnitude(float x, float y) | |
public static float Round(float x) | ||
{ | ||
// ************************************************************************************ | ||
// IMPORTANT: Do not change this implementation without also updating Math.Round(double), | ||
// IMPORTANT: Do not change this implementation without also updating MathF.Round(float), | ||
// FloatingPointUtils::round(double), and FloatingPointUtils::round(float) | ||
// ************************************************************************************ | ||
|
||
// If the number has no fractional part do nothing | ||
// This shortcut is necessary to workaround precision loss in borderline cases on some platforms | ||
|
||
if (x == (float)((int)x)) | ||
// This is based on the 'Berkeley SoftFloat Release 3e' algorithm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference: https://github.com/ucb-bar/berkeley-softfloat-3/blob/master/source/f32_roundToInt.c The irrelevant support for the other rounding modes and error reporting was not included. |
||
|
||
uint bits = (uint)BitConverter.SingleToInt32Bits(x); | ||
int exponent = float.ExtractExponentFromBits(bits); | ||
|
||
if (exponent <= 0x7E) | ||
{ | ||
// Any value less than or equal to 0.5 will always round to exactly zero. | ||
// However, we need to preserve the original sign for IEEE compliance. | ||
|
||
return CopySign(0, x); | ||
} | ||
|
||
if (exponent >= 0x96) | ||
{ | ||
// Any value greater than or equal to 2^23 cannot have a fractional part, | ||
// So it will always round to exactly itself. | ||
|
||
return x; | ||
} | ||
|
||
// We had a number that was equally close to 2 integers. | ||
// We need to return the even one. | ||
// The absolute value should be greater than or equal to 1.0 and less than 2^23 | ||
Debug.Assert((0x7F <= exponent) && (exponent <= 0x95)); | ||
|
||
float flrTempVal = Floor(x + 0.5f); | ||
// Determine the last bit that represents the integral portion of the value | ||
// and the bits representing the fractional portion | ||
|
||
if ((x == (Floor(x) + 0.5f)) && (FMod(flrTempVal, 2.0f) != 0)) | ||
uint lastBitMask = 1U << (0x96 - exponent); | ||
uint roundBitsMask = lastBitMask - 1; | ||
|
||
// Increment the first fractional bit, which represents the midpoint between | ||
// two integral values in the current window. | ||
|
||
bits += lastBitMask >> 1; | ||
|
||
if ((bits & roundBitsMask) == 0) | ||
{ | ||
// If that overflowed and the rest of the fractional bits are zero | ||
// then we were exactly x.5 and we want to round to the even result | ||
|
||
bits &= ~lastBitMask; | ||
} | ||
else | ||
{ | ||
flrTempVal -= 1.0f; | ||
// Otherwise, we just want to strip the fractional bits off, truncating | ||
// to the current integer value. | ||
|
||
bits &= ~roundBitsMask; | ||
} | ||
|
||
return CopySign(flrTempVal, x); | ||
return BitConverter.Int32BitsToSingle((int)bits); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference: https://github.com/ucb-bar/berkeley-softfloat-3/blob/master/source/f64_roundToInt.c
The irrelevant support for the other rounding modes and error reporting was not included.
Code comments were added for readability, they do not exist in the reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth adding that note (i.e. that the other rounding modes from that algorithm are not included).