if (m < n || n <= 0 || v[n - 1] == 0)
return 1; // Return if invalid param.
- if (n == 1)
- { // Take care of
+ if (n == 1) { // Take care of
k = 0; // the case of a
- for (j = m - 1; j >= 0; j--)
- { // single-digit
+ 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];
}
high-order digit on the dividend; we do that unconditionally. */
s = nlz(v[n - 1]); // 0 <= s <= 31.
+
vn = (unsigned *)alloca(4 * n);
biglsh(s, vn, v, n);
un = (unsigned *)alloca(4 * (m + 1));
- un[m] = (unsigned long long)u[m - 1] >> (32 - s);
+ un[m] = (unsigned long long)u[m - 1] >> (32 - s); // extra digit
biglsh(s, un, u, m);
- for (j = m - n; j >= 0; j--)
- { // Main loop.
+ for (j = m - n; j >= 0; j--) { // Main loop.
// Compute estimate qhat of q[j] from top 2 digits
uint64_t dig2 = ((uint64_t)un[j + n] << 32) | un[j + n - 1];
qhat = dig2 / vn[n - 1];
rhat = dig2 % vn[n - 1];
again:
// use 3rd-from-top digit to obtain better accuracy
- if (qhat >= b || qhat * vn[n - 2] > b * rhat + un[j + n - 2])
- {
+ if (qhat >= b || qhat * vn[n - 2] > b * rhat + un[j + n - 2]) {
qhat = qhat - 1;
rhat = rhat + vn[n - 1];
if (rhat < b)
bool need_fixup = bigmulsub(qhat, vn, un_j, j, m, n);
q[j] = qhat; // Store quotient digit.
- if (need_fixup)
- { // If we subtracted too
+ if (need_fixup) { // If we subtracted too
q[j] = q[j] - 1; // much, add back.
bigadd(un_j, vn, un_j, m, n, 0);
}
// If the caller wants the remainder, unnormalize
// it and pass it back.
if (r != NULL)
- {
bigrsh(s, r, un, n);
- }
return 0;
}