To: gmp-devel@gmplib.org
Subject: mpf_div_ui with a short dividend, and a manual note on summing rationals

Hello,

Two small patches, both with measurements that can be repeated.  They are
attached, and they are also at

    https://github.com/mikedotexe/gmp-patch

with a script that builds 6.3.0 stock and patched, runs the test suite and
times the two taking turns.  The patches are against 6.3.0, and they also
apply unchanged to the repository as mirrored at github.com/gmp-mirror/gmp.


1. mpf_div_ui when the dividend is shorter than the result

When u has fewer limbs than the precision of r, mpf_div_ui pads u with
zero limbs and runs all of them through mpn_divrem_1, one division step
for each limb of the quotient.  The low limbs of that quotient are the
fraction limbs of rem/v, and those can be made by multiplying.  With
U = floor (B^k / v) and rho = B^k mod v,

    floor (B^(k+j) / v) = U * B^j + rho * U_j + floor (rho * rho_j / v)

for j <= k, where U_j is the high j limbs of U and rho_j = B^j mod v.
So the next j limbs are one mpn_mul_1 and a one-limb carry.  It is
Newton's iteration for 1/v, exact because the residual is one limb.
And rho_j needs no division: it is minus the low limb of U_j, times v.

The patch only adds lines.  A new static function of about thirty lines
grows 1/v in the low half of the result, makes the high half as rem times
those limbs, and makes the low half in place.  It allocates nothing.
mpf_div_ui calls it when the fraction part is at least 24 limbs, and the
existing code is not touched.

mpf_div_ui of a one-limb dividend by 10, nanoseconds per call, on an
x86-64 (Xeon 2.3 GHz, gcc 13.3, GMP's own flags), medians of runs in
which the two libraries took turns:

    limbs       stock     patched
       16        68.3        67.0     (old code in both)
       24        93.3        73.1
       64         220         112
     1024        3240        1000
    65536      212102       60848

The two methods met between 12 and 20 limbs on that machine, depending on
the divisor and on whether assembly is used.  With --disable-assembly the
gain is 1.18 times at 24 limbs and 1.95 times at 65536.  The threshold is
a #define in the file and has not been tuned per CPU.  With a dividend as
long as the result the old code runs as before, and those timings are
unchanged.

A new test, tests/mpf/t-div_ui.c, checks every precision up to 300 limbs
and random ones up to 2000 against refmpf_validate_division.  The test
suite passes with the patch, and the mpf tests pass with --enable-assert
--enable-alloca=debug.  I have not been able to test 32-bit limbs.

Something I noticed on the way, separate from the patch: for the padded
limbs mpf_div_ui takes about 3.2 ns per limb here, while mpn_divrem_1
asked for fraction limbs through qxn takes about 2.7.  mpf_set_q pads in
the same way.


2. A note for the manual on summing many rationals

Adding n rationals to one mpq_t total takes time quadratic in n, since
every mpq_add goes through the whole total.  Adding them in pairs does
not, and a few partial sums kept like the digits of a binary counter give
the same pairing when the terms arrive one at a time.  This is folklore,
and GMP does the same for the products in mpz_fac_ui, but the Efficiency
section doesn't mention it.  The patch adds two paragraphs to the
"Rational Arithmetic" item.  Nanoseconds per term on the same machine,
stock 6.3.0:

                             terms    one total    in pairs
    1/2 + 1/3 + 1/4 + ...    10000          669         266
                            100000         5728         581
    a/b, nine-digit b        10000         6695        1410
                            100000        56938        2697

With one denominator for every term the total stays small and there is
no gain, which is why the text says "with different denominators".


How this was made, since you should know.  I worked these out over many
days with an AI assistant (Claude), which also wrote the code and ran
the measurements under my direction.  I have read the change and can
answer questions about it.  If that rules the patches out for GMP, the
idea and the numbers are yours to use as you see fit, and the test and
the benchmark may still be useful.

[If you are willing to sign the FSF's copyright papers, say so here.]

Thank you for GMP.

YOUR NAME
