xref: /dragonfly/contrib/gmp/mpz/rrandomb.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpz_rrandomb -- Generate a positive random mpz_t of specified bit size, with
2*86d7f5d3SJohn Marino    long runs of consecutive ones and zeros in the binary representation.
3*86d7f5d3SJohn Marino    Meant for testing of other MP routines.
4*86d7f5d3SJohn Marino 
5*86d7f5d3SJohn Marino Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
6*86d7f5d3SJohn Marino 
7*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
8*86d7f5d3SJohn Marino 
9*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
10*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
11*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
12*86d7f5d3SJohn Marino option) any later version.
13*86d7f5d3SJohn Marino 
14*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
15*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17*86d7f5d3SJohn Marino License for more details.
18*86d7f5d3SJohn Marino 
19*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
20*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
21*86d7f5d3SJohn Marino 
22*86d7f5d3SJohn Marino #include "gmp.h"
23*86d7f5d3SJohn Marino #include "gmp-impl.h"
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino static void gmp_rrandomb __GMP_PROTO ((mp_ptr, gmp_randstate_t, mp_bitcnt_t));
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino void
mpz_rrandomb(mpz_ptr x,gmp_randstate_t rstate,mp_bitcnt_t nbits)28*86d7f5d3SJohn Marino mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, mp_bitcnt_t nbits)
29*86d7f5d3SJohn Marino {
30*86d7f5d3SJohn Marino   mp_size_t nl;
31*86d7f5d3SJohn Marino 
32*86d7f5d3SJohn Marino   nl = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
33*86d7f5d3SJohn Marino   if (nbits != 0)
34*86d7f5d3SJohn Marino     {
35*86d7f5d3SJohn Marino       MPZ_REALLOC (x, nl);
36*86d7f5d3SJohn Marino       gmp_rrandomb (PTR(x), rstate, nbits);
37*86d7f5d3SJohn Marino     }
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino   SIZ(x) = nl;
40*86d7f5d3SJohn Marino }
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino /* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold.
43*86d7f5d3SJohn Marino    Thus, we get the same random number sequence in the common cases.
44*86d7f5d3SJohn Marino    FIXME: We should always generate the same random number sequence!  */
45*86d7f5d3SJohn Marino #if GMP_NUMB_BITS < 32
46*86d7f5d3SJohn Marino #define BITS_PER_RANDCALL GMP_NUMB_BITS
47*86d7f5d3SJohn Marino #else
48*86d7f5d3SJohn Marino #define BITS_PER_RANDCALL 32
49*86d7f5d3SJohn Marino #endif
50*86d7f5d3SJohn Marino 
51*86d7f5d3SJohn Marino static void
gmp_rrandomb(mp_ptr rp,gmp_randstate_t rstate,mp_bitcnt_t nbits)52*86d7f5d3SJohn Marino gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits)
53*86d7f5d3SJohn Marino {
54*86d7f5d3SJohn Marino   mp_bitcnt_t bi;
55*86d7f5d3SJohn Marino   mp_limb_t ranm;		/* buffer for random bits */
56*86d7f5d3SJohn Marino   unsigned cap_chunksize, chunksize;
57*86d7f5d3SJohn Marino   mp_size_t i;
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino   /* Set entire result to 111..1  */
60*86d7f5d3SJohn Marino   i = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS - 1;
61*86d7f5d3SJohn Marino   rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS;
62*86d7f5d3SJohn Marino   for (i = i - 1; i >= 0; i--)
63*86d7f5d3SJohn Marino     rp[i] = GMP_NUMB_MAX;
64*86d7f5d3SJohn Marino 
65*86d7f5d3SJohn Marino   _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
66*86d7f5d3SJohn Marino   cap_chunksize = nbits / (ranm % 4 + 1);
67*86d7f5d3SJohn Marino   cap_chunksize += cap_chunksize == 0; /* make it at least 1 */
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino   bi = nbits;
70*86d7f5d3SJohn Marino 
71*86d7f5d3SJohn Marino   for (;;)
72*86d7f5d3SJohn Marino     {
73*86d7f5d3SJohn Marino       _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
74*86d7f5d3SJohn Marino       chunksize = 1 + ranm % cap_chunksize;
75*86d7f5d3SJohn Marino       bi = (bi < chunksize) ? 0 : bi - chunksize;
76*86d7f5d3SJohn Marino 
77*86d7f5d3SJohn Marino       if (bi == 0)
78*86d7f5d3SJohn Marino 	break;			/* low chunk is ...1 */
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino       rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS;
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino       _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
83*86d7f5d3SJohn Marino       chunksize = 1 + ranm % cap_chunksize;
84*86d7f5d3SJohn Marino       bi = (bi < chunksize) ? 0 : bi - chunksize;
85*86d7f5d3SJohn Marino 
86*86d7f5d3SJohn Marino       mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS);
87*86d7f5d3SJohn Marino 
88*86d7f5d3SJohn Marino       if (bi == 0)
89*86d7f5d3SJohn Marino 	break;			/* low chunk is ...0 */
90*86d7f5d3SJohn Marino     }
91*86d7f5d3SJohn Marino }
92