beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / random2.c
blob980b15367fcfe9ff0016173f2fb43322cb2d634f
1 /* mpn_random2 -- Generate random numbers with relatively long strings
2 of ones and zeroes. Suitable for border testing.
4 Copyright 1992-1994, 1996, 2000-2002, 2004, 2012 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 either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
35 static void gmp_rrandomb (mp_ptr, gmp_randstate_t, mp_bitcnt_t);
37 /* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold.
38 Thus, we get the same random number sequence in the common cases.
39 FIXME: We should always generate the same random number sequence! */
40 #if GMP_NUMB_BITS < 32
41 #define BITS_PER_RANDCALL GMP_NUMB_BITS
42 #else
43 #define BITS_PER_RANDCALL 32
44 #endif
46 void
47 mpn_random2 (mp_ptr rp, mp_size_t n)
49 gmp_randstate_ptr rstate = RANDS;
50 int bit_pos; /* bit number of least significant bit where
51 next bit field to be inserted */
52 mp_limb_t ran, ranm; /* buffer for random bits */
54 /* FIXME: Is n==0 supposed to be allowed? */
55 ASSERT (n >= 0);
57 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
58 ran = ranm;
60 /* Start off at a random bit position in the most significant limb. */
61 bit_pos = ran % GMP_NUMB_BITS;
63 gmp_rrandomb (rp, rstate, n * GMP_NUMB_BITS - bit_pos);
66 static void
67 gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits)
69 mp_bitcnt_t bi;
70 mp_limb_t ranm; /* buffer for random bits */
71 unsigned cap_chunksize, chunksize;
72 mp_size_t i;
74 /* Set entire result to 111..1 */
75 i = BITS_TO_LIMBS (nbits) - 1;
76 rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS;
77 for (i = i - 1; i >= 0; i--)
78 rp[i] = GMP_NUMB_MAX;
80 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
81 cap_chunksize = nbits / (ranm % 4 + 1);
82 cap_chunksize += cap_chunksize == 0; /* make it at least 1 */
84 bi = nbits;
86 for (;;)
88 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
89 chunksize = 1 + ranm % cap_chunksize;
90 bi = (bi < chunksize) ? 0 : bi - chunksize;
92 if (bi == 0)
93 break; /* low chunk is ...1 */
95 rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS;
97 _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
98 chunksize = 1 + ranm % cap_chunksize;
99 bi = (bi < chunksize) ? 0 : bi - chunksize;
101 mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS);
103 if (bi == 0)
104 break; /* low chunk is ...0 */