GMP 6.3.0: structured fraction-limb generation Replace serial zero-padded fraction division with exact reciprocal-prefix growth and direct fills for terminating or fixed remainder states. The default threshold remains 24 fraction limbs; nails use the stock path. Includes focused mpf_div_ui regression coverage and test registration. This consolidated stock-to-candidate diff reproduces the tested implementation byte for byte. For the release tarball, also apply the accompanying tests-mpf-Makefile.in.patch to register the test without autoreconf. --- 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); --- /dev/null +++ b/tests/mpf/t-div_ui.c @@ -0,0 +1,309 @@ +/* Test mpf_div_ui. + +Copyright 2026 Free Software Foundation, Inc. + +This file is part of the GNU MP Library test suite. + +The GNU MP Library test suite is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 3 of the License, +or (at your option) any later version. + +The GNU MP Library test suite is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +Public License for more details. + +You should have received a copy of the GNU General Public License along with +the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ + +#include +#include +#include "gmp-impl.h" +#include "tests.h" + + +/* mpf_div_ui makes the low limbs of a quotient differently when the + dividend is much shorter than the precision of the result. The sizes + here go well past that point on both sides. */ + +#ifndef SWEEP_PREC +#define SWEEP_PREC 300 +#endif + +static const unsigned long vdata[] = { + 1, 2, 3, 5, 7, 10, 255, 256, 257, 641, 1000, 65535, 65536, 65537, + ULONG_HIGHBIT - 1, ULONG_HIGHBIT, ULONG_HIGHBIT + 1, + ULONG_MAX / 3, ULONG_MAX / 3 + 1, ULONG_MAX - 2, ULONG_MAX - 1, ULONG_MAX, +}; + +void +check_one (const char *desc, mpf_ptr got, mpf_srcptr u, unsigned long v) +{ + mpf_t vf; + mp_limb_t vlimbs[2]; + mp_size_t vsize; + + vlimbs[0] = v & GMP_NUMB_MASK; + vsize = (v != 0); +#if BITS_PER_ULONG > GMP_NUMB_BITS + vlimbs[1] = v >> GMP_NUMB_BITS; + vsize += (vlimbs[1] != 0); +#endif + PTR(vf) = vlimbs; + SIZ(vf) = vsize; + EXP(vf) = vsize; + + if (! refmpf_validate_division ("mpf_div_ui", got, u, vf)) + { + mp_trace_base = -16; + mpf_trace (" u", u); + printf (" v 0x%lX (%lu)\n", v, v); + printf (" %s\n", desc); + abort (); + } +} + +/* Short dividends at every precision up to SWEEP_PREC, so every length of + fraction part is seen. */ +void +check_sizes (void) +{ + unsigned long min_prec = __GMPF_BITS_TO_PREC (1); + gmp_randstate_ptr rands = RANDS; + unsigned long prec, v; + mpf_t got, u; + size_t i; + + mpf_init (got); + mpf_init (u); + refmpf_set_prec_limbs (u, 3L); + + for (prec = min_prec; prec <= SWEEP_PREC; prec++) + { + refmpf_set_prec_limbs (got, prec); + + for (i = 0; i < numberof (vdata); i++) + { + v = vdata[i]; + + /* 1/v */ + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, v); + check_one ("1/v", got, u, v); + + /* (v-1)/v, the largest remainder */ + if (v > 1) + { + mpf_set_ui (u, v - 1); + mpf_div_ui (got, u, v); + check_one ("(v-1)/v", got, u, v); + } + + /* -v/v, an exact quotient */ + mpf_set_ui (u, v); + mpf_neg (u, u); + mpf_div_ui (got, u, v); + check_one ("-v/v", got, u, v); + } + + /* one, two or three random limbs over a random v */ + do { + v = gmp_urandomb_ui (rands, gmp_urandomm_ui (rands, BITS_PER_ULONG+1)); + } while (v == 0); + mpf_random2 (u, (mp_size_t) (1 + prec % 3), (mp_exp_t) 20); + mpf_div_ui (got, u, v); + check_one ("sizes, random", got, u, v); + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_rand (int reps) +{ + unsigned long min_prec = __GMPF_BITS_TO_PREC (1); + gmp_randstate_ptr rands = RANDS; + unsigned long prec, uprec, v; + mpf_t got, u; + int i; + + mpf_init (got); + mpf_init (u); + + for (i = 0; i < reps; i++) + { + /* got precision, now and then a long one */ + if ((i % 16) == 0) + prec = min_prec + gmp_urandomm_ui (rands, 2000L); + else + prec = min_prec + gmp_urandomm_ui (rands, 150L); + refmpf_set_prec_limbs (got, prec); + + /* u precision, mostly short against got, sometimes longer than it */ + if ((i % 4) == 0) + uprec = min_prec + gmp_urandomm_ui (rands, prec + 5); + else + uprec = min_prec + gmp_urandomm_ui (rands, 6L); + refmpf_set_prec_limbs (u, uprec); + + /* u, non-zero, possibly negative */ + do { + mpf_random2 (u, PREC(u), (mp_exp_t) 20); + } while (SIZ(u) == 0); + if (gmp_urandomb_ui (rands, 1L)) + mpf_neg (u, u); + + /* v, non-zero */ + do { + v = gmp_urandomb_ui (rands, gmp_urandomm_ui (rands, BITS_PER_ULONG+1)); + } while (v == 0); + + if ((i % 2) == 0) + { + /* src != dst */ + mpf_div_ui (got, u, v); + check_one ("separate", got, u, v); + } + else + { + /* src == dst */ + prec = refmpf_set_overlap (got, u); + mpf_div_ui (got, got, v); + check_one ("overlap src==dst", got, u, v); + + mpf_set_prec_raw (got, prec); + } + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_various (void) +{ + mpf_t got, u; + + mpf_init (got); + mpf_init (u); + + /* 100/4 == 25 */ + mpf_set_prec (got, 20L); + mpf_set_ui (u, 100L); + mpf_div_ui (got, u, 4L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0); + + /* 1/2^3 == 0.125, with a long run of zero limbs below it */ + refmpf_set_prec_limbs (got, 500L); + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, 8L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (mpf_cmp_d (got, 0.125) == 0); + + /* 1/3 is the same limb over and over, when a limb has an even number of + bits */ + if (GMP_NUMB_BITS % 2 == 0) + { + mp_size_t i; + + refmpf_set_prec_limbs (got, 500L); + mpf_set_ui (u, 1L); + mpf_div_ui (got, u, 3L); + MPF_CHECK_FORMAT (got); + ASSERT_ALWAYS (EXP(got) == 0); + ASSERT_ALWAYS (SIZ(got) == 500); + for (i = 0; i < 500; i++) + ASSERT_ALWAYS (PTR(got)[i] == GMP_NUMB_MAX / 3); + } + + mpf_clear (got); + mpf_clear (u); +} + +void +check_fixed_states (void) +{ +#if GMP_NUMB_BITS >= 3 + static const unsigned long divisors[] = { 8, 3 }; + static const unsigned long precs[] = { 23, 24, 25 }; + mpf_t got, u; + mp_limb_t limb; + mp_size_t n, j; + unsigned long v; + size_t vi, pi; + int neg, overlap; + + mpf_init (got); + mpf_init (u); + + for (vi = 0; vi < numberof (divisors); vi++) + { + v = divisors[vi]; + if (v == 3 && GMP_NUMB_BITS % 2 != 0) + continue; + + /* With B = 2^GMP_NUMB_BITS, B mod v is 0 for v=8 and 1 for + v=3. Use rem=v-1 > 1: filling in place must not overwrite + the reciprocal seed before multiplying it by rem. */ + limb = GMP_NUMB_MAX / v + (v == 8); + limb *= v - 1; + + for (pi = 0; pi < numberof (precs); pi++) + { + n = precs[pi]; + refmpf_set_prec_limbs (got, precs[pi]); + for (neg = 0; neg <= 1; neg++) + for (overlap = 0; overlap <= 1; overlap++) + { + mpf_set_ui (u, v - 1); + if (neg) + mpf_neg (u, u); + + /* A one-limb dividend gives exactly PREC(got) + fractional limbs, straddling the default threshold 24. */ + ASSERT_ALWAYS (ABS (SIZ(u)) == 1); + ASSERT_ALWAYS (PREC(got) == n); + if (overlap) + { + mpf_set (got, u); + mpf_div_ui (got, got, v); + } + else + mpf_div_ui (got, u, v); + + MPF_CHECK_FORMAT (got); + check_one (overlap ? "fixed state, overlap" : "fixed state", + got, u, v); + ASSERT_ALWAYS (EXP(got) == 0); + ASSERT_ALWAYS (SIZ(got) == (neg ? -n : n)); + for (j = 0; j < n; j++) + ASSERT_ALWAYS (PTR(got)[j] + == (v == 3 || j == n - 1 ? limb : 0)); + } + } + } + + mpf_clear (got); + mpf_clear (u); +#endif +} + +int +main (int argc, char **argv) +{ + int reps = 2000; + + tests_start (); + TESTS_REPS (reps, argv, argc); + + check_various (); + check_fixed_states (); + check_sizes (); + check_rand (reps); + + tests_end (); + exit (0); +} --- a/tests/mpf/Makefile.am +++ b/tests/mpf/Makefile.am @@ -23,7 +23,7 @@ LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la check_PROGRAMS = t-dm2exp t-conv t-add t-sub t-sqrt t-sqrt_ui t-muldiv reuse \ - t-cmp_d t-cmp_si t-div t-fits t-get_d t-get_d_2exp \ + t-cmp_d t-cmp_si t-div t-div_ui t-fits t-get_d t-get_d_2exp \ t-get_si t-get_ui t-gsprec t-inp_str t-int_p t-mul_ui \ t-set t-set_q t-set_si t-set_ui t-trunc t-ui_div t-eq t-pow_ui TESTS = $(check_PROGRAMS)