1
/* mpn_random2 -- Generate random numbers with relatively long strings
2
of ones and zeroes. Suitable for border testing.
4
Copyright 1992, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 2.1 of the License, or (at your
11
option) any later version.
13
The GNU MP Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
MA 02111-1307, USA. */
27
/* It's a bit tricky to get this right, so please test the code well if you
28
hack with it. Some early versions of the function produced random numbers
29
with the leading limb == 0, and some versions never made the most
32
This code and mpz_rrandomb are almost identical, though the latter makes bit
33
runs of 1 to 16, and doesn't force the first block to contain 1-bits.
35
The RANDS random state currently produces 32 random bits per underlying lc
36
invocation (BITS_PER_RANDCALL). We therefore ask for that, presuming that
37
limbs are at least 32 bits. FIXME: Handle smaller limbs, such as 4-bit
38
limbs useful for testing purposes, or limbs truncated by nailing.
40
For efficiency, we make sure to use most bits returned from _gmp_rand, since
41
the underlying random number generator is slow. Keep returned bits in
42
ranm/ran, and a count of how many bits remaining in ran_nbits. */
44
#define LOGBITS_PER_BLOCK 5
45
#define BITS_PER_RANDCALL 32
48
mpn_random2 (mp_ptr rp, mp_size_t n)
50
gmp_randstate_ptr rstate = RANDS;
52
int bit_pos; /* bit number of least significant bit where
53
next bit field to be inserted */
54
mp_size_t ri; /* index in rp */
55
mp_limb_t ran, ranm; /* buffer for random bits */
56
mp_limb_t acc; /* accumulate output random data here */
57
int ran_nbits; /* number of valid bits in ran */
59
/* FIXME: Is n==0 supposed to be allowed? */
61
ASSERT_ALWAYS (BITS_PER_MP_LIMB > LOGBITS_PER_BLOCK);
63
_gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
66
/* Start off at a random bit position in the most significant limb. */
67
bit_pos = ran % BITS_PER_MP_LIMB;
68
ran >>= 6; /* Ideally log2(BITS_PER_MP_LIMB) */
69
ran_nbits = BITS_PER_RANDCALL - 6; /* Ideally - log2(BITS_PER_MP_LIMB) */
71
/* Bit 0 of ran chooses string of ones/string of zeroes.
72
Make most significant limb be non-zero by setting bit 0 of RAN. */
80
if (ran_nbits < LOGBITS_PER_BLOCK + 1)
82
_gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
84
ran_nbits = BITS_PER_RANDCALL;
87
nb = (ran >> 1) % (1 << LOGBITS_PER_BLOCK) + 1;
90
/* Generate a string of nb ones. */
93
rp[ri--] = acc | (((mp_limb_t) 2 << bit_pos) - 1);
94
bit_pos += BITS_PER_MP_LIMB;
96
acc = (~(mp_limb_t) 1) << bit_pos;
101
acc |= (((mp_limb_t) 2 << nb) - 2) << bit_pos;
106
/* Generate a string of nb zeroes. */
111
bit_pos += BITS_PER_MP_LIMB;
115
ran_nbits -= LOGBITS_PER_BLOCK + 1;
116
ran >>= LOGBITS_PER_BLOCK + 1;