Home | History | Annotate | Line # | Download | only in rand
randbui.c revision 1.1.1.1.8.2
      1  1.1.1.1.8.2  tls /* gmp_urandomb_ui -- random bits returned in a ulong.
      2  1.1.1.1.8.2  tls 
      3  1.1.1.1.8.2  tls Copyright 2003, 2004 Free Software Foundation, Inc.
      4  1.1.1.1.8.2  tls 
      5  1.1.1.1.8.2  tls This file is part of the GNU MP Library.
      6  1.1.1.1.8.2  tls 
      7  1.1.1.1.8.2  tls The GNU MP Library is free software; you can redistribute it and/or modify
      8  1.1.1.1.8.2  tls it under the terms of the GNU Lesser General Public License as published by
      9  1.1.1.1.8.2  tls the Free Software Foundation; either version 3 of the License, or (at your
     10  1.1.1.1.8.2  tls option) any later version.
     11  1.1.1.1.8.2  tls 
     12  1.1.1.1.8.2  tls The GNU MP Library is distributed in the hope that it will be useful, but
     13  1.1.1.1.8.2  tls WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     14  1.1.1.1.8.2  tls or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     15  1.1.1.1.8.2  tls License for more details.
     16  1.1.1.1.8.2  tls 
     17  1.1.1.1.8.2  tls You should have received a copy of the GNU Lesser General Public License
     18  1.1.1.1.8.2  tls along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     19  1.1.1.1.8.2  tls 
     20  1.1.1.1.8.2  tls #include "gmp.h"
     21  1.1.1.1.8.2  tls #include "gmp-impl.h"
     22  1.1.1.1.8.2  tls 
     23  1.1.1.1.8.2  tls 
     24  1.1.1.1.8.2  tls /* Currently bits>=BITS_PER_ULONG is quietly truncated to BITS_PER_ULONG,
     25  1.1.1.1.8.2  tls    maybe this should raise an exception or something.  */
     26  1.1.1.1.8.2  tls 
     27  1.1.1.1.8.2  tls unsigned long
     28  1.1.1.1.8.2  tls gmp_urandomb_ui (gmp_randstate_ptr rstate, unsigned long bits)
     29  1.1.1.1.8.2  tls {
     30  1.1.1.1.8.2  tls   mp_limb_t  a[LIMBS_PER_ULONG];
     31  1.1.1.1.8.2  tls 
     32  1.1.1.1.8.2  tls   /* start with zeros, since if bits==0 then _gmp_rand will store nothing at
     33  1.1.1.1.8.2  tls      all, or if bits <= GMP_NUMB_BITS then it will store only a[0] */
     34  1.1.1.1.8.2  tls   a[0] = 0;
     35  1.1.1.1.8.2  tls #if LIMBS_PER_ULONG > 1
     36  1.1.1.1.8.2  tls   a[1] = 0;
     37  1.1.1.1.8.2  tls #endif
     38  1.1.1.1.8.2  tls 
     39  1.1.1.1.8.2  tls   _gmp_rand (a, rstate, MIN (bits, BITS_PER_ULONG));
     40  1.1.1.1.8.2  tls 
     41  1.1.1.1.8.2  tls #if LIMBS_PER_ULONG == 1
     42  1.1.1.1.8.2  tls   return a[0];
     43  1.1.1.1.8.2  tls #else
     44  1.1.1.1.8.2  tls   return a[0] | (a[1] << GMP_NUMB_BITS);
     45  1.1.1.1.8.2  tls #endif
     46  1.1.1.1.8.2  tls }
     47