direct conversion from int64_t

Atze Dijkstra atze at xs4all.nl
Tue Mar 10 22:59:54 CET 2009


Thanks! mpz_import + mpz_export works fine.

For mpz_export I'd suggest to also allow truncation (i.e. throwing  
away most significant bits beyond a limit, in my case 64 bits),  
perhaps by using *countp also as an input parameter. As it is  
described now in the manual, to make the result fit in 64 bits I now  
have to either (1) mask the given mpz before exporting, or (2)  
allocate enough memory, and take what is needed from the result.  
Because all is running in a garbage collected environment (for a  
functional/side-effect free language implementation) I'd prefer to  
avoid that.

regards,

On  10 Mar, 2009, at 22:14 , Marc Glisse wrote:

> On Tue, 10 Mar 2009, Atze Dijkstra wrote:
>
>> I'd like to convert a int64_t directly to an mpz. As far as I can  
>> tell function 'mpz_set_si' is the most direct way to do this, but  
>> the 'signed long int' parameter turns out to be 32 bits only  
>> (MacOSX 10.5.6). Is there any way to directly convert a 64 bits  
>> value without splitting a 64 bits value into a most + least  
>> significant part, building separates mpz for these parts and  
>> shifting + multiplying these (or do 2 to+from string  
>> conversions...)? Am I overlooking alternatives to mpz_set_si, or is  
>> conversion from/to 64 bits values not supported on 32 bit platforms?.
>
> Does mpz_import (big hammer) work for you?
>
> There is regularly talk about adding support for long long, but I  
> have no idea how far away that may be.
>
> (there are also ways to do this by hand, but it would be preferable  
> to stick to the official API so it does not break with the next  
> version of gmp)
>
> Note that even with your solution you don't need to create two  
> mpz_t, you can create one from the high 32 bits, shift it, and  
> add_ui the low 32 bits (still need to take care of the sign).
>
> -- 
> Marc Glisse


                 - Atze -

Atze Dijkstra, Department of Information and Computing Sciences. /|\
Utrecht University, PO Box 80089, 3508 TB Utrecht, Netherlands. / | \
Tel.: +31-30-2534118/1454 | WWW  : http://www.cs.uu.nl/~atze . /--|  \
Fax : +31-30-2513971 .... | Email: atze at cs.uu.nl ............ /   |___\





More information about the gmp-discuss mailing list