~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/mpf/random2.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
   long runs of consecutive ones and zeros in the binary representation.
3
3
   Intended for testing of other MP routines.
4
4
 
5
 
Copyright 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
 
5
Copyright 1995, 1996, 2001, 2002, 2003 Free Software Foundation, Inc.
6
6
 
7
7
This file is part of the GNU MP Library.
8
8
 
18
18
 
19
19
You should have received a copy of the GNU Lesser General Public License
20
20
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
21
 
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
 
MA 02111-1307, USA. */
 
21
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
22
MA 02110-1301, USA. */
23
23
 
24
24
#include "gmp.h"
25
25
#include "gmp-impl.h"
26
26
 
27
27
 
28
28
void
29
 
mpf_random2 (mpf_ptr x, mp_size_t size, mp_exp_t exp)
 
29
mpf_random2 (mpf_ptr x, mp_size_t xs, mp_exp_t exp)
30
30
{
31
 
  mp_size_t asize;
32
 
  mp_size_t prec = x->_mp_prec;
 
31
  mp_size_t xn;
 
32
  mp_size_t prec;
33
33
  mp_limb_t elimb;
34
34
 
35
 
  asize = ABS (size);
36
 
  if (asize != 0)
37
 
    {
38
 
      if (asize > prec + 1)
39
 
        asize = prec + 1;
40
 
 
41
 
      mpn_random2 (x->_mp_d, asize);
42
 
    }
43
 
 
44
 
  if (exp != 0)
45
 
    {
46
 
      _gmp_rand (&elimb, RANDS, GMP_NUMB_BITS);
47
 
      exp = elimb % (2 * exp) - exp;
48
 
    }
49
 
  x->_mp_exp = asize == 0 ? 0 : exp;
50
 
  x->_mp_size = size < 0 ? -asize : asize;
 
35
  xn = ABS (xs);
 
36
  prec = PREC(x);
 
37
 
 
38
  if (xn == 0)
 
39
    {
 
40
      EXP(x) = 0;
 
41
      SIZ(x) = 0;
 
42
      return;
 
43
    }
 
44
 
 
45
  if (xn > prec + 1)
 
46
    xn = prec + 1;
 
47
 
 
48
  /* General random mantissa.  */
 
49
  mpn_random2 (PTR(x), xn);
 
50
 
 
51
  /* Generate random exponent.  */
 
52
  _gmp_rand (&elimb, RANDS, GMP_NUMB_BITS);
 
53
  exp = ABS (exp);
 
54
  exp = elimb % (2 * exp + 1) - exp;
 
55
 
 
56
  EXP(x) = exp;
 
57
  SIZ(x) = xs < 0 ? -xn : xn;
51
58
}