GMP 6.3.0: generate mpf_div_ui fraction limbs from a reciprocal prefix Grow the output from an exact reciprocal prefix using multiplication. When B mod v is 0 or 1, fill the terminating or repeating tail directly. Preserve the existing output precision, sign and aliasing behavior. Apply to stock GMP 6.3.0 with patch -p1. Proof, timings and reproduction: https://gmp.mikedotexe.com/ --- a/mpf/div_ui.c +++ b/mpf/div_ui.c @@ -31,6 +31,121 @@ #include "gmp-impl.h" #include "longlong.h" + +/* When the dividend is shorter than the quotient wanted, the low limbs of + the quotient are the fraction limbs of rem/v, where rem is the remainder + left by the limbs the dividend does have. From MPF_DIV_UI_FRAC_THRESHOLD + such limbs they are made by multiplying, instead of one division step at + a time from a dividend padded with zeros. + + Write B for the limb base. Let U = floor (B^k / v) be the first k + fraction limbs of 1/v and rho = B^k mod v what is left after them, so + that B^k = U*v + rho. Two things follow. + + The limbs that come next are the limbs already written times what is + left. For j <= k, + + floor (B^(k+j) / v) = U * B^j + rho * U_j + floor (rho * rho_j / v) + + where U_j is the high j limbs of U and rho_j = B^j mod v. That is one + mpn_mul_1 and a carry of one limb, and what is left becomes + rho * rho_j mod v. It is Newton's iteration for 1/v, exact because its + residual is the single limb rho. + + What is left can be read from the last limb written. rho_j is less than + v, so it is its own low limb, and that is minus the low limb of U_j times + v, since B^j = U_j * v + rho_j. + + The numerator comes in last, in the same way: the first k fraction limbs + of rem/v are rem * U + floor (rem * rho / v), and leave rem * rho mod v. + + The threshold must be at least 2. The default is a little above where + the two methods met on an x86-64, with and without the assembly code. It + has not been tuned per CPU. The method is for limbs without nails, and + with nails the threshold is never reached. */ + +#ifndef MPF_DIV_UI_FRAC_THRESHOLD +#if GMP_NAIL_BITS == 0 +#define MPF_DIV_UI_FRAC_THRESHOLD 24 +#else +#define MPF_DIV_UI_FRAC_THRESHOLD MP_SIZE_T_MAX +#endif +#endif + +/* Set q to floor (x*y / v) and r to x*y mod v, for x < v, where vn is v + shifted left by cnt to set its high bit and vinv is the inverse of vn. + The quotient is a single limb since x < v. */ +#define MULDIV_PREINV(q, r, x, y, vn, vinv, cnt) \ + do { \ + mp_limb_t __ph, __pl; \ + umul_ppmm (__ph, __pl, (x) << (cnt), y); \ + udiv_qrnnd_preinv (q, r, __ph, __pl, vn, vinv); \ + (r) >>= (cnt); \ + } while (0) + +/* Set {qp,n} to floor (rem * B^n / v), the first n fraction limbs of rem/v. + Requires 0 < rem < v and n >= 2. No other space is used. + + 1/v is grown in the low l = ceil (n/2) limbs of qp, high limb first. The + high n-l limbs of the result are then rem times the first n-l limbs of + 1/v, and the low l limbs are what that leaves times all of 1/v, which is + done in place. */ +static void +mpf_div_ui_frac (mp_ptr qp, mp_size_t n, mp_limb_t rem, mp_limb_t v) +{ + mp_size_t l, h, k, j; + mp_limb_t vn, vinv, rho, rho_j, c; + int cnt; + + ASSERT (n >= 2); + ASSERT (rem > 0 && rem < v); + + count_leading_zeros (cnt, v); + vn = v << cnt; + invert_limb (vinv, vn); + + l = (n + 1) / 2; + h = n - l; + + /* The first limb of 1/v, B = qp[l-1] * v + rho. */ + udiv_qrnnd_preinv (qp[l - 1], rho, CNST_LIMB (1) << cnt, CNST_LIMB (0), + vn, vinv); + rho >>= cnt; + + /* If B mod v is zero, the first fraction limb terminates. If it + is one, the remainder is unchanged and that limb repeats. Save + the digit before filling over the reciprocal seed. */ + if (rho <= 1) + { + c = rem * qp[l - 1]; + MPN_FILL (qp, n - 1, rho ? c : CNST_LIMB (0)); + qp[n - 1] = c; + return; + } + + /* {qp+l-k,k} is the first k limbs of 1/v and rho is what they leave. + Make the next j. */ + for (k = 1; k < l; k += j) + { + j = MIN (k, l - k); + rho_j = - qp[l - j] * v; + + ASSERT_NOCARRY (mpn_mul_1 (qp + l - k - j, qp + l - j, j, rho)); + MULDIV_PREINV (c, rho, rho, rho_j, vn, vinv, cnt); + MPN_INCR_U (qp + l - k - j, j, c); + } + + /* The high h limbs of the result, from the first h limbs of 1/v. */ + rho_j = - qp[l - h] * v; + ASSERT_NOCARRY (mpn_mul_1 (qp + l, qp + l - h, h, rem)); + MULDIV_PREINV (c, rem, rem, rho_j, vn, vinv, cnt); + MPN_INCR_U (qp + l, h, c); + + /* The low l limbs, from all of 1/v, in place. */ + ASSERT_NOCARRY (mpn_mul_1 (qp, qp, l, rem)); + MULDIV_PREINV (c, rem, rem, rho, vn, vinv, cnt); + MPN_INCR_U (qp, l, c); +} void mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v) @@ -82,6 +197,25 @@ up = u->_mp_d; tsize = 1 + prec; + + if (tsize - usize >= MPF_DIV_UI_FRAC_THRESHOLD) + { + /* A short dividend. Divide the limbs it has, and make the fraction + limbs below them from the remainder. */ + mp_size_t fn = tsize - usize; + mp_limb_t rem; + + tp = TMP_ALLOC_LIMBS (usize); + MPN_COPY (tp, up, usize); /* r and u can be the same */ + + rem = mpn_divrem_1 (rp + fn, (mp_size_t) 0, tp, usize, (mp_limb_t) v); + if (rem == 0) + MPN_ZERO (rp, fn); + else + mpf_div_ui_frac (rp, fn, rem, (mp_limb_t) v); + goto done; + } + tp = TMP_ALLOC_LIMBS (tsize + 1); if (usize > tsize) @@ -100,6 +234,8 @@ MPN_COPY (rtp, up, usize); mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v); + + done: q_limb = rp[tsize - 1]; rsize = tsize - (q_limb == 0);