n_mod_precomp
#include <flint/ulong_extras.h>n_mod_precomp takes the quotient to be a * ninv truncated to an integer, and corrects the remainder by at most one multiple of n. This suffices: the processor is assumed to round to nearest, so ninv is correct to 53 bits (call the weight of the last bit a place) and is at most half a place out. Since a is exact and the mantissa of ninv is below 2, the product a * ninv is at most one place out; truncating it to an integer gives either the exact quotient or one less, because half a place is less than 1 when the product is below n < 2^{53}. The only other case is that the product was rounded up to an integer, which implies that a/n lies within 2^{-54} of an integer; this is handled by subtracting 1 from the quotient. The computed quotient is therefore exact or one too small, and a single correction step suffices.
The FLINT team, “n_mod_precomp (ulong_extras.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/ulong_extras/n_mod_precomp.html (generated 2026-10-20).
@misc{flint-doc-ulong-extras-n-mod-precomp,
author = {{The FLINT team}},
title = {{n\_mod\_precomp (ulong\_extras.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/ulong_extras/n_mod_precomp.html},
note = {Generated 2026-10-20}
}