Skip to content

Commit

Permalink
Updating the various generic math functions to have default implement…
Browse files Browse the repository at this point in the history
…ations (#70445)

* Updating the various generic math functions to have default implementations

* Ensure GenericMathHelpers keep the right constraints
  • Loading branch information
tannergooding authored Jun 10, 2022
1 parent 57c88e9 commit 5786435
Show file tree
Hide file tree
Showing 39 changed files with 224 additions and 474 deletions.
12 changes: 6 additions & 6 deletions src/libraries/Common/tests/System/GenericMathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static class EqualityOperatorsHelper<TSelf, TOther>
}

public static class ExponentialFunctionsHelper<TSelf>
where TSelf : IExponentialFunctions<TSelf>
where TSelf : IExponentialFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Exp(TSelf x) => TSelf.Exp(x);

Expand Down Expand Up @@ -223,7 +223,7 @@ public static class FloatingPointIeee754Helper<TSelf>
}

public static class HyperbolicFunctionsHelper<TSelf>
where TSelf : IHyperbolicFunctions<TSelf>
where TSelf : IHyperbolicFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Acosh(TSelf x) => TSelf.Acosh(x);

Expand All @@ -247,7 +247,7 @@ public static class IncrementOperatorsHelper<TSelf>
}

public static class LogarithmicFunctionsHelper<TSelf>
where TSelf : ILogarithmicFunctions<TSelf>
where TSelf : ILogarithmicFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Log(TSelf x) => TSelf.Log(x);

Expand Down Expand Up @@ -390,13 +390,13 @@ public static class ParsableHelper<TSelf>
}

public static class PowerFunctionsHelper<TSelf>
where TSelf : IPowerFunctions<TSelf>
where TSelf : IPowerFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Pow(TSelf x, TSelf y) => TSelf.Pow(x, y);
}

public static class RootFunctionsHelper<TSelf>
where TSelf : IRootFunctions<TSelf>
where TSelf : IRootFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Cbrt(TSelf x) => TSelf.Cbrt(x);

Expand Down Expand Up @@ -436,7 +436,7 @@ public static class SubtractionOperatorsHelper<TSelf, TOther, TResult>
}

public static class TrigonometricFunctionsHelper<TSelf>
where TSelf : ITrigonometricFunctions<TSelf>
where TSelf : ITrigonometricFunctions<TSelf>, INumberBase<TSelf>
{
public static TSelf Acos(TSelf x) => TSelf.Acos(x);

Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,6 @@ bool IBinaryInteger<byte>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static byte IDivisionOperators<byte, byte, byte>.operator /(byte left, byte right) => (byte)(left / right);

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static byte IDivisionOperators<byte, byte, byte>.operator checked /(byte left, byte right) => (byte)(left / right);

//
// IEqualityOperators
//
Expand Down
31 changes: 0 additions & 31 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,6 @@ public static int ConvertToUtf32(string s, int index)
// IBinaryInteger
//

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

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

Expand Down Expand Up @@ -1284,9 +1281,6 @@ bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static char IDivisionOperators<char, char, char>.operator /(char left, char right) => (char)(left / right);

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static char IDivisionOperators<char, char, char>.operator checked /(char left, char right) => (char)(left / right);

//
// IEqualityOperators
//
Expand Down Expand Up @@ -1341,31 +1335,6 @@ bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
static char IMultiplyOperators<char, char, char>.operator checked *(char left, char right) => checked((char)(left * right));

//
// INumber
//

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

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

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

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

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

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

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

//
// INumberBase
//
Expand Down
49 changes: 0 additions & 49 deletions src/libraries/System.Private.CoreLib/src/System/Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,34 +1118,13 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
return Convert.DefaultToType((IConvertible)this, type, provider);
}

//
// IAdditionOperators
//

/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
static decimal IAdditionOperators<decimal, decimal, decimal>.operator checked +(decimal left, decimal right) => left + right;

//
// IAdditiveIdentity
//

/// <inheritdoc cref="IAdditiveIdentity{TSelf, TResult}.AdditiveIdentity" />
static decimal IAdditiveIdentity<decimal, decimal>.AdditiveIdentity => AdditiveIdentity;

//
// IDecrementOperators
//

/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
static decimal IDecrementOperators<decimal>.operator checked --(decimal value) => --value;

//
// IDivisionOperators
//

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static decimal IDivisionOperators<decimal, decimal, decimal>.operator checked /(decimal left, decimal right) => left / right;

//
// IFloatingPoint
//
Expand Down Expand Up @@ -1260,13 +1239,6 @@ bool IFloatingPoint<decimal>.TryWriteSignificandLittleEndian(Span<byte> destinat
}
}

//
// IIncrementOperators
//

/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
static decimal IIncrementOperators<decimal>.operator checked ++(decimal value) => ++value;

//
// IMinMaxValue
//
Expand All @@ -1284,13 +1256,6 @@ bool IFloatingPoint<decimal>.TryWriteSignificandLittleEndian(Span<byte> destinat
/// <inheritdoc cref="IMultiplicativeIdentity{TSelf, TResult}.MultiplicativeIdentity" />
static decimal IMultiplicativeIdentity<decimal, decimal>.MultiplicativeIdentity => MultiplicativeIdentity;

//
// IMultiplyOperators
//

/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
public static decimal operator checked *(decimal left, decimal right) => left * right;

//
// INumber
//
Expand Down Expand Up @@ -1792,19 +1757,5 @@ private static bool TryConvertTo<TOther>(decimal value, [NotNullWhen(true)] out

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

//
// ISubtractionOperators
//

/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
static decimal ISubtractionOperators<decimal, decimal, decimal>.operator checked -(decimal left, decimal right) => left - right;

//
// IUnaryNegationOperators
//

/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
static decimal IUnaryNegationOperators<decimal, decimal>.operator checked -(decimal value) => -value;
}
}
21 changes: 0 additions & 21 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,6 @@ object IConvertible.ToType(Type type, IFormatProvider? provider)
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
static double IAdditionOperators<double, double, double>.operator +(double left, double right) => left + right;

/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
static double IAdditionOperators<double, double, double>.operator checked +(double left, double right) => left + right;

//
// IAdditiveIdentity
//
Expand Down Expand Up @@ -601,19 +598,13 @@ public static bool IsPow2(double value)
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_Decrement(TSelf)" />
static double IDecrementOperators<double>.operator --(double value) => --value;

/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
static double IDecrementOperators<double>.operator checked --(double value) => --value;

//
// IDivisionOperators
//

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static double IDivisionOperators<double, double, double>.operator /(double left, double right) => left / right;

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static double IDivisionOperators<double, double, double>.operator checked /(double left, double right) => left / right;

//
// IExponentialFunctions
//
Expand Down Expand Up @@ -865,9 +856,6 @@ bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destinati
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_Increment(TSelf)" />
static double IIncrementOperators<double>.operator ++(double value) => ++value;

/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
static double IIncrementOperators<double>.operator checked ++(double value) => ++value;

//
// ILogarithmicFunctions
//
Expand Down Expand Up @@ -921,9 +909,6 @@ bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destinati
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
static double IMultiplyOperators<double, double, double>.operator *(double left, double right) => left * right;

/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
static double IMultiplyOperators<double, double, double>.operator checked *(double left, double right) => left * right;

//
// INumber
//
Expand Down Expand Up @@ -1401,9 +1386,6 @@ private static bool TryConvertTo<TOther>(double value, [NotNullWhen(true)] out T
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_Subtraction(TSelf, TOther)" />
static double ISubtractionOperators<double, double, double>.operator -(double left, double right) => left - right;

/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
static double ISubtractionOperators<double, double, double>.operator checked -(double left, double right) => left - right;

//
// ITrigonometricFunctions
//
Expand Down Expand Up @@ -1460,9 +1442,6 @@ private static bool TryConvertTo<TOther>(double value, [NotNullWhen(true)] out T
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_UnaryNegation(TSelf)" />
static double IUnaryNegationOperators<double, double>.operator -(double value) => -value;

/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
static double IUnaryNegationOperators<double, double>.operator checked -(double value) => -value;

//
// IUnaryPlusOperators
//
Expand Down
21 changes: 0 additions & 21 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,6 @@ private static double CreateDoubleNaN(bool sign, ulong significand)
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
public static Half operator +(Half left, Half right) => (Half)((float)left + (float)right);

/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
static Half IAdditionOperators<Half, Half, Half>.operator checked +(Half left, Half right) => left + right;

//
// IAdditiveIdentity
//
Expand Down Expand Up @@ -1072,19 +1069,13 @@ public static bool IsPow2(Half value)
return (Half)tmp;
}

/// <inheritdoc cref="IDecrementOperators{TSelf}.op_CheckedDecrement(TSelf)" />
static Half IDecrementOperators<Half>.operator checked --(Half value) => --value;

//
// IDivisionOperators
//

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
public static Half operator /(Half left, Half right) => (Half)((float)left / (float)right);

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static Half IDivisionOperators<Half, Half, Half>.operator checked /(Half left, Half right) => left / right;

//
// IExponentialFunctions
//
Expand Down Expand Up @@ -1317,9 +1308,6 @@ bool IFloatingPoint<Half>.TryWriteSignificandLittleEndian(Span<byte> destination
return (Half)tmp;
}

/// <inheritdoc cref="IIncrementOperators{TSelf}.op_CheckedIncrement(TSelf)" />
static Half IIncrementOperators<Half>.operator checked ++(Half value) => ++value;

//
// ILogarithmicFunctions
//
Expand Down Expand Up @@ -1363,9 +1351,6 @@ bool IFloatingPoint<Half>.TryWriteSignificandLittleEndian(Span<byte> destination
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
public static Half operator *(Half left, Half right) => (Half)((float)left * (float)right);

/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_CheckedMultiply(TSelf, TOther)" />
static Half IMultiplyOperators<Half, Half, Half>.operator checked *(Half left, Half right) => left * right;

//
// INumber
//
Expand Down Expand Up @@ -1836,9 +1821,6 @@ private static bool TryConvertTo<TOther>(Half value, [NotNullWhen(true)] out TOt
/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_Subtraction(TSelf, TOther)" />
public static Half operator -(Half left, Half right) => (Half)((float)left - (float)right);

/// <inheritdoc cref="ISubtractionOperators{TSelf, TOther, TResult}.op_CheckedSubtraction(TSelf, TOther)" />
static Half ISubtractionOperators<Half, Half, Half>.operator checked -(Half left, Half right) => left - right;

//
// ITrigonometricFunctions
//
Expand Down Expand Up @@ -1899,9 +1881,6 @@ public static (Half Sin, Half Cos) SinCos(Half x)
/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_UnaryNegation(TSelf)" />
public static Half operator -(Half value) => (Half)(-(float)value);

/// <inheritdoc cref="IUnaryNegationOperators{TSelf, TResult}.op_CheckedUnaryNegation(TSelf)" />
static Half IUnaryNegationOperators<Half, Half>.operator checked -(Half value) => -value;

//
// IUnaryPlusOperators
//
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,6 @@ public static short Log2(short value)
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static short IDivisionOperators<short, short, short>.operator /(short left, short right) => (short)(left / right);

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static short IDivisionOperators<short, short, short>.operator checked /(short left, short right) => (short)(left / right);

//
// IEqualityOperators
//
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,6 @@ public static int Log2(int value)
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static int IDivisionOperators<int, int, int>.operator /(int left, int right) => left / right;

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static int IDivisionOperators<int, int, int>.operator checked /(int left, int right) => left / right;

//
// IEqualityOperators
//
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,6 @@ public static long Log2(long value)
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static long IDivisionOperators<long, long, long>.operator /(long left, long right) => left / right;

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static long IDivisionOperators<long, long, long>.operator checked /(long left, long right) => left / right;

//
// IEqualityOperators
//
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/IntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,6 @@ public static nint Log2(nint value)
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static nint IDivisionOperators<nint, nint, nint>.operator /(nint left, nint right) => left / right;

/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_CheckedDivision(TSelf, TOther)" />
static nint IDivisionOperators<nint, nint, nint>.operator checked /(nint left, nint right) => left / right;

//
// IIncrementOperators
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface IAdditionOperators<TSelf, TOther, TResult>
/// <param name="right">The value which is added to <paramref name="left" />.</param>
/// <returns>The sum of <paramref name="left" /> and <paramref name="right" />.</returns>
/// <exception cref="OverflowException">The sum of <paramref name="left" /> and <paramref name="right" /> is not representable by <typeparamref name="TResult" />.</exception>
static abstract TResult operator checked +(TSelf left, TOther right);
static virtual TResult operator checked +(TSelf left, TOther right) => left + right;
}
}
Loading

0 comments on commit 5786435

Please sign in to comment.