Changes to mp_bases table

Torbjorn Granlund tg at
Sun Aug 7 21:49:41 CEST 2011

I checked in a change eliminating the chars_per_limb_exactly field from
gmp-impl.h's struct bases.  it is replaced by two new fields, log2b and
logb2, which are of the type mp_limb_t.

This change will improve the accuracy of size computation wihin GMP.

Until now, we have relied on the libm log() function (of the compilaion
hosts) to give us accurate enough values, and we have trusted this value
(at most 53 bits) too much.  For the 64-bit exponent in mpf_t, things
could go very wrong: there could be too many or too few (such as 0)
digits printed, and conversion could cause crashes and buffer overruns.
Such bad behaviour would only occur when printing numbers with really
huge exponents (the exact point determined by libm's log function's

With the change, we get the precision we need, and our own gen-bases.c
computes the logarithms correctly to the last bit.

There should also be no more dependencies on the arithmetic on 'double'
or 'float' within GMP now (unless compilers use it for integer types).
This might be good for some embedded systems without FPU.

Finally, the change should speed up some conversion operations slightly,
as we no longer depend on slow (floating poin) division.  Instead, we
need word * word => doubleword integer multiply, which is usually very
fast on today's machines; on machines where it is not, GMP's arithmetic
will already run very poorly!

There are still scalar integer divisions by the chars_per_limb field in
a few places.  This is a performance problem that ought to be addressed.


More information about the gmp-devel mailing list