Skip to content

Commit

Permalink
Fix some generic math DIMs to have the correct behavior (#98510)
Browse files Browse the repository at this point in the history
* Fix the test BinaryIntegerWrapper relational operators

* Make sure the IBinaryFloatingPointIeee754 has their DIMs covered by tests

* Fix some DIMs to do the correct behavior

* Fix the TryConvertTo wrapper

* Ensure that TryConvertFrom handles the underlying T for the helper wrappers

* Minor test fix

* Update DimTests.GenericMath.cs
  • Loading branch information
tannergooding committed Mar 19, 2024
1 parent a184e7d commit 6f31582
Show file tree
Hide file tree
Showing 6 changed files with 1,084 additions and 671 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static virtual TSelf LeadingZeroCount(TSelf value)
return TSelf.CreateChecked(bitCount);
}

return (bitCount - TSelf.One) ^ TSelf.Log2(value);
return TSelf.IsNegative(value) ? TSelf.Zero : ((bitCount - TSelf.One) ^ TSelf.Log2(value));
}

/// <summary>Computes the number of bits that are set in a value.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,14 @@ static virtual TSelf Min(TSelf x, TSelf y)
// otherwise returns the larger of the inputs. It
// treats +0 as larger than -0 as per the specification.

if ((x != y) && !TSelf.IsNaN(x))
if (x != y)
{
return x < y ? x : y;
if (!TSelf.IsNaN(x))
{
return x < y ? x : y;
}

return x;
}

return TSelf.IsNegative(x) ? x : y;
Expand Down Expand Up @@ -160,6 +165,10 @@ static virtual int Sign(TSelf value)
{
if (value != TSelf.Zero)
{
if (TSelf.IsNaN(value))
{
ThrowHelper.ThrowArithmeticException(SR.Arithmetic_NaN);
}
return TSelf.IsNegative(value) ? -1 : +1;
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ namespace System
[StackTraceHidden]
internal static class ThrowHelper
{
[DoesNotReturn]
internal static void ThrowArithmeticException(string message)
{
throw new ArithmeticException(message);
}

[DoesNotReturn]
internal static void ThrowAccessViolationException()
{
Expand Down
Loading

0 comments on commit 6f31582

Please sign in to comment.