Add default Smack manifest for mpfr.spec
[toolchains/mpfr.git] / urandomb.c
1 /* mpfr_urandomb (rop, state, nbits) -- Generate a uniform pseudorandom
2    real number between 0 (inclusive) and 1 (exclusive) of size NBITS,
3    using STATE as the random state previously initialized by a call to
4    gmp_randinit_lc_2exp_size().
5
6 Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7 Contributed by the Arenaire and Cacao projects, INRIA.
8
9 This file is part of the GNU MPFR Library.
10
11 The GNU MPFR Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 The GNU MPFR Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
23 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
25
26
27 #define MPFR_NEED_LONGLONG_H
28 #include "mpfr-impl.h"
29
30 void
31 mpfr_rand_raw (mp_ptr mp, gmp_randstate_t rstate, unsigned long int nbits)
32 {
33   mpz_t z;
34
35   /* To be sure to avoid the potential allocation of mpz_urandomb */
36   ALLOC(z) = SIZ(z) = (nbits / GMP_NUMB_BITS) + 1;
37   PTR(z)   = mp;
38   mpz_urandomb(z, rstate, nbits);
39 }
40
41 int
42 mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
43 {
44   mp_ptr rp;
45   mpfr_prec_t nbits;
46   mp_size_t nlimbs;
47   mp_size_t k; /* number of high zero limbs */
48   mpfr_exp_t exp;
49   int cnt;
50
51   rp = MPFR_MANT (rop);
52   nbits = MPFR_PREC (rop);
53   nlimbs = MPFR_LIMB_SIZE (rop);
54   MPFR_SET_POS (rop);
55
56   /* Uniform non-normalized significand */
57   mpfr_rand_raw (rp, rstate, nlimbs * GMP_NUMB_BITS);
58
59   /* If nbits isn't a multiple of GMP_NUMB_BITS, mask the low bits */
60   cnt = nlimbs * GMP_NUMB_BITS - nbits;
61   if (MPFR_LIKELY (cnt != 0))
62     rp[0] &= ~MPFR_LIMB_MASK (cnt);
63
64   /* Count the null significant limbs and remaining limbs */
65   exp = 0;
66   k = 0;
67   while (nlimbs != 0 && rp[nlimbs - 1] == 0)
68     {
69       k ++;
70       nlimbs --;
71       exp -= GMP_NUMB_BITS;
72     }
73
74   if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
75     {
76       count_leading_zeros (cnt, rp[nlimbs - 1]);
77       /* Normalization */
78       if (mpfr_set_exp (rop, exp - cnt))
79         {
80           /* If the exponent is not in the current exponent range, we
81              choose to return a NaN as this is probably a user error.
82              Indeed this can happen only if the exponent range has been
83              reduced to a very small interval and/or the precision is
84              huge (very unlikely). */
85           MPFR_SET_NAN (rop);
86           __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
87           return 1;
88         }
89       if (cnt != 0)
90         mpn_lshift (rp + k, rp, nlimbs, cnt);
91       if (k != 0)
92         MPN_ZERO (rp, k);
93     }
94   else
95     MPFR_SET_ZERO (rop);
96
97   return 0;
98 }