diff options
Diffstat (limited to 'src/set_uj.c')
-rw-r--r-- | src/set_uj.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/set_uj.c b/src/set_uj.c index 7b94a2a09..5a79ee772 100644 --- a/src/set_uj.c +++ b/src/set_uj.c @@ -41,7 +41,7 @@ mpfr_set_uj (mpfr_t x, uintmax_t j, mpfr_rnd_t rnd) int mpfr_set_uj_2exp (mpfr_t x, uintmax_t j, intmax_t e, mpfr_rnd_t rnd) { - int cnt; + int cnt, inex; mp_size_t i, k; mp_limb_t limb; mp_limb_t yp[uintmaxpml]; @@ -57,6 +57,10 @@ mpfr_set_uj_2exp (mpfr_t x, uintmax_t j, intmax_t e, mpfr_rnd_t rnd) MPFR_RET(0); } + /* early overflow detection to avoid a possible integer overflow below */ + if (MPFR_UNLIKELY(e >= __gmpfr_emax)) + return mpfr_overflow (x, rnd, MPFR_SIGN_POS); + MPFR_ASSERTN (sizeof(uintmax_t) % sizeof(mp_limb_t) == 0); /* Create an auxiliary var */ @@ -107,7 +111,9 @@ mpfr_set_uj_2exp (mpfr_t x, uintmax_t j, intmax_t e, mpfr_rnd_t rnd) e += k * GMP_NUMB_BITS - cnt; /* Update Expo */ MPFR_ASSERTD (MPFR_LIMB_MSB(yp[numberof (yp) - 1]) != 0); - /* Check expo underflow / overflow (can't use mpfr_check_range) */ + MPFR_RNDRAW (inex, x, yp, uintmax_bit_size, rnd, MPFR_SIGN_POS, e++); + + /* Check expo underflow / overflow */ if (MPFR_UNLIKELY(e < __gmpfr_emin)) { /* The following test is necessary because in the rounding to the @@ -116,16 +122,18 @@ mpfr_set_uj_2exp (mpfr_t x, uintmax_t j, intmax_t e, mpfr_rnd_t rnd) * _ |x| < 2^(emin-2), or * _ |x| = 2^(emin-2) and the absolute value of the exact * result is <= 2^(emin-2). */ - if (rnd == MPFR_RNDN && (e+1 < __gmpfr_emin || mpfr_powerof2_raw(y))) + if (rnd == MPFR_RNDN && + (e + 1 < __gmpfr_emin || + (mpfr_powerof2_raw (x) && inex >= 0))) rnd = MPFR_RNDZ; return mpfr_underflow (x, rnd, MPFR_SIGN_POS); } if (MPFR_UNLIKELY(e > __gmpfr_emax)) return mpfr_overflow (x, rnd, MPFR_SIGN_POS); - MPFR_SET_EXP (y, e); - /* Final: set x to y (rounding if necessary) */ - return mpfr_set (x, y, rnd); + MPFR_SET_SIGN (x, MPFR_SIGN_POS); + MPFR_SET_EXP (x, e); + MPFR_RET (inex); } #endif |