Reduced number of allocated limbs after calling mpz_remove

Niels Möller nisse at lysator.liu.se
Mon May 20 16:40:20 CEST 2024


Albin Ahlbäck <albin.ahlback at gmail.com> writes:

> In the project I help maintaining, we have started to rely on that mpz
> never reduce the number of limbs. This is to keep a minimum number of
> limbs allocated to 2, so that if one where to do a multiplication of
> two limbs, the result is guaranteed to fit inside the mpz without
> having to do a reallocation.

Thanks for explaining. To me, it seems a bit brittle to rely in this.

Could you use mpz_limbs_write (x, 2) for the functions that want a
result area of at least two limbs? Added in gmp-6-0.0. There's also the
mpz_realloc2 function, which does something related, but which may also
shrink the allocation.

> Since this indeed seems to be the exception, I am fine with either
> documenting that this indeed is the exception (perhaps it should then
> be stated under the section "Memory management" as well as the
> docstring for `mpz_remove`), or fixing this in `mpz_remove`.

I don't have a strong opinion on how to fix this discrepancy between
docs and implementation. But I feel rather strongly that mini-gmp
shpuldn't make this promise, so code that rely on it will not work with
mini-gmp.

Regards,
/Niels

-- 
Niels Möller. PGP key CB4962D070D77D7FCB8BA36271D8F1FF368C6677.
Internet email is subject to wholesale government surveillance.


More information about the gmp-bugs mailing list