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.
An independent GMP 6.3.0 experiment
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.
Once this state is certified, the remaining limbs can be filled with the same value.
Compared with stock GMP 6.3.0. One host per architecture.
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.
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
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.
| Input | ARM64 | x86-64 |
|---|---|---|
| Generic short inputs | 1.25–6.89× faster | 1.29–7.55× faster |
| Below the threshold | 2.43% slower | 0.77% slower |
| Full-length dividends | 2.73% slower | 2.04% slower |
| Exact division | 14.68–35.95× faster | 17.85–61.23× faster |
| Half-length inputs | 1.25–1.95× faster | 1.13–1.91× faster |
The setup
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.
When mpf_div_ui needs more quotient limbs than the input provides, the original path divides a dividend padded with zeros.
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.
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
Here one limb is a byte, with values from 0 to 255. The same identity applies to the benchmark’s much larger base, 264.
| Step | Remainder in | Limb out | Remainder out |
|---|---|---|---|
| 1 | 2 | 170 | 2 |
| 2 | 2 | 170 | 2 |
| 3 | 2 | 170 | 2 |
| 4 | 2 | 170 | 2 |
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
Stock and proposed builds use the same inputs and precision on each host. Timings are for repeated inputs.
Nanoseconds per call · shorter is faster
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.
| Divisor | Fraction limbs | Stock GMP 6.3.0 | Proposed enhancement | Compared with stock |
|---|---|---|---|---|
| 8 | 25 | 82.6 | 15.4 | 5.36× |
| 8 | 1,025 | 3,427.7 | 112.5 | 30.46× |
| 8 | 16,385 | 54,984.4 | 1,460.0 | 37.66× |
| 3 | 25 | 82.7 | 15.4 | 5.37× |
| 3 | 1,025 | 3,434.6 | 113.4 | 30.28× |
| 3 | 16,385 | 55,000.0 | 1,493.7 | 36.82× |
| 10 | 25 | 83.3 | 66.0 | 1.26× |
| 10 | 1,025 | 3,446.3 | 603.3 | 5.71× |
| 10 | 16,385 | 55,015.6 | 8,263.7 | 6.66× |
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.
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
Review one combined patch against stock GMP 6.3.0: reciprocal-prefix growth, direct fills for fixed remainders, and regression tests.
Download the review bundleSource, patches, proof, runner, and raw reference evidence. No compiled binaries.
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.