Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does GMP stores its integers, on an arbitrary number of bytes?

2^64 is still far from the "infinity" my ram/hard drive can handle...

First I wonder how GMP works with memory/processor since it does some kind of shady optimisations...

I was also wondering if there is a way of storing an integer (unsigned, it's easier) on an arbitrary number of bytes. For example, on 50 bytes, I would have a cap of 2^400 -1. The thing to do is to work well with carries to keep the number consistent from one byte to another, I have some idea about that, but I'm really not sure it would be the fastest way to do this. I'm not even sure if I'm right.

I'm guessing GMP uses this kind of way to store its data, but I just want some (even little) explanation or some forwarding to some theory (I don't have any doctorate, so don't be tough).

like image 858
gokoon Avatar asked Jul 13 '10 23:07

gokoon


1 Answers

GMP dynamically allocates space to represent numbers (and reallocates when it needs to grow).

This is described in light detail in Integer Internals, in the GMP manual, it describes how it chunks up the representation into "limbs" and stores the limbs in an array.

The description of the term "limbs" comes from GMP Basics: Nomenclature and Types:

A limb means the part of a multi-precision number that fits in a single word. (We chose this word because a limb of the human body is analogous to a digit, only larger, and containing several digits.) Normally a limb contains 32 or 64 bits. The C data type for a limb is mp_limb_t.

So, representing a number in GMP works by grouping a number of limbs together to represent the magnitude of the integer, stored with a sign bit (the sign bit is dual purposed to store the number of limbs).

What does this mean to you? Well, normally an int64 is represented in 64 bits. Done. If you package a bunch of these together, you can significantly increase that. Put two together, 2^64*2^64, or 2^128. Add two more limbs and you get 2^256. That's a lot of numbers, stored in 4 words (plus the representation overhead).

Of course, the representation of floats is more complicated (see here), storing the representation using a mantissa (consisting of a sign and magnitude) and an exponent.

like image 179
Stephen Avatar answered Oct 04 '22 20:10

Stephen