Skip to content

Commit 6f31582

Browse files
Fix some generic math DIMs to have the correct behavior (#98510)
* 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
1 parent a184e7d commit 6f31582

File tree

6 files changed

+1084
-671
lines changed

6 files changed

+1084
-671
lines changed

src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static virtual TSelf LeadingZeroCount(TSelf value)
3737
return TSelf.CreateChecked(bitCount);
3838
}
3939

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

4343
/// <summary>Computes the number of bits that are set in a value.</summary>

src/libraries/System.Private.CoreLib/src/System/Numerics/INumber.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,14 @@ static virtual TSelf Min(TSelf x, TSelf y)
118118
// otherwise returns the larger of the inputs. It
119119
// treats +0 as larger than -0 as per the specification.
120120

121-
if ((x != y) && !TSelf.IsNaN(x))
121+
if (x != y)
122122
{
123-
return x < y ? x : y;
123+
if (!TSelf.IsNaN(x))
124+
{
125+
return x < y ? x : y;
126+
}
127+
128+
return x;
124129
}
125130

126131
return TSelf.IsNegative(x) ? x : y;
@@ -160,6 +165,10 @@ static virtual int Sign(TSelf value)
160165
{
161166
if (value != TSelf.Zero)
162167
{
168+
if (TSelf.IsNaN(value))
169+
{
170+
ThrowHelper.ThrowArithmeticException(SR.Arithmetic_NaN);
171+
}
163172
return TSelf.IsNegative(value) ? -1 : +1;
164173
}
165174
return 0;

src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ namespace System
5353
[StackTraceHidden]
5454
internal static class ThrowHelper
5555
{
56+
[DoesNotReturn]
57+
internal static void ThrowArithmeticException(string message)
58+
{
59+
throw new ArithmeticException(message);
60+
}
61+
5662
[DoesNotReturn]
5763
internal static void ThrowAccessViolationException()
5864
{

0 commit comments

Comments
 (0)