Blame rand/randmui.c

Packit 5c3484
/* gmp_urandomm_ui -- uniform random number 0 to N-1 for ulong N.
Packit 5c3484
Packit 5c3484
Copyright 2003, 2004 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* If n is a power of 2 then the test ret
Packit 5c3484
   unnecessary, but there's no need to add special code for this.  Just get
Packit 5c3484
   the "bits" calculation correct and let it go through normally.
Packit 5c3484
Packit 5c3484
   If n is 1 then will have bits==0 and _gmp_rand will produce no output and
Packit 5c3484
   we always return 0.  Again there seems no need for a special case, just
Packit 5c3484
   initialize a[0]=0 and let it go through normally.  */
Packit 5c3484
Packit 5c3484
#define MAX_URANDOMM_ITER  80
Packit 5c3484
Packit 5c3484
unsigned long
Packit 5c3484
gmp_urandomm_ui (gmp_randstate_ptr rstate, unsigned long n)
Packit 5c3484
{
Packit 5c3484
  mp_limb_t      a[LIMBS_PER_ULONG];
Packit 5c3484
  unsigned long  ret, bits, leading;
Packit 5c3484
  int            i;
Packit 5c3484
Packit 5c3484
  if (UNLIKELY (n == 0))
Packit 5c3484
    DIVIDE_BY_ZERO;
Packit 5c3484
Packit 5c3484
  /* start with zeros, since if bits==0 then _gmp_rand will store nothing at
Packit 5c3484
     all (bits==0 arises when n==1), or if bits <= GMP_NUMB_BITS then it
Packit 5c3484
     will store only a[0].  */
Packit 5c3484
  a[0] = 0;
Packit 5c3484
#if LIMBS_PER_ULONG > 1
Packit 5c3484
  a[1] = 0;
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
  count_leading_zeros (leading, (mp_limb_t) n);
Packit 5c3484
  bits = GMP_LIMB_BITS - leading - (POW2_P(n) != 0);
Packit 5c3484
Packit 5c3484
  for (i = 0; i < MAX_URANDOMM_ITER; i++)
Packit 5c3484
    {
Packit 5c3484
      _gmp_rand (a, rstate, bits);
Packit 5c3484
#if LIMBS_PER_ULONG == 1
Packit 5c3484
      ret = a[0];
Packit 5c3484
#else
Packit 5c3484
      ret = a[0] | (a[1] << GMP_NUMB_BITS);
Packit 5c3484
#endif
Packit 5c3484
      if (LIKELY (ret < n))   /* usually one iteration suffices */
Packit 5c3484
        goto done;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* Too many iterations, there must be something degenerate about the
Packit 5c3484
     rstate algorithm.  Return r%n.  */
Packit 5c3484
  ret -= n;
Packit 5c3484
  ASSERT (ret < n);
Packit 5c3484
Packit 5c3484
 done:
Packit 5c3484
  return ret;
Packit 5c3484
}