Fix a bug in 64-bit signed integer division. (#37570)
authorAnton Lapounov <antonl@microsoft.com>
Tue, 16 Jun 2020 19:03:43 +0000 (12:03 -0700)
committerGitHub <noreply@github.com>
Tue, 16 Jun 2020 19:03:43 +0000 (12:03 -0700)
Calculation of `absNc` involves adding the sign bit of the denominator. It incorrectly used bit 31 of the denominator instead of bit 63.

src/coreclr/src/jit/utils.cpp

index 9e5d874..9af74b5 100644 (file)
@@ -2399,10 +2399,9 @@ T GetSignedMagic(T denom, int* shift /*out*/)
     UT  q2;
     UT  t;
     T   result_magic;
-    int iters = 0;
 
     absDenom = abs(denom);
-    t        = two_nminus1 + ((unsigned int)denom >> 31);
+    t        = two_nminus1 + (UT(denom) >> bits_minus_1);
     absNc    = t - 1 - (t % absDenom);        // absolute value of nc
     p        = bits_minus_1;                  // initialize p
     q1       = two_nminus1 / absNc;           // initialize q1 = 2^p / abs(nc)
@@ -2412,7 +2411,6 @@ T GetSignedMagic(T denom, int* shift /*out*/)
 
     do
     {
-        iters++;
         p++;
         q1 *= 2; // update q1 = 2^p / abs(nc)
         r1 *= 2; // update r1 = rem(2^p / abs(nc))