Skip to content

Commit

Permalink
Ensure the generic math APIs are publicly exposed on nint/nuint where…
Browse files Browse the repository at this point in the history
… relevant (#68693)
  • Loading branch information
tannergooding authored Apr 29, 2022
1 parent 66bd8b1 commit 8e624b3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 207 deletions.
106 changes: 26 additions & 80 deletions src/libraries/System.Private.CoreLib/src/System/IntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,72 +288,22 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro
//

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

/// <inheritdoc cref="IBinaryInteger{TSelf}.LeadingZeroCount(TSelf)" />
static nint IBinaryInteger<nint>.LeadingZeroCount(nint value)
{
if (Environment.Is64BitProcess)
{
return BitOperations.LeadingZeroCount((ulong)value);
}
else
{
return BitOperations.LeadingZeroCount((uint)value);
}
}
public static nint LeadingZeroCount(nint value) => BitOperations.LeadingZeroCount((nuint)value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.PopCount(TSelf)" />
static nint IBinaryInteger<nint>.PopCount(nint value)
{
if (Environment.Is64BitProcess)
{
return BitOperations.PopCount((ulong)value);
}
else
{
return BitOperations.PopCount((uint)value);
}
}
public static nint PopCount(nint value) => BitOperations.PopCount((nuint)value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateLeft(TSelf, int)" />
static nint IBinaryInteger<nint>.RotateLeft(nint value, int rotateAmount)
{
if (Environment.Is64BitProcess)
{
return (nint)BitOperations.RotateLeft((ulong)value, rotateAmount);
}
else
{
return (nint)BitOperations.RotateLeft((uint)value, rotateAmount);
}
}
public static nint RotateLeft(nint value, int rotateAmount) => (nint)BitOperations.RotateLeft((nuint)value, rotateAmount);

/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateRight(TSelf, int)" />
static nint IBinaryInteger<nint>.RotateRight(nint value, int rotateAmount)
{
if (Environment.Is64BitProcess)
{
return (nint)BitOperations.RotateRight((ulong)value, rotateAmount);
}
else
{
return (nint)BitOperations.RotateRight((uint)value, rotateAmount);
}
}
public static nint RotateRight(nint value, int rotateAmount) => (nint)BitOperations.RotateRight((nuint)value, rotateAmount);

/// <inheritdoc cref="IBinaryInteger{TSelf}.TrailingZeroCount(TSelf)" />
static nint IBinaryInteger<nint>.TrailingZeroCount(nint value)
{
if (Environment.Is64BitProcess)
{
return BitOperations.TrailingZeroCount((ulong)value);
}
else
{
return BitOperations.TrailingZeroCount((uint)value);
}
}
public static nint TrailingZeroCount(nint value) => BitOperations.TrailingZeroCount(value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.GetShortestBitLength()" />
unsafe long IBinaryInteger<nint>.GetShortestBitLength()
Expand Down Expand Up @@ -396,24 +346,16 @@ unsafe bool IBinaryInteger<nint>.TryWriteLittleEndian(Span<byte> destination, ou
//

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

/// <inheritdoc cref="IBinaryNumber{TSelf}.Log2(TSelf)" />
static nint IBinaryNumber<nint>.Log2(nint value)
public static nint Log2(nint value)
{
if (value < 0)
{
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
}

if (Environment.Is64BitProcess)
{
return BitOperations.Log2((ulong)value);
}
else
{
return BitOperations.Log2((uint)value);
}
return BitOperations.Log2((nuint)value);
}

//
Expand Down Expand Up @@ -517,13 +459,13 @@ static nint IBinaryNumber<nint>.Log2(nint value)
//

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

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

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

Expand All @@ -547,7 +489,8 @@ static nint INumber<nint>.CopySign(nint value, nint sign)

/// <inheritdoc cref="INumber{TSelf}.CreateChecked{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static nint INumber<nint>.CreateChecked<TOther>(TOther value)
public static nint CreateChecked<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down Expand Up @@ -614,7 +557,8 @@ static nint INumber<nint>.CreateChecked<TOther>(TOther value)

/// <inheritdoc cref="INumber{TSelf}.CreateSaturating{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static nint INumber<nint>.CreateSaturating<TOther>(TOther value)
public static nint CreateSaturating<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down Expand Up @@ -692,7 +636,8 @@ static nint INumber<nint>.CreateSaturating<TOther>(TOther value)

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

/// <inheritdoc cref="INumber{TSelf}.IsNegative(TSelf)" />
static bool INumber<nint>.IsNegative(nint value) => value < 0;
public static bool IsNegative(nint value) => value < 0;

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

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

Expand Down Expand Up @@ -794,10 +739,10 @@ static nint INumber<nint>.MaxMagnitude(nint x, nint y)
}

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

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

Expand Down Expand Up @@ -827,11 +772,12 @@ static nint INumber<nint>.MinMagnitude(nint x, nint y)
}

/// <inheritdoc cref="INumber{TSelf}.Sign(TSelf)" />
static int INumber<nint>.Sign(nint value) => Math.Sign(value);
public static int Sign(nint value) => Math.Sign(value);

/// <inheritdoc cref="INumber{TSelf}.TryCreate{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumber<nint>.TryCreate<TOther>(TOther value, out nint result)
public static bool TryCreate<TOther>(TOther value, out nint result)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down
110 changes: 22 additions & 88 deletions src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,72 +280,22 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro
//

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

/// <inheritdoc cref="IBinaryInteger{TSelf}.LeadingZeroCount(TSelf)" />
static nuint IBinaryInteger<nuint>.LeadingZeroCount(nuint value)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.LeadingZeroCount((ulong)value);
}
else
{
return (nuint)BitOperations.LeadingZeroCount((uint)value);
}
}
public static nuint LeadingZeroCount(nuint value) => (nuint)BitOperations.LeadingZeroCount(value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.PopCount(TSelf)" />
static nuint IBinaryInteger<nuint>.PopCount(nuint value)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.PopCount((ulong)value);
}
else
{
return (nuint)BitOperations.PopCount((uint)value);
}
}
public static nuint PopCount(nuint value) => (nuint)BitOperations.PopCount(value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateLeft(TSelf, int)" />
static nuint IBinaryInteger<nuint>.RotateLeft(nuint value, int rotateAmount)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.RotateLeft((ulong)value, rotateAmount);
}
else
{
return (nuint)BitOperations.RotateLeft((uint)value, rotateAmount);
}
}
public static nuint RotateLeft(nuint value, int rotateAmount) => BitOperations.RotateLeft(value, rotateAmount);

/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateRight(TSelf, int)" />
static nuint IBinaryInteger<nuint>.RotateRight(nuint value, int rotateAmount)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.RotateRight((ulong)value, rotateAmount);
}
else
{
return (nuint)BitOperations.RotateRight((uint)value, rotateAmount);
}
}
public static nuint RotateRight(nuint value, int rotateAmount) => BitOperations.RotateRight(value, rotateAmount);

/// <inheritdoc cref="IBinaryInteger{TSelf}.TrailingZeroCount(TSelf)" />
static nuint IBinaryInteger<nuint>.TrailingZeroCount(nuint value)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.TrailingZeroCount((ulong)value);
}
else
{
return (nuint)BitOperations.TrailingZeroCount((uint)value);
}
}
public static nuint TrailingZeroCount(nuint value) => (nuint)BitOperations.TrailingZeroCount(value);

/// <inheritdoc cref="IBinaryInteger{TSelf}.GetShortestBitLength()" />
unsafe long IBinaryInteger<nuint>.GetShortestBitLength() => (sizeof(nuint) * 8) - BitOperations.LeadingZeroCount((nuint)_value);
Expand Down Expand Up @@ -376,30 +326,10 @@ unsafe bool IBinaryInteger<nuint>.TryWriteLittleEndian(Span<byte> destination, o
//

/// <inheritdoc cref="IBinaryNumber{TSelf}.IsPow2(TSelf)" />
static bool IBinaryNumber<nuint>.IsPow2(nuint value)
{
if (Environment.Is64BitProcess)
{
return BitOperations.IsPow2((ulong)value);
}
else
{
return BitOperations.IsPow2((uint)value);
}
}
public static bool IsPow2(nuint value) => BitOperations.IsPow2(value);

/// <inheritdoc cref="IBinaryNumber{TSelf}.Log2(TSelf)" />
static nuint IBinaryNumber<nuint>.Log2(nuint value)
{
if (Environment.Is64BitProcess)
{
return (nuint)BitOperations.Log2((ulong)value);
}
else
{
return (nuint)BitOperations.Log2((uint)value);
}
}
public static nuint Log2(nuint value) => (nuint)BitOperations.Log2(value);

//
// IBitwiseOperators
Expand Down Expand Up @@ -505,14 +435,15 @@ static nuint IBinaryNumber<nuint>.Log2(nuint value)
static nuint INumber<nuint>.Abs(nuint value) => value;

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

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

/// <inheritdoc cref="INumber{TSelf}.CreateChecked{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static nuint INumber<nuint>.CreateChecked<TOther>(TOther value)
public static nuint CreateChecked<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down Expand Up @@ -579,7 +510,8 @@ static nuint INumber<nuint>.CreateChecked<TOther>(TOther value)

/// <inheritdoc cref="INumber{TSelf}.CreateSaturating{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static nuint INumber<nuint>.CreateSaturating<TOther>(TOther value)
public static nuint CreateSaturating<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down Expand Up @@ -660,7 +592,8 @@ static nuint INumber<nuint>.CreateSaturating<TOther>(TOther value)

/// <inheritdoc cref="INumber{TSelf}.CreateTruncating{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static nuint INumber<nuint>.CreateTruncating<TOther>(TOther value)
public static nuint CreateTruncating<TOther>(TOther value)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down Expand Up @@ -729,23 +662,24 @@ static nuint INumber<nuint>.CreateTruncating<TOther>(TOther value)
static bool INumber<nuint>.IsNegative(nuint value) => false;

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

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

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

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

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

/// <inheritdoc cref="INumber{TSelf}.TryCreate{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumber<nuint>.TryCreate<TOther>(TOther value, out nuint result)
public static bool TryCreate<TOther>(TOther value, out nuint result)
where TOther : INumber<TOther>
{
if (typeof(TOther) == typeof(byte))
{
Expand Down
Loading

0 comments on commit 8e624b3

Please sign in to comment.