Annotation of OpenXM_contrib/gmp/mpfr/random.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpfr_random -- generate a random floating-point number
2:
1.1.1.2 ! ohara 3: Copyright 1999, 2001 Free Software Foundation, Inc.
1.1 maekawa 4:
5: This file is part of the MPFR Library.
6:
7: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara 8: it under the terms of the GNU Lesser General Public License as published by
! 9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1 maekawa 10: option) any later version.
11:
12: The MPFR Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 ! ohara 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 18: along with the MPFR Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include <stdio.h>
23: #include "gmp.h"
24: #include "gmp-impl.h"
25: #include "longlong.h"
26: #include "mpfr.h"
1.1.1.2 ! ohara 27: #include "mpfr-impl.h"
1.1 maekawa 28:
1.1.1.2 ! ohara 29: /* Computes a random mpfr in [0, 1[ with precision MPFR_PREC */
1.1 maekawa 30:
31: void
1.1.1.2 ! ohara 32: mpfr_random (mpfr_ptr x)
1.1 maekawa 33: {
1.1.1.2 ! ohara 34: mp_limb_t *xp; unsigned long xn, cnt, prec = MPFR_PREC(x);
1.1 maekawa 35:
1.1.1.2 ! ohara 36: MPFR_CLEAR_FLAGS(x);
! 37: xp = MPFR_MANT(x);
1.1 maekawa 38: xn = (prec-1)/BITS_PER_MP_LIMB + 1;
39:
1.1.1.2 ! ohara 40: mpn_random (xp, xn);
1.1 maekawa 41:
1.1.1.2 ! ohara 42: if (xp[xn - 1] == 0)
! 43: xp[xn - 1] = 1;
! 44: /* since count_leading_zeros doesn't like zeroes */
! 45:
! 46: count_leading_zeros (cnt, xp[xn - 1]);
! 47: if (cnt)
! 48: mpn_lshift (xp, xp, xn, cnt);
! 49: MPFR_EXP(x) = -cnt;
! 50: if (MPFR_SIGN(x) < 0)
! 51: MPFR_CHANGE_SIGN(x);
1.1 maekawa 52:
1.1.1.2 ! ohara 53: cnt = xn * BITS_PER_MP_LIMB - prec;
! 54: /* cnt is the number of non significant bits in the low limb */
! 55: xp[0] &= ~((MP_LIMB_T_ONE << cnt) - MP_LIMB_T_ONE);
1.1 maekawa 56: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>