From 94498de15f2619d6620543cad70b8a3c9fa43a61 Mon Sep 17 00:00:00 2001 From: vlefevre Date: Sun, 25 Nov 2001 07:49:47 +0000 Subject: mpfr_mul_2exp rewritten. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1579 280ebfd0-de03-0410-8827-d642c229c3f4 --- mul_2exp.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'mul_2exp.c') diff --git a/mul_2exp.c b/mul_2exp.c index e070896a3..90d1e2fed 100644 --- a/mul_2exp.c +++ b/mul_2exp.c @@ -19,7 +19,6 @@ along with the MPFR Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include #include "gmp.h" #include "gmp-impl.h" #include "mpfr.h" @@ -28,12 +27,30 @@ MA 02111-1307, USA. */ int mpfr_mul_2exp (mpfr_ptr y, mpfr_srcptr x, unsigned long int n, mp_rnd_t rnd_mode) { - int inexact = 0; + int inexact; - /* Important particular case */ - if (y != x) - inexact = mpfr_set (y, x, rnd_mode); - return ((MPFR_EXP(y) += n) > __mpfr_emax) - ? mpfr_set_overflow (y, rnd_mode, MPFR_SIGN(y)) : inexact; -} + inexact = y != x ? mpfr_set (y, x, rnd_mode) : 0; + + if (MPFR_IS_FP(y) && MPFR_NOTZERO(y)) + { + /* n will have to be casted to long to make sure that the addition + and subtraction below (for overflow detection) are signed */ + while (n > LONG_MAX) + { + int inex2; + + n -= LONG_MAX; + inex2 = mpfr_mul_2exp(y, y, LONG_MAX, rnd_mode); + if (inex2) + return inex2; /* overflow */ + } + if (__mpfr_emax < MPFR_EMIN_MIN + (long) n || + MPFR_EXP(y) > __mpfr_emax - (long) n) + return mpfr_set_overflow (y, rnd_mode, MPFR_SIGN(y)); + + MPFR_EXP(y) += (long) n; + } + + return inexact; +} -- cgit v1.2.1