| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- // Licensed to the .NET Foundation under one or more agreements.
- // 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
- **
- ===========================================================*/
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- namespace System
- {
- public static partial class MathF
- {
- public const float E = 2.71828183f;
- public const float PI = 3.14159265f;
- private const int maxRoundingDigits = 6;
- // This table is required for the Round function which can specify the number of digits to round to
- private static readonly float[] roundPower10Single = new float[] {
- 1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f
- };
- private const float singleRoundLimit = 1e8f;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Abs(float x)
- {
- return Math.Abs(x);
- }
- public static float BitDecrement(float x)
- {
- int bits = BitConverter.SingleToInt32Bits(x);
- if ((bits & 0x7F800000) >= 0x7F800000)
- {
- // NaN returns NaN
- // -Infinity returns -Infinity
- // +Infinity returns float.MaxValue
- return (bits == 0x7F800000) ? float.MaxValue : x;
- }
- if (bits == 0x00000000)
- {
- // +0.0 returns -float.Epsilon
- return -float.Epsilon;
- }
- // Negative values need to be incremented
- // Positive values need to be decremented
- bits += ((bits < 0) ? +1 : -1);
- return BitConverter.Int32BitsToSingle(bits);
- }
- public static float BitIncrement(float x)
- {
- int bits = BitConverter.SingleToInt32Bits(x);
- if ((bits & 0x7F800000) >= 0x7F800000)
- {
- // NaN returns NaN
- // -Infinity returns float.MinValue
- // +Infinity returns +Infinity
- return (bits == unchecked((int)(0xFF800000))) ? float.MinValue : x;
- }
- if (bits == unchecked((int)(0x80000000)))
- {
- // -0.0 returns float.Epsilon
- return float.Epsilon;
- }
- // Negative values need to be decremented
- // Positive values need to be incremented
- bits += ((bits < 0) ? -1 : +1);
- return BitConverter.Int32BitsToSingle(bits);
- }
- public static unsafe float CopySign(float x, float y)
- {
- // This method is required to work for all inputs,
- // including NaN, so we operate on the raw bits.
- int xbits = BitConverter.SingleToInt32Bits(x);
- int ybits = BitConverter.SingleToInt32Bits(y);
- // If the sign bits of x and y are not the same,
- // flip the sign bit of x and return the new value;
- // otherwise, just return x
- if ((xbits ^ ybits) < 0)
- {
- return BitConverter.Int32BitsToSingle(xbits ^ int.MinValue);
- }
- return x;
- }
- public static float IEEERemainder(float x, float y)
- {
- if (float.IsNaN(x))
- {
- return x; // IEEE 754-2008: NaN payload must be preserved
- }
- if (float.IsNaN(y))
- {
- return y; // IEEE 754-2008: NaN payload must be preserved
- }
- float regularMod = x % y;
- if (float.IsNaN(regularMod))
- {
- return float.NaN;
- }
- if ((regularMod == 0) && float.IsNegative(x))
- {
- return float.NegativeZero;
- }
- float alternativeResult = (regularMod - (Abs(y) * Sign(x)));
- if (Abs(alternativeResult) == Abs(regularMod))
- {
- float divisionResult = x / y;
- float roundedResult = Round(divisionResult);
- if (Abs(roundedResult) > Abs(divisionResult))
- {
- return alternativeResult;
- }
- else
- {
- return regularMod;
- }
- }
- if (Abs(alternativeResult) < Abs(regularMod))
- {
- return alternativeResult;
- }
- else
- {
- return regularMod;
- }
- }
- public static float Log(float x, float y)
- {
- if (float.IsNaN(x))
- {
- return x; // IEEE 754-2008: NaN payload must be preserved
- }
- if (float.IsNaN(y))
- {
- return y; // IEEE 754-2008: NaN payload must be preserved
- }
- if (y == 1)
- {
- return float.NaN;
- }
- if ((x != 1) && ((y == 0) || float.IsPositiveInfinity(y)))
- {
- return float.NaN;
- }
- return Log(x) / Log(y);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Max(float x, float y)
- {
- return Math.Max(x, y);
- }
- public static float MaxMagnitude(float x, float y)
- {
- // This matches the IEEE 754:2019 `maximumMagnitude` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the input with a larger magnitude.
- // It treats +0 as larger than -0 as per the specification.
- float ax = Abs(x);
- float ay = Abs(y);
- if ((ax > ay) || float.IsNaN(ax))
- {
- return x;
- }
- if (ax == ay)
- {
- return float.IsNegative(x) ? y : x;
- }
- return y;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Min(float x, float y)
- {
- return Math.Min(x, y);
- }
- public static float MinMagnitude(float x, float y)
- {
- // This matches the IEEE 754:2019 `minimumMagnitude` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the input with a larger magnitude.
- // It treats +0 as larger than -0 as per the specification.
- float ax = Abs(x);
- float ay = Abs(y);
- if ((ax < ay) || float.IsNaN(ax))
- {
- return x;
- }
- if (ax == ay)
- {
- return float.IsNegative(x) ? x : y;
- }
- return y;
- }
- [Intrinsic]
- public static float Round(float x)
- {
- // ************************************************************************************
- // IMPORTANT: Do not change this implementation without also updating MathF.Round(float),
- // FloatingPointUtils::round(double), and FloatingPointUtils::round(float)
- // ************************************************************************************
- // This is based on the 'Berkeley SoftFloat Release 3e' algorithm
- uint bits = (uint)BitConverter.SingleToInt32Bits(x);
- int exponent = float.ExtractExponentFromBits(bits);
- if (exponent <= 0x7E)
- {
- if ((bits << 1) == 0)
- {
- // Exactly +/- zero should return the original value
- return x;
- }
- // Any value less than or equal to 0.5 will always round to exactly zero
- // and any value greater than 0.5 will always round to exactly one. However,
- // we need to preserve the original sign for IEEE compliance.
- float result = ((exponent == 0x7E) && (float.ExtractSignificandFromBits(bits) != 0)) ? 1.0f : 0.0f;
- return CopySign(result, 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;
- }
- // The absolute value should be greater than or equal to 1.0 and less than 2^23
- Debug.Assert((0x7F <= exponent) && (exponent <= 0x95));
- // Determine the last bit that represents the integral portion of the value
- // and the bits representing the fractional portion
- 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
- {
- // Otherwise, we just want to strip the fractional bits off, truncating
- // to the current integer value.
- bits &= ~roundBitsMask;
- }
- return BitConverter.Int32BitsToSingle((int)bits);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Round(float x, int digits)
- {
- return Round(x, digits, MidpointRounding.ToEven);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Round(float x, MidpointRounding mode)
- {
- return Round(x, 0, mode);
- }
- public static unsafe float Round(float x, int digits, MidpointRounding mode)
- {
- if ((digits < 0) || (digits > maxRoundingDigits))
- {
- throw new ArgumentOutOfRangeException(nameof(digits), SR.ArgumentOutOfRange_RoundingDigits);
- }
- if (mode < MidpointRounding.ToEven || mode > MidpointRounding.ToPositiveInfinity)
- {
- throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, nameof(MidpointRounding)), nameof(mode));
- }
- if (Abs(x) < singleRoundLimit)
- {
- float power10 = roundPower10Single[digits];
- x *= power10;
- switch (mode)
- {
- // Rounds to the nearest value; if the number falls midway,
- // it is rounded to the nearest value with an even least significant digit
- case MidpointRounding.ToEven:
- {
- x = Round(x);
- break;
- }
- // 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)
- case MidpointRounding.AwayFromZero:
- {
- float fraction = ModF(x, &x);
- if (Abs(fraction) >= 0.5)
- {
- x += Sign(fraction);
- }
- break;
- }
- // Directed rounding: Round to the nearest value, toward to zero
- case MidpointRounding.ToZero:
- {
- x = Truncate(x);
- break;
- }
- // Directed Rounding: Round down to the next value, toward negative infinity
- case MidpointRounding.ToNegativeInfinity:
- {
- x = Floor(x);
- break;
- }
- // Directed rounding: Round up to the next value, toward positive infinity
- case MidpointRounding.ToPositiveInfinity:
- {
- x = Ceiling(x);
- break;
- }
- default:
- {
- throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, nameof(MidpointRounding)), nameof(mode));
- }
- }
- x /= power10;
- }
- return x;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Sign(float x)
- {
- return Math.Sign(x);
- }
- public static unsafe float Truncate(float x)
- {
- ModF(x, &x);
- return x;
- }
- }
- }
|