tags:

views:

196

answers:

3

I am looking for a library for big integers but with fixed width (128 or 256 would be enough). The reason is I don't want any allocation on the heap. I tried to make them myself but implementing multiplication, division and modulo an efficient way seems to be quite a pain.

Does this already exists somewhere ?

Thanks

+4  A: 

Take a look at the GMP library: www.gmplib.org

Quoting from the function categories:

Low-level positive-integer, hard-to-use, very low overhead functions are found in the mpn category. No memory management is performed; the caller must ensure enough space is available for the results. (...)

That seems to be what you need.

Nils Pipenbrinck
nice ! I am having a look on this
Ben
+3  A: 

This at least looks promising (hit number 8 for int128 library on Google).

http://www.mx-3.cz/tringi/www/langen.php?id=int128

"Unlike other large number classes, you can work with these just like with other P.O.D. types (for example store and load from files using fwrite/fread). Internal representation of these is correct 128-bit little-endian integer."

Steve Jessop
+1  A: 

If you find GMP too complicated for your taste, Dave Hanson has some very nice functions in his book C Interfaces and Implementations. There is a low-level interface that does no allocation (you control everything), and then there are two higher-level interfaces that manage progressively more allocation on the heap.

Norman Ramsey
I made it work with gmp, but I'll have a look on this
Ben