Add APIC_ID to extract apic id from local apic id field
[dragonfly.git] / contrib / mpfr / 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().
5
6 Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 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 2.1 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.LIB.  If not, write to
23 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 MA 02110-1301, USA. */
25
26
27 #define MPFR_NEED_LONGLONG_H
28 #include "mpfr-impl.h"
29
30 int
31 mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
32 {
33   mp_ptr rp;
34   mp_prec_t nbits;
35   mp_size_t nlimbs;
36   mp_size_t k; /* number of high zero limbs */
37   mp_exp_t exp;
38   int cnt;
39
40   MPFR_CLEAR_FLAGS (rop);
41
42   rp = MPFR_MANT (rop);
43   nbits = MPFR_PREC (rop);
44   nlimbs = MPFR_LIMB_SIZE (rop);
45   MPFR_SET_POS (rop);
46
47   /* Uniform non-normalized significand */
48   _gmp_rand (rp, rstate, nlimbs * BITS_PER_MP_LIMB);
49
50   /* If nbits isn't a multiple of BITS_PER_MP_LIMB, mask the low bits */
51   cnt = nlimbs * BITS_PER_MP_LIMB - nbits;
52   if (MPFR_LIKELY (cnt != 0))
53     rp[0] &= ~MPFR_LIMB_MASK (cnt);
54
55   /* Count the null significant limbs and remaining limbs */
56   exp = 0;
57   k = 0;
58   while (nlimbs != 0 && rp[nlimbs - 1] == 0)
59     {
60       k ++;
61       nlimbs --;
62       exp -= BITS_PER_MP_LIMB;
63     }
64
65   if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
66     {
67       count_leading_zeros (cnt, rp[nlimbs - 1]);
68       /* Normalization */
69       if (mpfr_set_exp (rop, exp - cnt))
70         {
71           /* If the exponent is not in the current exponent range, we
72              choose to return a NaN as this is probably a user error.
73              Indeed this can happen only if the exponent range has been
74              reduced to a very small interval and/or the precision is
75              huge (very unlikely). */
76           MPFR_SET_NAN (rop);
77           __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
78           return 1;
79         }
80       if (cnt != 0)
81         mpn_lshift (rp + k, rp, nlimbs, cnt);
82       if (k != 0)
83         MPN_ZERO (rp, k);
84     }
85   else
86     MPFR_SET_ZERO (rop);
87
88   return 0;
89 }