diff options
author | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2003-06-25 10:16:43 +0000 |
---|---|---|
committer | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2003-06-25 10:16:43 +0000 |
commit | e81472a1d9c12d70c474f2fe93c5020e412ec993 (patch) | |
tree | 885144858efe785157af8b2f2c36818b988e913d /urandomb.c | |
parent | 37d801c1de9b48e6ab9ccfe68c7a30c8f798733e (diff) | |
download | mpfr-e81472a1d9c12d70c474f2fe93c5020e412ec993.tar.gz |
fixed several problems noticed by Kevin
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@2372 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'urandomb.c')
-rw-r--r-- | urandomb.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/urandomb.c b/urandomb.c index 423359c8e..2ac85ec01 100644 --- a/urandomb.c +++ b/urandomb.c @@ -34,6 +34,7 @@ mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate) mp_ptr rp; mp_prec_t nbits; mp_size_t nlimbs; + mp_size_t k; /* number of high zero limbs */ mp_exp_t exp; int cnt; @@ -43,16 +44,19 @@ mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate) nbits = MPFR_PREC(rop); nlimbs = (nbits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB; - _gmp_rand (rp, rstate, nbits); + _gmp_rand (rp, rstate, nlimbs * BITS_PER_MP_LIMB); - /* If nbits isn't a multiple of BITS_PER_MP_LIMB, shift up. */ - if (nbits % BITS_PER_MP_LIMB != 0) - mpn_lshift (rp, rp, nlimbs, BITS_PER_MP_LIMB - nbits % BITS_PER_MP_LIMB); + /* If nbits isn't a multiple of BITS_PER_MP_LIMB, mask the low bits */ + cnt = nlimbs * BITS_PER_MP_LIMB - nbits; + if (cnt != 0) + rp[0] &= ~((MP_LIMB_T_ONE << cnt) - MP_LIMB_T_ONE); exp = 0; + k = 0; while (nlimbs != 0 && rp[nlimbs - 1] == 0) { - nlimbs--; + k ++; + nlimbs --; exp -= BITS_PER_MP_LIMB; } @@ -66,11 +70,9 @@ mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate) return 1; } if (cnt != 0) - mpn_lshift (rp, rp, nlimbs, cnt); - - cnt = (mp_prec_t) nlimbs * BITS_PER_MP_LIMB - nbits; - /* cnt is the number of non significant bits in the low limb */ - rp[0] &= ~((MP_LIMB_T_ONE << cnt) - 1); + mpn_lshift (rp + k, rp, nlimbs, cnt); + if (k) + MPN_ZERO (rp, k); } MPFR_SET_POS (rop); |