Conversion between mp_limb_t's and mpz_t's

Sisyphus sisyphus1 at optusnet.com.au
Fri Sep 23 06:56:38 CEST 2005


Hi,
How do I efficiently fill an array of mp_limb_t's with the value contained
in an mpz_t ?

I can do it by copying limb-by-limb as per this simple code:

--------------------------------
#include <gmp.h>

int main(void) {
mpz_t x;
mp_limb_t  * xp;
unsigned long i;

mpz_init_set_str(x, "1234567890987654321234567890987654321", 16);

xp = malloc(sizeof(mp_limb_t) * mpz_size(x));

for(i = 0; i < mpz_size(x); ++i) xp[i] = mpz_getlimbn(x, i);

// Check that it looks right:
for(i = 0; i < mpz_size(x); ++i) printf("%u %u\n", xp[i], mpz_getlimbn(x,
i));

mpz_clear(x);
free(xp);

return 0;
}
------------------------

Is there a smarter way of filling xp with the appropriate values ?
Also, how to best do the reverse operation (assuming an mpz_t of sufficient
storage space already exists) ? - as there's no mpz_setlimbn() function,
afaict.

Cheers,
Rob




More information about the gmp-discuss mailing list