Need help with mpz_t...

David Cleaver wraithx at morpheus.net
Mon Jun 14 06:32:23 CEST 2010


My apologies, I found out that the problem was actually with my own printf 
statement.  When I was displaying the 2 64-bit int's, I used %016x which only 
showed the lower 32-bits.  When I switched that to %016I64x it showed all 
64-bits, and then I could see that yes, the GMP number was the concatenation of 
the 2 64-bit ints.

I found another way to do this that looks much better in code, but is about 5x 
slower in practice:

for (j = 0; j < 64; j++)
{
   if (results2[i]&((u64_t)1<<j))
     mpz_setbit(my_result, j+64);
   if (results1[i]&((u64_t)1<<j))
     mpz_setbit(my_result, j);
}/* end for */

However, if anyone knows of a faster way to do this conversion, I'd still like 
to hear about it.  Thanks for your time.

-David C.

David Cleaver wrote:
> Hello all,
> 
> I've run into some trouble trying to "load" mpz_t's with 64-bit int's.  
> I was wondering if someone here could show me what I'm doing wrong or 
> suggest a better way to accomplish the following:
> 
> I have 2 64-bit numbers.  They are both an unsigned long long.  I am 
> compiling on Windows with mingw64.  I am trying to just basically 
> concatenate the 2 64-bit numbers into 1 128-bit mpz_t.
> 
> Here is what I am currently trying:
> mpz_set_ui(my_result, 0);
> mpz_add_ui(my_result, my_result, (u32_t)((results2[i]>>32)&0xffffffff));
> mpz_mul_2exp(my_result, my_result, 32);
> mpz_add_ui(my_result, my_result, (u32_t)(results2[i]&0xffffffff));
> mpz_mul_2exp(my_result, my_result, 32);
> mpz_add_ui(my_result, my_result, (u32_t)((results1[i]>>32)&0xffffffff));
> mpz_mul_2exp(my_result, my_result, 32);
> mpz_add_ui(my_result, my_result, (u32_t)(results1[i]&0xffffffff));
> 
> (I've typedef'd u32_t to be unsigned int)
> 
> It seems the bottom 32-bits from both inputs are correct in the output.  
> But the top 32-bits are 'not-always' correct in the output.  Here are a 
> few examples of what the code above is producing for me:
> 
> 00000000000100000000000010000020 (top number is 2 ULL next to each other)
> 00002000000100000000000010000020 (bottom number is mpz_t from above code)
> 
> 00000000000000000000000000000000
> 00020000000000000400000000000000
<snip>


More information about the gmp-discuss mailing list