[BACK]Return to random2.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr

Annotation of OpenXM_contrib/gmp/mpfr/random2.c, Revision 1.1.1.1

1.1       ohara       1: /* mpf_random2 -- Generate a positive random mpf_t of specified size, with
                      2:    long runs of consecutive ones and zeros in the binary representation.
                      3:    Intended for testing of other MP routines.
                      4:
                      5: Copyright 1999, 2001 Free Software Foundation, Inc.
                      6: (Copied from the GNU MP Library.)
                      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_random2 (mpfr_ptr x, mp_size_t size, mp_exp_t exp)
                     34: {
                     35:   mp_size_t xn;
                     36:   unsigned long cnt;
                     37:   mp_ptr xp = MPFR_MANT(x), yp[1];
                     38:   mp_size_t prec = (MPFR_PREC(x) - 1)/BITS_PER_MP_LIMB;
                     39:
                     40:   MPFR_CLEAR_FLAGS(x);
                     41:   xn = ABS (size);
                     42:   if (xn != 0)
                     43:     {
                     44:       if (xn > prec + 1)
                     45:        xn = prec + 1;
                     46:
                     47:       mpn_random2 (xp, xn);
                     48:     }
                     49:
                     50:   if (exp != 0) {
                     51:     /* use mpn_random instead of random since that function is not
                     52:        available on all platforms (for example HPUX, DEC OSF, ...) */
                     53:     mpn_random ((mp_limb_t*) yp, 1);
                     54:     exp = (mp_exp_t) yp[0] % (2 * exp) - exp;
                     55:   }
                     56:
                     57:   count_leading_zeros(cnt, xp[xn - 1]);
                     58:   if (cnt) mpn_lshift(xp, xp, xn, cnt);
                     59:   MPFR_EXP(x) = exp-cnt;
                     60:   cnt = xn*BITS_PER_MP_LIMB - prec;
                     61:   /* cnt is the number of non significant bits in the low limb */
                     62:   xp[0] &= ~((MP_LIMB_T_ONE << cnt) - MP_LIMB_T_ONE);
                     63: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>