gmp_gcdext method on 32bit vs 64 bit machines

Waiki Wright waiki at linux.vnet.ibm.com
Fri Apr 29 22:54:18 CEST 2011


>> This list is for GMP, the C (and C++) library, not the PHP interface, 
>> so you could try and reproduce this with a C program, and preferably 
>> using the latest GMP version.
>>
>> The guarantees promised by the documentation are only: "abs(s) <= 
>> abs(b) and abs(t) <= abs(a)" which allows for many valid outputs. I 
>> believe the code was changed to better canonicalize the result in 
>> later versions (though it isn't documented), search the archives of 
>> the lists if you are interested.
>>
>> In 64 bits, your numbers fit in 1 limb (an unsigned long), while in 
>> 32 bits they require 2 limbs, so it isn't too surprising that the 
>> computations could turn out differently.
>>
Here is my C program and I ran it on Fedora 14 on both 32 and 64 bit 
machines:


#include<stdio.h>
#include<gmp.h>

int main()
{
   mpz_t A,B,S,T,G;
   mpz_init_set_d(A,34293864345);
   mpz_init_set_d(B,23434293864345);
   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");

   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

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

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

Thanks!



More information about the gmp-discuss mailing list