The heart of long division is finding a single quotient limb - the largest multiplier of the divisor that does not overshoot the dividend. Today you find it by binary search, which is exact for any divisor.
Find a single quotient limb for a dividend with a one-limb quotient - the largest q with divisor times q not exceeding the dividend.
You cannot read a quotient limb straight off the way single-limb division lets you.
The classical method is to estimate it from the divisor’s top limb and correct,
but that estimate is only reliably close after a normalization step (scaling the
divisor so its top limb is large); skip the normalization and a small top limb can
send the estimate wildly high, needing a runaway number of corrections. So we take
the simpler, always-correct route: search for the digit directly. The quotient
limb is the largest q in [0, Base) for which divisor * q does not exceed the
dividend, and that “largest value satisfying a monotonic test” is exactly what
binary search finds - in about thirty steps regardless of the divisor’s shape.
Today isolates that single step on a dividend whose quotient fits in one limb -
1000000000000000000 over 1000000007 is 999999993 remainder 49. Each search
step multiplies the divisor by a candidate (mulScalar) and compares (cmpMag);
once the largest non-overshooting q is found, the remainder is `dividend - divisor
// largest q in [0, Base) with b*q <= a; remainder is a - b*qfunc oneQuotientLimb(a, b mag) (q uint32, rem mag) {lo, hi := uint32(0), uint32(Base-1)for lo < hi {mid := (lo + hi + 1) / 2if cmpMag(mulScalar(b, mid), a) <= 0 { lo = mid } else { hi = mid - 1 }}return lo, subMag(a, mulScalar(b, lo))}