diff options
author | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-11-08 19:38:29 +0100 |
---|---|---|
committer | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2015-11-08 19:38:29 +0100 |
commit | 5f3a83c93822be0ac82b6d5f590d528fdbd65dc2 (patch) | |
tree | 375fe493bb76931a92225d559d4e2fdd4c898f57 /mpz/urandomm.c | |
parent | 6f98a9bd3cd26131ad5729751e2775d3e45d753d (diff) | |
download | gmp-5f3a83c93822be0ac82b6d5f590d528fdbd65dc2.tar.gz |
mpz/urandomm.c: Use mpn_zero_p to shorten code.
Diffstat (limited to 'mpz/urandomm.c')
-rw-r--r-- | mpz/urandomm.c | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/mpz/urandomm.c b/mpz/urandomm.c index 434359cdd..44126211a 100644 --- a/mpz/urandomm.c +++ b/mpz/urandomm.c @@ -2,7 +2,7 @@ integer in the range 0 to N-1, using STATE as the random state previously initialized by a call to gmp_randinit(). -Copyright 2000, 2002, 2012 Free Software Foundation, Inc. +Copyright 2000, 2002, 2012, 2015 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -40,8 +40,9 @@ see https://www.gnu.org/licenses/. */ void mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) { - mp_ptr rp, np, nlast; + mp_ptr rp, np; mp_size_t nbits, size; + mp_limb_t nh; int count; int pow2; int cmp; @@ -51,19 +52,13 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) if (UNLIKELY (size == 0)) DIVIDE_BY_ZERO; - nlast = &PTR (n)[size - 1]; + np = PTR (n); + nh = np[size - 1]; /* Detect whether n is a power of 2. */ - pow2 = POW2_P (*nlast); - if (pow2 != 0) - for (np = PTR (n); np < nlast; np++) - if (*np != 0) - { - pow2 = 0; /* Mark n as `not a power of two'. */ - break; - } - - count_leading_zeros (count, *nlast); + pow2 = POW2_P (nh) && (size == 1 || mpn_zero_p (np, size - 1)); + + count_leading_zeros (count, nh); nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2; if (nbits == 0) /* nbits == 0 means that n was == 1. */ { @@ -72,7 +67,6 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) } TMP_MARK; - np = PTR (n); if (rop == n) { mp_ptr tp; @@ -83,7 +77,7 @@ mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) /* Here the allocated size can be one too much if n is a power of (2^GMP_NUMB_BITS) but it's convenient for using mpn_cmp below. */ - rp = MPZ_REALLOC (rop, size); + rp = MPZ_NEWALLOC (rop, size); /* Clear last limb to prevent the case in which size is one too much. */ rp[size - 1] = 0; |