Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries

Daniel Lemire, Owen Kaser, Nathan Kurz
2/5/2019
cs.MScs.PF

Official code: lemire/constantdivisionbenchmarks (11 stars, C++, last commit 2018-11-23).

Abstract

On common processors, integer multiplication is many times faster than integer division. Dividing a numerator n by a divisor d is mathematically equivalent to multiplication by the inverse of the divisor (n / d = n x 1/d).

Show full abstract

If the divisor is known in advance---or if repeated integer divisions will be performed with the same divisor---it can be beneficial to substitute a less costly multiplication for an expensive division. Currently, the remainder of the division by a constant is computed from the quotient by a multiplication and a subtraction. But if just the remainder is desired and the quotient is unneeded, this may be suboptimal. We present a generally applicable algorithm to compute the remainder more directly. Specifically, we use the fractional portion of the product of the numerator and the inverse of the divisor. On this basis, we also present a new, simpler divisibility algorithm to detect nonzero remainders. We also derive new tight bounds on the precision required when representing the inverse of the divisor. Furthermore, we present simple C implementations that beat the optimized code produced by state-of-art C compilers on recent x64 processors (e.g., Intel Skylake and AMD Ryzen), sometimes by more than 25%. On all tested platforms including 64-bit ARM and POWER8, our divisibility-test functions are faster than state-of-the-art Granlund-Montgomery divisibility-test functions, sometimes by more than 50%.

View on arXivView PDF

Code Implementations(1)

Benchmarks for constant-division problems (not a library! for research only!)

110C++, CApr 2, 20188 years ago

Similar Papers

Cite this paper

@article{lemire2019faster,
  title  = {Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries},
  author = {Daniel Lemire and Owen Kaser and Nathan Kurz},
  year   = {2019},
  eprint = {1902.01961},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/1902.01961}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/1902.01961.svg)](https://codeofpaper.com/paper/1902.01961)

Discussion