Radix integer division#2580
Merged
fredrik-johansson merged 6 commits intoflintlib:mainfrom Feb 15, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
radix_divremradix_divrem_preinvradix_divexactradix_div(checked exact division)radix_inv_approxand various algorithm variants. Plus wrappers for
radix_integer_t:radix_integer_divradix_integer_divexactradix_integer_tdiv_qradix_integer_tdiv_rradix_integer_tdiv_qrradix_integer_fdiv_qradix_integer_fdiv_rradix_integer_fdiv_qrradix_integer_cdiv_qradix_integer_cdiv_rradix_integer_cdiv_qrAsymptotically, division is implemented using an approximate Newton reciprocal ($O(M(n))$ complexity. The code uses fixed-point arithmetic with short and middle products to get a good practical constant factor. Middle product isn't optimized to use the FFT trick yet, but that's a missing optimization in
radix_inv_approx) withradix_mulmidwhich is independent of the division code.Shortcomings of this code:
mpn_tdiv_qr, and convert back. This solution isn't awful, but it's not necessarily optimal either.This can be regarded as something of a prototype for implementing similar division code for$3 B^{-n}$ . It should be possible to prove such a bound either for the current algorithm or for a slightly tweaked algorithm using similar techniques as in https://members.loria.fr/PZimmermann/papers/invert.pdf (note that
mpn, a priori error bounds for the Newton reciprocal being the main missing ingredient. Empirically, the relative error is smallerradix_inv_approxis more general as it doesn't assume an even radix or normalized input). Ideally an error bound should be done for the full Karp-Markstein division, though.Profile of division with remainder versus our
mpndivision code (there is actually noflint_mpn_divremyet; I mocked one up for this profile program usingmpn_tdiv_qrfor small input andfmpz_tdiv_qrfor large input):