This was accidently not svn added when the compiler was updated.
[coreboot.git] / util / crossgcc / patches / mpfr-3.0.0_allpatches_20101117.patch
1 diff -rupN mpfr-3.0.0.orig/Makefile.in mpfr-3.0.0/Makefile.in
2 --- mpfr-3.0.0.orig/Makefile.in 2010-11-17 14:50:50.054040400 -0700
3 +++ mpfr-3.0.0/Makefile.in      2010-06-10 05:00:52.000000000 -0600
4 @@ -239,6 +239,7 @@ GZIP_ENV = --best
5  distuninstallcheck_listfiles = find . -type f -print
6  distcleancheck_listfiles = find . -type f -print
7  ACLOCAL = @ACLOCAL@
8 +ALLOCA = @ALLOCA@
9  AMTAR = @AMTAR@
10  AR = @AR@
11  AS = @AS@
12 diff -rupN mpfr-3.0.0.orig/PATCHES mpfr-3.0.0/PATCHES
13 --- mpfr-3.0.0.orig/PATCHES     2010-11-17 14:50:50.081041900 -0700
14 +++ mpfr-3.0.0/PATCHES  2010-11-17 14:51:05.016295800 -0700
15 @@ -0,0 +1,8 @@
16 +macros
17 +mpfr_set_ld
18 +mpfr_sub1
19 +tcan_round
20 +mpfr_cmp/set_ui/si
21 +gamma_underflow
22 +alloca
23 +mpfr_out_str
24 diff -rupN mpfr-3.0.0.orig/VERSION mpfr-3.0.0/VERSION
25 --- mpfr-3.0.0.orig/VERSION     2010-11-17 14:50:50.821082000 -0700
26 +++ mpfr-3.0.0/VERSION  2010-11-09 08:15:07.000000000 -0700
27 @@ -1 +1 @@
28 -3.0.0
29 +3.0.0-p8
30 diff -rupN mpfr-3.0.0.orig/acinclude.m4 mpfr-3.0.0/acinclude.m4
31 --- mpfr-3.0.0.orig/acinclude.m4        2010-11-17 14:50:49.896031300 -0700
32 +++ mpfr-3.0.0/acinclude.m4     2010-06-10 05:00:14.000000000 -0600
33 @@ -59,6 +59,9 @@ AC_CHECK_HEADER([stdarg.h],[AC_DEFINE([H
34  dnl sys/fpu.h - MIPS specific
35  AC_CHECK_HEADERS([sys/time.h sys/fpu.h])
36
37 +dnl Check how to get `alloca'
38 +AC_FUNC_ALLOCA
39 +
40  dnl SIZE_MAX macro
41  gl_SIZE_MAX
42
43 diff -rupN mpfr-3.0.0.orig/configure mpfr-3.0.0/configure
44 --- mpfr-3.0.0.orig/configure   2010-11-17 14:50:49.945034100 -0700
45 +++ mpfr-3.0.0/configure        2010-06-25 07:23:05.000000000 -0600
46 @@ -783,6 +783,7 @@ LIBTOOL
47  OBJDUMP
48  DLLTOOL
49  AS
50 +ALLOCA
51  MPFR_LIBM
52  ANSI2KNR
53  U
54 @@ -5622,6 +5623,197 @@ fi
55  done
56
57
58 +# The Ultrix 4.2 mips builtin alloca declared by alloca.h only works
59 +# for constant arguments.  Useless!
60 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for working alloca.h" >&5
61 +$as_echo_n "checking for working alloca.h... " >&6; }
62 +if test "${ac_cv_working_alloca_h+set}" = set; then :
63 +  $as_echo_n "(cached) " >&6
64 +else
65 +  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
66 +/* end confdefs.h.  */
67 +#include <alloca.h>
68 +int
69 +main ()
70 +{
71 +char *p = (char *) alloca (2 * sizeof (int));
72 +                         if (p) return 0;
73 +  ;
74 +  return 0;
75 +}
76 +_ACEOF
77 +if ac_fn_c_try_link "$LINENO"; then :
78 +  ac_cv_working_alloca_h=yes
79 +else
80 +  ac_cv_working_alloca_h=no
81 +fi
82 +rm -f core conftest.err conftest.$ac_objext \
83 +    conftest$ac_exeext conftest.$ac_ext
84 +fi
85 +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_working_alloca_h" >&5
86 +$as_echo "$ac_cv_working_alloca_h" >&6; }
87 +if test $ac_cv_working_alloca_h = yes; then
88 +
89 +$as_echo "#define HAVE_ALLOCA_H 1" >>confdefs.h
90 +
91 +fi
92 +
93 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for alloca" >&5
94 +$as_echo_n "checking for alloca... " >&6; }
95 +if test "${ac_cv_func_alloca_works+set}" = set; then :
96 +  $as_echo_n "(cached) " >&6
97 +else
98 +  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
99 +/* end confdefs.h.  */
100 +#ifdef __GNUC__
101 +# define alloca __builtin_alloca
102 +#else
103 +# ifdef _MSC_VER
104 +#  include <malloc.h>
105 +#  define alloca _alloca
106 +# else
107 +#  ifdef HAVE_ALLOCA_H
108 +#   include <alloca.h>
109 +#  else
110 +#   ifdef _AIX
111 + #pragma alloca
112 +#   else
113 +#    ifndef alloca /* predefined by HP cc +Olibcalls */
114 +char *alloca ();
115 +#    endif
116 +#   endif
117 +#  endif
118 +# endif
119 +#endif
120 +
121 +int
122 +main ()
123 +{
124 +char *p = (char *) alloca (1);
125 +                                   if (p) return 0;
126 +  ;
127 +  return 0;
128 +}
129 +_ACEOF
130 +if ac_fn_c_try_link "$LINENO"; then :
131 +  ac_cv_func_alloca_works=yes
132 +else
133 +  ac_cv_func_alloca_works=no
134 +fi
135 +rm -f core conftest.err conftest.$ac_objext \
136 +    conftest$ac_exeext conftest.$ac_ext
137 +fi
138 +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_alloca_works" >&5
139 +$as_echo "$ac_cv_func_alloca_works" >&6; }
140 +
141 +if test $ac_cv_func_alloca_works = yes; then
142 +
143 +$as_echo "#define HAVE_ALLOCA 1" >>confdefs.h
144 +
145 +else
146 +  # The SVR3 libPW and SVR4 libucb both contain incompatible functions
147 +# that cause trouble.  Some versions do not even contain alloca or
148 +# contain a buggy version.  If you still want to use their alloca,
149 +# use ar to extract alloca.o from them instead of compiling alloca.c.
150 +
151 +ALLOCA=\${LIBOBJDIR}alloca.$ac_objext
152 +
153 +$as_echo "#define C_ALLOCA 1" >>confdefs.h
154 +
155 +
156 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether \`alloca.c' needs Cray hooks" >&5
157 +$as_echo_n "checking whether \`alloca.c' needs Cray hooks... " >&6; }
158 +if test "${ac_cv_os_cray+set}" = set; then :
159 +  $as_echo_n "(cached) " >&6
160 +else
161 +  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
162 +/* end confdefs.h.  */
163 +#if defined CRAY && ! defined CRAY2
164 +webecray
165 +#else
166 +wenotbecray
167 +#endif
168 +
169 +_ACEOF
170 +if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
171 +  $EGREP "webecray" >/dev/null 2>&1; then :
172 +  ac_cv_os_cray=yes
173 +else
174 +  ac_cv_os_cray=no
175 +fi
176 +rm -f conftest*
177 +
178 +fi
179 +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_os_cray" >&5
180 +$as_echo "$ac_cv_os_cray" >&6; }
181 +if test $ac_cv_os_cray = yes; then
182 +  for ac_func in _getb67 GETB67 getb67; do
183 +    as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
184 +ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
185 +eval as_val=\$$as_ac_var
186 +   if test "x$as_val" = x""yes; then :
187 +
188 +cat >>confdefs.h <<_ACEOF
189 +#define CRAY_STACKSEG_END $ac_func
190 +_ACEOF
191 +
192 +    break
193 +fi
194 +
195 +  done
196 +fi
197 +
198 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking stack direction for C alloca" >&5
199 +$as_echo_n "checking stack direction for C alloca... " >&6; }
200 +if test "${ac_cv_c_stack_direction+set}" = set; then :
201 +  $as_echo_n "(cached) " >&6
202 +else
203 +  if test "$cross_compiling" = yes; then :
204 +  ac_cv_c_stack_direction=0
205 +else
206 +  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
207 +/* end confdefs.h.  */
208 +$ac_includes_default
209 +int
210 +find_stack_direction ()
211 +{
212 +  static char *addr = 0;
213 +  auto char dummy;
214 +  if (addr == 0)
215 +    {
216 +      addr = &dummy;
217 +      return find_stack_direction ();
218 +    }
219 +  else
220 +    return (&dummy > addr) ? 1 : -1;
221 +}
222 +
223 +int
224 +main ()
225 +{
226 +  return find_stack_direction () < 0;
227 +}
228 +_ACEOF
229 +if ac_fn_c_try_run "$LINENO"; then :
230 +  ac_cv_c_stack_direction=1
231 +else
232 +  ac_cv_c_stack_direction=-1
233 +fi
234 +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
235 +  conftest.$ac_objext conftest.beam conftest.$ac_ext
236 +fi
237 +
238 +fi
239 +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_stack_direction" >&5
240 +$as_echo "$ac_cv_c_stack_direction" >&6; }
241 +cat >>confdefs.h <<_ACEOF
242 +#define STACK_DIRECTION $ac_cv_c_stack_direction
243 +_ACEOF
244 +
245 +
246 +fi
247 +
248 +
249
250    for ac_header in stdint.h
251  do :
252 @@ -7564,13 +7756,13 @@ if test "${lt_cv_nm_interface+set}" = se
253  else
254    lt_cv_nm_interface="BSD nm"
255    echo "int some_variable = 0;" > conftest.$ac_ext
256 -  (eval echo "\"\$as_me:7567: $ac_compile\"" >&5)
257 +  (eval echo "\"\$as_me:7759: $ac_compile\"" >&5)
258    (eval "$ac_compile" 2>conftest.err)
259    cat conftest.err >&5
260 -  (eval echo "\"\$as_me:7570: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
261 +  (eval echo "\"\$as_me:7762: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
262    (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
263    cat conftest.err >&5
264 -  (eval echo "\"\$as_me:7573: output\"" >&5)
265 +  (eval echo "\"\$as_me:7765: output\"" >&5)
266    cat conftest.out >&5
267    if $GREP 'External.*some_variable' conftest.out > /dev/null; then
268      lt_cv_nm_interface="MS dumpbin"
269 @@ -8772,7 +8964,7 @@ ia64-*-hpux*)
270    ;;
271  *-*-irix6*)
272    # Find out which ABI we are using.
273 -  echo '#line 8775 "configure"' > conftest.$ac_ext
274 +  echo '#line 8967 "configure"' > conftest.$ac_ext
275    if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
276    (eval $ac_compile) 2>&5
277    ac_status=$?
278 @@ -10032,11 +10224,11 @@ else
279     -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
280     -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
281     -e 's:$: $lt_compiler_flag:'`
282 -   (eval echo "\"\$as_me:10035: $lt_compile\"" >&5)
283 +   (eval echo "\"\$as_me:10227: $lt_compile\"" >&5)
284     (eval "$lt_compile" 2>conftest.err)
285     ac_status=$?
286     cat conftest.err >&5
287 -   echo "$as_me:10039: \$? = $ac_status" >&5
288 +   echo "$as_me:10231: \$? = $ac_status" >&5
289     if (exit $ac_status) && test -s "$ac_outfile"; then
290       # The compiler can only warn and ignore the option if not recognized
291       # So say no if there are warnings other than the usual output.
292 @@ -10371,11 +10563,11 @@ else
293     -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
294     -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
295     -e 's:$: $lt_compiler_flag:'`
296 -   (eval echo "\"\$as_me:10374: $lt_compile\"" >&5)
297 +   (eval echo "\"\$as_me:10566: $lt_compile\"" >&5)
298     (eval "$lt_compile" 2>conftest.err)
299     ac_status=$?
300     cat conftest.err >&5
301 -   echo "$as_me:10378: \$? = $ac_status" >&5
302 +   echo "$as_me:10570: \$? = $ac_status" >&5
303     if (exit $ac_status) && test -s "$ac_outfile"; then
304       # The compiler can only warn and ignore the option if not recognized
305       # So say no if there are warnings other than the usual output.
306 @@ -10476,11 +10668,11 @@ else
307     -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
308     -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
309     -e 's:$: $lt_compiler_flag:'`
310 -   (eval echo "\"\$as_me:10479: $lt_compile\"" >&5)
311 +   (eval echo "\"\$as_me:10671: $lt_compile\"" >&5)
312     (eval "$lt_compile" 2>out/conftest.err)
313     ac_status=$?
314     cat out/conftest.err >&5
315 -   echo "$as_me:10483: \$? = $ac_status" >&5
316 +   echo "$as_me:10675: \$? = $ac_status" >&5
317     if (exit $ac_status) && test -s out/conftest2.$ac_objext
318     then
319       # The compiler can only warn and ignore the option if not recognized
320 @@ -10531,11 +10723,11 @@ else
321     -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
322     -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
323     -e 's:$: $lt_compiler_flag:'`
324 -   (eval echo "\"\$as_me:10534: $lt_compile\"" >&5)
325 +   (eval echo "\"\$as_me:10726: $lt_compile\"" >&5)
326     (eval "$lt_compile" 2>out/conftest.err)
327     ac_status=$?
328     cat out/conftest.err >&5
329 -   echo "$as_me:10538: \$? = $ac_status" >&5
330 +   echo "$as_me:10730: \$? = $ac_status" >&5
331     if (exit $ac_status) && test -s out/conftest2.$ac_objext
332     then
333       # The compiler can only warn and ignore the option if not recognized
334 @@ -12915,7 +13107,7 @@ else
335    lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
336    lt_status=$lt_dlunknown
337    cat > conftest.$ac_ext <<_LT_EOF
338 -#line 12918 "configure"
339 +#line 13110 "configure"
340  #include "confdefs.h"
341
342  #if HAVE_DLFCN_H
343 @@ -13011,7 +13203,7 @@ else
344    lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
345    lt_status=$lt_dlunknown
346    cat > conftest.$ac_ext <<_LT_EOF
347 -#line 13014 "configure"
348 +#line 13206 "configure"
349  #include "confdefs.h"
350
351  #if HAVE_DLFCN_H
352 diff -rupN mpfr-3.0.0.orig/gamma.c mpfr-3.0.0/gamma.c
353 --- mpfr-3.0.0.orig/gamma.c     2010-11-17 14:50:50.003037400 -0700
354 +++ mpfr-3.0.0/gamma.c  2010-07-09 18:11:46.000000000 -0600
355 @@ -274,7 +274,7 @@ mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr
356        /* we want an upper bound for x * [log(2-x)-1].
357           since x < 0, we need a lower bound on log(2-x) */
358        mpfr_ui_sub (xp, 2, x, MPFR_RNDD);
359 -      mpfr_log (xp, xp, MPFR_RNDD);
360 +      mpfr_log2 (xp, xp, MPFR_RNDD);
361        mpfr_sub_ui (xp, xp, 1, MPFR_RNDD);
362        mpfr_mul (xp, xp, x, MPFR_RNDU);
363
364 @@ -303,8 +303,8 @@ mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr
365          {
366            mpfr_sub (tmp, tmp, tmp2, MPFR_RNDZ); /* low bnd on |sin(Pi*(2-x))| */
367            mpfr_ui_div (tmp, 12, tmp, MPFR_RNDU); /* upper bound */
368 -          mpfr_log (tmp, tmp, MPFR_RNDU);
369 -          mpfr_add (tmp, tmp, xp, MPFR_RNDU);
370 +          mpfr_log2 (tmp, tmp, MPFR_RNDU);
371 +          mpfr_add (xp, tmp, xp, MPFR_RNDU);
372            underflow = mpfr_cmp_si (xp, expo.saved_emin - 2) <= 0;
373          }
374
375 diff -rupN mpfr-3.0.0.orig/mpfr.h mpfr-3.0.0/mpfr.h
376 --- mpfr-3.0.0.orig/mpfr.h      2010-11-17 14:50:50.066041000 -0700
377 +++ mpfr-3.0.0/mpfr.h   2010-11-09 08:15:07.000000000 -0700
378 @@ -27,7 +27,7 @@ http://www.gnu.org/licenses/ or write to
379  #define MPFR_VERSION_MAJOR 3
380  #define MPFR_VERSION_MINOR 0
381  #define MPFR_VERSION_PATCHLEVEL 0
382 -#define MPFR_VERSION_STRING "3.0.0"
383 +#define MPFR_VERSION_STRING "3.0.0-p8"
384
385  /* Macros dealing with MPFR VERSION */
386  #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
387 @@ -67,6 +67,16 @@ MPFR_VERSION_NUM(MPFR_VERSION_MAJOR,MPFR
388  # define _MPFR_H_HAVE_INTMAX_T 1
389  #endif
390
391 +/* Avoid some problems with macro expansion if the user defines macros
392 +   with the same name as keywords. By convention, identifiers and macro
393 +   names starting with mpfr_ are reserved by MPFR. */
394 +typedef void            mpfr_void;
395 +typedef int             mpfr_int;
396 +typedef unsigned int    mpfr_uint;
397 +typedef long            mpfr_long;
398 +typedef unsigned long   mpfr_ulong;
399 +typedef size_t          mpfr_size_t;
400 +
401  /* Definition of rounding modes (DON'T USE MPFR_RNDNA!).
402     Warning! Changing the contents of this enum should be seen as an
403     interface change since the old and the new types are not compatible
404 @@ -136,7 +146,7 @@ typedef int          mpfr_sign_t;
405  typedef mp_exp_t     mpfr_exp_t;
406
407  /* Definition of the standard exponent limits */
408 -#define MPFR_EMAX_DEFAULT ((mpfr_exp_t) (((unsigned long) 1 << 30) - 1))
409 +#define MPFR_EMAX_DEFAULT ((mpfr_exp_t) (((mpfr_ulong) 1 << 30) - 1))
410  #define MPFR_EMIN_DEFAULT (-(MPFR_EMAX_DEFAULT))
411
412  /* Definition of the main structure */
413 @@ -725,13 +735,13 @@ __MPFR_DECLSPEC int    mpfr_custom_get_k
414     unexpected results with future compilers and aggressive optimisations.
415     Why not working only with signed types, using INT_MIN and LONG_MIN? */
416  #if __GMP_MP_SIZE_T_INT
417 -#define __MPFR_EXP_NAN  ((mpfr_exp_t)((~((~(unsigned int)0)>>1))+2))
418 -#define __MPFR_EXP_ZERO ((mpfr_exp_t)((~((~(unsigned int)0)>>1))+1))
419 -#define __MPFR_EXP_INF  ((mpfr_exp_t)((~((~(unsigned int)0)>>1))+3))
420 +#define __MPFR_EXP_NAN  ((mpfr_exp_t)((~((~(mpfr_uint)0)>>1))+2))
421 +#define __MPFR_EXP_ZERO ((mpfr_exp_t)((~((~(mpfr_uint)0)>>1))+1))
422 +#define __MPFR_EXP_INF  ((mpfr_exp_t)((~((~(mpfr_uint)0)>>1))+3))
423  #else
424 -#define __MPFR_EXP_NAN  ((mpfr_exp_t)((~((~(unsigned long)0)>>1))+2))
425 -#define __MPFR_EXP_ZERO ((mpfr_exp_t)((~((~(unsigned long)0)>>1))+1))
426 -#define __MPFR_EXP_INF  ((mpfr_exp_t)((~((~(unsigned long)0)>>1))+3))
427 +#define __MPFR_EXP_NAN  ((mpfr_exp_t)((~((~(mpfr_ulong)0)>>1))+2))
428 +#define __MPFR_EXP_ZERO ((mpfr_exp_t)((~((~(mpfr_ulong)0)>>1))+1))
429 +#define __MPFR_EXP_INF  ((mpfr_exp_t)((~((~(mpfr_ulong)0)>>1))+3))
430  #endif
431
432  /* Define MPFR_USE_EXTENSION to avoid "gcc -pedantic" warnings. */
433 @@ -760,9 +770,9 @@ __MPFR_DECLSPEC int    mpfr_custom_get_k
434  #define mpfr_inf_p(_x)      ((_x)->_mpfr_exp == __MPFR_EXP_INF)
435  #define mpfr_zero_p(_x)     ((_x)->_mpfr_exp == __MPFR_EXP_ZERO)
436  #define mpfr_regular_p(_x)  ((_x)->_mpfr_exp >  __MPFR_EXP_INF)
437 -#define mpfr_sgn(_x)                                          \
438 -  ((_x)->_mpfr_exp < __MPFR_EXP_INF ?                         \
439 -   (mpfr_nan_p (_x) ? mpfr_set_erangeflag () : (void) 0), 0 : \
440 +#define mpfr_sgn(_x)                                               \
441 +  ((_x)->_mpfr_exp < __MPFR_EXP_INF ?                              \
442 +   (mpfr_nan_p (_x) ? mpfr_set_erangeflag () : (mpfr_void) 0), 0 : \
443     MPFR_SIGN (_x))
444
445  /* Prevent them from using as lvalues */
446 @@ -798,50 +808,72 @@ __MPFR_DECLSPEC int    mpfr_custom_get_k
447       anyway. Checking with other ICC versions is needed. Possibly detect
448       whether warnings are produced or not with a configure test.
449     + Remove C++ too, since it complains too much. */
450 +/* Added casts to improve robustness in case of undefined behavior and
451 +   compiler extensions based on UB (in particular -fwrapv). MPFR doesn't
452 +   use such extensions, but these macros will be used by 3rd-party code,
453 +   where such extensions may be required.
454 +   Moreover casts to unsigned long have been added to avoid warnings in
455 +   programs that use MPFR and are compiled with -Wconversion; such casts
456 +   are OK since if X is a constant expression, then (unsigned long) X is
457 +   also a constant expression, so that the optimizations still work. The
458 +   warnings are probably related to the following two bugs:
459 +     http://gcc.gnu.org/bugzilla/show_bug.cgi?id=4210
460 +     http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38470 (possibly a variant)
461 +   and the casts could be removed once these bugs are fixed.
462 +   Casts shouldn't be used on the generic calls (to the ..._2exp functions),
463 +   where implicit conversions are performed. Indeed, having at least one
464 +   implicit conversion in the macro allows the compiler to emit diagnostics
465 +   when normally expected, for instance in the following call:
466 +     mpfr_set_ui (x, "foo", MPFR_RNDN);
467 +   If this is not possible (for future macros), one of the tricks described
468 +   on http://groups.google.com/group/comp.std.c/msg/e92abd24bf9eaf7b could
469 +   be used. */
470  #if defined (__GNUC__) && !defined(__ICC) && !defined(__cplusplus)
471  #if (__GNUC__ >= 2)
472  #undef mpfr_cmp_ui
473 -/* We use the fact that mpfr_sgn on NaN sets the erange flag and returns 0. */
474 -#define mpfr_cmp_ui(_f,_u)                 \
475 - (__builtin_constant_p (_u) && (_u) == 0 ? \
476 -   mpfr_sgn (_f) :                         \
477 -   mpfr_cmp_ui_2exp ((_f),(_u),0))
478 +/* We use the fact that mpfr_sgn on NaN sets the erange flag and returns 0.
479 +   But warning! mpfr_sgn is specified as a macro in the API, thus the macro
480 +   mustn't be used if side effects are possible, like here. */
481 +#define mpfr_cmp_ui(_f,_u)                                      \
482 +  (__builtin_constant_p (_u) && (mpfr_ulong) (_u) == 0 ?        \
483 +   (mpfr_sgn) (_f) :                                            \
484 +   mpfr_cmp_ui_2exp ((_f), (_u), 0))
485  #undef mpfr_cmp_si
486 -#define mpfr_cmp_si(_f,_s)                 \
487 - (__builtin_constant_p (_s) && (_s) >= 0 ? \
488 -   mpfr_cmp_ui ((_f), (_s)) :              \
489 +#define mpfr_cmp_si(_f,_s)                                      \
490 +  (__builtin_constant_p (_s) && (mpfr_long) (_s) >= 0 ?         \
491 +   mpfr_cmp_ui ((_f), (mpfr_ulong) (mpfr_long) (_s)) :          \
492     mpfr_cmp_si_2exp ((_f), (_s), 0))
493  #if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
494  #undef mpfr_set_ui
495 -#define mpfr_set_ui(_f,_u,_r)              \
496 - (__builtin_constant_p (_u) && (_u) == 0 ? \
497 -   __extension__ ({                        \
498 -     mpfr_ptr _p = (_f);                   \
499 -     _p->_mpfr_sign = 1;                   \
500 -     _p->_mpfr_exp = __MPFR_EXP_ZERO;      \
501 -     (void) (_r); 0; }) :                  \
502 +#define mpfr_set_ui(_f,_u,_r)                                   \
503 +  (__builtin_constant_p (_u) && (mpfr_ulong) (_u) == 0 ?        \
504 +   __extension__ ({                                             \
505 +       mpfr_ptr _p = (_f);                                      \
506 +       _p->_mpfr_sign = 1;                                      \
507 +       _p->_mpfr_exp = __MPFR_EXP_ZERO;                         \
508 +       (mpfr_void) (_r); 0; }) :                                \
509     mpfr_set_ui_2exp ((_f), (_u), 0, (_r)))
510  #endif
511  #undef mpfr_set_si
512 -#define mpfr_set_si(_f,_s,_r)              \
513 - (__builtin_constant_p (_s) && (_s) >= 0 ? \
514 -   mpfr_set_ui ((_f), (_s), (_r)) :        \
515 +#define mpfr_set_si(_f,_s,_r)                                   \
516 +  (__builtin_constant_p (_s) && (mpfr_long) (_s) >= 0 ?         \
517 +   mpfr_set_ui ((_f), (mpfr_ulong) (mpfr_long) (_s), (_r)) :    \
518     mpfr_set_si_2exp ((_f), (_s), 0, (_r)))
519  #endif
520  #endif
521
522  /* Macro version of mpfr_stack interface for fast access */
523 -#define mpfr_custom_get_size(p) ((size_t)                            \
524 +#define mpfr_custom_get_size(p) ((mpfr_size_t)                          \
525         (((p)+GMP_NUMB_BITS-1)/GMP_NUMB_BITS*sizeof (mp_limb_t)))
526  #define mpfr_custom_init(m,p) do {} while (0)
527 -#define mpfr_custom_get_significand(x) ((void*)((x)->_mpfr_d))
528 +#define mpfr_custom_get_significand(x) ((mpfr_void*)((x)->_mpfr_d))
529  #define mpfr_custom_get_exp(x) ((x)->_mpfr_exp)
530  #define mpfr_custom_move(x,m) do { ((x)->_mpfr_d = (mp_limb_t*)(m)); } while (0)
531  #define mpfr_custom_init_set(x,k,e,p,m) do {                   \
532    mpfr_ptr _x = (x);                                           \
533    mpfr_exp_t _e;                                               \
534    mpfr_kind_t _t;                                              \
535 -  int _s, _k;                                                  \
536 +  mpfr_int _s, _k;                                             \
537    _k = (k);                                                    \
538    if (_k >= 0)  {                                              \
539      _t = (mpfr_kind_t) _k;                                     \
540 @@ -858,11 +890,13 @@ __MPFR_DECLSPEC int    mpfr_custom_get_k
541    _x->_mpfr_exp  = _e;                                         \
542    _x->_mpfr_d    = (mp_limb_t*) (m);                           \
543   } while (0)
544 -#define mpfr_custom_get_kind(x)                                              \
545 -  ( (x)->_mpfr_exp >  __MPFR_EXP_INF ? (int)MPFR_REGULAR_KIND*MPFR_SIGN (x) \
546 -  : (x)->_mpfr_exp == __MPFR_EXP_INF ? (int)MPFR_INF_KIND*MPFR_SIGN (x)     \
547 -  : (x)->_mpfr_exp == __MPFR_EXP_NAN ? (int)MPFR_NAN_KIND                   \
548 -  : (int) MPFR_ZERO_KIND * MPFR_SIGN (x) )
549 +#define mpfr_custom_get_kind(x)                                         \
550 +  ( (x)->_mpfr_exp >  __MPFR_EXP_INF ?                                  \
551 +    (mpfr_int) MPFR_REGULAR_KIND * MPFR_SIGN (x)                        \
552 +  : (x)->_mpfr_exp == __MPFR_EXP_INF ?                                  \
553 +    (mpfr_int) MPFR_INF_KIND * MPFR_SIGN (x)                            \
554 +  : (x)->_mpfr_exp == __MPFR_EXP_NAN ? (mpfr_int) MPFR_NAN_KIND         \
555 +  : (mpfr_int) MPFR_ZERO_KIND * MPFR_SIGN (x) )
556
557
558  #endif /* MPFR_USE_NO_MACRO */
559 diff -rupN mpfr-3.0.0.orig/mpfr.texi mpfr-3.0.0/mpfr.texi
560 --- mpfr-3.0.0.orig/mpfr.texi   2010-11-17 14:50:50.069041200 -0700
561 +++ mpfr-3.0.0/mpfr.texi        2010-06-23 05:03:12.000000000 -0600
562 @@ -2050,7 +2050,7 @@ first digit and a trailing exponent in b
563  are printed. If @var{base} is greater than 10, @samp{@@} will be used
564  instead of @samp{e} as exponent delimiter.
565
566 -Return the number of bytes written, or if an error occurred, return 0.
567 +Return the number of characters written, or if an error occurred, return 0.
568  @end deftypefun
569
570  @deftypefun size_t mpfr_inp_str (mpfr_t @var{rop}, FILE *@var{stream}, int @var{base}, mpfr_rnd_t @var{rnd})
571 diff -rupN mpfr-3.0.0.orig/out_str.c mpfr-3.0.0/out_str.c
572 --- mpfr-3.0.0.orig/out_str.c   2010-11-17 14:50:50.080041800 -0700
573 +++ mpfr-3.0.0/out_str.c        2010-06-23 05:03:12.000000000 -0600
574 @@ -22,6 +22,16 @@ http://www.gnu.org/licenses/ or write to
575
576  #include "mpfr-impl.h"
577
578 +/* Warning! S should not contain "%". */
579 +#define OUT_STR_RET(S)                          \
580 +  do                                            \
581 +    {                                           \
582 +      int r;                                    \
583 +      r = fprintf (stream, (S));                \
584 +      return r < 0 ? 0 : r;                     \
585 +    }                                           \
586 +  while (0)
587 +
588  size_t
589  mpfr_out_str (FILE *stream, int base, size_t n_digits, mpfr_srcptr op,
590                mpfr_rnd_t rnd_mode)
591 @@ -29,6 +39,7 @@ mpfr_out_str (FILE *stream, int base, si
592    char *s, *s0;
593    size_t l;
594    mpfr_exp_t e;
595 +  int err;
596
597    MPFR_ASSERTN (base >= 2 && base <= 62);
598
599 @@ -36,37 +47,16 @@ mpfr_out_str (FILE *stream, int base, si
600    if (stream == NULL)
601      stream = stdout;
602
603 -  if (MPFR_IS_NAN(op))
604 -    {
605 -      fprintf (stream, "@NaN@");
606 -      return 3;
607 -    }
608 -
609 -  if (MPFR_IS_INF(op))
610 -    {
611 -      if (MPFR_SIGN(op) > 0)
612 -        {
613 -          fprintf (stream, "@Inf@");
614 -          return 3;
615 -        }
616 -      else
617 -        {
618 -          fprintf (stream, "-@Inf@");
619 -          return 4;
620 -        }
621 -    }
622 -
623 -  if (MPFR_IS_ZERO(op))
624 +  if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (op)))
625      {
626 -      if (MPFR_SIGN(op) > 0)
627 -        {
628 -          fprintf(stream, "0");
629 -          return 1;
630 -        }
631 +      if (MPFR_IS_NAN (op))
632 +        OUT_STR_RET ("@NaN@");
633 +      else if (MPFR_IS_INF (op))
634 +        OUT_STR_RET (MPFR_IS_POS (op) ? "@Inf@" : "-@Inf@");
635        else
636          {
637 -          fprintf(stream, "-0");
638 -          return 2;
639 +          MPFR_ASSERTD (MPFR_IS_ZERO (op));
640 +          OUT_STR_RET (MPFR_IS_POS (op) ? "0" : "-0");
641          }
642      }
643
644 @@ -77,21 +67,31 @@ mpfr_out_str (FILE *stream, int base, si
645
646    l = strlen (s) + 1; /* size of allocated block returned by mpfr_get_str
647                           - may be incorrect, as only an upper bound? */
648 -  if (*s == '-')
649 -    fputc (*s++, stream);
650
651 -  /* outputs mantissa */
652 -  fputc (*s++, stream); e--; /* leading digit */
653 -  fputc ((unsigned char) MPFR_DECIMAL_POINT, stream);
654 -  fputs (s, stream);         /* rest of mantissa */
655 +  /* outputs possible sign and significand */
656 +  err = (*s == '-' && fputc (*s++, stream) == EOF)
657 +    || fputc (*s++, stream) == EOF  /* leading digit */
658 +    || fputc ((unsigned char) MPFR_DECIMAL_POINT, stream) == EOF
659 +    || fputs (s, stream) == EOF;     /* trailing significand */
660    (*__gmp_free_func) (s0, l);
661 +  if (MPFR_UNLIKELY (err))
662 +    return 0;
663 +
664 +  e--;  /* due to the leading digit */
665
666    /* outputs exponent */
667    if (e)
668      {
669 +      int r;
670 +
671        MPFR_ASSERTN(e >= LONG_MIN);
672        MPFR_ASSERTN(e <= LONG_MAX);
673 -      l += fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), (long) e);
674 +
675 +      r = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), (long) e);
676 +      if (MPFR_UNLIKELY (r < 0))
677 +        return 0;
678 +
679 +      l += r;
680      }
681
682    return l;
683 diff -rupN mpfr-3.0.0.orig/set_ld.c mpfr-3.0.0/set_ld.c
684 --- mpfr-3.0.0.orig/set_ld.c    2010-11-17 14:50:50.224050100 -0700
685 +++ mpfr-3.0.0/set_ld.c 2010-10-21 15:18:26.000000000 -0600
686 @@ -102,21 +102,25 @@ mpfr_set_ld (mpfr_ptr r, long double d,
687              {
688                x /= div13; /* exact */
689                shift_exp += 8192;
690 +              mpfr_div_2si (t, t, 8192, MPFR_RNDZ);
691              }
692            if (ABS (x) >= div12)
693              {
694                x /= div12; /* exact */
695                shift_exp += 4096;
696 +              mpfr_div_2si (t, t, 4096, MPFR_RNDZ);
697              }
698            if (ABS (x) >= div11)
699              {
700                x /= div11; /* exact */
701                shift_exp += 2048;
702 +              mpfr_div_2si (t, t, 2048, MPFR_RNDZ);
703              }
704            if (ABS (x) >= div10)
705              {
706                x /= div10; /* exact */
707                shift_exp += 1024;
708 +              mpfr_div_2si (t, t, 1024, MPFR_RNDZ);
709              }
710            /* warning: we may have DBL_MAX=2^1024*(1-2^(-53)) < x < 2^1024,
711               therefore we have one extra exponent reduction step */
712 @@ -124,9 +128,10 @@ mpfr_set_ld (mpfr_ptr r, long double d,
713              {
714                x /= div9; /* exact */
715                shift_exp += 512;
716 +              mpfr_div_2si (t, t, 512, MPFR_RNDZ);
717              }
718          } /* Check overflow of double */
719 -      else
720 +      else /* no overflow on double */
721          {
722            long double div9, div10, div11;
723
724 @@ -149,29 +154,34 @@ mpfr_set_ld (mpfr_ptr r, long double d,
725                  {
726                    x /= div13; /* exact */
727                    shift_exp -= 8192;
728 +                  mpfr_mul_2si (t, t, 8192, MPFR_RNDZ);
729                  }
730                if (ABS (x) <= div12)
731                  {
732                    x /= div12; /* exact */
733                    shift_exp -= 4096;
734 +                  mpfr_mul_2si (t, t, 4096, MPFR_RNDZ);
735                  }
736                if (ABS (x) <= div11)
737                  {
738                    x /= div11; /* exact */
739                    shift_exp -= 2048;
740 +                  mpfr_mul_2si (t, t, 2048, MPFR_RNDZ);
741                  }
742                if (ABS (x) <= div10)
743                  {
744                    x /= div10; /* exact */
745                    shift_exp -= 1024;
746 +                  mpfr_mul_2si (t, t, 1024, MPFR_RNDZ);
747                  }
748                if (ABS(x) <= div9)
749                  {
750                    x /= div9;  /* exact */
751                    shift_exp -= 512;
752 +                  mpfr_mul_2si (t, t, 512, MPFR_RNDZ);
753                  }
754              }
755 -          else
756 +          else /* no underflow */
757              {
758                inexact = mpfr_set_d (u, (double) x, MPFR_RNDZ);
759                MPFR_ASSERTD (inexact == 0);
760 diff -rupN mpfr-3.0.0.orig/sub1.c mpfr-3.0.0/sub1.c
761 --- mpfr-3.0.0.orig/sub1.c      2010-11-17 14:50:50.254051800 -0700
762 +++ mpfr-3.0.0/sub1.c   2010-10-21 14:59:32.000000000 -0600
763 @@ -37,7 +37,9 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
764    mp_size_t cancel2, an, bn, cn, cn0;
765    mp_limb_t *ap, *bp, *cp;
766    mp_limb_t carry, bb, cc, borrow = 0;
767 -  int inexact, shift_b, shift_c, is_exact = 1, down = 0, add_exp = 0;
768 +  int inexact, shift_b, shift_c, add_exp = 0;
769 +  int cmp_low = 0; /* used for rounding to nearest: 0 if low(b) = low(c),
770 +                      negative if low(b) < low(c), positive if low(b)>low(c) */
771    int sh, k;
772    MPFR_TMP_DECL(marker);
773
774 @@ -196,7 +198,8 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
775      }
776
777  #ifdef DEBUG
778 -  printf ("shift_b=%d shift_c=%d diffexp=%lu\n", shift_b, shift_c,
779 +  printf ("rnd=%s shift_b=%d shift_c=%d diffexp=%lu\n",
780 +          mpfr_print_rnd_mode (rnd_mode), shift_b, shift_c,
781            (unsigned long) diff_exp);
782  #endif
783
784 @@ -307,17 +310,18 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
785      {
786        if (MPFR_LIKELY(sh))
787          {
788 -          is_exact = (carry == 0);
789            /* can decide except when carry = 2^(sh-1) [middle]
790               or carry = 0 [truncate, but cannot decide inexact flag] */
791 -          down = (carry < (MPFR_LIMB_ONE << (sh - 1)));
792            if (carry > (MPFR_LIMB_ONE << (sh - 1)))
793              goto add_one_ulp;
794 -          else if ((0 < carry) && down)
795 +          else if ((0 < carry) && (carry < (MPFR_LIMB_ONE << (sh - 1))))
796              {
797                inexact = -1; /* result if smaller than exact value */
798                goto truncate;
799              }
800 +          /* now carry = 2^(sh-1), in which case cmp_low=2,
801 +             or carry = 0, in which case cmp_low=0 */
802 +          cmp_low = (carry == 0) ? 0 : 2;
803          }
804      }
805    else /* directed rounding: set rnd_mode to RNDZ iff toward zero */
806 @@ -344,12 +348,32 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
807    cn -= (long int) an + cancel2;
808
809  #ifdef DEBUG
810 -  printf ("last %d bits from a are %lu, bn=%ld, cn=%ld\n",
811 +  printf ("last sh=%d bits from a are %lu, bn=%ld, cn=%ld\n",
812            sh, (unsigned long) carry, (long) bn, (long) cn);
813  #endif
814
815 +  /* for rounding to nearest, we couldn't conclude up to here in the following
816 +     cases:
817 +     1. sh = 0, then cmp_low=0: we can either truncate, subtract one ulp
818 +        or add one ulp: -1 ulp < low(b)-low(c) < 1 ulp
819 +     2. sh > 0 but the low sh bits from high(b)-high(c) equal 2^(sh-1):
820 +        -0.5 ulp <= -1/2^sh < low(b)-low(c)-0.5 < 1/2^sh <= 0.5 ulp
821 +        we can't decide the rounding, in that case cmp_low=2:
822 +        either we truncate and flag=-1, or we add one ulp and flag=1
823 +     3. the low sh>0 bits from high(b)-high(c) equal 0: we know we have to
824 +        truncate but we can't decide the ternary value, here cmp_low=0:
825 +        -0.5 ulp <= -1/2^sh < low(b)-low(c) < 1/2^sh <= 0.5 ulp
826 +        we always truncate and inexact can be any of -1,0,1
827 +  */
828 +
829 +  /* note: here cn might exceed cn0, in which case we consider a zero limb */
830    for (k = 0; (bn > 0) || (cn > 0); k = 1)
831      {
832 +      /* if cmp_low < 0, we know low(b) - low(c) < 0
833 +         if cmp_low > 0, we know low(b) - low(c) > 0
834 +            (more precisely if cmp_low = 2, low(b) - low(c) = 0.5 ulp so far)
835 +         if cmp_low = 0, so far low(b) - low(c) = 0 */
836 +
837        /* get next limbs */
838        bb = (bn > 0) ? bp[--bn] : 0;
839        if ((cn > 0) && (cn-- <= cn0))
840 @@ -357,76 +381,115 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
841        else
842          cc = 0;
843
844 -      /* down is set when low(b) < low(c) */
845 -      if (down == 0)
846 -        down = (bb < cc);
847 +      /* cmp_low compares low(b) and low(c) */
848 +      if (cmp_low == 0) /* case 1 or 3 */
849 +        cmp_low = (bb < cc) ? -2+k : (bb > cc) ? 1 : 0;
850 +
851 +      /* Case 1 for k=0 splits into 7 subcases:
852 +         1a: bb > cc + half
853 +         1b: bb = cc + half
854 +         1c: 0 < bb - cc < half
855 +         1d: bb = cc
856 +         1e: -half < bb - cc < 0
857 +         1f: bb - cc = -half
858 +         1g: bb - cc < -half
859 +
860 +         Case 2 splits into 3 subcases:
861 +         2a: bb > cc
862 +         2b: bb = cc
863 +         2c: bb < cc
864 +
865 +         Case 3 splits into 3 subcases:
866 +         3a: bb > cc
867 +         3b: bb = cc
868 +         3c: bb < cc
869 +      */
870
871        /* the case rounding to nearest with sh=0 is special since one couldn't
872           subtract above 1/2 ulp in the trailing limb of the result */
873 -      if ((rnd_mode == MPFR_RNDN) && sh == 0 && k == 0)
874 +      if (rnd_mode == MPFR_RNDN && sh == 0 && k == 0) /* case 1 for k=0 */
875          {
876            mp_limb_t half = MPFR_LIMB_HIGHBIT;
877
878 -          is_exact = (bb == cc);
879 -
880            /* add one ulp if bb > cc + half
881               truncate if cc - half < bb < cc + half
882               sub one ulp if bb < cc - half
883            */
884
885 -          if (down)
886 +          if (cmp_low < 0) /* bb < cc: -1 ulp < low(b) - low(c) < 0,
887 +                              cases 1e, 1f and 1g */
888              {
889                if (cc >= half)
890                  cc -= half;
891 -              else
892 +              else /* since bb < cc < half, bb+half < 2*half */
893                  bb += half;
894 +              /* now we have bb < cc + half:
895 +                 we have to subtract one ulp if bb < cc,
896 +                 and truncate if bb > cc */
897              }
898 -          else /* bb >= cc */
899 +          else if (cmp_low >= 0) /* bb >= cc, cases 1a to 1d */
900              {
901                if (cc < half)
902                  cc += half;
903 -              else
904 +              else /* since bb >= cc >= half, bb - half >= 0 */
905                  bb -= half;
906 +              /* now we have bb > cc - half: we have to add one ulp if bb > cc,
907 +                 and truncate if bb < cc */
908 +              if (cmp_low > 0)
909 +                cmp_low = 2;
910              }
911          }
912
913  #ifdef DEBUG
914 -      printf ("    bb=%lu cc=%lu down=%d is_exact=%d\n",
915 -              (unsigned long) bb, (unsigned long) cc, down, is_exact);
916 +      printf ("k=%u bb=%lu cc=%lu cmp_low=%d\n", k,
917 +              (unsigned long) bb, (unsigned long) cc, cmp_low);
918  #endif
919 -      if (bb < cc)
920 +      if (cmp_low < 0) /* low(b) - low(c) < 0: either truncate or subtract
921 +                          one ulp */
922          {
923            if (rnd_mode == MPFR_RNDZ)
924 -            goto sub_one_ulp;
925 +            goto sub_one_ulp; /* set inexact=-1 */
926            else if (rnd_mode != MPFR_RNDN) /* round away */
927              {
928                inexact = 1;
929                goto truncate;
930              }
931 -          else /* round to nearest: special case here since for sh=k=0
932 -                  bb = bb0 - MPFR_LIMB_HIGHBIT */
933 +          else /* round to nearest */
934              {
935 -              if (is_exact && sh == 0)
936 -                {
937 -                  /* For k=0 we can't decide exactness since it may depend
938 -                     from low order bits.
939 -                     For k=1, the first low limbs matched: low(b)-low(c)<0. */
940 -                  if (k)
941 -                    {
942 -                      inexact = 1;
943 -                      goto truncate;
944 -                    }
945 -                }
946 -              else if (down && sh == 0)
947 -                goto sub_one_ulp;
948 -              else
949 -                {
950 -                  inexact = (is_exact) ? 1 : -1;
951 +              /* If cmp_low < 0 and bb > cc, then -0.5 ulp < low(b)-low(c) < 0,
952 +                 whatever the value of sh.
953 +                 If sh>0, then cmp_low < 0 implies that the initial neglected
954 +                 sh bits were 0 (otherwise cmp_low=2 initially), thus the
955 +                 weight of the new bits is less than 0.5 ulp too.
956 +                 If k > 0 (and sh=0) this means that either the first neglected
957 +                 limbs bb and cc were equal (thus cmp_low was 0 for k=0),
958 +                 or we had bb - cc = -0.5 ulp or 0.5 ulp.
959 +                 The last case is not possible here since we would have
960 +                 cmp_low > 0 which is sticky.
961 +                 In the first case (where we have cmp_low = -1), we truncate,
962 +                 whereas in the 2nd case we have cmp_low = -2 and we subtract
963 +                 one ulp.
964 +              */
965 +              if (bb > cc || sh > 0 || cmp_low == -1)
966 +                {  /* -0.5 ulp < low(b)-low(c) < 0,
967 +                      bb > cc corresponds to cases 1e and 1f1
968 +                      sh > 0 corresponds to cases 3c and 3b3
969 +                      cmp_low = -1 corresponds to case 1d3 (also 3b3) */
970 +                  inexact = 1;
971                    goto truncate;
972                  }
973 +              else if (bb < cc) /* here sh = 0 and low(b)-low(c) < -0.5 ulp,
974 +                                   this corresponds to cases 1g and 1f3 */
975 +                goto sub_one_ulp;
976 +              /* the only case where we can't conclude is sh=0 and bb=cc,
977 +                 i.e., we have low(b) - low(c) = -0.5 ulp (up to now), thus
978 +                 we don't know if we must truncate or subtract one ulp.
979 +                 Note: for sh=0 we can't have low(b) - low(c) = -0.5 ulp up to
980 +                 now, since low(b) - low(c) > 1/2^sh */
981              }
982          }
983 -      else if (bb > cc)
984 +      else if (cmp_low > 0) /* 0 < low(b) - low(c): either truncate or
985 +                               add one ulp */
986          {
987            if (rnd_mode == MPFR_RNDZ)
988              {
989 @@ -437,34 +500,70 @@ mpfr_sub1 (mpfr_ptr a, mpfr_srcptr b, mp
990              goto add_one_ulp;
991            else /* round to nearest */
992              {
993 -              if (is_exact)
994 +              if (bb > cc)
995                  {
996 -                  inexact = -1;
997 -                  goto truncate;
998 +                  /* if sh=0, then bb>cc means that low(b)-low(c) > 0.5 ulp,
999 +                     and similarly when cmp_low=2 */
1000 +                  if (cmp_low == 2) /* cases 1a, 1b1, 2a and 2b1 */
1001 +                    goto add_one_ulp;
1002 +                  /* sh > 0 and cmp_low > 0: this implies that the sh initial
1003 +                     neglected bits were 0, and the remaining low(b)-low(c)>0,
1004 +                     but its weight is less than 0.5 ulp */
1005 +                  else /* 0 < low(b) - low(c) < 0.5 ulp, this corresponds to
1006 +                          cases 3a, 1d1 and 3b1 */
1007 +                    {
1008 +                      inexact = -1;
1009 +                      goto truncate;
1010 +                    }
1011                  }
1012 -              else if (down)
1013 +              else if (bb < cc) /* 0 < low(b) - low(c) < 0.5 ulp, cases 1c,
1014 +                                   1b3, 2b3 and 2c */
1015                  {
1016 -                  inexact = 1;
1017 +                  inexact = -1;
1018                    goto truncate;
1019                  }
1020 -              else
1021 -                goto add_one_ulp;
1022 -            }
1023 -        }
1024 +              /* the only case where we can't conclude is bb=cc, i.e.,
1025 +                 low(b) - low(c) = 0.5 ulp (up to now), thus we don't know
1026 +                 if we must truncate or add one ulp. */
1027 +            }
1028 +        }
1029 +      /* after k=0, we cannot conclude in the following cases, we split them
1030 +         according to the values of bb and cc for k=1:
1031 +         1b. sh=0 and cmp_low = 1 and bb-cc = half [around 0.5 ulp]
1032 +             1b1. bb > cc: add one ulp, inex = 1
1033 +             1b2: bb = cc: cannot conclude
1034 +             1b3: bb < cc: truncate, inex = -1
1035 +         1d. sh=0 and cmp_low = 0 and bb-cc = 0 [around 0]
1036 +             1d1: bb > cc: truncate, inex = -1
1037 +             1d2: bb = cc: cannot conclude
1038 +             1d3: bb < cc: truncate, inex = +1
1039 +         1f. sh=0 and cmp_low = -1 and bb-cc = -half [around -0.5 ulp]
1040 +             1f1: bb > cc: truncate, inex = +1
1041 +             1f2: bb = cc: cannot conclude
1042 +             1f3: bb < cc: sub one ulp, inex = -1
1043 +         2b. sh > 0 and cmp_low = 2 and bb=cc [around 0.5 ulp]
1044 +             2b1. bb > cc: add one ulp, inex = 1
1045 +             2b2: bb = cc: cannot conclude
1046 +             2b3: bb < cc: truncate, inex = -1
1047 +         3b. sh > 0 and cmp_low = 0 [around 0]
1048 +             3b1. bb > cc: truncate, inex = -1
1049 +             3b2: bb = cc: cannot conclude
1050 +             3b3: bb < cc: truncate, inex = +1
1051 +      */
1052      }
1053
1054 -  if ((rnd_mode == MPFR_RNDN) && !is_exact)
1055 +  if ((rnd_mode == MPFR_RNDN) && cmp_low != 0)
1056      {
1057        /* even rounding rule */
1058        if ((ap[0] >> sh) & 1)
1059          {
1060 -          if (down)
1061 +          if (cmp_low < 0)
1062              goto sub_one_ulp;
1063            else
1064              goto add_one_ulp;
1065          }
1066        else
1067 -        inexact = (down) ? 1 : -1;
1068 +        inexact = (cmp_low > 0) ? -1 : 1;
1069      }
1070    else
1071      inexact = 0;
1072 diff -rupN mpfr-3.0.0.orig/tests/Makefile.in mpfr-3.0.0/tests/Makefile.in
1073 --- mpfr-3.0.0.orig/tests/Makefile.in   2010-11-17 14:50:50.267052500 -0700
1074 +++ mpfr-3.0.0/tests/Makefile.in        2010-06-10 05:00:52.000000000 -0600
1075 @@ -960,6 +960,7 @@ am__tty_colors = \
1076  red=; grn=; lgn=; blu=; std=
1077  DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
1078  ACLOCAL = @ACLOCAL@
1079 +ALLOCA = @ALLOCA@
1080  AMTAR = @AMTAR@
1081  AR = @AR@
1082  AS = @AS@
1083 diff -rupN mpfr-3.0.0.orig/tests/tcan_round.c mpfr-3.0.0/tests/tcan_round.c
1084 --- mpfr-3.0.0.orig/tests/tcan_round.c  2010-11-17 14:50:50.414058700 -0700
1085 +++ mpfr-3.0.0/tests/tcan_round.c       2010-10-21 14:28:38.000000000 -0600
1086 @@ -41,7 +41,7 @@ check_round_p (void)
1087        /* avoid mpn_random which leaks memory */
1088        for (i = 0; i < n; i++)
1089          buf[i] = randlimb ();
1090 -      p = (mpfr_prec_t) randlimb() % ((n-1) * GMP_NUMB_BITS) + MPFR_PREC_MIN;
1091 +      p = randlimb() % ((n-1) * GMP_NUMB_BITS) + MPFR_PREC_MIN;
1092        err = p + randlimb () % GMP_NUMB_BITS;
1093        r1 = mpfr_round_p (buf, n, err, p);
1094        r2 = mpfr_can_round_raw (buf, n, MPFR_SIGN_POS, err,
1095 diff -rupN mpfr-3.0.0.orig/tests/tcmp_ui.c mpfr-3.0.0/tests/tcmp_ui.c
1096 --- mpfr-3.0.0.orig/tests/tcmp_ui.c     2010-11-17 14:50:50.421059100 -0700
1097 +++ mpfr-3.0.0/tests/tcmp_ui.c  2010-09-07 02:45:12.000000000 -0600
1098 @@ -88,6 +88,126 @@ check_nan (void)
1099    mpfr_clear (x);
1100  }
1101
1102 +/* Since mpfr_cmp_ui and mpfr_cmp_si are also implemented by a macro
1103 +   with __builtin_constant_p for GCC, check that side effects are
1104 +   handled correctly. */
1105 +static void
1106 +check_macros (void)
1107 +{
1108 +  mpfr_t x;
1109 +  int c;
1110 +
1111 +  mpfr_init2 (x, 32);
1112 +
1113 +  c = 0;
1114 +  mpfr_set_ui (x, 17, MPFR_RNDN);
1115 +  if (mpfr_cmp_ui (x, 17) != 0)
1116 +    {
1117 +      printf ("Error 1 on mpfr_cmp_ui(x,17) in check_macros\n");
1118 +      exit (1);
1119 +    }
1120 +  if (mpfr_cmp_ui (x, (c++, 17)) != 0)
1121 +    {
1122 +      printf ("Error 2 on mpfr_cmp_ui(x,17) in check_macros\n");
1123 +      exit (1);
1124 +    }
1125 +  if (c != 1)
1126 +    {
1127 +      printf ("Error 3 on mpfr_cmp_ui(x,17) in check_macros\n"
1128 +              "(c = %d instead of 1)\n", c);
1129 +      exit (1);
1130 +    }
1131 +  if (mpfr_cmp_si (x, 17) != 0)
1132 +    {
1133 +      printf ("Error 1 on mpfr_cmp_si(x,17) in check_macros\n");
1134 +      exit (1);
1135 +    }
1136 +  if (mpfr_cmp_si (x, (c++, 17)) != 0)
1137 +    {
1138 +      printf ("Error 2 on mpfr_cmp_si(x,17) in check_macros\n");
1139 +      exit (1);
1140 +    }
1141 +  if (c != 2)
1142 +    {
1143 +      printf ("Error 3 on mpfr_cmp_si(x,17) in check_macros\n"
1144 +              "(c = %d instead of 2)\n", c);
1145 +      exit (1);
1146 +    }
1147 +
1148 +  c = 0;
1149 +  mpfr_set_ui (x, 0, MPFR_RNDN);
1150 +  if (mpfr_cmp_ui (x, 0) != 0)
1151 +    {
1152 +      printf ("Error 1 on mpfr_cmp_ui(x,0) in check_macros\n");
1153 +      exit (1);
1154 +    }
1155 +  if (mpfr_cmp_ui (x, (c++, 0)) != 0)
1156 +    {
1157 +      printf ("Error 2 on mpfr_cmp_ui(x,0) in check_macros\n");
1158 +      exit (1);
1159 +    }
1160 +  if (c != 1)
1161 +    {
1162 +      printf ("Error 3 on mpfr_cmp_ui(x,0) in check_macros\n"
1163 +              "(c = %d instead of 1)\n", c);
1164 +      exit (1);
1165 +    }
1166 +  if (mpfr_cmp_si (x, 0) != 0)
1167 +    {
1168 +      printf ("Error 1 on mpfr_cmp_si(x,0) in check_macros\n");
1169 +      exit (1);
1170 +    }
1171 +  if (mpfr_cmp_si (x, (c++, 0)) != 0)
1172 +    {
1173 +      printf ("Error 2 on mpfr_cmp_si(x,0) in check_macros\n");
1174 +      exit (1);
1175 +    }
1176 +  if (c != 2)
1177 +    {
1178 +      printf ("Error 3 on mpfr_cmp_si(x,0) in check_macros\n"
1179 +              "(c = %d instead of 2)\n", c);
1180 +      exit (1);
1181 +    }
1182 +
1183 +  mpfr_clear (x);
1184 +}
1185 +
1186 +/* Bug in r7114 */
1187 +static void
1188 +test_macros (void)
1189 +{
1190 +  mpfr_t x[3];
1191 +  mpfr_ptr p;
1192 +
1193 +  mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
1194 +  mpfr_set_ui (x[0], 0, MPFR_RNDN);
1195 +  p = x[0];
1196 +  if (mpfr_cmp_ui (p++, 0) != 0)
1197 +    {
1198 +      printf ("Error in mpfr_cmp_ui macro: result should be 0.\n");
1199 +      exit (1);
1200 +    }
1201 +  if (p != x[1])
1202 +    {
1203 +      printf ("Error in mpfr_cmp_ui macro: p - x[0] = %d (expecting 1)\n",
1204 +              (int) (p - x[0]));
1205 +      exit (1);
1206 +    }
1207 +  p = x[0];
1208 +  if (mpfr_cmp_si (p++, 0) != 0)
1209 +    {
1210 +      printf ("Error in mpfr_cmp_si macro: result should be 0.\n");
1211 +      exit (1);
1212 +    }
1213 +  if (p != x[1])
1214 +    {
1215 +      printf ("Error in mpfr_cmp_si macro: p - x[0] = %d (expecting 1)\n",
1216 +              (int) (p - x[0]));
1217 +      exit (1);
1218 +    }
1219 +  mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
1220 +}
1221 +
1222  int
1223  main (void)
1224  {
1225 @@ -216,6 +336,8 @@ main (void)
1226    mpfr_clear (x);
1227
1228    check_nan ();
1229 +  check_macros ();
1230 +  test_macros ();
1231
1232    tests_end_mpfr ();
1233    return 0;
1234 diff -rupN mpfr-3.0.0.orig/tests/tfma.c mpfr-3.0.0/tests/tfma.c
1235 --- mpfr-3.0.0.orig/tests/tfma.c        2010-11-17 14:50:50.701075100 -0700
1236 +++ mpfr-3.0.0/tests/tfma.c     2010-10-21 14:59:32.000000000 -0600
1237 @@ -337,6 +337,94 @@ test_underflow2 (void)
1238    mpfr_clears (x, y, z, r, (mpfr_ptr) 0);
1239  }
1240
1241 +static void
1242 +bug20101018 (void)
1243 +{
1244 +  mpfr_t x, y, z, t, u;
1245 +  int i;
1246 +
1247 +  mpfr_init2 (x, 64);
1248 +  mpfr_init2 (y, 64);
1249 +  mpfr_init2 (z, 64);
1250 +  mpfr_init2 (t, 64);
1251 +  mpfr_init2 (u, 64);
1252 +
1253 +  mpfr_set_str (x, "0xf.fffffffffffffffp-14766", 16, MPFR_RNDN);
1254 +  mpfr_set_str (y, "-0xf.fffffffffffffffp+317", 16, MPFR_RNDN);
1255 +  mpfr_set_str (z, "0x8.3ffffffffffe3ffp-14443", 16, MPFR_RNDN);
1256 +  mpfr_set_str (t, "0x8.7ffffffffffc7ffp-14444", 16, MPFR_RNDN);
1257 +  i = mpfr_fma (u, x, y, z, MPFR_RNDN);
1258 +  if (mpfr_cmp (u, t) != 0)
1259 +    {
1260 +      printf ("Wrong result in bug20101018 (a)\n");
1261 +      printf ("Expected ");
1262 +      mpfr_out_str (stdout, 16, 0, t, MPFR_RNDN);
1263 +      printf ("\nGot      ");
1264 +      mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN);
1265 +      printf ("\n");
1266 +      exit (1);
1267 +    }
1268 +  if (i <= 0)
1269 +    {
1270 +      printf ("Wrong ternary value in bug20101018 (a)\n");
1271 +      printf ("Expected > 0\n");
1272 +      printf ("Got      %d\n", i);
1273 +      exit (1);
1274 +    }
1275 +
1276 +  mpfr_set_str (x, "-0xf.fffffffffffffffp-11420", 16, MPFR_RNDN);
1277 +  mpfr_set_str (y, "0xf.fffffffffffffffp+9863", 16, MPFR_RNDN);
1278 +  mpfr_set_str (z, "0x8.fffff80ffffffffp-1551", 16, MPFR_RNDN);
1279 +  mpfr_set_str (t, "0x9.fffff01ffffffffp-1552", 16, MPFR_RNDN);
1280 +  i = mpfr_fma (u, x, y, z, MPFR_RNDN);
1281 +  if (mpfr_cmp (u, t) != 0)
1282 +    {
1283 +      printf ("Wrong result in bug20101018 (b)\n");
1284 +      printf ("Expected ");
1285 +      mpfr_out_str (stdout, 16, 0, t, MPFR_RNDN);
1286 +      printf ("\nGot      ");
1287 +      mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN);
1288 +      printf ("\n");
1289 +      exit (1);
1290 +    }
1291 +  if (i <= 0)
1292 +    {
1293 +      printf ("Wrong ternary value in bug20101018 (b)\n");
1294 +      printf ("Expected > 0\n");
1295 +      printf ("Got      %d\n", i);
1296 +      exit (1);
1297 +    }
1298 +
1299 +  mpfr_set_str (x, "0xf.fffffffffffffffp-2125", 16, MPFR_RNDN);
1300 +  mpfr_set_str (y, "-0xf.fffffffffffffffp-6000", 16, MPFR_RNDN);
1301 +  mpfr_set_str (z, "0x8p-8119", 16, MPFR_RNDN);
1302 +  mpfr_set_str (t, "0x8.000000000000001p-8120", 16, MPFR_RNDN);
1303 +  i = mpfr_fma (u, x, y, z, MPFR_RNDN);
1304 +  if (mpfr_cmp (u, t) != 0)
1305 +    {
1306 +      printf ("Wrong result in bug20101018 (c)\n");
1307 +      printf ("Expected ");
1308 +      mpfr_out_str (stdout, 16, 0, t, MPFR_RNDN);
1309 +      printf ("\nGot      ");
1310 +      mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN);
1311 +      printf ("\n");
1312 +      exit (1);
1313 +    }
1314 +  if (i <= 0)
1315 +    {
1316 +      printf ("Wrong ternary value in bug20101018 (c)\n");
1317 +      printf ("Expected > 0\n");
1318 +      printf ("Got      %d\n", i);
1319 +      exit (1);
1320 +    }
1321 +
1322 +  mpfr_clear (x);
1323 +  mpfr_clear (y);
1324 +  mpfr_clear (z);
1325 +  mpfr_clear (t);
1326 +  mpfr_clear (u);
1327 +}
1328 +
1329  int
1330  main (int argc, char *argv[])
1331  {
1332 @@ -345,6 +433,8 @@ main (int argc, char *argv[])
1333
1334    tests_start_mpfr ();
1335
1336 +  bug20101018 ();
1337 +
1338    mpfr_init (x);
1339    mpfr_init (s);
1340    mpfr_init (y);
1341 diff -rupN mpfr-3.0.0.orig/tests/tgamma.c mpfr-3.0.0/tests/tgamma.c
1342 --- mpfr-3.0.0.orig/tests/tgamma.c      2010-11-17 14:50:50.706075400 -0700
1343 +++ mpfr-3.0.0/tests/tgamma.c   2010-07-09 18:11:46.000000000 -0600
1344 @@ -461,6 +461,20 @@ test20071231 (void)
1345    mpfr_clear (x);
1346  }
1347
1348 +/* bug found by Stathis, only occurs on 32-bit machines */
1349 +static void
1350 +test20100709 (void)
1351 +{
1352 +  mpfr_t x;
1353 +  int inex;
1354 +
1355 +  mpfr_init2 (x, 100);
1356 +  mpfr_set_str (x, "-4.6308260837372266e+07", 10, MPFR_RNDN);
1357 +  inex = mpfr_gamma (x, x, MPFR_RNDN);
1358 +  MPFR_ASSERTN(MPFR_IS_ZERO(x) && MPFR_IS_NEG(x) && inex > 0);
1359 +  mpfr_clear (x);
1360 +}
1361 +
1362  int
1363  main (int argc, char *argv[])
1364  {
1365 @@ -471,6 +485,7 @@ main (int argc, char *argv[])
1366    test_generic (2, 100, 2);
1367    gamma_integer ();
1368    test20071231 ();
1369 +  test20100709 ();
1370
1371    data_check ("data/gamma", mpfr_gamma, "mpfr_gamma");
1372
1373 diff -rupN mpfr-3.0.0.orig/tests/tout_str.c mpfr-3.0.0/tests/tout_str.c
1374 --- mpfr-3.0.0.orig/tests/tout_str.c    2010-11-17 14:50:50.753078100 -0700
1375 +++ mpfr-3.0.0/tests/tout_str.c 2010-06-23 05:03:12.000000000 -0600
1376 @@ -46,22 +46,54 @@ static void
1377  special (void)
1378  {
1379    mpfr_t x;
1380 +  unsigned int n;
1381
1382    mpfr_init (x);
1383
1384    mpfr_set_nan (x);
1385 -  mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1386 +  n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1387 +  if (n != 5)
1388 +    {
1389 +      printf ("Error: mpfr_out_str (file, 10, 0, NaN, MPFR_RNDN) wrote %u "
1390 +              "characters instead of 5.\n", n);
1391 +      exit (1);
1392 +    }
1393
1394    mpfr_set_inf (x, 1);
1395 -  mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1396 +  n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1397 +  if (n != 5)
1398 +    {
1399 +      printf ("Error: mpfr_out_str (file, 10, 0, +Inf, MPFR_RNDN) wrote %u "
1400 +               "characters instead of 5.\n", n);
1401 +      exit (1);
1402 +    }
1403
1404    mpfr_set_inf (x, -1);
1405 -  mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1406 +  n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1407 +  if (n != 6)
1408 +    {
1409 +      printf ("Error: mpfr_out_str (file, 10, 0, -Inf, MPFR_RNDN) wrote %u "
1410 +               "characters instead of 6.\n", n);
1411 +      exit (1);
1412 +    }
1413
1414    mpfr_set_ui (x, 0, MPFR_RNDN);
1415 -  mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1416 +  n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1417 +  if (n != 1)
1418 +    {
1419 +      printf ("Error: mpfr_out_str (file, 10, 0, +0, MPFR_RNDN) wrote %u "
1420 +               "characters instead of 1.\n", n);
1421 +      exit (1);
1422 +    }
1423 +
1424    mpfr_neg (x, x, MPFR_RNDN);
1425 -  mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1426 +  n = mpfr_out_str (fout, 10, 0, x, MPFR_RNDN);
1427 +  if (n != 2)
1428 +    {
1429 +      printf ("Error: mpfr_out_str (file, 10, 0, -0, MPFR_RNDN) wrote %u "
1430 +               "characters instead of 2.\n", n);
1431 +      exit (1);
1432 +    }
1433
1434    mpfr_clear (x);
1435  }
1436 diff -rupN mpfr-3.0.0.orig/tests/tset_ld.c mpfr-3.0.0/tests/tset_ld.c
1437 --- mpfr-3.0.0.orig/tests/tset_ld.c     2010-11-17 14:50:50.773079300 -0700
1438 +++ mpfr-3.0.0/tests/tset_ld.c  2010-10-21 15:18:26.000000000 -0600
1439 @@ -147,12 +147,39 @@ static void
1440  test_fixed_bugs (void)
1441  {
1442    mpfr_t x;
1443 -  long double d;
1444 +  long double l, m;
1445
1446    /* bug found by Steve Kargl (2009-03-14) */
1447    mpfr_init2 (x, 64);
1448    mpfr_set_ui_2exp (x, 1, -16447, MPFR_RNDN);
1449 -  d = mpfr_get_ld (x, MPFR_RNDN);  /* an assertion failed in init2.c:50 */
1450 +  mpfr_get_ld (x, MPFR_RNDN);  /* an assertion failed in init2.c:50 */
1451 +
1452 +  /* bug reported by Jakub Jelinek (2010-10-17)
1453 +     https://gforge.inria.fr/tracker/?func=detail&aid=11300 */
1454 +  mpfr_set_prec (x, MPFR_LDBL_MANT_DIG);
1455 +  /* l = 0x1.23456789abcdef0123456789abcdp-914L; */
1456 +  l = 8.215640181713713164092636634579e-276;
1457 +  mpfr_set_ld (x, l, MPFR_RNDN);
1458 +  m = mpfr_get_ld (x, MPFR_RNDN);
1459 +  if (m != l)
1460 +    {
1461 +      printf ("Error in get_ld o set_ld for l=%Le\n", l);
1462 +      printf ("Got m=%Le instead of l\n", m);
1463 +      exit (1);
1464 +    }
1465 +
1466 +  /* another similar test which failed with extended double precision and the
1467 +     generic code for mpfr_set_ld */
1468 +  /* l = 0x1.23456789abcdef0123456789abcdp-968L; */
1469 +  l = 4.560596445887084662336528403703e-292;
1470 +  mpfr_set_ld (x, l, MPFR_RNDN);
1471 +  m = mpfr_get_ld (x, MPFR_RNDN);
1472 +  if (m != l)
1473 +    {
1474 +      printf ("Error in get_ld o set_ld for l=%Le\n", l);
1475 +      printf ("Got m=%Le instead of l\n", m);
1476 +      exit (1);
1477 +    }
1478
1479    mpfr_clear (x);
1480  }
1481 diff -rupN mpfr-3.0.0.orig/tests/tsub.c mpfr-3.0.0/tests/tsub.c
1482 --- mpfr-3.0.0.orig/tests/tsub.c        2010-11-17 14:50:50.791080300 -0700
1483 +++ mpfr-3.0.0/tests/tsub.c     2010-10-21 14:59:32.000000000 -0600
1484 @@ -201,6 +201,8 @@ check_diverse (void)
1485    if (mpfr_cmp (z, x))
1486      {
1487        printf ("Error in mpfr_sub (2)\n");
1488 +      printf ("Expected "); mpfr_print_binary (x); puts ("");
1489 +      printf ("Got      "); mpfr_print_binary (z); puts ("");
1490        exit (1);
1491      }
1492    mpfr_set_str_binary (x, "1.1110111011110001110111011111111111101000011001011100101100101101");
1493 @@ -478,6 +480,156 @@ check_inexact (void)
1494    mpfr_clear (u);
1495  }
1496
1497 +/* Bug found by Jakub Jelinek
1498 + * http://bugzilla.redhat.com/643657
1499 + * https://gforge.inria.fr/tracker/index.php?func=detail&aid=11301
1500 + * The consequence can be either an assertion failure (i = 2 in the
1501 + * testcase below, in debug mode) or an incorrectly rounded value.
1502 + */
1503 +static void
1504 +bug20101017 (void)
1505 +{
1506 +  mpfr_t a, b, c;
1507 +  int inex;
1508 +  int i;
1509 +
1510 +  mpfr_init2 (a, GMP_NUMB_BITS * 2);
1511 +  mpfr_init2 (b, GMP_NUMB_BITS);
1512 +  mpfr_init2 (c, GMP_NUMB_BITS);
1513 +
1514 +  /* a = 2^(2N) + k.2^(2N-1) + 2^N and b = 1
1515 +     with N = GMP_NUMB_BITS and k = 0 or 1.
1516 +     c = a - b should round to the same value as a. */
1517 +
1518 +  for (i = 2; i <= 3; i++)
1519 +    {
1520 +      mpfr_set_ui_2exp (a, i, GMP_NUMB_BITS - 1, MPFR_RNDN);
1521 +      mpfr_add_ui (a, a, 1, MPFR_RNDN);
1522 +      mpfr_mul_2ui (a, a, GMP_NUMB_BITS, MPFR_RNDN);
1523 +      mpfr_set_ui (b, 1, MPFR_RNDN);
1524 +      inex = mpfr_sub (c, a, b, MPFR_RNDN);
1525 +      mpfr_set (b, a, MPFR_RNDN);
1526 +      if (! mpfr_equal_p (c, b))
1527 +        {
1528 +          printf ("Error in bug20101017 for i = %d.\n", i);
1529 +          printf ("Expected ");
1530 +          mpfr_out_str (stdout, 16, 0, b, MPFR_RNDN);
1531 +          putchar ('\n');
1532 +          printf ("Got      ");
1533 +          mpfr_out_str (stdout, 16, 0, c, MPFR_RNDN);
1534 +          putchar ('\n');
1535 +          exit (1);
1536 +        }
1537 +      if (inex >= 0)
1538 +        {
1539 +          printf ("Error in bug20101017 for i = %d: bad inex value.\n", i);
1540 +          printf ("Expected negative, got %d.\n", inex);
1541 +          exit (1);
1542 +        }
1543 +    }
1544 +
1545 +  mpfr_set_prec (a, 64);
1546 +  mpfr_set_prec (b, 129);
1547 +  mpfr_set_prec (c, 2);
1548 +  mpfr_set_str_binary (b, "0.100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001E65");
1549 +  mpfr_set_str_binary (c, "0.10E1");
1550 +  inex = mpfr_sub (a, b, c, MPFR_RNDN);
1551 +  if (mpfr_cmp_ui_2exp (a, 1, 64) != 0 || inex >= 0)
1552 +    {
1553 +      printf ("Error in mpfr_sub for b-c for b=2^64+1+2^(-64), c=1\n");
1554 +      printf ("Expected result 2^64 with inex < 0\n");
1555 +      printf ("Got "); mpfr_print_binary (a);
1556 +      printf (" with inex=%d\n", inex);
1557 +      exit (1);
1558 +    }
1559 +
1560 +  mpfr_clears (a, b, c, (mpfr_ptr) 0);
1561 +}
1562 +
1563 +/* hard test of rounding */
1564 +static void
1565 +check_rounding (void)
1566 +{
1567 +  mpfr_t a, b, c, res;
1568 +  mpfr_prec_t p;
1569 +  long k, l;
1570 +  int i;
1571 +
1572 +#define MAXKL (2 * GMP_NUMB_BITS)
1573 +  for (p = MPFR_PREC_MIN; p <= GMP_NUMB_BITS; p++)
1574 +    {
1575 +      mpfr_init2 (a, p);
1576 +      mpfr_init2 (res, p);
1577 +      mpfr_init2 (b, p + 1 + MAXKL);
1578 +      mpfr_init2 (c, MPFR_PREC_MIN);
1579 +
1580 +      /* b = 2^p + 1 + 2^(-k), c = 2^(-l) */
1581 +      for (k = 0; k <= MAXKL; k++)
1582 +        for (l = 0; l <= MAXKL; l++)
1583 +          {
1584 +            mpfr_set_ui_2exp (b, 1, p, MPFR_RNDN);
1585 +            mpfr_add_ui (b, b, 1, MPFR_RNDN);
1586 +            mpfr_mul_2ui (b, b, k, MPFR_RNDN);
1587 +            mpfr_add_ui (b, b, 1, MPFR_RNDN);
1588 +            mpfr_div_2ui (b, b, k, MPFR_RNDN);
1589 +            mpfr_set_ui_2exp (c, 1, -l, MPFR_RNDN);
1590 +            i = mpfr_sub (a, b, c, MPFR_RNDN);
1591 +            /* b - c = 2^p + 1 + 2^(-k) - 2^(-l), should be rounded to
1592 +               2^p for l <= k, and 2^p+2 for l < k */
1593 +            if (l <= k)
1594 +              {
1595 +                if (mpfr_cmp_ui_2exp (a, 1, p) != 0)
1596 +                  {
1597 +                    printf ("Wrong result in check_rounding\n");
1598 +                    printf ("p=%lu k=%ld l=%ld\n", p, k, l);
1599 +                    printf ("b="); mpfr_print_binary (b); puts ("");
1600 +                    printf ("c="); mpfr_print_binary (c); puts ("");
1601 +                    printf ("Expected 2^%lu\n", p);
1602 +                    printf ("Got      "); mpfr_print_binary (a); puts ("");
1603 +                    exit (1);
1604 +                  }
1605 +                if (i >= 0)
1606 +                  {
1607 +                    printf ("Wrong ternary value in check_rounding\n");
1608 +                    printf ("p=%lu k=%ld l=%ld\n", p, k, l);
1609 +                    printf ("b="); mpfr_print_binary (b); puts ("");
1610 +                    printf ("c="); mpfr_print_binary (c); puts ("");
1611 +                    printf ("a="); mpfr_print_binary (a); puts ("");
1612 +                    printf ("Expected < 0, got %d\n", i);
1613 +                    exit (1);
1614 +                  }
1615 +              }
1616 +            else /* l < k */
1617 +              {
1618 +                mpfr_set_ui_2exp (res, 1, p, MPFR_RNDN);
1619 +                mpfr_add_ui (res, res, 2, MPFR_RNDN);
1620 +                if (mpfr_cmp (a, res) != 0)
1621 +                  {
1622 +                    printf ("Wrong result in check_rounding\n");
1623 +                    printf ("b="); mpfr_print_binary (b); puts ("");
1624 +                    printf ("c="); mpfr_print_binary (c); puts ("");
1625 +                    printf ("Expected "); mpfr_print_binary (res); puts ("");
1626 +                    printf ("Got      "); mpfr_print_binary (a); puts ("");
1627 +                    exit (1);
1628 +                  }
1629 +                if (i <= 0)
1630 +                  {
1631 +                    printf ("Wrong ternary value in check_rounding\n");
1632 +                    printf ("b="); mpfr_print_binary (b); puts ("");
1633 +                    printf ("c="); mpfr_print_binary (c); puts ("");
1634 +                    printf ("Expected > 0, got %d\n", i);
1635 +                    exit (1);
1636 +                  }
1637 +              }
1638 +          }
1639 +
1640 +      mpfr_clear (a);
1641 +      mpfr_clear (res);
1642 +      mpfr_clear (b);
1643 +      mpfr_clear (c);
1644 +    }
1645 +}
1646 +
1647  #define TEST_FUNCTION test_sub
1648  #define TWO_ARGS
1649  #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), randlimb () % 100, RANDS)
1650 @@ -491,6 +643,8 @@ main (void)
1651
1652    tests_start_mpfr ();
1653
1654 +  bug20101017 ();
1655 +  check_rounding ();
1656    check_diverse ();
1657    check_inexact ();
1658    bug_ddefour ();
1659 diff -rupN mpfr-3.0.0.orig/version.c mpfr-3.0.0/version.c
1660 --- mpfr-3.0.0.orig/version.c   2010-11-17 14:50:50.822082100 -0700
1661 +++ mpfr-3.0.0/version.c        2010-11-09 08:15:07.000000000 -0700
1662 @@ -25,5 +25,5 @@ http://www.gnu.org/licenses/ or write to
1663  const char *
1664  mpfr_get_version (void)
1665  {
1666 -  return "3.0.0";
1667 +  return "3.0.0-p8";
1668  }