示例#1
0
void mpn_randomb(mp_ptr rp, gmp_randstate_t rnd, mp_size_t n)
{
   ASSERT(n > 0);

   _gmp_rand(rp, rnd, n*GMP_NUMB_BITS);

   while (rp[n - 1] == 0)
      _gmp_rand(rp + n - 1, rnd, GMP_NUMB_BITS);

   return;
}
示例#2
0
文件: misc.c 项目: Cl3Kener/gmp
mp_limb_t
urandom (void)
{
#if GMP_NAIL_BITS == 0
  mp_limb_t  n;
  _gmp_rand (&n, RANDS, GMP_LIMB_BITS);
  return n;
#else
  mp_limb_t n[2];
  _gmp_rand (n, RANDS, GMP_LIMB_BITS);
  return n[0] + (n[1] << GMP_NUMB_BITS);
#endif
}
示例#3
0
文件: misc.c 项目: HRF92/mpir
mp_limb_t
urandom (gmp_randstate_t rands)
{
#if GMP_NAIL_BITS == 0
  mp_limb_t  n;
  _gmp_rand (&n, rands, BITS_PER_MP_LIMB);
  return n;
#else
  mp_limb_t n[2];
  _gmp_rand (n, rands, BITS_PER_MP_LIMB);
  return n[0] + (n[1] << GMP_NUMB_BITS);
#endif
}
示例#4
0
文件: random2.c 项目: romance-ii/ecl
void
mpf_random2 (mpf_ptr x, mp_size_t xs, mp_exp_t exp)
{
    mp_size_t xn;
    mp_size_t prec;
    mp_limb_t elimb;

    xn = ABS (xs);
    prec = PREC(x);

    if (xn == 0)
    {
        EXP(x) = 0;
        SIZ(x) = 0;
        return;
    }

    if (xn > prec + 1)
        xn = prec + 1;

    /* General random mantissa.  */
    mpn_random2 (PTR(x), xn);

    /* Generate random exponent.  */
    _gmp_rand (&elimb, RANDS, GMP_NUMB_BITS);
    exp = ABS (exp);
    exp = elimb % (2 * exp + 1) - exp;

    EXP(x) = exp;
    SIZ(x) = xs < 0 ? -xn : xn;
}
示例#5
0
int
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;

  MPFR_CLEAR_FLAGS (rop);

  rp = MPFR_MANT (rop);
  nbits = MPFR_PREC (rop);
  nlimbs = MPFR_LIMB_SIZE (rop);
  MPFR_SET_POS (rop);

  /* Uniform non-normalized significand */
  _gmp_rand (rp, rstate, nlimbs * 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 (MPFR_LIKELY (cnt != 0))
    rp[0] &= ~MPFR_LIMB_MASK (cnt);

  /* Count the null significant limbs and remaining limbs */
  exp = 0;
  k = 0;
  while (nlimbs != 0 && rp[nlimbs - 1] == 0)
    {
      k ++;
      nlimbs --;
      exp -= BITS_PER_MP_LIMB;
    }

  if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
    {
      count_leading_zeros (cnt, rp[nlimbs - 1]);
      /* Normalization */
      if (mpfr_set_exp (rop, exp - cnt))
        {
          /* If the exponent is not in the current exponent range, we
             choose to return a NaN as this is probably a user error.
             Indeed this can happen only if the exponent range has been
             reduced to a very small interval and/or the precision is
             huge (very unlikely). */
          MPFR_SET_NAN (rop);
          __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
          return 1;
        }
      if (cnt != 0)
        mpn_lshift (rp + k, rp, nlimbs, cnt);
      if (k != 0)
        MPN_ZERO (rp, k);
    }
  else
    MPFR_SET_ZERO (rop);

  return 0;
}
示例#6
0
文件: misc.c 项目: Cl3Kener/gmp
void
mpz_negrandom (mpz_ptr rop, gmp_randstate_t rstate)
{
  mp_limb_t  n;
  _gmp_rand (&n, rstate, 1);
  if (n != 0)
    mpz_neg (rop, rop);
}
示例#7
0
void
mpn_random (mp_ptr ptr, mp_size_t size)
{
  gmp_randstate_ptr  rands;

  /* FIXME: Is size==0 supposed to be allowed? */
  ASSERT (size >= 0);

  if (size == 0)
    return;

  rands = RANDS;
  _gmp_rand (ptr, rands, size * GMP_NUMB_BITS);

  /* Make sure the most significant limb is non-zero.  */
  while (ptr[size-1] == 0)
    _gmp_rand (&ptr[size-1], rands, GMP_NUMB_BITS);
}
示例#8
0
static void
gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mpir_ui nbits)
{
  mpir_ui bi;
  mp_limb_t ranm;		/* buffer for random bits */
  unsigned cap_chunksize, chunksize;
  mp_size_t i;

  /* Set entire result to 111..1  */
  i = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS - 1;
  rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS;
  for (i = i - 1; i >= 0; i--)
    rp[i] = GMP_NUMB_MAX;

  _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
  cap_chunksize = nbits / (ranm % 4 + 1);
  cap_chunksize += cap_chunksize == 0; /* make it at least 1 */

  bi = nbits;

  for (;;)
    {
      _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
      chunksize = 1 + ranm % cap_chunksize;
      bi = (bi < chunksize) ? 0 : bi - chunksize;

      if (bi == 0)
	break;			/* low chunk is ...1 */

      rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS;

      _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
      chunksize = 1 + ranm % cap_chunksize;
      bi = (bi < chunksize) ? 0 : bi - chunksize;

      mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS);

      if (bi == 0)
	break;			/* low chunk is ...0 */
    }
}
示例#9
0
void
mpz_urandomb (mpz_ptr rop, gmp_randstate_t rstate, unsigned long int nbits)
{
  mp_ptr rp;
  mp_size_t size;

  size = BITS_TO_LIMBS (nbits);
  rp = MPZ_REALLOC (rop, size);

  _gmp_rand (rp, rstate, nbits);
  MPN_NORMALIZE (rp, size);
  SIZ (rop) = size;
}
示例#10
0
void
mpn_rrandom (mp_ptr rp, gmp_randstate_t rnd, mp_size_t n)
{
  int bit_pos;			/* bit number of least significant bit where
				   next bit field to be inserted */
  mp_limb_t ran, ranm;		/* buffer for random bits */

  /* FIXME: Is n==0 supposed to be allowed? */
  ASSERT (n >= 0);

  _gmp_rand (&ranm, rnd, BITS_PER_RANDCALL);
  ran = ranm;

  /* Start off at a random bit position in the most significant limb.  */
  bit_pos = ran % GMP_NUMB_BITS;

  gmp_rrandomb (rp, rnd, n * GMP_NUMB_BITS - bit_pos);
}
示例#11
0
void
mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
{
  mp_ptr rp;
  mp_size_t nlimbs;
  mp_exp_t exp;
  unsigned long cnt, nbits;

  MPFR_CLEAR_FLAGS(rop);

  rp = MPFR_MANT(rop);
  nbits = MPFR_PREC(rop);
  nlimbs = (nbits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB;

  _gmp_rand (rp, rstate, nbits);

  /* If nbits isn't a multiple of BITS_PER_MP_LIMB, shift up.  */
  if (nlimbs != 0)
    {
      if (nbits % BITS_PER_MP_LIMB != 0)
	mpn_lshift (rp, rp, nlimbs,
		    BITS_PER_MP_LIMB - nbits % BITS_PER_MP_LIMB);
    }

  exp = 0;
  while (nlimbs != 0 && rp[nlimbs - 1] == 0)
    {
      nlimbs--;
      exp--;
    }

  if (nlimbs != 0) /* otherwise value is zero */
    {
      count_leading_zeros (cnt, rp[nlimbs - 1]);
      if (cnt) mpn_lshift (rp, rp, nlimbs, cnt); 
      exp -= cnt;

      cnt = 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);
    } 

  MPFR_EXP (rop) = exp;
}
示例#12
0
unsigned long
gmp_urandomb_ui (gmp_randstate_ptr rstate, unsigned long bits)
{
  mp_limb_t  a[LIMBS_PER_ULONG];

  /* start with zeros, since if bits==0 then _gmp_rand will store nothing at
     all, or if bits <= GMP_NUMB_BITS then it will store only a[0] */
  a[0] = 0;
#if LIMBS_PER_ULONG > 1
  a[1] = 0;
#endif

  _gmp_rand (a, rstate, MIN (bits, BITS_PER_ULONG));

#if LIMBS_PER_ULONG == 1
  return a[0];
#else
  return a[0] | (a[1] << GMP_NUMB_BITS);
#endif
}
示例#13
0
unsigned long
gmp_urandomm_ui (gmp_randstate_ptr rstate, unsigned long n)
{
  mp_limb_t      a[LIMBS_PER_ULONG];
  unsigned long  ret, bits, leading;
  int            i;

  if (UNLIKELY (n == 0))
    DIVIDE_BY_ZERO;

  /* start with zeros, since if bits==0 then _gmp_rand will store nothing at
     all (bits==0 arises when n==1), or if bits <= GMP_NUMB_BITS then it
     will store only a[0].  */
  a[0] = 0;
#if LIMBS_PER_ULONG > 1
  a[1] = 0;
#endif

  count_leading_zeros (leading, (mp_limb_t) n);
  bits = GMP_LIMB_BITS - leading - (POW2_P(n) != 0);

  for (i = 0; i < MAX_URANDOMM_ITER; i++)
    {
      _gmp_rand (a, rstate, bits);
#if LIMBS_PER_ULONG == 1
      ret = a[0];
#else
      ret = a[0] | (a[1] << GMP_NUMB_BITS);
#endif
      if (LIKELY (ret < n))   /* usually one iteration suffices */
        goto done;
    }

  /* Too many iterations, there must be something degenerate about the
     rstate algorithm.  Return r%n.  */
  ret -= n;
  ASSERT (ret < n);

 done:
  return ret;
}
示例#14
0
文件: random2.c 项目: mahdiz/mpclib
void
mpf_random2 (mpf_ptr x, mp_size_t size, mp_exp_t exp)
{
  mp_size_t asize;
  mp_size_t prec = x->_mp_prec;
  mp_limb_t elimb;

  asize = ABS (size);
  if (asize != 0)
    {
      if (asize > prec + 1)
	asize = prec + 1;

      mpn_random2 (x->_mp_d, asize);
    }

  if (exp != 0)
    {
      _gmp_rand (&elimb, RANDS, GMP_NUMB_BITS);
      exp = elimb % (2 * exp) - exp;
    }
  x->_mp_exp = asize == 0 ? 0 : exp;
  x->_mp_size = size < 0 ? -asize : asize;
}
示例#15
0
void mpn_urandomb(mp_ptr rp, gmp_randstate_t rnd, unsigned long n)
{_gmp_rand(rp,rnd,n);return;}
示例#16
0
void mpn_urandomb(mp_ptr rp, gmp_randstate_t rnd, mpir_ui n)
{
   _gmp_rand(rp, rnd, n);
}
示例#17
0
void
mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n)
{
  mp_ptr rp, np, nlast;
  mp_size_t nbits, size;
  int count;
  int pow2;
  int cmp;
  TMP_DECL;

  size = ABSIZ (n);
  if (size == 0)
    DIVIDE_BY_ZERO;

  nlast = &PTR (n)[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);
  nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2;
  if (nbits == 0)		/* nbits == 0 means that n was == 1.  */
    {
      SIZ (rop) = 0;
      return;
    }

  TMP_MARK;
  np = PTR (n);
  if (rop == n)
    {
      mp_ptr tp;
      tp = TMP_ALLOC_LIMBS (size);
      MPN_COPY (tp, np, size);
      np = tp;
    }

  /* 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);
  /* Clear last limb to prevent the case in which size is one too much.  */
  rp[size - 1] = 0;

  count = MAX_URANDOMM_ITER;	/* Set iteration count limit.  */
  do
    {
      _gmp_rand (rp, rstate, nbits);
      MPN_CMP (cmp, rp, np, size);
    }
  while (cmp >= 0 && --count != 0);

  if (count == 0)
    /* Too many iterations; return result mod n == result - n */
    mpn_sub_n (rp, rp, np, size);

  MPN_NORMALIZE (rp, size);
  SIZ (rop) = size;
  TMP_FREE;
}