use div/rem rather than re-calculate modulo from multiply
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 24 Apr 2022 19:27:27 +0000 (20:27 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 24 Apr 2022 19:27:27 +0000 (20:27 +0100)
openpower/sv/biginteger/divmnu64.c

index b6c4b52e763b87ce842ac214e9901b7065c54092..52e70a116f25af1f4f345a4cc316676f50f24e2c 100644 (file)
@@ -98,8 +98,9 @@ int divmnu(unsigned q[], unsigned r[], const unsigned u[], const unsigned v[],
         k = 0; // the case of a
         for (j = m - 1; j >= 0; j--)
         {                                 // single-digit
-            q[j] = (k * b + u[j]) / v[0]; // divisor here.
-            k = (k * b + u[j]) - q[j] * v[0];
+            uint64_t dig2 = (k * b + u[j]);
+            q[j] = dig2 / v[0]; // divisor here.
+            k = dig2 % v[0]; // modulo bak into next loop
         }
         if (r != NULL)
             r[0] = k;