Type-generic macros for GMP with C11's _Generic keyword

Marc Glisse marc.glisse at inria.fr
Mon Jun 14 21:08:14 UTC 2021


On Mon, 14 Jun 2021, John Scott wrote:

> One feature I would like to see in GMP and haven't found discussed on
> this mailing list before are type-generic macros for functions. This
> would not require C11 support to build GMP, as a C11 compiler would
> only be needed when an application includes the GMP header file, and
> the macros could be guarded by checking __STDC_VERSION__.
>
> A naive implementation could look something like this for example:
>
> #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
> #define mpz_set(X, Y) _Generic((Y), \
> 	default: mpz_set, \
> 	unsigned long int: mpz_set_ui, \
> 	signed long int: mpz_set_si, \
> 	double: mpz_set_d, \
> 	const mpq_t: mpz_set_q, \
> 	const mpf_t: mpz_set_f)(X, Y)
> #endif

Why not a gmp_set that would dispatch on both X and Y while you are at it?

I'd like to see where and how you would use those macros, some examples. 
If I have a double d, what do I gain by writing mpz_set(z, d) instead of 
mpz_set_d(z, d)? Or would they only be used in other generic macros? The 
motivation could help convince someone to implement it.

In general, I am not particularly enthusiastic about the strange ways C 
tries to provide alternatives to C++ features. Why not use a language with 
generics baked in?

> Since the user-friendly function names like mpz_set are themselves
> defined as macros, this isn't feasible as-is and is not suitable to
> include in applications. It seems implementing this will require more
> knowledge of GMP internals and the macro trickery in gmp.h.
>
> Perhaps this is someone's low-hanging fruit, or one could help me to
> understand GMP conventions and put together a patch.

-- 
Marc Glisse


More information about the gmp-discuss mailing list