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