Annotation of OpenXM_contrib/gmp/mpz/rrandomb.c, Revision 1.1
1.1 ! maekawa 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.
! 4:
! 5: Copyright (C) 2000 Free Software Foundation, Inc.
! 6:
! 7: This file is part of the GNU MP Library.
! 8:
! 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.
! 13:
! 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.
! 18:
! 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. */
! 23:
! 24: #include "gmp.h"
! 25: #include "gmp-impl.h"
! 26:
! 27: static void gmp_rrandomb _PROTO ((mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits));
! 28:
! 29: void
! 30: #if __STDC__
! 31: mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, unsigned long int nbits)
! 32: #else
! 33: mpz_rrandomb (x, rstate, nbits)
! 34: mpz_ptr x;
! 35: gmp_randstate_t rstate;
! 36: unsigned long int nbits;
! 37: #endif
! 38: {
! 39: mp_size_t nl = 0;
! 40:
! 41: if (nbits != 0)
! 42: {
! 43: mp_ptr xp;
! 44: nl = (nbits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB;
! 45: if (x->_mp_alloc < nl)
! 46: _mpz_realloc (x, nl);
! 47:
! 48: xp = PTR(x);
! 49: gmp_rrandomb (xp, rstate, nbits);
! 50: MPN_NORMALIZE (xp, nl);
! 51: }
! 52:
! 53: SIZ(x) = nl;
! 54: }
! 55:
! 56: #define BITS_PER_CHUNK 4
! 57:
! 58: static void
! 59: #if __STDC__
! 60: gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, unsigned long int nbits)
! 61: #else
! 62: gmp_rrandomb (rp, rstate, nbits)
! 63: mp_ptr rp;
! 64: gmp_randstate_t rstate;
! 65: unsigned long int nbits;
! 66: #endif
! 67: {
! 68: int nb;
! 69: int bit_pos;
! 70: mp_size_t limb_pos;
! 71: mp_limb_t ran, ranm;
! 72: mp_limb_t acc;
! 73: mp_size_t n;
! 74:
! 75: bit_pos = nbits % BITS_PER_MP_LIMB;
! 76: limb_pos = nbits / BITS_PER_MP_LIMB;
! 77: if (bit_pos == 0)
! 78: {
! 79: bit_pos = BITS_PER_MP_LIMB;
! 80: limb_pos--;
! 81: }
! 82:
! 83: acc = 0;
! 84: while (limb_pos >= 0)
! 85: {
! 86: _gmp_rand (&ranm, rstate, BITS_PER_CHUNK + 1);
! 87: ran = ranm;
! 88: nb = (ran >> 1) + 1;
! 89: if ((ran & 1) != 0)
! 90: {
! 91: /* Generate a string of ones. */
! 92: if (nb > bit_pos)
! 93: {
! 94: rp[limb_pos--] = acc | ((((mp_limb_t) 1) << bit_pos) - 1);
! 95: bit_pos += BITS_PER_MP_LIMB;
! 96: bit_pos -= nb;
! 97: acc = (~(mp_limb_t) 0) << bit_pos;
! 98: }
! 99: else
! 100: {
! 101: bit_pos -= nb;
! 102: acc |= ((((mp_limb_t) 1) << nb) - 1) << bit_pos;
! 103: }
! 104: }
! 105: else
! 106: {
! 107: /* Generate a string of zeroes. */
! 108: if (nb > bit_pos)
! 109: {
! 110: rp[limb_pos--] = acc;
! 111: acc = 0;
! 112: bit_pos += BITS_PER_MP_LIMB;
! 113: }
! 114: bit_pos -= nb;
! 115: }
! 116: }
! 117: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>