mpz_limbs interface

Zimmermann Paul Paul.Zimmermann at inria.fr
Tue Jan 21 14:28:00 UTC 2014


       Hi,

we have looked at the mpz_limbs interface, it seems it won't be sufficient for
our needs in MPFR. Consider for example the following function, which
generates nbits random bits into mp[]:

/* generate nbits random bits into mp[], assuming mp was allocated to contain
   a sufficient number of limbs */
void
mpfr_rand_raw (mpfr_limb_ptr mp, gmp_randstate_t rstate,
               mpfr_prec_t nbits)
{
  mpz_t z;

  MPFR_ASSERTN (nbits >= 1);
  /* To be sure to avoid the potential allocation of mpz_urandomb */
  ALLOC(z) = SIZ(z) = MPFR_PREC2LIMBS (nbits);
  PTR(z)   = mp;
#if __MPFR_GMP(5,0,0)
  /* Check for integer overflow (unless mp_bitcnt_t is signed,
     but according to the GMP manual, this shouldn't happen).
     Note: mp_bitcnt_t has been introduced in GMP 5.0.0. */
  MPFR_ASSERTN ((mp_bitcnt_t) -1 < 0 || nbits <= (mp_bitcnt_t) -1);
#endif
  mpz_urandomb (z, rstate, nbits);
}

The only mpz_limbs function that takes as input an array of limbs is
mpz_roinit_n(), but in this function X is read-only, and in mpfr_rand_raw()
above z is not read-only.

Thus we need a function that creates a valid mpz_t from given alloc and size
fields.

Paul

PS: when doing "make check" with mpfr development version and the gmp snapshot
from last night, I get one failure:

Seed GMP_CHECK_RANDOMIZE=1391280408 (include this in bug reports)
repl-vsnprintf.c:379: GNU MP assertion failed: len < total_width
/bin/bash: line 5:  7656 Aborted                 (core dumped) MPFR_QUIET=1 ${dir}$tst
FAIL: tprintf



More information about the gmp-devel mailing list