diff options
author | Stephen Toub <stoub@microsoft.com> | 2016-11-15 07:28:20 -0500 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2016-11-15 07:28:20 -0500 |
commit | bd1f21d17c439481d7bc2a5219779e2474419a96 (patch) | |
tree | 63632c0a2f4e2bfa438572a1220b356a3183cdf9 /src/mscorlib | |
parent | 7bd7335b34b3e4ee650b5db4991d922e3fbc56ea (diff) | |
download | coreclr-bd1f21d17c439481d7bc2a5219779e2474419a96.tar.gz coreclr-bd1f21d17c439481d7bc2a5219779e2474419a96.tar.bz2 coreclr-bd1f21d17c439481d7bc2a5219779e2474419a96.zip |
Improve Math.DivRem performance
Until the JIT is able to eliminate one of the two idiv operations, using a multiplication and subtraction is measurably faster than an extra division.
Diffstat (limited to 'src/mscorlib')
-rw-r--r-- | src/mscorlib/src/System/Math.cs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/mscorlib/src/System/Math.cs b/src/mscorlib/src/System/Math.cs index cf2b0ee15c..9ff0a9f6c8 100644 --- a/src/mscorlib/src/System/Math.cs +++ b/src/mscorlib/src/System/Math.cs @@ -726,13 +726,21 @@ namespace System { } public static int DivRem(int a, int b, out int result) { - result = a%b; - return a/b; + // TODO https://github.com/dotnet/coreclr/issues/3439: + // Restore to using % and / when the JIT is able to eliminate one of the idivs. + // In the meantime, a * and - is measurably faster than an extra /. + int div = a / b; + result = a - (div * b); + return div; } public static long DivRem(long a, long b, out long result) { - result = a%b; - return a/b; + // TODO https://github.com/dotnet/coreclr/issues/3439: + // Restore to using % and / when the JIT is able to eliminate one of the idivs. + // In the meantime, a * and - is measurably faster than an extra /. + long div = a / b; + result = a - (div * b); + return div; } } } |