diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-11-25 08:04:07 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-11-25 08:04:07 +0000 |
commit | d35e7912a742c47896a9637786b73aafada34adb (patch) | |
tree | 8e2184b05ca27a43e178908ab7fd69fde8b9ffb1 | |
parent | 94498de15f2619d6620543cad70b8a3c9fa43a61 (diff) | |
download | mpfr-d35e7912a742c47896a9637786b73aafada34adb.tar.gz |
mpfr_div_2exp rewritten.
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1580 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r-- | div_2exp.c | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/div_2exp.c b/div_2exp.c index 68284f0a4..691fb1be4 100644 --- a/div_2exp.c +++ b/div_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 <stdio.h> #include "gmp.h" #include "gmp-impl.h" #include "mpfr.h" @@ -28,12 +27,30 @@ MA 02111-1307, USA. */ int mpfr_div_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_emin) - ? mpfr_set_underflow (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_div_2exp(y, y, LONG_MAX, rnd_mode); + if (inex2) + return inex2; /* underflow */ + } + if (__mpfr_emin > MPFR_EMAX_MAX - (long) n || + MPFR_EXP(y) < __mpfr_emin + (long) n) + return mpfr_set_underflow (y, rnd_mode, MPFR_SIGN(y)); + + MPFR_EXP(y) -= (long) n; + } + + return inexact; +} |