An independent GMP 6.3.0 experiment

When a remainder
predicts the rest.

Grow fraction limbs with multiplication. When a remainder proves the tail, fill it directly.

A proposed enhancement to stock GMP 6.3.0. Every requested output limb is still produced, exactly. Producing n limbs still takes O(n) work.

Same remainder, same next limbB = 256
23=0.170170170…
remainder 2remainder 2
2 × 256 = 170 × 3 + 2

Once this state is certified, the remaining limbs can be filled with the same value.

5.34–39.10×
ARM64 speedupTargeted short inputs
3.77–12.00×
x86-64 speedupTargeted short inputs

Compared with stock GMP 6.3.0. One host per architecture.

What counts as a targeted short input?

The dividend fits in one 64-bit limb, division leaves a nonzero remainder, and the output needs at least 24 extra fraction limbs. The divisor v gives 264 mod v equal to 0 or 1, so the tail terminates like 7/8 or repeats one limb like 2/3.

A case that ran slower

On ARM64, a full-length dividend divided by 3 at 1,024 limbs of requested precision needed no extra fraction limbs.

2.73% slower 3,433.6 ns stock · 3,527.3 ns proposed

The same case was 1.12% faster in the other run. See the full controls.

Keep the controls

Measure the ordinary cases, too.

The slowest full-length case on each host was faster in the other run. With identical code under both labels, the A/A control showed up to 3.10% apparent slowdown on ARM64 and 2.65% on x86-64.

Compared with stock over two runs per host. Speedups are ranges. Slowdowns are maxima.
InputARM64x86-64
Generic short inputs1.25–6.89× faster1.29–7.55× faster
Below the threshold2.43% slower0.77% slower
Full-length dividends2.73% slower2.04% slower
Exact division14.68–35.95× faster17.85–61.23× faster
Half-length inputs1.25–1.95× faster1.13–1.91× faster

The setup

Big numbers,
small building blocks.

GMP, the GNU Multiple Precision Arithmetic Library, performs arithmetic on numbers larger than a machine’s built-in number types.

It stores those numbers in chunks called limbs. Think of each limb as one very large digit. In both measured builds, a limb holds 64 bits, so its digit base is 264.

Stock GMP 6.3.0

Divide through the zero padding

When mpf_div_ui needs more quotient limbs than the input provides, the original path divides a dividend padded with zeros.

Proposed enhancement · general path

Grow a prefix with multiplication

A known prefix of 1/v and its remainder determine the next block. The proposed helper grows that prefix in the output space using multiplication, then incorporates the numerator remainder.

Proposed enhancement · fixed states

Recognize a fixed remainder

The first reciprocal remainder can already prove that the output ends in zeros or repeats one limb. One short branch handles those two cases directly.

See the state, then the shortcut

Try a smaller base.

Here one limb is a byte, with values from 0 to 255. The same identity applies to the benchmark’s much larger base, 264.

Illustration only · base 256

Emitted limbs

4 of 8 shown
170170170170————
Ordinary long division: remainder × 256 = limb × divisor + next remainder.
StepRemainder inLimb outRemainder out
121702
221702
321702
421702

One small branch inside the enhancement

The seed satisfies B = seed·v + ρ, with incoming remainder 0 < rem < v.

If ρ = 0: rem·B = (rem·seed)·v. The first limb is rem·seed; all later limbs are zero.

If ρ = 1: rem·B = (rem·seed)·v + rem. That same remainder produces that same limb at every step.

The product fits in one limb. Saving it before the fill matters: the fill overwrites the workspace containing the seed.

if (rho <= 1)
  {
    c = rem * qp[l - 1];
    MPN_FILL (qp, n - 1,
              rho ? c : CNST_LIMB (0));
    qp[n - 1] = c;
    return;
  }

Compared with stock GMP 6.3.0

Explore the timings.

Stock and proposed builds use the same inputs and precision on each host. Timings are for repeated inputs.

Divisor 8 · 1,025 fraction limbs

Nanoseconds per call · shorter is faster

Stock GMP 6.3.0
3,427.7 ns
Proposed enhancement
112.5 ns
30.46× fasterthan stock GMP 6.3.0 for this case.

Each number is the median of two runs’ per-round medians. Both runs used 15 shuffled rounds. Bars use one linear scale within the selected case.

Inspect all nine measurements · ARM64
Nanoseconds per call. Fraction limbs are actual output fraction lengths, not the harness’s requested precision. Ratios use timings before display rounding.
DivisorFraction limbsStock GMP 6.3.0Proposed enhancementCompared with stock
82582.615.45.36×
81,0253,427.7112.530.46×
816,38554,984.41,460.037.66×
32582.715.45.37×
31,0253,434.6113.430.28×
316,38555,000.01,493.736.82×
102583.366.01.26×
101,0253,446.3603.35.71×
1016,38555,015.68,263.76.66×

Correctness came first.

Each host passed 54,264 exact comparisons, including aliased inputs. The native and portable GMP suites each passed 178 tests with one skip on both hosts. Sanitizers checked the changed C and harness.

Two hosts, same fixture grid.

ARM64 used macOS and Apple clang. x86-64 used Amazon Linux 2023 and GCC 11.5 on an Intel Xeon Platinum 8488C. Both builds used 64-bit limbs and the 24-limb threshold.

Each host ran 73 cases twice plus one identical-code control. ARM64 comparison · x86-64 comparison · x86-64 raw data and logs.

For reviewers and benchmarkers

Read it.
Run it.
Challenge it.

Review one combined patch against stock GMP 6.3.0: reciprocal-prefix growth, direct fills for fixed remainders, and regression tests.

Download the review bundle

Source, patches, proof, runner, and raw reference evidence. No compiled binaries.

Reproduce in a fresh directory

tar -xzf gmp-fraction-review.tar.gz
cd gmp-fraction-review
python3 gmp_study/reproduce.py --output /tmp/gmp-fraction-run

Use a new output path. Requires Python 3.10+, a C build toolchain, and the tools listed in the reviewer guide. The runner supports Linux/macOS, 64-bit limbs and unsigned long, no nails, and no LTO.