Skip to content

Commit

Permalink
Delete redundant DivMod implementation (#45652)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas authored Dec 6, 2020
1 parent 4e16e2f commit a5d0bcf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 deletions.
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.
/// </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));
}
}
}

0 comments on commit a5d0bcf

Please sign in to comment.