Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3c5f7b9
Create INumberBase and allow Complex to implement it
tannergooding Mar 27, 2022
0eff505
Move DivRem to only be available for IBinaryInteger
tannergooding Mar 27, 2022
75d1ec4
Split apart various floating-point interfaces for better extensibility
tannergooding Mar 28, 2022
5b86605
Annotate the generic math interfaces to implement on BigInteger and C…
tannergooding Mar 28, 2022
1fd5d95
Moving various generic math interfaces into the System.Numerics names…
tannergooding Mar 30, 2022
a292bd3
Split various generic math interfaces into their own file for easier …
tannergooding Mar 30, 2022
7be857c
IParseable -> IParsable
tannergooding Mar 30, 2022
e29f531
Update ISignedNumber and IUnsignedNumber to be "marker" interfaces
tannergooding Mar 31, 2022
c28f26f
PI -> Pi and IEEERemainder -> Ieee754Remainder
tannergooding Mar 31, 2022
42ccfdb
Removing the various TInteger constraints in favor of using int where…
tannergooding Mar 31, 2022
80abbdf
Moving IDivisionOperators and ISpanFormattable down to INumberBase
tannergooding Mar 31, 2022
864adde
Moving CopySign, IsNegative, MaxMagnitude, and MinMagnitude down to I…
tannergooding Apr 1, 2022
2a91e6b
Create<TOther> -> CreateChecked<TOther>
tannergooding Apr 1, 2022
a3004d5
Merge remote-tracking branch 'dotnet/main' into generic-math-p2
tannergooding Apr 1, 2022
856cafb
Updating various generic math tests
tannergooding Apr 1, 2022
770a93b
Update src/libraries/System.Private.CoreLib/src/System/Numerics/INumb…
tannergooding Apr 1, 2022
5b315f9
Fixing the reference assembly for System.Numerics.Complex
tannergooding Apr 3, 2022
8adf637
Removing generic math support from System.Numerics.Complex until the …
tannergooding Apr 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions src/libraries/System.Numerics.Vectors/tests/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,62 +91,53 @@ public static T GenerateSingleValue<T>(int min = 1, int max = 100) where T : str
return value;
}

[RequiresPreviewFeatures]
public static T Abs<T>(T value) where T : INumber<T>
{
return T.Abs(value);
}
[RequiresPreviewFeatures]

public static T Sqrt<T>(T value) where T : struct, INumber<T>
{
double dValue = Create<T, double>(value);
double dValue = CreateChecked<T, double>(value);
double dSqrt = Math.Sqrt(dValue);
return T.CreateTruncating<double>(dSqrt);
}

[RequiresPreviewFeatures]
private static TSelf Create<TOther, TSelf>(TOther value)
private static TResult CreateChecked<TOther, TResult>(TOther value)
where TOther : INumber<TOther>
where TSelf : INumber<TSelf>
=> TSelf.Create<TOther>(value);
where TResult : INumber<TResult>
=> TResult.CreateChecked<TOther>(value);

[RequiresPreviewFeatures]
public static T Multiply<T>(T left, T right) where T : INumber<T>
{
return left * right;
}

[RequiresPreviewFeatures]
public static T Divide<T>(T left, T right) where T : INumber<T>
{
return left / right;
}

[RequiresPreviewFeatures]
public static T Add<T>(T left, T right) where T : INumber<T>
{
return left + right;
}

[RequiresPreviewFeatures]
public static T Subtract<T>(T left, T right) where T : INumber<T>
{
return left - right;
}

[RequiresPreviewFeatures]
public static T Xor<T>(T left, T right) where T : IBitwiseOperators<T, T, T>
{
return left ^ right;
}

[RequiresPreviewFeatures]
public static T AndNot<T>(T left, T right) where T : IBitwiseOperators<T, T, T>
{
return left & ~ right;
}

[RequiresPreviewFeatures]
public static T OnesComplement<T>(T left) where T : IBitwiseOperators<T, T, T>
{
return ~left;
Expand All @@ -157,43 +148,36 @@ public static float Clamp(float value, float min, float max)
return value > max ? max : value < min ? min : value;
}

[RequiresPreviewFeatures]
public static T Zero<T>() where T : struct, INumber<T>
{
return T.Zero;
}

[RequiresPreviewFeatures]
public static T One<T>() where T : struct, INumber<T>
{
return T.One;
}

[RequiresPreviewFeatures]
public static bool GreaterThan<T>(T left, T right) where T : INumber<T>
{
return left > right;
}

[RequiresPreviewFeatures]
public static bool GreaterThanOrEqual<T>(T left, T right) where T : INumber<T>
{
public static bool GreaterThanOrEqual<T>(T left, T right) where T : INumber<T>
{
return left >= right;
}

[RequiresPreviewFeatures]
public static bool LessThan<T>(T left, T right) where T : INumber<T>
{
return left < right;
}

[RequiresPreviewFeatures]
public static bool LessThanOrEqual<T>(T left, T right) where T : INumber<T>
{
return left <= right;
}

[RequiresPreviewFeatures]
public static bool AnyEqual<T>(T[] left, T[] right) where T : INumber<T>
{
for (int g = 0; g < left.Length; g++)
Expand All @@ -206,7 +190,6 @@ public static bool AnyEqual<T>(T[] left, T[] right) where T : INumber<T>
return false;
}

[RequiresPreviewFeatures]
public static bool AllEqual<T>(T[] left, T[] right) where T : INumber<T>
{
for (int g = 0; g < left.Length; g++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2373,26 +2373,38 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)System\IAdditionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IAdditiveIdentity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IBitwiseOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IComparisonOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IDecrementOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IDivisionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IEqualityOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IFloatingPoint.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IIncrementOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IInteger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IMinMaxValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IModulusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IMultiplicativeIdentity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IMultiplyOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\INumber.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IParseable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IShiftOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ISpanParseable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ISubtractionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IUnaryNegationOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IParsable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ISpanParsable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IAdditionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IAdditiveIdentity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IBinaryFloatingPointIeee754.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IBinaryInteger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IBinaryNumber.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IBitwiseOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IComparisonOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IDecrementOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IDivisionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IEqualityOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IExponentialFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IFloatingPoint.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IFloatingPointIeee754.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IHyperbolicFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IIncrementOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\ILogarithmicFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IMinMaxValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IModulusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IMultiplicativeIdentity.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IMultiplyOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\INumber.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\INumberBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IPowerFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IRootFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IShiftOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\ISignedNumber.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\ISubtractionOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\ITrigonometricFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryNegationOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
</Project>
61 changes: 37 additions & 24 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public readonly struct Byte
public const byte MinValue = 0;

/// <summary>Represents the additive identity (0).</summary>
public const byte AdditiveIdentity = 0;
private const byte AdditiveIdentity = 0;

/// <summary>Represents the multiplicative identity (1).</summary>
public const byte MultiplicativeIdentity = 1;
private const byte MultiplicativeIdentity = 1;

/// <summary>Represents the number one (1).</summary>
public const byte One = 1;
private const byte One = 1;

/// <summary>Represents the number zero (0).</summary>
public const byte Zero = 0;
private const byte Zero = 0;

// Compares this object to another object, returning an integer that
// indicates the relationship.
Expand Down Expand Up @@ -314,6 +314,9 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
// IBinaryInteger
//

/// <inheritdoc cref="IBinaryInteger{TSelf}.DivRem(TSelf, TSelf)" />
public static (byte Quotient, byte Remainder) DivRem(byte left, byte right) => Math.DivRem(left, right);

/// <inheritdoc cref="IBinaryInteger{TSelf}.LeadingZeroCount(TSelf)" />
public static byte LeadingZeroCount(byte value) => (byte)(BitOperations.LeadingZeroCount(value) - 24);

Expand Down Expand Up @@ -449,21 +452,18 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
// INumber
//

/// <inheritdoc cref="INumber{TSelf}.One" />
static byte INumber<byte>.One => One;

/// <inheritdoc cref="INumber{TSelf}.Zero" />
static byte INumber<byte>.Zero => Zero;

/// <inheritdoc cref="INumber{TSelf}.Abs(TSelf)" />
public static byte Abs(byte value) => value;
static byte INumber<byte>.Abs(byte value) => value;

/// <inheritdoc cref="INumber{TSelf}.Clamp(TSelf, TSelf, TSelf)" />
public static byte Clamp(byte value, byte min, byte max) => Math.Clamp(value, min, max);

/// <inheritdoc cref="INumber{TSelf}.Create{TOther}(TOther)" />
/// <inheritdoc cref="INumber{TSelf}.CopySign(TSelf, TSelf)" />
static byte INumber<byte>.CopySign(byte value, byte sign) => value;

/// <inheritdoc cref="INumber{TSelf}.CreateChecked{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Create<TOther>(TOther value)
public static byte CreateChecked<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
Expand Down Expand Up @@ -685,17 +685,23 @@ public static byte CreateTruncating<TOther>(TOther value)
}
}

/// <inheritdoc cref="INumber{TSelf}.DivRem(TSelf, TSelf)" />
public static (byte Quotient, byte Remainder) DivRem(byte left, byte right) => Math.DivRem(left, right);
/// <inheritdoc cref="INumber{TSelf}.IsNegative(TSelf)" />
static bool INumber<byte>.IsNegative(byte value) => false;

/// <inheritdoc cref="INumber{TSelf}.Max(TSelf, TSelf)" />
public static byte Max(byte x, byte y) => Math.Max(x, y);

/// <inheritdoc cref="INumber{TSelf}.MaxMagnitude(TSelf, TSelf)" />
static byte INumber<byte>.MaxMagnitude(byte x, byte y) => Max(x, y);

/// <inheritdoc cref="INumber{TSelf}.Min(TSelf, TSelf)" />
public static byte Min(byte x, byte y) => Math.Min(x, y);

/// <inheritdoc cref="INumber{TSelf}.MinMagnitude(TSelf, TSelf)" />
static byte INumber<byte>.MinMagnitude(byte x, byte y) => Min(x, y);

/// <inheritdoc cref="INumber{TSelf}.Sign(TSelf)" />
public static byte Sign(byte value) => (byte)((value == 0) ? 0 : 1);
public static int Sign(byte value) => (value == 0) ? 0 : 1;

/// <inheritdoc cref="INumber{TSelf}.TryCreate{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -885,10 +891,20 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
}

//
// IParseable
// INumberBase
//

/// <inheritdoc cref="IParseable{TSelf}.TryParse(string?, IFormatProvider?, out TSelf)" />
/// <inheritdoc cref="INumberBase{TSelf}.One" />
static byte INumberBase<byte>.One => One;

/// <inheritdoc cref="INumberBase{TSelf}.Zero" />
static byte INumberBase<byte>.Zero => Zero;

//
// IParsable
//

/// <inheritdoc cref="IParsable{TSelf}.TryParse(string?, IFormatProvider?, out TSelf)" />
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out byte result) => TryParse(s, NumberStyles.Integer, provider, out result);

//
Expand All @@ -905,13 +921,13 @@ public static bool TryCreate<TOther>(TOther value, out byte result)
// static byte IShiftOperators<byte, byte>.operator >>>(byte value, int shiftAmount) => (byte)(value >> shiftAmount);

//
// ISpanParseable
// ISpanParsable
//

/// <inheritdoc cref="ISpanParseable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
/// <inheritdoc cref="ISpanParsable{TSelf}.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public static byte Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => Parse(s, NumberStyles.Integer, provider);

/// <inheritdoc cref="ISpanParseable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
/// <inheritdoc cref="ISpanParsable{TSelf}.TryParse(ReadOnlySpan{char}, IFormatProvider?, out TSelf)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out byte result) => TryParse(s, NumberStyles.Integer, provider, out result);

//
Expand Down Expand Up @@ -940,8 +956,5 @@ public static bool TryCreate<TOther>(TOther value, out byte result)

/// <inheritdoc cref="IUnaryPlusOperators{TSelf, TResult}.op_UnaryPlus(TSelf)" />
static byte IUnaryPlusOperators<byte, byte>.operator +(byte value) => (byte)(+value);

// /// <inheritdoc cref="IUnaryPlusOperators{TSelf, TResult}.op_CheckedUnaryPlus(TSelf)" />
// static byte IUnaryPlusOperators<byte, byte>.operator checked +(byte value) => checked((byte)(+value));
}
}
Loading