summaryrefslogtreecommitdiff
path: root/src/urandomb.c
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2010-08-17 09:10:13 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2010-08-17 09:10:13 +0000
commitc9583bdfe064e1069828e518533f7bc29a8fdddb (patch)
tree2400842d4095628b8486fbeabaf7bc7b8af4ed02 /src/urandomb.c
parent50ac5b5985174201c7fa6e20496cd2b096107001 (diff)
downloadmpfr-c9583bdfe064e1069828e518533f7bc29a8fdddb.tar.gz
Source reorganization. In short:
* Added directories and moved related files into them: - src for the MPFR source files (to build the library). - doc for documentation files (except INSTALL, README...). - tools for various tools (scripts) and mbench. - tune for tuneup-related source files. - other for other source files (not distributed in tarballs). Existing directories: - tests for the source files of the test suite (make check). - examples for examples. - m4 for m4 files. * Renamed configure.in to configure.ac. * Added/updated Makefile.am files where needed. * Updated acinclude.m4 and configure.ac (AC_CONFIG_FILES line). * Updated the documentation (INSTALL, README, doc/README.dev and doc/mpfr.texi). * Updated NEWS and TODO. * Updated the scripts now in tools. The following script was used: #!/usr/bin/env zsh svn mkdir doc other src tools tune svn mv ${${(M)$(sed -n '/libmpfr_la_SOURCES/,/[^\]$/p' \ Makefile.am):#*.[ch]}:#get_patches.c} mparam_h.in \ round_raw_generic.c jyn_asympt.c src svn mv mbench check_inits_clears coverage get_patches.sh mpfrlint \ nightly-test update-patchv update-version tools svn mv bidimensional_sample.c speed.c tuneup.c tune svn mv *.{c,h} other svn mv FAQ.html README.dev algorithm* faq.xsl fdl.texi mpfr.texi \ update-faq doc svn mv configure.in configure.ac svn cp Makefile.am src/Makefile.am svn rm replace_all [Modifying some files, see above] svn add doc/Makefile.am svn add tune/Makefile.am git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@7087 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/urandomb.c')
-rw-r--r--src/urandomb.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/urandomb.c b/src/urandomb.c
new file mode 100644
index 000000000..e632ca83d
--- /dev/null
+++ b/src/urandomb.c
@@ -0,0 +1,98 @@
+/* mpfr_urandomb (rop, state, nbits) -- Generate a uniform pseudorandom
+ real number between 0 (inclusive) and 1 (exclusive) of size NBITS,
+ using STATE as the random state previously initialized by a call to
+ gmp_randinit_lc_2exp_size().
+
+Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+Contributed by the Arenaire and Caramel projects, INRIA.
+
+This file is part of the GNU MPFR Library.
+
+The GNU MPFR Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+The GNU MPFR Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
+http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+
+#define MPFR_NEED_LONGLONG_H
+#include "mpfr-impl.h"
+
+void
+mpfr_rand_raw (mp_ptr mp, gmp_randstate_t rstate, unsigned long int nbits)
+{
+ mpz_t z;
+
+ /* To be sure to avoid the potential allocation of mpz_urandomb */
+ ALLOC(z) = SIZ(z) = (nbits / GMP_NUMB_BITS) + 1;
+ PTR(z) = mp;
+ mpz_urandomb(z, rstate, nbits);
+}
+
+int
+mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate)
+{
+ mp_ptr rp;
+ mpfr_prec_t nbits;
+ mp_size_t nlimbs;
+ mp_size_t k; /* number of high zero limbs */
+ mpfr_exp_t exp;
+ int cnt;
+
+ rp = MPFR_MANT (rop);
+ nbits = MPFR_PREC (rop);
+ nlimbs = MPFR_LIMB_SIZE (rop);
+ MPFR_SET_POS (rop);
+
+ /* Uniform non-normalized significand */
+ mpfr_rand_raw (rp, rstate, nlimbs * GMP_NUMB_BITS);
+
+ /* If nbits isn't a multiple of GMP_NUMB_BITS, mask the low bits */
+ cnt = nlimbs * GMP_NUMB_BITS - nbits;
+ if (MPFR_LIKELY (cnt != 0))
+ rp[0] &= ~MPFR_LIMB_MASK (cnt);
+
+ /* Count the null significant limbs and remaining limbs */
+ exp = 0;
+ k = 0;
+ while (nlimbs != 0 && rp[nlimbs - 1] == 0)
+ {
+ k ++;
+ nlimbs --;
+ exp -= GMP_NUMB_BITS;
+ }
+
+ if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */
+ {
+ count_leading_zeros (cnt, rp[nlimbs - 1]);
+ /* Normalization */
+ if (mpfr_set_exp (rop, exp - cnt))
+ {
+ /* If the exponent is not in the current exponent range, we
+ choose to return a NaN as this is probably a user error.
+ Indeed this can happen only if the exponent range has been
+ reduced to a very small interval and/or the precision is
+ huge (very unlikely). */
+ MPFR_SET_NAN (rop);
+ __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */
+ return 1;
+ }
+ if (cnt != 0)
+ mpn_lshift (rp + k, rp, nlimbs, cnt);
+ if (k != 0)
+ MPN_ZERO (rp, k);
+ }
+ else
+ MPFR_SET_ZERO (rop);
+
+ return 0;
+}