summaryrefslogtreecommitdiff
path: root/mpz/rrandomb.c
blob: e5d9070a6ccfd7ed32180ec18a9bd634c68feba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* mpz_rrandomb -- Generate a positive random mpz_t of specified bit size, with
   long runs of consecutive ones and zeros in the binary representation.
   Meant for testing of other MP routines.

Copyright 2000, 2001, 2002 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"
#include "gmp-impl.h"

static void gmp_rrandomb _PROTO ((mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits));

void
mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, unsigned long int nbits)
{
  mp_size_t nl = 0;

  if (nbits != 0)
    {
      mp_ptr xp;
      nl = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
      MPZ_REALLOC (x, nl);

      xp = PTR(x);
      gmp_rrandomb (xp, rstate, nbits);
      MPN_NORMALIZE (xp, nl);
    }

  SIZ(x) = nl;
}

/* It's a bit tricky to get this right, so please test the code well if you
   hack with it.  Some early versions of the function produced random numbers
   with the leading limb == 0, and some versions never made the most
   significant bit set.

   This code and mpn_random2 are almost identical, though the latter makes bit
   runs of 1 to 32, and forces the first block to contain 1-bits.

   The random state produces some number of random bits per underlying lc
   invocation (BITS_PER_RANDCALL).  We should perhaps ask for that, instead of
   asking for 32, presuming that limbs are at least 32 bits.  FIXME: Handle
   smaller limbs, such as 4-bit limbs useful for testing purposes, or limbs
   truncated by nailing.

   For efficiency, we make sure to use most bits returned from _gmp_rand, since
   the underlying random number generator is slow.  We keep returned bits in
   ranm/ran, and a count of how many bits remaining in ran_nbits.  */

#define LOGBITS_PER_BLOCK 4

/* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold.
   Thus, we get the same random number sequence in the common cases.
   FIXME: We should always generate the same random number sequence!  */
#if GMP_NUMB_BITS < 32
#define BITS_PER_RANDCALL GMP_NUMB_BITS
#else
#define BITS_PER_RANDCALL 32
#endif

static void
gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits)
{
  int nb;
  int bit_pos;			/* bit number of least significant bit where
				   next bit field to be inserted */
  mp_size_t ri;			/* index in rp */
  mp_limb_t ran, ranm;		/* buffer for random bits */
  mp_limb_t acc;		/* accumulate output random data here */
  int ran_nbits;		/* number of valid bits in ran */

  ran_nbits = 0;
  bit_pos = (nbits - 1) % GMP_NUMB_BITS;
  ri = (nbits - 1) / GMP_NUMB_BITS;

  acc = 0;
  while (ri >= 0)
    {
      if (ran_nbits < LOGBITS_PER_BLOCK + 1)
	{
	  _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
	  ran = ranm;
	  ran_nbits = BITS_PER_RANDCALL;
	}

      nb = (ran >> 1) % (1 << LOGBITS_PER_BLOCK) + 1;
      if ((ran & 1) != 0)
	{
	  /* Generate a string of nb ones.  */
	  if (nb > bit_pos)
	    {
	      rp[ri--] = acc | (((mp_limb_t) 2 << bit_pos) - 1);
	      bit_pos += GMP_NUMB_BITS;
	      bit_pos -= nb;
	      acc = ((~(mp_limb_t) 1) << bit_pos) & GMP_NUMB_MASK;
	    }
	  else
	    {
	      bit_pos -= nb;
	      acc |= (((mp_limb_t) 2 << nb) - 2) << bit_pos;
	    }
	}
      else
	{
	  /* Generate a string of nb zeroes.  */
	  if (nb > bit_pos)
	    {
	      rp[ri--] = acc;
	      acc = 0;
	      bit_pos += GMP_NUMB_BITS;
	    }
	  bit_pos -= nb;
	}
      ran_nbits -= LOGBITS_PER_BLOCK + 1;
      ran >>= LOGBITS_PER_BLOCK + 1;
    }
}