[BACK]Return to random2.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / gmp-2.0.2-ssh-2 / mpn / generic

Annotation of OpenXM/src/kan96xx/gmp-2.0.2-ssh-2/mpn/generic/random2.c, Revision 1.1

1.1     ! takayama    1: /* mpn_random2 -- Generate random numbers with relatively long strings
        !             2:    of ones and zeroes.  Suitable for border testing.
        !             3:
        !             4: Copyright (C) 1992, 1993, 1994, 1996 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 Library General Public License as published by
        !            10: the Free Software Foundation; either version 2 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 Library General Public
        !            16: License for more details.
        !            17:
        !            18: You should have received a copy of the GNU Library 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: #if defined (__hpux) || defined (alpha__)  || defined (__svr4__) || defined (__SVR4)
        !            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
        !            44: mpn_random2 (res_ptr, size)
        !            45:      mp_ptr res_ptr;
        !            46:      mp_size_t size;
        !            47: {
        !            48:   int n_bits;
        !            49:   int bit_pos;
        !            50:   mp_size_t limb_pos;
        !            51:   unsigned int ran;
        !            52:   mp_limb_t limb;
        !            53:
        !            54:   limb = 0;
        !            55:
        !            56:   /* Start off in a random bit position in the most significant limb.  */
        !            57:   bit_pos = random () & (BITS_PER_MP_LIMB - 1);
        !            58:
        !            59:   /* Least significant bit of RAN chooses string of ones/string of zeroes.
        !            60:      Make most significant limb be non-zero by setting bit 0 of RAN.  */
        !            61:   ran = random () | 1;
        !            62:
        !            63:   for (limb_pos = size - 1; limb_pos >= 0; )
        !            64:     {
        !            65:       n_bits = (ran >> 1) % BITS_PER_MP_LIMB + 1;
        !            66:       if ((ran & 1) != 0)
        !            67:        {
        !            68:          /* Generate a string of ones.  */
        !            69:          if (n_bits >= bit_pos)
        !            70:            {
        !            71:              res_ptr[limb_pos--] = limb | ((((mp_limb_t) 2) << bit_pos) - 1);
        !            72:              bit_pos += BITS_PER_MP_LIMB;
        !            73:              limb = (~(mp_limb_t) 0) << (bit_pos - n_bits);
        !            74:            }
        !            75:          else
        !            76:            {
        !            77:              limb |= ((((mp_limb_t) 1) << n_bits) - 1) << (bit_pos - n_bits + 1);
        !            78:            }
        !            79:        }
        !            80:       else
        !            81:        {
        !            82:          /* Generate a string of zeroes.  */
        !            83:          if (n_bits >= bit_pos)
        !            84:            {
        !            85:              res_ptr[limb_pos--] = limb;
        !            86:              limb = 0;
        !            87:              bit_pos += BITS_PER_MP_LIMB;
        !            88:            }
        !            89:        }
        !            90:       bit_pos -= n_bits;
        !            91:       ran = random ();
        !            92:     }
        !            93: }

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