xref: /netbsd-src/external/lgpl3/mpfr/dist/src/urandomb.c (revision ba125506a622fe649968631a56eba5d42ff57863)
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-2004, 2006-2023 Free Software Foundation, Inc.
7 Contributed by the AriC and Caramba 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 https://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 /* generate nbits random bits into mp[], assuming mp was allocated to contain
31    a sufficient number of limbs */
32 void
mpfr_rand_raw(mpfr_limb_ptr mp,gmp_randstate_t rstate,mpfr_prec_t nbits)33 mpfr_rand_raw (mpfr_limb_ptr mp, gmp_randstate_t rstate,
34                mpfr_prec_t nbits)
35 {
36   mpz_t z;
37 
38   MPFR_ASSERTN (nbits >= 1);
39   /* Check for integer overflow (unless mp_bitcnt_t is signed,
40      but according to the GMP manual, this shouldn't happen).
41      Note: mp_bitcnt_t has been introduced in GMP 5.0.0. */
42   MPFR_ASSERTN ((mp_bitcnt_t) -1 < 0 || nbits <= (mp_bitcnt_t) -1);
43   mpz_init (z);
44   mpz_urandomb (z, rstate, nbits);
45   MPN_COPY(mp, PTR(z), MPFR_PREC2LIMBS (nbits));
46   mpz_clear (z);
47 }
48 
49 int
mpfr_urandomb(mpfr_ptr rop,gmp_randstate_t rstate)50 mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
51 {
52   mpfr_limb_ptr rp;
53   mpfr_prec_t nbits;
54   mp_size_t nlimbs;
55   mp_size_t k; /* number of high zero limbs */
56   mpfr_exp_t exp;
57   int cnt;
58 
59   rp = MPFR_MANT (rop);
60   nbits = MPFR_PREC (rop);
61   nlimbs = MPFR_LIMB_SIZE (rop);
62   MPFR_SET_POS (rop);
63   cnt = nlimbs * GMP_NUMB_BITS - nbits;
64 
65   /* Uniform non-normalized significand */
66   /* generate exactly nbits so that the random generator stays in the same
67      state, independent of the machine word size GMP_NUMB_BITS */
68   mpfr_rand_raw (rp, rstate, nbits);
69   if (MPFR_LIKELY (cnt != 0)) /* this will put the low bits to zero */
70     mpn_lshift (rp, rp, nlimbs, cnt);
71 
72   /* Count the null significant limbs and remaining limbs */
73   exp = 0;
74   k = 0;
75   while (nlimbs != 0 && rp[nlimbs - 1] == 0)
76     {
77       k ++;
78       nlimbs --;
79       exp -= GMP_NUMB_BITS;
80     }
81 
82   if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
83     {
84       count_leading_zeros (cnt, rp[nlimbs - 1]);
85       /* Normalization */
86       exp -= cnt;
87       if (MPFR_UNLIKELY (! MPFR_EXP_IN_RANGE (exp)))
88         {
89           /* If the exponent is not in the current exponent range, we
90              choose to return a NaN as this is probably a user error.
91              Indeed this can happen only if the exponent range has been
92              reduced to a very small interval and/or the precision is
93              huge (very unlikely). */
94           MPFR_SET_NAN (rop);
95           __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
96           return 1;
97         }
98       MPFR_SET_EXP (rop, exp);
99       if (cnt != 0)
100         mpn_lshift (rp + k, rp, nlimbs, cnt);
101       else if (k != 0)
102         mpn_copyd (rp + k, rp, nlimbs);
103       if (k != 0)
104         MPN_ZERO (rp, k);
105     }
106   else
107     MPFR_SET_ZERO (rop);
108 
109   return 0;
110 }
111