| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
16.1 Integer Internals
mpz_t variables represent integers using sign and magnitude, in space
dynamically allocated and reallocated. The fields are as follows.
_mp_sizeThe number of limbs, or the negative of that when representing a negative integer. Zero is represented by
_mp_sizeset to zero, in which case the_mp_ddata is unused._mp_dA pointer to an array of limbs which is the magnitude. These are stored “little endian” as per the
mpnfunctions, so_mp_d[0]is the least significant limb and_mp_d[ABS(_mp_size)-1]is the most significant. Whenever_mp_sizeis non-zero, the most significant limb is non-zero.Currently there’s always at least one limb allocated, so for instance
mpz_set_uinever needs to reallocate, andmpz_get_uican fetch_mp_d[0]unconditionally (though its value is then only wanted if_mp_sizeis non-zero)._mp_alloc_mp_allocis the number of limbs currently allocated at_mp_d, and naturally_mp_alloc >= ABS(_mp_size). When anmpzroutine is about to (or might be about to) increase_mp_size, it checks_mp_allocto see whether there’s enough space, and reallocates if not.MPZ_REALLOCis generally used for this.
The various bitwise logical functions like mpz_and behave as if
negative values were twos complement. But sign and magnitude is always used
internally, and necessary adjustments are made during the calculations.
Sometimes this isn’t pretty, but sign and magnitude are best for other
routines.
Some internal temporary variables are setup with MPZ_TMP_INIT and these
have _mp_d space obtained from TMP_ALLOC rather than the memory
allocation functions. Care is taken to ensure that these are big enough that
no reallocation is necessary (since it would have unpredictable consequences).
_mp_size and _mp_alloc are int, although mp_size_t
is usually a long. This is done to make the fields just 32 bits on
some 64 bits systems, thereby saving a few bytes of data space but still
providing plenty of range.
| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.
