14a238c70SJohn Marino /* mpfr_urandom (rop, state, rnd_mode) -- Generate a uniform pseudorandom
24a238c70SJohn Marino real number between 0 and 1 (exclusive) and round it to the precision of rop
34a238c70SJohn Marino according to the given rounding mode.
44a238c70SJohn Marino
5*ab6d115fSJohn Marino Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
6*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
74a238c70SJohn Marino
84a238c70SJohn Marino This file is part of the GNU MPFR Library.
94a238c70SJohn Marino
104a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
114a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
124a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
134a238c70SJohn Marino option) any later version.
144a238c70SJohn Marino
154a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
164a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
174a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
184a238c70SJohn Marino License for more details.
194a238c70SJohn Marino
204a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
214a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
224a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
234a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
244a238c70SJohn Marino
254a238c70SJohn Marino
264a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
274a238c70SJohn Marino #include "mpfr-impl.h"
284a238c70SJohn Marino
294a238c70SJohn Marino
304a238c70SJohn Marino /* generate one random bit */
314a238c70SJohn Marino static int
random_rounding_bit(gmp_randstate_t rstate)324a238c70SJohn Marino random_rounding_bit (gmp_randstate_t rstate)
334a238c70SJohn Marino {
344a238c70SJohn Marino mp_limb_t r;
354a238c70SJohn Marino
364a238c70SJohn Marino mpfr_rand_raw (&r, rstate, 1);
374a238c70SJohn Marino return r & MPFR_LIMB_ONE;
384a238c70SJohn Marino }
394a238c70SJohn Marino
404a238c70SJohn Marino
414a238c70SJohn Marino int
mpfr_urandom(mpfr_ptr rop,gmp_randstate_t rstate,mpfr_rnd_t rnd_mode)424a238c70SJohn Marino mpfr_urandom (mpfr_ptr rop, gmp_randstate_t rstate, mpfr_rnd_t rnd_mode)
434a238c70SJohn Marino {
444a238c70SJohn Marino mpfr_limb_ptr rp;
454a238c70SJohn Marino mpfr_prec_t nbits;
464a238c70SJohn Marino mp_size_t nlimbs;
474a238c70SJohn Marino mp_size_t n;
484a238c70SJohn Marino mpfr_exp_t exp;
494a238c70SJohn Marino mpfr_exp_t emin;
504a238c70SJohn Marino int cnt;
514a238c70SJohn Marino int inex;
524a238c70SJohn Marino
534a238c70SJohn Marino rp = MPFR_MANT (rop);
544a238c70SJohn Marino nbits = MPFR_PREC (rop);
554a238c70SJohn Marino nlimbs = MPFR_LIMB_SIZE (rop);
564a238c70SJohn Marino MPFR_SET_POS (rop);
574a238c70SJohn Marino exp = 0;
584a238c70SJohn Marino emin = mpfr_get_emin ();
594a238c70SJohn Marino if (MPFR_UNLIKELY (emin > 0))
604a238c70SJohn Marino {
614a238c70SJohn Marino if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA
624a238c70SJohn Marino || (emin == 1 && rnd_mode == MPFR_RNDN
634a238c70SJohn Marino && random_rounding_bit (rstate)))
644a238c70SJohn Marino {
654a238c70SJohn Marino mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode);
664a238c70SJohn Marino return +1;
674a238c70SJohn Marino }
684a238c70SJohn Marino else
694a238c70SJohn Marino {
704a238c70SJohn Marino MPFR_SET_ZERO (rop);
714a238c70SJohn Marino return -1;
724a238c70SJohn Marino }
734a238c70SJohn Marino }
744a238c70SJohn Marino
754a238c70SJohn Marino /* Exponent */
764a238c70SJohn Marino #define DRAW_BITS 8 /* we draw DRAW_BITS at a time */
774a238c70SJohn Marino cnt = DRAW_BITS;
784a238c70SJohn Marino MPFR_ASSERTN(DRAW_BITS <= GMP_NUMB_BITS);
794a238c70SJohn Marino while (cnt == DRAW_BITS)
804a238c70SJohn Marino {
814a238c70SJohn Marino /* generate DRAW_BITS in rp[0] */
824a238c70SJohn Marino mpfr_rand_raw (rp, rstate, DRAW_BITS);
834a238c70SJohn Marino if (MPFR_UNLIKELY (rp[0] == 0))
844a238c70SJohn Marino cnt = DRAW_BITS;
854a238c70SJohn Marino else
864a238c70SJohn Marino {
874a238c70SJohn Marino count_leading_zeros (cnt, rp[0]);
884a238c70SJohn Marino cnt -= GMP_NUMB_BITS - DRAW_BITS;
894a238c70SJohn Marino }
904a238c70SJohn Marino
914a238c70SJohn Marino if (MPFR_UNLIKELY (exp < emin + cnt))
924a238c70SJohn Marino {
934a238c70SJohn Marino /* To get here, we have been drawing more than -emin zeros
944a238c70SJohn Marino in a row, then return 0 or the smallest representable
954a238c70SJohn Marino positive number.
964a238c70SJohn Marino
974a238c70SJohn Marino The rounding to nearest mode is subtle:
984a238c70SJohn Marino If exp - cnt == emin - 1, the rounding bit is set, except
994a238c70SJohn Marino if cnt == DRAW_BITS in which case the rounding bit is
1004a238c70SJohn Marino outside rp[0] and must be generated. */
1014a238c70SJohn Marino if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA
1024a238c70SJohn Marino || (rnd_mode == MPFR_RNDN && cnt == exp - emin - 1
1034a238c70SJohn Marino && (cnt != DRAW_BITS || random_rounding_bit (rstate))))
1044a238c70SJohn Marino {
1054a238c70SJohn Marino mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode);
1064a238c70SJohn Marino return +1;
1074a238c70SJohn Marino }
1084a238c70SJohn Marino else
1094a238c70SJohn Marino {
1104a238c70SJohn Marino MPFR_SET_ZERO (rop);
1114a238c70SJohn Marino return -1;
1124a238c70SJohn Marino }
1134a238c70SJohn Marino }
1144a238c70SJohn Marino exp -= cnt;
1154a238c70SJohn Marino }
1164a238c70SJohn Marino MPFR_EXP (rop) = exp; /* Warning: may be outside the current
1174a238c70SJohn Marino exponent range */
1184a238c70SJohn Marino
1194a238c70SJohn Marino
1204a238c70SJohn Marino /* Significand: we need generate only nbits-1 bits, since the most
1214a238c70SJohn Marino significant is 1 */
1224a238c70SJohn Marino mpfr_rand_raw (rp, rstate, nbits - 1);
1234a238c70SJohn Marino n = nlimbs * GMP_NUMB_BITS - nbits;
1244a238c70SJohn Marino if (MPFR_LIKELY (n != 0)) /* this will put the low bits to zero */
1254a238c70SJohn Marino mpn_lshift (rp, rp, nlimbs, n);
1264a238c70SJohn Marino
1274a238c70SJohn Marino /* Set the msb to 1 since it was fixed by the exponent choice */
1284a238c70SJohn Marino rp[nlimbs - 1] |= MPFR_LIMB_HIGHBIT;
1294a238c70SJohn Marino
1304a238c70SJohn Marino /* Rounding */
1314a238c70SJohn Marino if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA
1324a238c70SJohn Marino || (rnd_mode == MPFR_RNDN && random_rounding_bit (rstate)))
1334a238c70SJohn Marino {
1344a238c70SJohn Marino /* Take care of the exponent range: it may have been reduced */
1354a238c70SJohn Marino if (exp < emin)
1364a238c70SJohn Marino mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode);
1374a238c70SJohn Marino else if (exp > mpfr_get_emax ())
1384a238c70SJohn Marino mpfr_set_inf (rop, +1); /* overflow, flag set by mpfr_check_range */
1394a238c70SJohn Marino else
1404a238c70SJohn Marino mpfr_nextabove (rop);
1414a238c70SJohn Marino inex = +1;
1424a238c70SJohn Marino }
1434a238c70SJohn Marino else
1444a238c70SJohn Marino inex = -1;
1454a238c70SJohn Marino
1464a238c70SJohn Marino return mpfr_check_range (rop, inex, rnd_mode);
1474a238c70SJohn Marino }
148