186d7f5d3SJohn Marino /* gmp_urandomm_ui -- uniform random number 0 to N-1 for ulong N.
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Copyright 2003, 2004 Free Software Foundation, Inc.
486d7f5d3SJohn Marino
586d7f5d3SJohn Marino This file is part of the GNU MP Library.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1086d7f5d3SJohn Marino option) any later version.
1186d7f5d3SJohn Marino
1286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1586d7f5d3SJohn Marino License for more details.
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1886d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
1986d7f5d3SJohn Marino
2086d7f5d3SJohn Marino #include "gmp.h"
2186d7f5d3SJohn Marino #include "gmp-impl.h"
2286d7f5d3SJohn Marino #include "longlong.h"
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino /* If n is a power of 2 then the test ret<n is always true and the loop is
2686d7f5d3SJohn Marino unnecessary, but there's no need to add special code for this. Just get
2786d7f5d3SJohn Marino the "bits" calculation correct and let it go through normally.
2886d7f5d3SJohn Marino
2986d7f5d3SJohn Marino If n is 1 then will have bits==0 and _gmp_rand will produce no output and
3086d7f5d3SJohn Marino we always return 0. Again there seems no need for a special case, just
3186d7f5d3SJohn Marino initialize a[0]=0 and let it go through normally. */
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marino #define MAX_URANDOMM_ITER 80
3486d7f5d3SJohn Marino
3586d7f5d3SJohn Marino unsigned long
gmp_urandomm_ui(gmp_randstate_ptr rstate,unsigned long n)3686d7f5d3SJohn Marino gmp_urandomm_ui (gmp_randstate_ptr rstate, unsigned long n)
3786d7f5d3SJohn Marino {
3886d7f5d3SJohn Marino mp_limb_t a[LIMBS_PER_ULONG];
3986d7f5d3SJohn Marino unsigned long ret, bits, leading;
4086d7f5d3SJohn Marino int i;
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino if (UNLIKELY (n == 0))
4386d7f5d3SJohn Marino DIVIDE_BY_ZERO;
4486d7f5d3SJohn Marino
4586d7f5d3SJohn Marino /* start with zeros, since if bits==0 then _gmp_rand will store nothing at
4686d7f5d3SJohn Marino all (bits==0 arises when n==1), or if bits <= GMP_NUMB_BITS then it
4786d7f5d3SJohn Marino will store only a[0]. */
4886d7f5d3SJohn Marino a[0] = 0;
4986d7f5d3SJohn Marino #if LIMBS_PER_ULONG > 1
5086d7f5d3SJohn Marino a[1] = 0;
5186d7f5d3SJohn Marino #endif
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino count_leading_zeros (leading, (mp_limb_t) n);
5486d7f5d3SJohn Marino bits = GMP_LIMB_BITS - leading - (POW2_P(n) != 0);
5586d7f5d3SJohn Marino
5686d7f5d3SJohn Marino for (i = 0; i < MAX_URANDOMM_ITER; i++)
5786d7f5d3SJohn Marino {
5886d7f5d3SJohn Marino _gmp_rand (a, rstate, bits);
5986d7f5d3SJohn Marino #if LIMBS_PER_ULONG == 1
6086d7f5d3SJohn Marino ret = a[0];
6186d7f5d3SJohn Marino #else
6286d7f5d3SJohn Marino ret = a[0] | (a[1] << GMP_NUMB_BITS);
6386d7f5d3SJohn Marino #endif
6486d7f5d3SJohn Marino if (LIKELY (ret < n)) /* usually one iteration suffices */
6586d7f5d3SJohn Marino goto done;
6686d7f5d3SJohn Marino }
6786d7f5d3SJohn Marino
6886d7f5d3SJohn Marino /* Too many iterations, there must be something degenerate about the
6986d7f5d3SJohn Marino rstate algorithm. Return r%n. */
7086d7f5d3SJohn Marino ret -= n;
7186d7f5d3SJohn Marino ASSERT (ret < n);
7286d7f5d3SJohn Marino
7386d7f5d3SJohn Marino done:
7486d7f5d3SJohn Marino return ret;
7586d7f5d3SJohn Marino }
76