xref: /dflybsd-src/contrib/mpfr/src/urandomb.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_urandomb (rop, state, nbits) -- Generate a uniform pseudorandom
24a238c70SJohn Marino    real number between 0 (inclusive) and 1 (exclusive) of size NBITS,
34a238c70SJohn Marino    using STATE as the random state previously initialized by a call to
44a238c70SJohn Marino    gmp_randinit_lc_2exp_size().
54a238c70SJohn Marino 
6*ab6d115fSJohn Marino Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
7*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
84a238c70SJohn Marino 
94a238c70SJohn Marino This file is part of the GNU MPFR Library.
104a238c70SJohn Marino 
114a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
124a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
134a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
144a238c70SJohn Marino option) any later version.
154a238c70SJohn Marino 
164a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
174a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
184a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
194a238c70SJohn Marino License for more details.
204a238c70SJohn Marino 
214a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
224a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
234a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
244a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
254a238c70SJohn Marino 
264a238c70SJohn Marino 
274a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
284a238c70SJohn Marino #include "mpfr-impl.h"
294a238c70SJohn Marino 
304a238c70SJohn Marino /* generate nbits random bits into mp[], assuming mp was allocated to contain
314a238c70SJohn Marino    a sufficient number of limbs */
324a238c70SJohn Marino void
mpfr_rand_raw(mpfr_limb_ptr mp,gmp_randstate_t rstate,mpfr_prec_t nbits)334a238c70SJohn Marino mpfr_rand_raw (mpfr_limb_ptr mp, gmp_randstate_t rstate,
34*ab6d115fSJohn Marino                mpfr_prec_t nbits)
354a238c70SJohn Marino {
364a238c70SJohn Marino   mpz_t z;
374a238c70SJohn Marino 
38*ab6d115fSJohn Marino   MPFR_ASSERTN (nbits >= 1);
394a238c70SJohn Marino   /* To be sure to avoid the potential allocation of mpz_urandomb */
40*ab6d115fSJohn Marino   ALLOC(z) = SIZ(z) = MPFR_PREC2LIMBS (nbits);
414a238c70SJohn Marino   PTR(z)   = mp;
42*ab6d115fSJohn Marino #if __MPFR_GMP(5,0,0)
43*ab6d115fSJohn Marino   /* Check for integer overflow (unless mp_bitcnt_t is signed,
44*ab6d115fSJohn Marino      but according to the GMP manual, this shouldn't happen).
45*ab6d115fSJohn Marino      Note: mp_bitcnt_t has been introduced in GMP 5.0.0. */
46*ab6d115fSJohn Marino   MPFR_ASSERTN ((mp_bitcnt_t) -1 < 0 || nbits <= (mp_bitcnt_t) -1);
47*ab6d115fSJohn Marino #endif
484a238c70SJohn Marino   mpz_urandomb (z, rstate, nbits);
494a238c70SJohn Marino }
504a238c70SJohn Marino 
514a238c70SJohn Marino int
mpfr_urandomb(mpfr_ptr rop,gmp_randstate_t rstate)524a238c70SJohn Marino mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
534a238c70SJohn Marino {
544a238c70SJohn Marino   mpfr_limb_ptr rp;
554a238c70SJohn Marino   mpfr_prec_t nbits;
564a238c70SJohn Marino   mp_size_t nlimbs;
574a238c70SJohn Marino   mp_size_t k; /* number of high zero limbs */
584a238c70SJohn Marino   mpfr_exp_t exp;
594a238c70SJohn Marino   int cnt;
604a238c70SJohn Marino 
614a238c70SJohn Marino   rp = MPFR_MANT (rop);
624a238c70SJohn Marino   nbits = MPFR_PREC (rop);
634a238c70SJohn Marino   nlimbs = MPFR_LIMB_SIZE (rop);
644a238c70SJohn Marino   MPFR_SET_POS (rop);
654a238c70SJohn Marino   cnt = nlimbs * GMP_NUMB_BITS - nbits;
664a238c70SJohn Marino 
674a238c70SJohn Marino   /* Uniform non-normalized significand */
684a238c70SJohn Marino   /* generate exactly nbits so that the random generator stays in the same
694a238c70SJohn Marino      state, independent of the machine word size GMP_NUMB_BITS */
704a238c70SJohn Marino   mpfr_rand_raw (rp, rstate, nbits);
714a238c70SJohn Marino   if (MPFR_LIKELY (cnt != 0)) /* this will put the low bits to zero */
724a238c70SJohn Marino     mpn_lshift (rp, rp, nlimbs, cnt);
734a238c70SJohn Marino 
744a238c70SJohn Marino   /* Count the null significant limbs and remaining limbs */
754a238c70SJohn Marino   exp = 0;
764a238c70SJohn Marino   k = 0;
774a238c70SJohn Marino   while (nlimbs != 0 && rp[nlimbs - 1] == 0)
784a238c70SJohn Marino     {
794a238c70SJohn Marino       k ++;
804a238c70SJohn Marino       nlimbs --;
814a238c70SJohn Marino       exp -= GMP_NUMB_BITS;
824a238c70SJohn Marino     }
834a238c70SJohn Marino 
844a238c70SJohn Marino   if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
854a238c70SJohn Marino     {
864a238c70SJohn Marino       count_leading_zeros (cnt, rp[nlimbs - 1]);
874a238c70SJohn Marino       /* Normalization */
884a238c70SJohn Marino       if (mpfr_set_exp (rop, exp - cnt))
894a238c70SJohn Marino         {
904a238c70SJohn Marino           /* If the exponent is not in the current exponent range, we
914a238c70SJohn Marino              choose to return a NaN as this is probably a user error.
924a238c70SJohn Marino              Indeed this can happen only if the exponent range has been
934a238c70SJohn Marino              reduced to a very small interval and/or the precision is
944a238c70SJohn Marino              huge (very unlikely). */
954a238c70SJohn Marino           MPFR_SET_NAN (rop);
964a238c70SJohn Marino           __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
974a238c70SJohn Marino           return 1;
984a238c70SJohn Marino         }
994a238c70SJohn Marino       if (cnt != 0)
1004a238c70SJohn Marino         mpn_lshift (rp + k, rp, nlimbs, cnt);
1014a238c70SJohn Marino       if (k != 0)
1024a238c70SJohn Marino         MPN_ZERO (rp, k);
1034a238c70SJohn Marino     }
1044a238c70SJohn Marino   else
1054a238c70SJohn Marino     MPFR_SET_ZERO (rop);
1064a238c70SJohn Marino 
1074a238c70SJohn Marino   return 0;
1084a238c70SJohn Marino }
109