__builtin_constant_p for trivial operations

Marc Glisse marc.glisse at inria.fr
Sat Sep 17 09:27:46 CEST 2011


On Sun, 20 Feb 2011, Torbjorn Granlund wrote:

> Marc Glisse <marc.glisse at inria.fr> writes:
>
>  would something like the attached ugliness be acceptable? (don't take
>  it as a real patch, it wasn't properly tested and is incomplete)
>
> I think we cannot change e.g. mpz_add_ui from a function to a macro,
> since we have documented it as a function.  There are surely programs
> out there which make a function pointer, perhaps through a function
> table, to mpz_add_ui.

Actually, something similar should still be possible:
#define __gmpz_add_ui(X,Y,Z) \
   __builtin_constant_p(Z) ? ... : (__gmpz_add_ui)(X,Y,Z)
lets &__gmpz_add_ui return the address to the library function. And no 
inline function ;-)

>  The goal is that, if I have a C++ mpz_class variable z, z*=-1 and z/2
>  get translated to mpz_neg and mpz_tdiv_q_2exp instead of mpz_mul_si
>  and mpz_tdiv_q_ui. With zero runtime penalty.
>
> Could that not be made at the C++ level?

That's what I did a few weeks ago, as you may have seen in the commits.

-- 
Marc Glisse


More information about the gmp-discuss mailing list