gmpxx and allocation

Marc Glisse marc.glisse at
Tue Mar 6 09:07:52 CET 2012

On Tue, 6 Mar 2012, bodrato at wrote:

> Il Mar, 6 Marzo 2012 1:07 am, Marc Glisse ha scritto:
>> int main(){
>>    mpz_class a=1;
>>    for(int i=0;i<5000000;i++){
>>      mpz_class b=2;
>>      a=a+b-a*b;
>>    }
>>    return a.get_si()-1;
>> }
> ... If you substitute mpz_class with int, and compile it with gcc -O2, the
> assembler code generated for the function main is:
>        xorl    %eax, %eax
>        ret
> I do not know C++ very well... but it would be very nice if we could
> suggest to the compiler how optimise operation when the "value" of classes
> could be argued at compile time.

Indeed. But that may be hard to do without compiler support (and my 
compiler skills are not sufficient to write a gcc plugin for that).

> Is it possible to write something like
> if (__GMPXX_CONSTANT (SIZ (b)) && SIZ (b)==1) ... ?
> I don't know if this may have some chance to be optimised...

You can certainly write it, but since all those values are set in opaque 
gmp functions, __builtin_constant_p will return false. I tried things like 
that some time ago, and if I remember correctly gcc sometimes managed to 
notice at compile time that a variable was 0 (I don't think it managed 
to notice a variable was 1, but I am not sure).

With the small number optimization, if the fast path is inline and doesn't 
use any asm (or has a __builtin_constant_p branch without asm), the 
compiler should have a chance at simplifying at compile-time.

> My comment goes in a completely orthogonal direction, but I hope it is not
> meaningless :-)

Tangential, orthogonal, any angle is fine with me ;-)

Marc Glisse

More information about the gmp-discuss mailing list