summaryrefslogtreecommitdiff
path: root/mul_2exp.c
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2001-11-25 07:49:47 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2001-11-25 07:49:47 +0000
commit94498de15f2619d6620543cad70b8a3c9fa43a61 (patch)
tree5296a0064ae525328ae959af08622ff8568c2338 /mul_2exp.c
parent002ccefcbf40baa74d0fd119e1b3c59be2780d1d (diff)
downloadmpfr-94498de15f2619d6620543cad70b8a3c9fa43a61.tar.gz
mpfr_mul_2exp rewritten.
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1579 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'mul_2exp.c')
-rw-r--r--mul_2exp.c33
1 files changed, 25 insertions, 8 deletions
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 <stdio.h>
#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;
+}