using mpz_import/export with unsigned long longs

Alan McFarlane alan.mcfarlane at gmail.com
Thu Jun 16 21:53:25 CEST 2005


Hi folks,

I've been experimenting with the mpz_import/mpz_export function with 
unsigned long longs (unger GCC/MinGW/i386) and have produced the 
following few simple routines:

[code]

void mpz_set_ull( mpz_t rop, unsigned long long op )
{
   mpz_import(rop, sizeof(unsigned long long), -1, 1, 0, 0, &op);
}

int mpz_fits_ulonglong_p( mpz_t op )
{
   mpz_t limit;
   int   result;

   mpz_init_set_ull(limit, 0xFFFFFFFFFFFFFFFFULL); // 2^64-1

   result = (mpz_cmp(op, limit) <= 0);

   mpz_clear(limit);

   return result;
}

unsigned long long mpz_get_ull( mpz_t op )
{
   /* NOTE: no bounds checking */
   unsigned long long rop = 0;

   mpz_export(&rop, NULL, -1, 1, 0, 0, op);

   return rop;
}

[/code]

Can anyone confirm if I have written these correctly, or suggest any 
alterations that could be made.

Thanks in advance

--
Alan McFarlane



More information about the gmp-discuss mailing list