summaryrefslogtreecommitdiff
path: root/mpf
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2003-06-28 01:59:09 +0200
committerKevin Ryde <user42@zip.com.au>2003-06-28 01:59:09 +0200
commitff5b4cdd122a061e3ba8a1f6091b3f0dac912d45 (patch)
tree5b31f6e3c6dcd8fa0b5a3875ba676ef1b4adb1fa /mpf
parent2c4d52c47f75a8e231567b53d3b1acd0e63707d7 (diff)
downloadgmp-ff5b4cdd122a061e3ba8a1f6091b3f0dac912d45.tar.gz
* mpz/get_d_2exp.c, mpf/get_d_2exp.c: Avoid res==1.0 when floats round
upwards.
Diffstat (limited to 'mpf')
-rw-r--r--mpf/get_d_2exp.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/mpf/get_d_2exp.c b/mpf/get_d_2exp.c
index c16752b7d..3ba97f647 100644
--- a/mpf/get_d_2exp.c
+++ b/mpf/get_d_2exp.c
@@ -1,6 +1,6 @@
/* double mpf_get_d_2exp (signed long int *exp, mpf_t src).
-Copyright 2001, 2002 Free Software Foundation, Inc.
+Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -31,6 +31,7 @@ mpf_get_d_2exp (signed long int *exp2, mpf_srcptr src)
int negative;
mp_ptr qp;
int cnt;
+ long exp;
size = SIZ(src);
if (size == 0)
@@ -49,8 +50,27 @@ mpf_get_d_2exp (signed long int *exp2, mpf_srcptr src)
for (i = 1; i < n_limbs_to_use; i++)
res = (res + qp[i]) / MP_BASE_AS_DOUBLE;
count_leading_zeros (cnt, qp[n_limbs_to_use - 1]);
- *exp2 = EXP(src) * GMP_NUMB_BITS - cnt + GMP_NAIL_BITS;
+ exp = EXP(src) * GMP_NUMB_BITS - cnt + GMP_NAIL_BITS;
res = res * ((mp_limb_t) 1 << cnt);
+ /* gcc on m68k and x86 holds floats in the coprocessor, which may mean
+ "res" has extra precision. Force it through memory to ensure any
+ rounding takes place now and won't become 1.0 in the caller. */
+#if (HAVE_HOST_CPU_FAMILY_m68k || HAVE_HOST_CPU_FAMILY_x86) \
+ && defined (__GNUC__)
+ asm ("" : "=m" (res) : "0" (res));
+#endif
+
+ /* if hardware floats are in round upwards mode then res may be 1.0 */
+ if (UNLIKELY (res >= 1.0))
+ {
+ res *= 0.5;
+ exp++;
+ }
+
+ ASSERT (res >= 0.5);
+ ASSERT (res < 1.0);
+
+ *exp2 = exp;
return negative ? -res : res;
}