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

冷暖自知 提交于 2019-12-01 01:17:26

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/3242256/how-does-gmp-stores-its-integers-on-an-arbitrary-number-of-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!