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

John Scott jscott at posteo.net
Mon Jun 14 20:22:41 UTC 2021


Hello,

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

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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: This is a digitally signed message part
URL: <https://gmplib.org/list-archives/gmp-discuss/attachments/20210614/ba77317c/attachment.bin>


More information about the gmp-discuss mailing list