Delete redundant DivMod implementation (#45652)
authorJan Kotas <jkotas@microsoft.com>
Sun, 6 Dec 2020 20:42:39 +0000 (12:42 -0800)
committerGitHub <noreply@github.com>
Sun, 6 Dec 2020 20:42:39 +0000 (12:42 -0800)
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/FormattingHelpers.cs
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.TimeSpan.cs
src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.Date.cs

index 2a6019d..6316a19 100644 (file)
@@ -149,32 +149,6 @@ namespace System.Buffers.Text
 
         #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.
         //
index 6e4fc4f..3fa8e67 100644 (file)
@@ -70,7 +70,8 @@ namespace System.Buffers.Text
                     }
                 }
 
-                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;
             }
 
@@ -114,7 +115,7 @@ namespace System.Buffers.Text
             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);
@@ -124,7 +125,7 @@ namespace System.Buffers.Text
             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);
@@ -137,7 +138,7 @@ namespace System.Buffers.Text
             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);
index d153241..6cf3561 100644 (file)
@@ -69,7 +69,7 @@ namespace System.Text.Json
                 // Remove trailing zeros
                 while (true)
                 {
-                    uint quotient = DivMod(fraction, 10, out uint remainder);
+                    (uint quotient, uint remainder) = DivRem(fraction, 10);
                     if (remainder != 0)
                     {
                         break;
@@ -132,14 +132,13 @@ namespace System.Text.Json
             }
         }
 
-        // 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));
         }
     }
 }