_ptr and _srcptr types

Niels Möller nisse at lysator.liu.se
Tue May 15 20:47:40 UTC 2018

tg at gmplib.org (Torbjörn Granlund) writes:

> Emmanuel Thomé <Emmanuel.Thome at inria.fr> writes:
>   I've personally always used the mpz_ptr and mpz_srcptr in functions that
>   take mpz arguments, taking the liberty to use this undocumented type.
> I suppose we could declare these.  I'd like to hear from the rest of the
> GMP "core team".

My fairly strong opinion is that we should document them. They are
important for user code that needs to handle pointers to mpz_t values.
One simple use case:

  void foo (mp_srcptr a, mp_srcptr b) 
    if (mpz_cmp (a, b) < 0)
        mp_srcptr t = a;
        a = b;
        b = t;
    ... code that requires a >= b ...

This gets a lot more awkward if the function has to be declared using
documented types, as

  void foo (const mpz_t a, const mpz_t b)

Simplest alternative I see would be to copy into local non-const mpz_t
variables and use mpz_swap, and that's an extra copy I don't want to
force users to make.

Functions returning pointers to an mpz_t is another important case
(mpz_roinit_n is one (unusual) example, spotted by Emmanuel). mpq_numref
and mpq_denref too, if they were implemented as (inline) functions
rather than macros.

I don't think need for handling pointers is that common in GMP user
code, but not so obscure that it should be ignored and unsupported.


Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.

More information about the gmp-devel mailing list