1 /* mpfr_random2 -- Generate a positive random mpfr_t of specified size, with 2 long runs of consecutive ones and zeros in the binary representation. 3 4 Copyright 1999, 2001-2004, 2006-2018 Free Software Foundation, Inc. 5 Contributed by the AriC and Caramba projects, INRIA. 6 7 This file is part of the GNU MPFR Library. 8 9 The GNU MPFR 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 3 of the License, or (at your 12 option) any later version. 13 14 The GNU MPFR 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 MPFR Library; see the file COPYING.LESSER. If not, see 21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24 #include "mpfr-test.h" 25 26 #define LOGBITS_PER_BLOCK 4 27 #if GMP_NUMB_BITS < 32 28 #define BITS_PER_RANDCALL GMP_NUMB_BITS 29 #else 30 #define BITS_PER_RANDCALL 32 31 #endif 32 33 /* exp is the maximum exponent in absolute value */ 34 void 35 mpfr_random2 (mpfr_ptr x, mp_size_t size, mpfr_exp_t exp, 36 gmp_randstate_t rstate) 37 { 38 mp_size_t xn, k, ri; 39 unsigned long sh; 40 mp_limb_t *xp; 41 mp_limb_t elimb, ran, acc; 42 int ran_nbits, bit_pos, nb; 43 44 if (MPFR_UNLIKELY(size == 0)) 45 { 46 MPFR_SET_ZERO (x); 47 MPFR_SET_POS (x); 48 return ; 49 } 50 else if (size > 0) 51 { 52 MPFR_SET_POS (x); 53 } 54 else 55 { 56 MPFR_SET_NEG (x); 57 size = -size; 58 } 59 60 xn = MPFR_LIMB_SIZE (x); 61 xp = MPFR_MANT (x); 62 if (size > xn) 63 size = xn; 64 k = xn - size; 65 66 /* Code extracted from GMP, function mpn_random2, to avoid the use 67 of GMP's internal random state in MPFR */ 68 69 mpfr_rand_raw (&elimb, rstate, BITS_PER_RANDCALL); 70 ran = elimb; 71 72 /* Start off at a random bit position in the most significant limb. */ 73 bit_pos = GMP_NUMB_BITS - 1; 74 ran >>= 6; /* Ideally log2(GMP_NUMB_BITS) */ 75 ran_nbits = BITS_PER_RANDCALL - 6; /* Ideally - log2(GMP_NUMB_BITS) */ 76 77 /* Bit 0 of ran chooses string of ones/string of zeroes. 78 Make most significant limb be non-zero by setting bit 0 of RAN. */ 79 ran |= 1; 80 ri = xn - 1; 81 82 acc = 0; 83 while (ri >= k) 84 { 85 if (ran_nbits < LOGBITS_PER_BLOCK + 1) 86 { 87 mpfr_rand_raw (&elimb, rstate, BITS_PER_RANDCALL); 88 ran = elimb; 89 ran_nbits = BITS_PER_RANDCALL; 90 } 91 92 nb = (ran >> 1) % (1 << LOGBITS_PER_BLOCK) + 1; 93 if ((ran & 1) != 0) 94 { 95 /* Generate a string of nb ones. */ 96 if (nb > bit_pos) 97 { 98 xp[ri--] = acc | (((mp_limb_t) 2 << bit_pos) - 1); 99 bit_pos += GMP_NUMB_BITS; 100 bit_pos -= nb; 101 acc = (~MPFR_LIMB_ONE) << bit_pos; 102 } 103 else 104 { 105 bit_pos -= nb; 106 acc |= (((mp_limb_t) 2 << nb) - 2) << bit_pos; 107 } 108 } 109 else 110 { 111 /* Generate a string of nb zeroes. */ 112 if (nb > bit_pos) 113 { 114 xp[ri--] = acc; 115 acc = 0; 116 bit_pos += GMP_NUMB_BITS; 117 } 118 bit_pos -= nb; 119 } 120 ran_nbits -= LOGBITS_PER_BLOCK + 1; 121 ran >>= LOGBITS_PER_BLOCK + 1; 122 } 123 124 /* Set mandatory most significant bit. */ 125 /* xp[xn - 1] |= MPFR_LIMB_HIGHBIT; */ 126 127 if (k != 0) 128 { 129 /* Clear last limbs */ 130 MPN_ZERO (xp, k); 131 } 132 else 133 { 134 /* Mask off non significant bits in the low limb. */ 135 MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC (x)); 136 xp[0] &= ~MPFR_LIMB_MASK (sh); 137 } 138 139 /* Generate random exponent. */ 140 mpfr_rand_raw (&elimb, RANDS, GMP_NUMB_BITS); 141 MPFR_ASSERTN (exp >= 0 && exp <= MPFR_EMAX_MAX); 142 exp = (mpfr_exp_t) (elimb % (2 * exp + 1)) - exp; 143 MPFR_SET_EXP (x, exp); 144 145 return ; 146 } 147