diff options
author | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2011-01-15 08:14:51 +0000 |
---|---|---|
committer | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2011-01-15 08:14:51 +0000 |
commit | bad8c9403365d3c9ee6d1a41242952706ad28474 (patch) | |
tree | 10d104473d3b337187ae0189b2953947c01104df /src | |
parent | c0e5954dae63e841345d9fb574a49ff6d3e06e92 (diff) | |
download | mpfr-bad8c9403365d3c9ee6d1a41242952706ad28474.tar.gz |
[src/frexp.c,tests/tfrexp.c] added new function mpfr_frexp
[src/urandom_gaussian.c] fixed copyright line
[src/out_raw.c] started writing code (work in progress, please continue if
you are interested)
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@7372 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/frexp.c | 56 | ||||
-rw-r--r-- | src/mpfr.h | 2 | ||||
-rw-r--r-- | src/out_raw.c | 68 | ||||
-rw-r--r-- | src/urandom_gaussian.c | 4 |
5 files changed, 129 insertions, 3 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 8e7a34def..2b7c09a4a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,7 +23,7 @@ mpfr-longlong.h mpfr-thread.h exceptions.c extract.c uceil_exp2.c \ uceil_log2.c ufloor_log2.c add.c add1.c add_ui.c agm.c clear.c cmp.c \ cmp_abs.c cmp_si.c cmp_ui.c comparisons.c div_2exp.c div_2si.c \ div_2ui.c div.c div_ui.c dump.c eq.c exp10.c exp2.c exp3.c exp.c \ -frac.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c \ +frac.c frexp.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c \ isinf.c isnan.c isnum.c const_log2.c log.c modf.c mul_2exp.c mul_2si.c \ mul_2ui.c mul.c mul_ui.c neg.c next.c out_str.c printf.c vasprintf.c \ const_pi.c pow.c pow_si.c pow_ui.c print_raw.c print_rnd_mode.c \ diff --git a/src/frexp.c b/src/frexp.c new file mode 100644 index 000000000..9a8177c27 --- /dev/null +++ b/src/frexp.c @@ -0,0 +1,56 @@ +/* mpfr_frexp -- convert to integral and fractional parts + +Copyright 2011 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. */ + +#include "mpfr-impl.h" + +int +mpfr_frexp (mpfr_exp_t *exp, mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd) +{ + int inex; + + if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x))) + { + if (MPFR_IS_NAN(x)) + { + MPFR_SET_NAN(y); + MPFR_RET_NAN; /* exp is unspecified */ + } + else if (MPFR_IS_INF(x)) + { + MPFR_SET_INF(y); + MPFR_SET_SAME_SIGN(y,x); + MPFR_RET(0); /* exp is unspecified */ + } + else + { + MPFR_SET_ZERO(y); + MPFR_SET_SAME_SIGN(y,x); + *exp = 0; + MPFR_RET(0); + } + } + + inex = mpfr_set (y, x, rnd); + *exp = MPFR_EXP(y); + MPFR_EXP(y) = 0; + return inex; +} diff --git a/src/mpfr.h b/src/mpfr.h index 61cfd8561..48b245f4a 100644 --- a/src/mpfr.h +++ b/src/mpfr.h @@ -355,6 +355,8 @@ __MPFR_DECLSPEC double mpfr_get_d_2exp _MPFR_PROTO ((long*, mpfr_srcptr, mpfr_rnd_t)); __MPFR_DECLSPEC long double mpfr_get_ld_2exp _MPFR_PROTO ((long*, mpfr_srcptr, mpfr_rnd_t)); +__MPFR_DECLSPEC int mpfr_frexp _MPFR_PROTO ((mpfr_exp_t*, mpfr_ptr, + mpfr_srcptr, mpfr_rnd_t)); __MPFR_DECLSPEC long mpfr_get_si _MPFR_PROTO ((mpfr_srcptr, mpfr_rnd_t)); __MPFR_DECLSPEC unsigned long mpfr_get_ui _MPFR_PROTO ((mpfr_srcptr, mpfr_rnd_t)); diff --git a/src/out_raw.c b/src/out_raw.c index f133a5169..4d044992d 100644 --- a/src/out_raw.c +++ b/src/out_raw.c @@ -1,3 +1,27 @@ +/* mpfr_out_raw -- output a floating-point number to binary portable format + +Copyright 2011 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. */ + +#include "mpfr-impl.h" + /* format for out_raw: A mpfr_t is represented by up to 3 fields, each one is represented by a sequence of 32-bit words. 32-bit words are stored as 4 bytes in little @@ -38,3 +62,47 @@ - a normal binary128 IEEE-754 number uses 192 bits: 32 for (a), 32 for (b), and 128 for (c). */ + +size_t +mpfr_out_raw (FILE *stream, mpfr_srcptr x) +{ + size_t n; /* number of bytes of the output */ + mpfr_prec_t prec = MPFR_PREC(x); + int prec_enc, exp_enc = 0; + char *s, *t; + + prec_enc = (prec >= 134217728); + n = 4 + 4 * prec_enc; + if (MPFR_IS_SINGULAR(x) == 0) + { + mpfr_exp_t e = MPFR_EXP(x); + mpfr_prec_t p = (prec - 1) / 32 + 1; /* ceil(prec/32) */ + + exp_enc = (e < -2147483648 || 2147483647 < e); + n += 4 + 4 * exp_enc + 4 * p; + } + t = s = malloc (n * sizeof (char)); + t[0] = mpfr_signbit (x) << 7; + if (MPFR_IS_NAN(x)) + t[0] += 3 << 5; + else if (MPFR_IS_INF(x)) + t[0] += 2 << 5; + else if (MPFR_IS_ZERO(x)) + t[0] += 1 << 5; + t[0] += exp_enc << 4; + t[0] += prec_enc << 3; + if (prec_enc == 1) + { + t[0] += prec >> 56; /* reduction mod 255 is implicit */ + t[1] += prec >> 48; + t[2] += prec >> 40; + t[3] += prec >> 32; + t += 4; + } + t[0] += prec >> 24; /* reduction mod 255 is implicit */ + t[1] = prec >> 16; + t[2] = prec >> 8; + t[3] = prec; + free (s); + return n; +} diff --git a/src/urandom_gaussian.c b/src/urandom_gaussian.c index 30d901500..f68ef2ba9 100644 --- a/src/urandom_gaussian.c +++ b/src/urandom_gaussian.c @@ -3,7 +3,7 @@ gaussian distribution and round it to the precision of rop1, rop2 according to the given rounding mode. -Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. +Copyright 2011 Free Software Foundation, Inc. Contributed by the Arenaire and Caramel projects, INRIA. This file is part of the GNU MPFR Library. @@ -24,7 +24,7 @@ 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 +/* #define MPFR_NEED_LONGLONG_H */ #include "mpfr-impl.h" |