Annotation of OpenXM_contrib/gmp/mpfr/urandomb.c, Revision 1.1
1.1 ! ohara 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 Free Software Foundation, Inc.
! 7:
! 8: This file is part of the MPFR Library.
! 9:
! 10: The MPFR Library is free software; you can redistribute it and/or modify
! 11: it under the terms of the GNU Lesser General Public License as published by
! 12: the Free Software Foundation; either version 2.1 of the License, or (at your
! 13: option) any later version.
! 14:
! 15: The MPFR Library is distributed in the hope that it will be useful, but
! 16: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 17: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 18: License for more details.
! 19:
! 20: You should have received a copy of the GNU Lesser General Public License
! 21: along with the MPFR Library; see the file COPYING.LIB. If not, write to
! 22: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 23: MA 02111-1307, USA. */
! 24:
! 25: #include <stdio.h>
! 26: #include "gmp.h"
! 27: #include "gmp-impl.h"
! 28: #include "longlong.h"
! 29: #include "mpfr.h"
! 30: #include "mpfr-impl.h"
! 31:
! 32: void
! 33: mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
! 34: {
! 35: mp_ptr rp;
! 36: mp_size_t nlimbs;
! 37: mp_exp_t exp;
! 38: unsigned long cnt, nbits;
! 39:
! 40: MPFR_CLEAR_FLAGS(rop);
! 41:
! 42: rp = MPFR_MANT(rop);
! 43: nbits = MPFR_PREC(rop);
! 44: nlimbs = (nbits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB;
! 45:
! 46: _gmp_rand (rp, rstate, nbits);
! 47:
! 48: /* If nbits isn't a multiple of BITS_PER_MP_LIMB, shift up. */
! 49: if (nlimbs != 0)
! 50: {
! 51: if (nbits % BITS_PER_MP_LIMB != 0)
! 52: mpn_lshift (rp, rp, nlimbs,
! 53: BITS_PER_MP_LIMB - nbits % BITS_PER_MP_LIMB);
! 54: }
! 55:
! 56: exp = 0;
! 57: while (nlimbs != 0 && rp[nlimbs - 1] == 0)
! 58: {
! 59: nlimbs--;
! 60: exp--;
! 61: }
! 62:
! 63: count_leading_zeros (cnt, rp[nlimbs - 1]);
! 64: if (cnt) mpn_lshift (rp, rp, nlimbs, cnt);
! 65: exp -= cnt;
! 66:
! 67: cnt = nlimbs*BITS_PER_MP_LIMB - nbits;
! 68: /* cnt is the number of non significant bits in the low limb */
! 69: rp[0] &= ~((MP_LIMB_T_ONE << cnt) - 1);
! 70:
! 71: MPFR_EXP (rop) = exp;
! 72: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>