Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that char doesn't box when using generic math #104684

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
75 changes: 72 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,9 @@ 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 @@ -1446,6 +1449,60 @@ bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int b
/// <inheritdoc cref="INumberBase{TSelf}.Abs(TSelf)" />
static char INumberBase<char>.Abs(char value) => value;

/// <inheritdoc cref="INumberBase{TSelf}.CreateChecked{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static char INumberBase<char>.CreateChecked<TOther>(TOther value)
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!TryConvertFromChecked(value, out result) && !TOther.TryConvertToChecked(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.CreateSaturating{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static char INumberBase<char>.CreateSaturating<TOther>(TOther value)
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!TryConvertFromSaturating(value, out result) && !TOther.TryConvertToSaturating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.CreateTruncating{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static char INumberBase<char>.CreateTruncating<TOther>(TOther value)
{
char result;

if (typeof(TOther) == typeof(char))
{
result = (char)(object)value;
}
else if (!TryConvertFromTruncating(value, out result) && !TOther.TryConvertToTruncating(value, out result))
{
ThrowHelper.ThrowNotSupportedException();
}

return result;
}

/// <inheritdoc cref="INumberBase{TSelf}.IsCanonical(TSelf)" />
static bool INumberBase<char>.IsCanonical(char value) => true;

Expand Down Expand Up @@ -1518,7 +1575,11 @@ bool IBinaryInteger<char>.TryWriteLittleEndian(Span<byte> destination, out int b

/// <inheritdoc cref="INumberBase{TSelf}.TryConvertFromChecked{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<char>.TryConvertFromChecked<TOther>(TOther value, out char result)
static bool INumberBase<char>.TryConvertFromChecked<TOther>(TOther value, out char result) => TryConvertFromChecked(value, out result);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryConvertFromChecked<TOther>(TOther value, out char result)
where TOther : INumberBase<TOther>
{
// In order to reduce overall code duplication and improve the inlinabilty of these
// methods for the corelib types we have `ConvertFrom` handle the same sign and
Expand Down Expand Up @@ -1580,7 +1641,11 @@ static bool INumberBase<char>.TryConvertFromChecked<TOther>(TOther value, out ch

/// <inheritdoc cref="INumberBase{TSelf}.TryConvertFromSaturating{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<char>.TryConvertFromSaturating<TOther>(TOther value, out char result)
static bool INumberBase<char>.TryConvertFromSaturating<TOther>(TOther value, out char result) => TryConvertFromSaturating(value, out result);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryConvertFromSaturating<TOther>(TOther value, out char result)
where TOther : INumberBase<TOther>
{
// In order to reduce overall code duplication and improve the inlinabilty of these
// methods for the corelib types we have `ConvertFrom` handle the same sign and
Expand Down Expand Up @@ -1643,7 +1708,11 @@ static bool INumberBase<char>.TryConvertFromSaturating<TOther>(TOther value, out

/// <inheritdoc cref="INumberBase{TSelf}.TryConvertFromTruncating{TOther}(TOther, out TSelf)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<char>.TryConvertFromTruncating<TOther>(TOther value, out char result)
static bool INumberBase<char>.TryConvertFromTruncating<TOther>(TOther value, out char result) => TryConvertFromTruncating(value, out result);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryConvertFromTruncating<TOther>(TOther value, out char result)
where TOther : INumberBase<TOther>
{
// In order to reduce overall code duplication and improve the inlinabilty of these
// methods for the corelib types we have `ConvertFrom` handle the same sign and
Expand Down
Loading