Skip to content

Commit

Permalink
Add AllBitsSet DIM (#72961)
Browse files Browse the repository at this point in the history
* Add AllBitsSet Dim

* Apply suggestion from code review

* Add unit test for DIM

* Apply suggestions from code review

Co-authored-by: Tanner Gooding <tagoo@outlook.com>

* Apply suggestions from code review

* Fix typo causing compile errors

Co-authored-by: Tanner Gooding <tagoo@outlook.com>
  • Loading branch information
dakersnar and tannergooding authored Aug 12, 2022
1 parent c4ea2ca commit 5eb776d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IBinaryNumber<TSelf>
where TSelf : IBinaryNumber<TSelf>
{
/// <summary>Gets an instance of the binary type in which all bits are set.</summary>
static abstract TSelf AllBitsSet { get; } // TODO: add the DIM once https://github.com/dotnet/linker/issues/2865 is fixed
static virtual TSelf AllBitsSet => ~TSelf.Zero;

/// <summary>Determines if a value is a power of two.</summary>
/// <param name="value">The value to be checked.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10259,7 +10259,7 @@ public partial interface IBinaryInteger<TSelf> : System.IComparable, System.ICom
}
public partial interface IBinaryNumber<TSelf> : System.IComparable, System.IComparable<TSelf>, System.IEquatable<TSelf>, System.IFormattable, System.IParsable<TSelf>, System.ISpanFormattable, System.ISpanParsable<TSelf>, System.Numerics.IAdditionOperators<TSelf, TSelf, TSelf>, System.Numerics.IAdditiveIdentity<TSelf, TSelf>, System.Numerics.IBitwiseOperators<TSelf, TSelf, TSelf>, System.Numerics.IComparisonOperators<TSelf, TSelf, bool>, System.Numerics.IDecrementOperators<TSelf>, System.Numerics.IDivisionOperators<TSelf, TSelf, TSelf>, System.Numerics.IEqualityOperators<TSelf, TSelf, bool>, System.Numerics.IIncrementOperators<TSelf>, System.Numerics.IModulusOperators<TSelf, TSelf, TSelf>, System.Numerics.IMultiplicativeIdentity<TSelf, TSelf>, System.Numerics.IMultiplyOperators<TSelf, TSelf, TSelf>, System.Numerics.INumber<TSelf>, System.Numerics.INumberBase<TSelf>, System.Numerics.ISubtractionOperators<TSelf, TSelf, TSelf>, System.Numerics.IUnaryNegationOperators<TSelf, TSelf>, System.Numerics.IUnaryPlusOperators<TSelf, TSelf> where TSelf : System.Numerics.IBinaryNumber<TSelf>
{
static abstract TSelf AllBitsSet { get; }
static virtual TSelf AllBitsSet { get { throw null; } }
static abstract bool IsPow2(TSelf value);
static abstract TSelf Log2(TSelf value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@

<ItemGroup>
<Compile Include="$(CommonTestPath)System\GenericMathHelpers.cs" Link="Common\System\GenericMathHelpers.cs" />
<Compile Include="System\Numerics\GenericMathDimHelpers.cs" />
<Compile Include="System\Numerics\IBinaryNumberTests.cs" />
<Compile Include="System\ByteTests.GenericMath.cs" />
<Compile Include="System\CharTests.GenericMath.cs" />
<Compile Include="System\DecimalTests.GenericMath.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Numerics.Tests
{
public struct BinaryNumberDimHelper : IBinaryNumber<BinaryNumberDimHelper>
{
public int Value = 0;

private BinaryNumberDimHelper(int value)
{
Value = value;
}

static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.Zero => new BinaryNumberDimHelper(0);

static BinaryNumberDimHelper IBitwiseOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator ~(BinaryNumberDimHelper value)
{
return new BinaryNumberDimHelper(~value.Value);
}

//
// The below are all not used for existing Dim tests, so they stay unimplemented
//

static BinaryNumberDimHelper IMultiplicativeIdentity<BinaryNumberDimHelper, BinaryNumberDimHelper>.MultiplicativeIdentity => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.One => throw new NotImplementedException();
static int INumberBase<BinaryNumberDimHelper>.Radix => throw new NotImplementedException();
static BinaryNumberDimHelper IAdditiveIdentity<BinaryNumberDimHelper, BinaryNumberDimHelper>.AdditiveIdentity => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.Abs(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsCanonical(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsComplexNumber(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsEvenInteger(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsFinite(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsImaginaryNumber(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsInfinity(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsInteger(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsNaN(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsNegative(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsNegativeInfinity(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsNormal(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsOddInteger(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsPositive(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsPositiveInfinity(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool IBinaryNumber<BinaryNumberDimHelper>.IsPow2(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsRealNumber(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsSubnormal(BinaryNumberDimHelper value) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.IsZero(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper IBinaryNumber<BinaryNumberDimHelper>.Log2(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.MaxMagnitude(BinaryNumberDimHelper x, BinaryNumberDimHelper y) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.MaxMagnitudeNumber(BinaryNumberDimHelper x, BinaryNumberDimHelper y) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.MinMagnitude(BinaryNumberDimHelper x, BinaryNumberDimHelper y) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.MinMagnitudeNumber(BinaryNumberDimHelper x, BinaryNumberDimHelper y) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider) => throw new NotImplementedException();
static BinaryNumberDimHelper INumberBase<BinaryNumberDimHelper>.Parse(string s, NumberStyles style, IFormatProvider? provider) => throw new NotImplementedException();
static BinaryNumberDimHelper ISpanParsable<BinaryNumberDimHelper>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => throw new NotImplementedException();
static BinaryNumberDimHelper IParsable<BinaryNumberDimHelper>.Parse(string s, IFormatProvider? provider) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertToChecked<TOther>(BinaryNumberDimHelper value, out TOther? result) where TOther : default => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertToSaturating<TOther>(BinaryNumberDimHelper value, out TOther? result) where TOther : default => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertToTruncating<TOther>(BinaryNumberDimHelper value, out TOther? result) where TOther : default => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out BinaryNumberDimHelper result) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryParse(string? s, NumberStyles style, IFormatProvider? provider, out BinaryNumberDimHelper result) => throw new NotImplementedException();
static bool ISpanParsable<BinaryNumberDimHelper>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out BinaryNumberDimHelper result) => throw new NotImplementedException();
static bool IParsable<BinaryNumberDimHelper>.TryParse(string? s, IFormatProvider? provider, out BinaryNumberDimHelper result) => throw new NotImplementedException();
int IComparable.CompareTo(object? obj) => throw new NotImplementedException();
string IFormattable.ToString(string? format, IFormatProvider? formatProvider) => throw new NotImplementedException();
bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) => throw new NotImplementedException();
int IComparable<BinaryNumberDimHelper>.CompareTo(BinaryNumberDimHelper other) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertFromChecked<TOther>(TOther value, out BinaryNumberDimHelper result) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertFromSaturating<TOther>(TOther value, out BinaryNumberDimHelper result) => throw new NotImplementedException();
static bool INumberBase<BinaryNumberDimHelper>.TryConvertFromTruncating<TOther>(TOther value, out BinaryNumberDimHelper result) => throw new NotImplementedException();
bool IEquatable<BinaryNumberDimHelper>.Equals(BinaryNumberDimHelper other) => throw new NotImplementedException();

static BinaryNumberDimHelper IUnaryPlusOperators<BinaryNumberDimHelper, BinaryNumberDimHelper>.operator +(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper IAdditionOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator +(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IUnaryNegationOperators<BinaryNumberDimHelper, BinaryNumberDimHelper>.operator -(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper ISubtractionOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator -(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IIncrementOperators<BinaryNumberDimHelper>.operator ++(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper IDecrementOperators<BinaryNumberDimHelper>.operator --(BinaryNumberDimHelper value) => throw new NotImplementedException();
static BinaryNumberDimHelper IMultiplyOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator *(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IDivisionOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator /(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IModulusOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator %(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IBitwiseOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator &(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IBitwiseOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator |(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static BinaryNumberDimHelper IBitwiseOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, BinaryNumberDimHelper>.operator ^(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IEqualityOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator ==(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IEqualityOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator !=(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IComparisonOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator <(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IComparisonOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator >(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IComparisonOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator <=(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
static bool IComparisonOperators<BinaryNumberDimHelper, BinaryNumberDimHelper, bool>.operator >=(BinaryNumberDimHelper left, BinaryNumberDimHelper right) => throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Numerics.Tests
{
public sealed class IBinaryNumberTests
{
[Fact]
public static void AllBitsSetTest()
{
Assert.Equal(unchecked((int)0xFFFF_FFFF), BinaryNumberHelper<BinaryNumberDimHelper>.AllBitsSet.Value);
Assert.Equal(0, ~BinaryNumberHelper<BinaryNumberDimHelper>.AllBitsSet.Value);
}
}
}

0 comments on commit 5eb776d

Please sign in to comment.