gmp_gcdext method on 32bit vs 64 bit machines

Marc Glisse marc.glisse at inria.fr
Sat Apr 30 07:57:17 CEST 2011


On Fri, 29 Apr 2011, Emmanuel Thomé wrote:

> On Fri, Apr 29, 2011 at 04:54:18PM -0400, Waiki Wright wrote:
>>   mpz_init_set_d(B,23434293864345);
>
> I was just about to say ``you sure know about compiler warnings, don't
> you ?''.
>
> truffe /tmp $ gcc-4.4 -m32 -W -Wall zozo.c -lgmp
> zozo.c: In function ‘main’:
> zozo.c:8: warning: integer constant is too large for ‘long’ type
> zozo.c:9: warning: integer constant is too large for ‘long’ type
>
> However it very much seems that gcc-4.5 now silently drops this warning
> (I tested 4.5.1 and 4.5.2, but with the 32-bit ABI of my 64-bit laptop).
> While your mistake is pretty obvious, I am slightly amazed to see that
> this potentially useful warning for a silly mistake is no longer emitted.
>
> You might want to file a bug for gcc (please search the BTS first to see
> if it hasn't been reported already).

gcc warns for mpz_init_set_ui but not mpz_init_set_d apparently.

To the OP, mpz_init_set_str is the simplest for this use.

>>   mpz_init(S);
>>   mpz_init(T);
>>   mpz_init(G);
>>   mpz_gcdext(G,S,T,A,B);
>>
>>   mpz_out_str(stdout, 10, G);
>>   printf("\n");
>>   mpz_out_str(stdout, 10, S);
>>   printf("\n");
>>   mpz_out_str(stdout, 10, T);
>>   printf("\n");

And here gmp_printf would help.

>>   return 0;
>> }
>> 
>> On a 32 bit machine, the returned g,s,t are: 195, -4760039754357, 6965866288
>> On a 64 bit machine, the returned g,s,t are: 195, -20983781660442,
>> 30707772373

With a recent GMP and using mpz_init_set_str:
195
46994884483
-68772552
(both 32 and 64 bits)

>> On the 32 bit machine, I am using: gmp-4.3.1, gcc version is 4.5.1
>> and the "uname -a" is:
>> Linux oc2517815826.ibm.com 2.6.35.9-64.fc14.i686 #1 SMP Fri Dec 3
>> 12:35:42 UTC 2010 i686 i686 i386 GNU/Linux
>> 
>> On the 64 bit machine, I am using: gmp-4.3.1, gcc version is 4.5.1
>> and the "uname -a" is:
>> Linux oc0144164261.ibm.com 2.6.35.9-64.fc14.x86_64 #1 SMP Fri Dec 3
>> 12:19:41 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux

And you are still using an old gmp version.

>> The question is why the values of s and t are different on 32 bit
>> and 64 bit machines?

As I told you last time, the documentation allows it to. It would even 
allow it to return randomly one or the other depending on the phase of the 
moon, because both are correct answers. Computations are done differently 
on a machine where your numbers fits in a long and a machine where it 
doesn't.

Now in more recent versions, GMP seems to canonicalize the answer better, 
and it would indeed be nice if the documentation reflected this.

-- 
Marc Glisse


More information about the gmp-discuss mailing list