[BACK]Return to random2.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpn / generic

Annotation of OpenXM_contrib/gmp/mpn/generic/random2.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpn_random2 -- Generate random numbers with relatively long strings
                      2:    of ones and zeroes.  Suitable for border testing.
                      3:
1.1.1.2 ! maekawa     4: Copyright (C) 1992, 1993, 1994, 1996, 2000 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     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
1.1       maekawa    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
1.1.1.2 ! maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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:
1.1.1.2 ! maekawa    26: #if defined (__hpux) || defined (__alpha)  || defined (__svr4__) || defined (__SVR4)
1.1       maekawa    27: /* HPUX lacks random().  DEC OSF/1 1.2 random() returns a double.  */
                     28: long mrand48 ();
                     29: static inline long
                     30: random ()
                     31: {
                     32:   return mrand48 ();
                     33: }
                     34: #else
                     35: long random ();
                     36: #endif
                     37:
                     38: /* It's a bit tricky to get this right, so please test the code well
                     39:    if you hack with it.  Some early versions of the function produced
                     40:    random numbers with the leading limb == 0, and some versions never
                     41:    made the most significant bit set.  */
                     42:
                     43: void
1.1.1.2 ! maekawa    44: #if __STDC__
        !            45: mpn_random2 (mp_ptr res_ptr, mp_size_t size)
        !            46: #else
1.1       maekawa    47: mpn_random2 (res_ptr, size)
                     48:      mp_ptr res_ptr;
                     49:      mp_size_t size;
1.1.1.2 ! maekawa    50: #endif
1.1       maekawa    51: {
                     52:   int n_bits;
                     53:   int bit_pos;
                     54:   mp_size_t limb_pos;
                     55:   unsigned int ran;
                     56:   mp_limb_t limb;
                     57:
                     58:   limb = 0;
                     59:
                     60:   /* Start off in a random bit position in the most significant limb.  */
                     61:   bit_pos = random () & (BITS_PER_MP_LIMB - 1);
                     62:
                     63:   /* Least significant bit of RAN chooses string of ones/string of zeroes.
                     64:      Make most significant limb be non-zero by setting bit 0 of RAN.  */
                     65:   ran = random () | 1;
                     66:
                     67:   for (limb_pos = size - 1; limb_pos >= 0; )
                     68:     {
                     69:       n_bits = (ran >> 1) % BITS_PER_MP_LIMB + 1;
                     70:       if ((ran & 1) != 0)
                     71:        {
                     72:          /* Generate a string of ones.  */
                     73:          if (n_bits >= bit_pos)
                     74:            {
                     75:              res_ptr[limb_pos--] = limb | ((((mp_limb_t) 2) << bit_pos) - 1);
                     76:              bit_pos += BITS_PER_MP_LIMB;
                     77:              limb = (~(mp_limb_t) 0) << (bit_pos - n_bits);
                     78:            }
                     79:          else
                     80:            {
                     81:              limb |= ((((mp_limb_t) 1) << n_bits) - 1) << (bit_pos - n_bits + 1);
                     82:            }
                     83:        }
                     84:       else
                     85:        {
                     86:          /* Generate a string of zeroes.  */
                     87:          if (n_bits >= bit_pos)
                     88:            {
                     89:              res_ptr[limb_pos--] = limb;
                     90:              limb = 0;
                     91:              bit_pos += BITS_PER_MP_LIMB;
                     92:            }
                     93:        }
                     94:       bit_pos -= n_bits;
                     95:       ran = random ();
                     96:     }
                     97: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>