Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert uint64 to GMP/MPIR number

Tags:

c++

gmp

uint64

I am using MPIR 2.4.0 on Windows (MSVC 2010) and I was trying to add an unsigned 64bit integer to a mpz_t number. However it seems that MPIR/GMP does not support direct conversion between 64bit Integers and mpz_t. Does this mean I have to convert my uint64 to a string and read this via mpz_init_set_str? Neither is this very appealing nor does it look very quick - two conversion for nothing.

Did I miss something or what is the trick/hack to use here?

Cheers,

Philipp

like image 754
ThE_-_BliZZarD Avatar asked Dec 16 '22 12:12

ThE_-_BliZZarD


2 Answers

As suggested by Banthar use mpz_import, but I'd suggest the following which does not rely on the platform endianness:

mpz_import(b, 1, 1, sizeof(a), 0, 0, &a);
like image 74
Frank Avatar answered Dec 19 '22 02:12

Frank


Use mpz_import:

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

EDIT: Code updated according to Frank's comment.

like image 22
Piotr Praszmo Avatar answered Dec 19 '22 03:12

Piotr Praszmo