Skip to content

Commit

Permalink
WIP: add AllBitsSet to BigInteger with the wrong DIM, add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
dakersnar committed Jun 24, 2022
1 parent 5877e8b commit 4af1fa8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libraries/Common/tests/System/GenericMathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static class BinaryIntegerHelper<TSelf>
public static class BinaryNumberHelper<TSelf>
where TSelf : IBinaryNumber<TSelf>
{
public static TSelf AllBitsSet => TSelf.AllBitsSet;

public static bool IsPow2(TSelf value) => TSelf.IsPow2(value);

public static TSelf Log2(TSelf value) => TSelf.Log2(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public interface IBinaryNumber<TSelf>
INumber<TSelf>
where TSelf : IBinaryNumber<TSelf>
{
/// <summary>Gets an instance of the binary type in which all bits are set.</summary>
static virtual TSelf AllBitsSet => TSelf.One; // TODO: add the correct DIM when I am confident everything works

/// <summary>Determines if a value is a power of two.</summary>
/// <param name="value">The value to be checked.</param>
/// <returns><c>true</c> if <paramref name="value" /> is a power of two; otherwise, <c>false</c>.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace System.Numerics
public static bool IsNegative(System.Numerics.BigInteger value) { throw null; }
public static bool IsOddInteger(System.Numerics.BigInteger value) { throw null; }
public static bool IsPositive(System.Numerics.BigInteger value) { throw null; }
static BigInteger System.Numerics.IBinaryNumber<BigInteger>.AllBitsSet { get { throw null; } }
public static bool IsPow2(System.Numerics.BigInteger value) { throw null; }
public static System.Numerics.BigInteger LeadingZeroCount(System.Numerics.BigInteger value) { throw null; }
public static double Log(System.Numerics.BigInteger value) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3821,6 +3821,9 @@ private int GetGenericMathByteCount()
// IBinaryNumber
//

/// <inheritdoc cref="IBinaryNumber{TSelf}.AllBitsSet" />
static BigInteger IBinaryNumber<BigInteger>.AllBitsSet => MinusOne;

/// <inheritdoc cref="IBinaryNumber{TSelf}.IsPow2(TSelf)" />
public static bool IsPow2(BigInteger value) => value.IsPowerOfTwo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ public static void TryWriteLittleEndianTest()
// IBinaryNumber
//

[Fact]
public static void AllBitsSetTest()
{
Assert.Equal(NegativeOne, BinaryNumberHelper<BigInteger>.AllBitsSet);
}

[Fact]
public static void IsPow2Test()
{
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10382,6 +10382,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>, System.Numerics.IDecrementOperators<TSelf>, System.Numerics.IDivisionOperators<TSelf, TSelf, TSelf>, System.Numerics.IEqualityOperators<TSelf, TSelf>, 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 virtual TSelf AllBitsSet => throw null;
static abstract bool IsPow2(TSelf value);
static abstract TSelf Log2(TSelf value);
}
Expand Down

0 comments on commit 4af1fa8

Please sign in to comment.