Number of limbs for an unsigned long

Torbjorn Granlund tg at gmplib.org
Tue Mar 15 16:29:13 CET 2011


Marc Glisse <marc.glisse at inria.fr> writes:

  what is the best way to check how many limbs are necessary to be able
  to store any unsigned long?
  
  mpz_set_si has the simplest test of all: two limbs if there are nails,
  1 otherwise. Is that safe/wasteful?
  
In gmp.h, we have (slightly edited for clarity):

#ifdef __GMP_SHORT_LIMB
typedef unsigned int		mp_limb_t;
#else
#ifdef _LONG_LONG_LIMB
typedef unsigned long long int	mp_limb_t;
#else
typedef unsigned long int	mp_limb_t;
#endif
#endif

There is no way of getting __GMP_SHORT_LIMB set, so we should perhaps
remove it.  (But if GMP became more used in low-end embedded
environments, i.e., 8-bit processors, we may want to allow for a very
small limb.)

So, disregarding the unused __GMP_SHORT_LIMB, the answer is that
mpz_set_si is right; you may assume any unsigned long fits in limb
unless we have nails.  For nails, we never supported a nail greater than
half the limb.

For nails (which incidentally do not work since GMP 5.0, but might be
revived) you need to check the actual value.  You may make the logic
more compex, since with LONG_LONG_LIMBS and nails, any unsinged long
will fit...

-- 
Torbjörn


More information about the gmp-devel mailing list