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

Fix some generic math DIMs to have the correct behavior #98510

Merged
merged 8 commits into from
Mar 19, 2024
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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log2 throws for negative integer values, so we need to explicitly check "is negative"

}

/// <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);
}
Comment on lines +168 to +171
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign is supposed to throw for NaN (unlike CopySign which does not). It's a historical behavior/contract of the API.

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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser</TargetFrameworks>
<DefineConstants Condition="'$(TargetOS)' == 'browser'">$(DefineConstants);TARGET_BROWSER</DefineConstants>
Expand Down Expand Up @@ -292,6 +292,7 @@

<ItemGroup>
<Compile Include="$(CommonTestPath)System\GenericMathHelpers.cs" Link="Common\System\GenericMathHelpers.cs" />
<Compile Include="System\Numerics\DimTests.GenericMath.cs" />
<Compile Include="System\Numerics\GenericMathDimHelpers.cs" />
<Compile Include="System\Numerics\IBinaryNumberTests.cs" />
<Compile Include="System\ByteTests.GenericMath.cs" />
Expand Down
Loading
Loading