summaryrefslogtreecommitdiff
path: root/src/get_sj.c
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2017-10-12 14:23:53 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2017-10-12 14:23:53 +0000
commitc25dc97f895f808eeb2a45d363358c0f6760f339 (patch)
tree9ccad4d8fbd567a48e93708c3d3ee4289e9bc827 /src/get_sj.c
parentfb50e23f3273d4fff908e58ad1b1728040361d17 (diff)
downloadmpfr-c25dc97f895f808eeb2a45d363358c0f6760f339.tar.gz
[src] Fixed the behavior of the mpfr_get_* functions in a very reduced
exponent range. (merged changesets r11770,11774,11775,11777 and r11779 manually from the trunk) git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@11783 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/get_sj.c')
-rw-r--r--src/get_sj.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/get_sj.c b/src/get_sj.c
index 5e92199d0..f270f375c 100644
--- a/src/get_sj.c
+++ b/src/get_sj.c
@@ -35,6 +35,7 @@ mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
intmax_t r;
mpfr_prec_t prec;
mpfr_t x;
+ MPFR_SAVE_EXPO_DECL (expo);
if (MPFR_UNLIKELY (!mpfr_fits_intmax_p (f, rnd)))
{
@@ -46,20 +47,24 @@ mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
if (MPFR_IS_ZERO (f))
return (intmax_t) 0;
- /* determine the precision of intmax_t */
- for (r = MPFR_INTMAX_MIN, prec = 0; r != 0; r /= 2, prec++)
+ /* Determine the precision of intmax_t. |INTMAX_MIN| may have one
+ more bit as an integer, but in this case, this is a power of 2,
+ thus fits in a precision-prec floating-point number. */
+ for (r = MPFR_INTMAX_MAX, prec = 0; r != 0; r /= 2, prec++)
{ }
- /* Note: though INTMAX_MAX would have been sufficient for the conversion,
- we chose INTMAX_MIN so that INTMAX_MIN - 1 is always representable in
- precision prec; this is useful to detect overflows in MPFR_RNDZ (will
- be needed later). */
- /* Now, r = 0. */
+ MPFR_ASSERTD (r == 0);
+
+ MPFR_SAVE_EXPO_MARK (expo);
mpfr_init2 (x, prec);
mpfr_rint (x, f, rnd);
MPFR_ASSERTN (MPFR_IS_FP (x));
+ /* The flags from mpfr_rint are the wanted ones. In particular,
+ it sets the inexact flag when necessary. */
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
+
if (MPFR_NOTZERO (x))
{
mp_limb_t *xp;
@@ -67,15 +72,15 @@ mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
xp = MPFR_MANT (x);
sh = MPFR_GET_EXP (x);
- MPFR_ASSERTN ((mpfr_prec_t) sh <= prec);
+ MPFR_ASSERTN ((mpfr_prec_t) sh <= prec + 1);
if (MPFR_INTMAX_MIN + MPFR_INTMAX_MAX != 0
- && MPFR_UNLIKELY ((mpfr_prec_t) sh == prec))
+ && MPFR_UNLIKELY ((mpfr_prec_t) sh > prec))
{
/* 2's complement and x <= INTMAX_MIN: in the case mp_limb_t
has the same size as intmax_t, we cannot use the code in
the for loop since the operations would be performed in
unsigned arithmetic. */
- MPFR_ASSERTN (MPFR_IS_NEG (x) && (mpfr_powerof2_raw (x)));
+ MPFR_ASSERTN (MPFR_IS_NEG (x) && mpfr_powerof2_raw (x));
r = MPFR_INTMAX_MIN;
}
else if (MPFR_IS_POS (x))
@@ -117,6 +122,8 @@ mpfr_get_sj (mpfr_srcptr f, mpfr_rnd_t rnd)
mpfr_clear (x);
+ MPFR_SAVE_EXPO_FREE (expo);
+
return r;
}