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

Delete redundant DivMod implementation #45652

Merged
merged 2 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,6 @@ public static void WriteTwoDecimalDigits(uint value, Span<byte> buffer, int star

#endregion UTF-8 Helper methods

#region Math Helper methods

/// <summary>
/// We don't have access to Math.DivRem, so this is a copy of the implementation.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong DivMod(ulong numerator, ulong denominator, out ulong modulo)
{
ulong div = numerator / denominator;
modulo = numerator - (div * denominator);
return div;
}

/// <summary>
/// We don't have access to Math.DivRem, so this is a copy of the implementation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one with similar comment about this helper:

// We don't have access to System.Buffers.Text.FormattingHelpers.DivMod,
// so this is a copy of the implementation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint DivMod(uint numerator, uint denominator, out uint modulo)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Text.Json is compiled downlevel, so it would need ifdefs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, maybe comment can be updated since there is no System.Buffers.Text.FormattingHelpers.DivMod after this change.

/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint DivMod(uint numerator, uint denominator, out uint modulo)
{
uint div = numerator / denominator;
modulo = numerator - (div * denominator);
return div;
}

#endregion Math Helper methods

//
// Enable use of ThrowHelper from TryFormat() routines without introducing dozens of non-code-coveraged "bytesWritten = 0; return false" boilerplate.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static bool TryFormat(TimeSpan value, Span<byte> destination, out int byt
}
}

totalSecondsRemaining = FormattingHelpers.DivMod((ulong)Math.Abs(value.Ticks), TimeSpan.TicksPerSecond, out ulong fraction64);
ulong fraction64;
(totalSecondsRemaining, fraction64) = Math.DivRem((ulong)Math.Abs(value.Ticks), TimeSpan.TicksPerSecond);
fraction = (uint)fraction64;
}

Expand Down Expand Up @@ -114,7 +115,7 @@ public static bool TryFormat(TimeSpan value, Span<byte> destination, out int byt
if (totalSecondsRemaining > 0)
{
// Only compute minutes if the TimeSpan has an absolute value of >= 1 minute.
totalMinutesRemaining = FormattingHelpers.DivMod(totalSecondsRemaining, 60 /* seconds per minute */, out seconds);
(totalMinutesRemaining, seconds) = Math.DivRem(totalSecondsRemaining, 60 /* seconds per minute */);
}

Debug.Assert(seconds < 60);
Expand All @@ -124,7 +125,7 @@ public static bool TryFormat(TimeSpan value, Span<byte> destination, out int byt
if (totalMinutesRemaining > 0)
{
// Only compute hours if the TimeSpan has an absolute value of >= 1 hour.
totalHoursRemaining = FormattingHelpers.DivMod(totalMinutesRemaining, 60 /* minutes per hour */, out minutes);
(totalHoursRemaining, minutes) = Math.DivRem(totalMinutesRemaining, 60 /* minutes per hour */);
}

Debug.Assert(minutes < 60);
Expand All @@ -137,7 +138,7 @@ public static bool TryFormat(TimeSpan value, Span<byte> destination, out int byt
if (totalHoursRemaining > 0)
{
// Only compute days if the TimeSpan has an absolute value of >= 1 day.
days = FormattingHelpers.DivMod((uint)totalHoursRemaining, 24 /* hours per day */, out hours);
(days, hours) = Math.DivRem((uint)totalHoursRemaining, 24 /* hours per day */);
}

Debug.Assert(hours < 24);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static void TrimDateTimeOffset(Span<byte> buffer, out int bytesWritten)
// Remove trailing zeros
while (true)
{
uint quotient = DivMod(fraction, 10, out uint remainder);
(uint quotient, uint remainder) = DivRem(fraction, 10);
if (remainder != 0)
{
break;
Expand Down Expand Up @@ -132,14 +132,13 @@ public static void TrimDateTimeOffset(Span<byte> buffer, out int bytesWritten)
}
}

// We don't have access to System.Buffers.Text.FormattingHelpers.DivMod,
// We don't always have access to System.Math.DivRem,
// so this is a copy of the implementation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint DivMod(uint numerator, uint denominator, out uint modulo)
public static (uint Quotient, uint Remainder) DivRem(uint left, uint right)
{
uint div = numerator / denominator;
modulo = numerator - (div * denominator);
return div;
uint quotient = left / right;
return (quotient, left - (quotient * right));
}
}
}