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 (C) 2000 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 the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or (at your
12 option) any later version.
14 The GNU MP Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 MA 02111-1307, USA. */
27 static void gmp_rrandomb _PROTO ((mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits));
31 mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, unsigned long int nbits)
33 mpz_rrandomb (x, rstate, nbits)
35 gmp_randstate_t rstate;
36 unsigned long int nbits;
44 nl = (nbits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB;
45 if (x->_mp_alloc < nl)
49 gmp_rrandomb (xp, rstate, nbits);
50 MPN_NORMALIZE (xp, nl);
56 #define BITS_PER_CHUNK 4
60 gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits)
62 gmp_rrandomb (rp, rstate, nbits)
64 gmp_randstate_t rstate;
65 unsigned long int nbits;
75 bit_pos = nbits % BITS_PER_MP_LIMB;
76 limb_pos = nbits / BITS_PER_MP_LIMB;
79 bit_pos = BITS_PER_MP_LIMB;
86 _gmp_rand (&ranm, rstate, BITS_PER_CHUNK + 1);
91 /* Generate a string of ones. */
94 rp[limb_pos--] = acc | ((((mp_limb_t) 1) << bit_pos) - 1);
95 bit_pos += BITS_PER_MP_LIMB;
97 acc = (~(mp_limb_t) 0) << bit_pos;
102 acc |= ((((mp_limb_t) 1) << nb) - 1) << bit_pos;
107 /* Generate a string of zeroes. */
110 rp[limb_pos--] = acc;
112 bit_pos += BITS_PER_MP_LIMB;