Expand description
Implementation of constant-time division via reciprocal precomputation, as described in “Improved Division by Invariant Integers” by Niels Möller and Torbjorn Granlund (DOI: 10.1109/TC.2010.143, https://gmplib.org/~tege/division-paper.pdf).
Structs§
- Reciprocal
- A pre-calculated reciprocal for division by a single limb.
Functions§
- div2by1 🔒
- Calculate the quotient and the remainder of the division of a wide word
(supplied as high and low words) by
d, with a precalculated reciprocalv. - div3by2 🔒
- Given two long integers
u = (..., u0, u1, u2)andv = (..., v0, v1)(whereu2andv1are the most significant limbs), wherefloor(u / v) <= Limb::MAX, calculatesqsuch thatq - 1 <= floor(u / v) <= q. In place ofv1takes its reciprocal, and assumes thatvwas already pre-shifted so that v1 has its most significant bit set (that is, the reciprocal’sshiftis 0). - mul_rem 🔒
- Computes
(a * b) % d. - reciprocal
- Calculates the reciprocal of the given 64-bit divisor with the highmost bit set.
- rem_
limb_ 🔒with_ reciprocal - Divides
uby the divisor encoded in thereciprocal, and returns the remainder. - short_
div 🔒 - Calculates
dividend / divisor, givendividendanddivisoralong with their maximum bitsizes.