Skip to content

Commit b4f46ac

Browse files
Updating BigInteger to implement IBinaryInteger and ISignedNumber (#68964)
* Updating BigInteger to implement the generic math interfaces * Adding generic math tests for BigInteger * Addressing PR feedback
1 parent 6d23405 commit b4f46ac

File tree

7 files changed

+2962
-117
lines changed

7 files changed

+2962
-117
lines changed

src/libraries/System.Runtime.Numerics/ref/System.Runtime.Numerics.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace System.Numerics
88
{
9-
public readonly partial struct BigInteger : System.IComparable, System.IComparable<System.Numerics.BigInteger>, System.IEquatable<System.Numerics.BigInteger>, System.IFormattable, System.ISpanFormattable
9+
public readonly partial struct BigInteger : System.IComparable, System.IComparable<System.Numerics.BigInteger>, System.IEquatable<System.Numerics.BigInteger>, System.IFormattable, System.IParsable<System.Numerics.BigInteger>, System.ISpanFormattable, System.ISpanParsable<System.Numerics.BigInteger>, System.Numerics.IAdditionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IAdditiveIdentity<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IBinaryInteger<System.Numerics.BigInteger>, System.Numerics.IBinaryNumber<System.Numerics.BigInteger>, System.Numerics.IBitwiseOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IComparisonOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IDecrementOperators<System.Numerics.BigInteger>, System.Numerics.IDivisionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IEqualityOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IIncrementOperators<System.Numerics.BigInteger>, System.Numerics.IModulusOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IMultiplicativeIdentity<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IMultiplyOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.INumber<System.Numerics.BigInteger>, System.Numerics.INumberBase<System.Numerics.BigInteger>, System.Numerics.IShiftOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.ISignedNumber<System.Numerics.BigInteger>, System.Numerics.ISubtractionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IUnaryNegationOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>, System.Numerics.IUnaryPlusOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>
1010
{
1111
private readonly object _dummy;
1212
private readonly int _dummyPrimitive;
@@ -29,16 +29,25 @@ namespace System.Numerics
2929
public static System.Numerics.BigInteger MinusOne { get { throw null; } }
3030
public static System.Numerics.BigInteger One { get { throw null; } }
3131
public int Sign { get { throw null; } }
32+
static System.Numerics.BigInteger System.Numerics.IAdditiveIdentity<System.Numerics.BigInteger, System.Numerics.BigInteger>.AdditiveIdentity { get { throw null; } }
33+
static System.Numerics.BigInteger System.Numerics.IMultiplicativeIdentity<System.Numerics.BigInteger, System.Numerics.BigInteger>.MultiplicativeIdentity { get { throw null; } }
34+
static System.Numerics.BigInteger System.Numerics.ISignedNumber<System.Numerics.BigInteger>.NegativeOne { get { throw null; } }
3235
public static System.Numerics.BigInteger Zero { get { throw null; } }
3336
public static System.Numerics.BigInteger Abs(System.Numerics.BigInteger value) { throw null; }
3437
public static System.Numerics.BigInteger Add(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
38+
public static System.Numerics.BigInteger Clamp(System.Numerics.BigInteger value, System.Numerics.BigInteger min, System.Numerics.BigInteger max) { throw null; }
3539
public static int Compare(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
3640
public int CompareTo(long other) { throw null; }
3741
public int CompareTo(System.Numerics.BigInteger other) { throw null; }
3842
public int CompareTo(object? obj) { throw null; }
3943
[System.CLSCompliantAttribute(false)]
4044
public int CompareTo(ulong other) { throw null; }
45+
public static System.Numerics.BigInteger CopySign(System.Numerics.BigInteger value, System.Numerics.BigInteger sign) { throw null; }
46+
public static System.Numerics.BigInteger CreateChecked<TOther>(TOther value) where TOther : System.Numerics.INumber<TOther> { throw null; }
47+
public static System.Numerics.BigInteger CreateSaturating<TOther>(TOther value) where TOther : System.Numerics.INumber<TOther> { throw null; }
48+
public static System.Numerics.BigInteger CreateTruncating<TOther>(TOther value) where TOther : System.Numerics.INumber<TOther> { throw null; }
4149
public static System.Numerics.BigInteger Divide(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor) { throw null; }
50+
public static (System.Numerics.BigInteger Quotient, System.Numerics.BigInteger Remainder) DivRem(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
4251
public static System.Numerics.BigInteger DivRem(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor, out System.Numerics.BigInteger remainder) { throw null; }
4352
public bool Equals(long other) { throw null; }
4453
public bool Equals(System.Numerics.BigInteger other) { throw null; }
@@ -49,11 +58,17 @@ namespace System.Numerics
4958
public int GetByteCount(bool isUnsigned = false) { throw null; }
5059
public override int GetHashCode() { throw null; }
5160
public static System.Numerics.BigInteger GreatestCommonDivisor(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
61+
public static bool IsNegative(System.Numerics.BigInteger value) { throw null; }
62+
public static bool IsPow2(System.Numerics.BigInteger value) { throw null; }
63+
public static System.Numerics.BigInteger LeadingZeroCount(System.Numerics.BigInteger value) { throw null; }
5264
public static double Log(System.Numerics.BigInteger value) { throw null; }
5365
public static double Log(System.Numerics.BigInteger value, double baseValue) { throw null; }
5466
public static double Log10(System.Numerics.BigInteger value) { throw null; }
67+
public static System.Numerics.BigInteger Log2(System.Numerics.BigInteger value) { throw null; }
5568
public static System.Numerics.BigInteger Max(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
69+
public static System.Numerics.BigInteger MaxMagnitude(System.Numerics.BigInteger x, System.Numerics.BigInteger y) { throw null; }
5670
public static System.Numerics.BigInteger Min(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
71+
public static System.Numerics.BigInteger MinMagnitude(System.Numerics.BigInteger x, System.Numerics.BigInteger y) { throw null; }
5772
public static System.Numerics.BigInteger ModPow(System.Numerics.BigInteger value, System.Numerics.BigInteger exponent, System.Numerics.BigInteger modulus) { throw null; }
5873
public static System.Numerics.BigInteger Multiply(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
5974
public static System.Numerics.BigInteger Negate(System.Numerics.BigInteger value) { throw null; }
@@ -78,6 +93,7 @@ namespace System.Numerics
7893
public static explicit operator short (System.Numerics.BigInteger value) { throw null; }
7994
public static explicit operator int (System.Numerics.BigInteger value) { throw null; }
8095
public static explicit operator long (System.Numerics.BigInteger value) { throw null; }
96+
public static explicit operator nint (System.Numerics.BigInteger value) { throw null; }
8197
[System.CLSCompliantAttribute(false)]
8298
public static explicit operator sbyte (System.Numerics.BigInteger value) { throw null; }
8399
public static explicit operator float (System.Numerics.BigInteger value) { throw null; }
@@ -87,6 +103,8 @@ namespace System.Numerics
87103
public static explicit operator uint (System.Numerics.BigInteger value) { throw null; }
88104
[System.CLSCompliantAttribute(false)]
89105
public static explicit operator ulong (System.Numerics.BigInteger value) { throw null; }
106+
[System.CLSCompliantAttribute(false)]
107+
public static explicit operator nuint (System.Numerics.BigInteger value) { throw null; }
90108
public static explicit operator System.Numerics.BigInteger (float value) { throw null; }
91109
public static bool operator >(long left, System.Numerics.BigInteger right) { throw null; }
92110
public static bool operator >(System.Numerics.BigInteger left, long right) { throw null; }
@@ -106,6 +124,7 @@ namespace System.Numerics
106124
public static implicit operator System.Numerics.BigInteger (short value) { throw null; }
107125
public static implicit operator System.Numerics.BigInteger (int value) { throw null; }
108126
public static implicit operator System.Numerics.BigInteger (long value) { throw null; }
127+
public static implicit operator System.Numerics.BigInteger (nint value) { throw null; }
109128
[System.CLSCompliantAttribute(false)]
110129
public static implicit operator System.Numerics.BigInteger (sbyte value) { throw null; }
111130
[System.CLSCompliantAttribute(false)]
@@ -114,6 +133,8 @@ namespace System.Numerics
114133
public static implicit operator System.Numerics.BigInteger (uint value) { throw null; }
115134
[System.CLSCompliantAttribute(false)]
116135
public static implicit operator System.Numerics.BigInteger (ulong value) { throw null; }
136+
[System.CLSCompliantAttribute(false)]
137+
public static implicit operator System.Numerics.BigInteger (nuint value) { throw null; }
117138
public static System.Numerics.BigInteger operator ++(System.Numerics.BigInteger value) { throw null; }
118139
public static bool operator !=(long left, System.Numerics.BigInteger right) { throw null; }
119140
public static bool operator !=(System.Numerics.BigInteger left, long right) { throw null; }
@@ -144,24 +165,44 @@ namespace System.Numerics
144165
public static System.Numerics.BigInteger operator -(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
145166
public static System.Numerics.BigInteger operator -(System.Numerics.BigInteger value) { throw null; }
146167
public static System.Numerics.BigInteger operator +(System.Numerics.BigInteger value) { throw null; }
168+
public static System.Numerics.BigInteger operator >>>(System.Numerics.BigInteger value, int shiftAmount) { throw null; }
147169
public static System.Numerics.BigInteger Parse(System.ReadOnlySpan<char> value, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, System.IFormatProvider? provider = null) { throw null; }
170+
public static System.Numerics.BigInteger Parse(System.ReadOnlySpan<char> s, System.IFormatProvider? provider) { throw null; }
148171
public static System.Numerics.BigInteger Parse(string value) { throw null; }
149172
public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style) { throw null; }
150173
public static System.Numerics.BigInteger Parse(string value, System.Globalization.NumberStyles style, System.IFormatProvider? provider) { throw null; }
151174
public static System.Numerics.BigInteger Parse(string value, System.IFormatProvider? provider) { throw null; }
175+
public static System.Numerics.BigInteger PopCount(System.Numerics.BigInteger value) { throw null; }
152176
public static System.Numerics.BigInteger Pow(System.Numerics.BigInteger value, int exponent) { throw null; }
153177
public static System.Numerics.BigInteger Remainder(System.Numerics.BigInteger dividend, System.Numerics.BigInteger divisor) { throw null; }
178+
public static System.Numerics.BigInteger RotateLeft(System.Numerics.BigInteger value, int rotateAmount) { throw null; }
179+
public static System.Numerics.BigInteger RotateRight(System.Numerics.BigInteger value, int rotateAmount) { throw null; }
154180
public static System.Numerics.BigInteger Subtract(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
181+
static System.Numerics.BigInteger System.Numerics.IAdditionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>.operator checked +(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
182+
int System.Numerics.IBinaryInteger<System.Numerics.BigInteger>.GetByteCount() { throw null; }
183+
long System.Numerics.IBinaryInteger<System.Numerics.BigInteger>.GetShortestBitLength() { throw null; }
184+
bool System.Numerics.IBinaryInteger<System.Numerics.BigInteger>.TryWriteLittleEndian(System.Span<byte> destination, out int bytesWritten) { throw null; }
185+
static System.Numerics.BigInteger System.Numerics.IDecrementOperators<System.Numerics.BigInteger>.operator checked --(System.Numerics.BigInteger value) { throw null; }
186+
static System.Numerics.BigInteger System.Numerics.IDivisionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>.operator checked /(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
187+
static System.Numerics.BigInteger System.Numerics.IIncrementOperators<System.Numerics.BigInteger>.operator checked ++(System.Numerics.BigInteger value) { throw null; }
188+
static System.Numerics.BigInteger System.Numerics.IMultiplyOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>.operator checked *(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
189+
static int System.Numerics.INumber<System.Numerics.BigInteger>.Sign(System.Numerics.BigInteger value) { throw null; }
190+
static System.Numerics.BigInteger System.Numerics.ISubtractionOperators<System.Numerics.BigInteger, System.Numerics.BigInteger, System.Numerics.BigInteger>.operator checked -(System.Numerics.BigInteger left, System.Numerics.BigInteger right) { throw null; }
191+
static System.Numerics.BigInteger System.Numerics.IUnaryNegationOperators<System.Numerics.BigInteger, System.Numerics.BigInteger>.operator checked -(System.Numerics.BigInteger value) { throw null; }
155192
public byte[] ToByteArray() { throw null; }
156193
public byte[] ToByteArray(bool isUnsigned = false, bool isBigEndian = false) { throw null; }
157194
public override string ToString() { throw null; }
158195
public string ToString(System.IFormatProvider? provider) { throw null; }
159196
public string ToString([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("NumericFormat")] string? format) { throw null; }
160197
public string ToString([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("NumericFormat")] string? format, System.IFormatProvider? provider) { throw null; }
198+
public static System.Numerics.BigInteger TrailingZeroCount(System.Numerics.BigInteger value) { throw null; }
199+
public static bool TryCreate<TOther>(TOther value, out System.Numerics.BigInteger result) where TOther : System.Numerics.INumber<TOther> { throw null; }
161200
public bool TryFormat(System.Span<char> destination, out int charsWritten, [System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("NumericFormat")] System.ReadOnlySpan<char> format = default(System.ReadOnlySpan<char>), System.IFormatProvider? provider = null) { throw null; }
162201
public static bool TryParse(System.ReadOnlySpan<char> value, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; }
202+
public static bool TryParse(System.ReadOnlySpan<char> s, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; }
163203
public static bool TryParse(System.ReadOnlySpan<char> value, out System.Numerics.BigInteger result) { throw null; }
164204
public static bool TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? value, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; }
205+
public static bool TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? s, System.IFormatProvider? provider, out System.Numerics.BigInteger result) { throw null; }
165206
public static bool TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? value, out System.Numerics.BigInteger result) { throw null; }
166207
public bool TryWriteBytes(System.Span<byte> destination, out int bytesWritten, bool isUnsigned = false, bool isBigEndian = false) { throw null; }
167208
}

src/libraries/System.Runtime.Numerics/src/Resources/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@
6666
<data name="Argument_InvalidHexStyle" xml:space="preserve">
6767
<value>With the AllowHexSpecifier bit set in the enum bit field, the only other valid bits that can be combined into the enum value must be a subset of those in HexNumber.</value>
6868
</data>
69+
<data name="Argument_MinMaxValue" xml:space="preserve">
70+
<value>'{0}' cannot be greater than {1}.</value>
71+
</data>
6972
<data name="Argument_MustBeBigInt" xml:space="preserve">
7073
<value>The parameter must be a BigInteger.</value>
7174
</data>
75+
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
76+
<value>Non-negative number required.</value>
77+
</data>
7278
<data name="Format_TooLarge" xml:space="preserve">
7379
<value>The value is too large to be represented by this format specifier.</value>
7480
</data>

src/libraries/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<RootNamespace>System.Numerics</RootNamespace>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
55
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
66
</PropertyGroup>
77
<ItemGroup>
8+
<Compile Include="System\ThrowHelper.cs" />
89
<Compile Include="System\Numerics\BigIntegerCalculator.AddSub.cs" />
910
<Compile Include="System\Numerics\BigIntegerCalculator.DivRem.cs" />
1011
<Compile Include="System\Numerics\BigIntegerCalculator.FastReducer.cs" />
@@ -18,13 +19,15 @@
1819
<Compile Include="System\Numerics\Complex.cs" />
1920
<Compile Include="System\Globalization\FormatProvider.BigInteger.cs" />
2021
<Compile Include="System\Globalization\FormatProvider.NumberBuffer.cs" />
22+
<Compile Include="Properties\InternalsVisibleTo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
2125
<Compile Include="$(CommonPath)System\Globalization\FormatProvider.Number.cs"
2226
Link="System\Globalization\FormatProvider.Number.cs" />
2327
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
2428
Link="CoreLib\System\Text\ValueStringBuilder.cs" />
2529
<Compile Include="$(CommonPath)System\HexConverter.cs"
2630
Link="Common\System\HexConverter.cs" />
27-
<Compile Include="Properties\InternalsVisibleTo.cs" />
2831
</ItemGroup>
2932
<ItemGroup>
3033
<Reference Include="System.Memory" />

0 commit comments

Comments
 (0)