~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpn/generic/random2.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpn_random2 -- Generate random numbers with relatively long strings
 
2
   of ones and zeroes.  Suitable for border testing.
 
3
 
 
4
Copyright 1992, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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. */
 
22
 
 
23
#include "gmp.h"
 
24
#include "gmp-impl.h"
 
25
 
 
26
 
 
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
 
30
   significant bit set.
 
31
 
 
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.
 
34
 
 
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.
 
39
 
 
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.  */
 
43
 
 
44
#define LOGBITS_PER_BLOCK 5
 
45
#define BITS_PER_RANDCALL 32
 
46
 
 
47
void
 
48
mpn_random2 (mp_ptr rp, mp_size_t n)
 
49
{
 
50
  gmp_randstate_ptr rstate = RANDS;
 
51
  int nb;
 
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 */
 
58
 
 
59
  /* FIXME: Is n==0 supposed to be allowed? */
 
60
  ASSERT (n >= 0);
 
61
  ASSERT_ALWAYS (BITS_PER_MP_LIMB > LOGBITS_PER_BLOCK);
 
62
 
 
63
  _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
 
64
  ran = ranm;
 
65
 
 
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) */
 
70
 
 
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.  */
 
73
  ran |= 1;
 
74
 
 
75
  ri = n - 1;
 
76
 
 
77
  acc = 0;
 
78
  while (ri >= 0)
 
79
    {
 
80
      if (ran_nbits < LOGBITS_PER_BLOCK + 1)
 
81
        {
 
82
          _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
 
83
          ran = ranm;
 
84
          ran_nbits = BITS_PER_RANDCALL;
 
85
        }
 
86
 
 
87
      nb = (ran >> 1) % (1 << LOGBITS_PER_BLOCK) + 1;
 
88
      if ((ran & 1) != 0)
 
89
        {
 
90
          /* Generate a string of nb ones.  */
 
91
          if (nb > bit_pos)
 
92
            {
 
93
              rp[ri--] = acc | (((mp_limb_t) 2 << bit_pos) - 1);
 
94
              bit_pos += BITS_PER_MP_LIMB;
 
95
              bit_pos -= nb;
 
96
              acc = (~(mp_limb_t) 1) << bit_pos;
 
97
            }
 
98
          else
 
99
            {
 
100
              bit_pos -= nb;
 
101
              acc |= (((mp_limb_t) 2 << nb) - 2) << bit_pos;
 
102
            }
 
103
        }
 
104
      else
 
105
        {
 
106
          /* Generate a string of nb zeroes.  */
 
107
          if (nb > bit_pos)
 
108
            {
 
109
              rp[ri--] = acc;
 
110
              acc = 0;
 
111
              bit_pos += BITS_PER_MP_LIMB;
 
112
            }
 
113
          bit_pos -= nb;
 
114
        }
 
115
      ran_nbits -= LOGBITS_PER_BLOCK + 1;
 
116
      ran >>= LOGBITS_PER_BLOCK + 1;
 
117
    }
 
118
}