Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / mpfr / random2.c
blobe271cd14735c55431936200a9f304986459456ba
1 /* mpfr_random2 -- Generate a positive random mpfr_t of specified size, with
2 long runs of consecutive ones and zeros in the binary representation.
3 Intended for testing.
5 Copyright 1999, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 Contributed by the Arenaire and Cacao projects, INRIA.
8 This file is part of the GNU MPFR Library.
10 The GNU 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.
15 The GNU 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.
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 MA 02110-1301, USA. */
25 #define MPFR_NEED_LONGLONG_H
26 #include "mpfr-impl.h"
28 #define LOGBITS_PER_BLOCK 4
29 #if GMP_NUMB_BITS < 32
30 #define BITS_PER_RANDCALL GMP_NUMB_BITS
31 #else
32 #define BITS_PER_RANDCALL 32
33 #endif
35 static void
36 mpfr_random2_raw (mpfr_ptr x, mp_size_t size, mp_exp_t exp,
37 gmp_randstate_t rstate)
39 mp_size_t xn, k, ri;
40 unsigned long sh;
41 mp_ptr xp;
42 mp_limb_t elimb, ran, acc;
43 int ran_nbits, bit_pos, nb;
45 MPFR_CLEAR_FLAGS (x);
47 if (MPFR_UNLIKELY(size == 0))
49 MPFR_SET_ZERO (x);
50 MPFR_SET_POS (x);
51 return ;
53 else if (size > 0)
55 MPFR_SET_POS (x);
57 else
59 MPFR_SET_NEG (x);
60 size = -size;
63 xn = MPFR_LIMB_SIZE (x);
64 xp = MPFR_MANT (x);
65 if (size > xn)
66 size = xn;
67 k = xn - size;
69 /* Code extracted from GMP, function mpn_random2, to avoid the use
70 of GMP's internal random state in MPFR */
72 _gmp_rand (&elimb, rstate, BITS_PER_RANDCALL);
73 ran = elimb;
75 /* Start off at a random bit position in the most significant limb. */
76 bit_pos = GMP_NUMB_BITS - 1;
77 ran >>= 6; /* Ideally log2(GMP_NUMB_BITS) */
78 ran_nbits = BITS_PER_RANDCALL - 6; /* Ideally - log2(GMP_NUMB_BITS) */
80 /* Bit 0 of ran chooses string of ones/string of zeroes.
81 Make most significant limb be non-zero by setting bit 0 of RAN. */
82 ran |= 1;
83 ri = xn - 1;
85 acc = 0;
86 while (ri >= k)
88 if (ran_nbits < LOGBITS_PER_BLOCK + 1)
90 _gmp_rand (&elimb, rstate, BITS_PER_RANDCALL);
91 ran = elimb;
92 ran_nbits = BITS_PER_RANDCALL;
95 nb = (ran >> 1) % (1 << LOGBITS_PER_BLOCK) + 1;
96 if ((ran & 1) != 0)
98 /* Generate a string of nb ones. */
99 if (nb > bit_pos)
101 xp[ri--] = acc | (((mp_limb_t) 2 << bit_pos) - 1);
102 bit_pos += GMP_NUMB_BITS;
103 bit_pos -= nb;
104 acc = ((~(mp_limb_t) 1) << bit_pos) & GMP_NUMB_MASK;
106 else
108 bit_pos -= nb;
109 acc |= (((mp_limb_t) 2 << nb) - 2) << bit_pos;
112 else
114 /* Generate a string of nb zeroes. */
115 if (nb > bit_pos)
117 xp[ri--] = acc;
118 acc = 0;
119 bit_pos += GMP_NUMB_BITS;
121 bit_pos -= nb;
123 ran_nbits -= LOGBITS_PER_BLOCK + 1;
124 ran >>= LOGBITS_PER_BLOCK + 1;
127 /* Set mandatory most significant bit. */
128 /* xp[xn - 1] |= MPFR_LIMB_HIGHBIT; */
130 if (k != 0)
132 /* Clear last limbs */
133 MPN_ZERO (xp, k);
135 else
137 /* Mask off non significant bits in the low limb. */
138 MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC (x));
139 xp[0] &= ~MPFR_LIMB_MASK (sh);
142 /* Generate random exponent. */
143 _gmp_rand (&elimb, RANDS, BITS_PER_MP_LIMB);
144 exp = ABS (exp);
145 MPFR_SET_EXP (x, elimb % (2 * exp + 1) - exp);
147 return ;
150 void
151 mpfr_random2 (mpfr_ptr x, mp_size_t size, mp_exp_t exp)
153 mpfr_random2_raw (x, size, exp, RANDS);