Division using GMP's low-level API

Bernhard Lindner private at bernhard-lindner.de
Sat Aug 27 23:01:20 CEST 2022

Hello all,

I'm using GMP's low-level interface (mpn_,
https://gmplib.org/manual/Low_002dlevel-Functions) to do some fixed-size 192 bit (three
limb) unsigned integer calculations.

Currently I'm trying to divide one random non-zero uint192 by another random non-zero
uint192 and fail to select the right function. There are multiple candidates:

* mpn_tdiv_qr - Documentation says "most significant limb of the divisor must be non-zero"
* mpn_divrem - Documentation says "obsolete"
* mpn_divrem_1 - Takes a single limb as a divisor
* mpn_divexact_1 - Documentation says "expecting ... to divide exactly"

So there is an obsolete function (mpn_divrem), a function that takes small (<= 64bit)
divisors only (mpn_divrem_1), one that has undefined behavior with remainders
(mpn_divexact_1) and one that takes large (>= 129bit) divisors only (mpn_tdiv_qr).

I tried mpn_tdiv_qr() (without limb overlaps) since I thought I'm misunderstanding the
documentation and because it replaces the obsolete function. But it actually crashes when
the most significant limb is zero.

Did I miss something? How can I divide two random uint192 non-zero numbers using mpn_?

Best Regards,
Bernhard Lindner

More information about the gmp-discuss mailing list