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

02/05/2019
by   Daniel Lemire, et al.
0

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). 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 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

READ FULL TEXT
research
03/02/2017

Faster truncated integer multiplication

We present new algorithms for computing the low n bits or the high n bit...
research
01/30/2019

Egyptian multiplication and some of its ramifications

Fast exponentiation with an integer exponent relies on squaring of the b...
research
12/22/2020

Integer Division by Constants: Optimal Bounds

The integer division of a numerator n by a divisor d gives a quotient q ...
research
02/06/2022

Alpha Blending with No Division Operations

Highly accurate alpha blending can be performed entirely with integer op...
research
09/30/2020

Communication-Optimal Parallel Standard and Karatsuba Integer Multiplication in the Distributed Memory Model

We present COPSIM a parallel implementation of standard integer multipli...
research
10/06/2022

Towards the Multiple Constant Multiplication at Minimal Hardware Cost

Multiple Constant Multiplication (MCM) over integers is a frequent opera...
research
04/09/2022

An Improved Integer Modular Multiplicative Inverse (modulo 2^w)

This paper presents an algorithm for the integer multiplicative inverse ...

Please sign up or login with your details

Forgot password? Click here to reset