--- /dev/null
+diff -Naurd mpfr-3.1.1-a/PATCHES mpfr-3.1.1-b/PATCHES
+--- mpfr-3.1.1-a/PATCHES 2012-08-30 09:28:51.000000000 +0000
++++ mpfr-3.1.1-b/PATCHES 2012-08-30 09:28:51.000000000 +0000
+@@ -0,0 +1 @@
++get_decimal64
+diff -Naurd mpfr-3.1.1-a/VERSION mpfr-3.1.1-b/VERSION
+--- mpfr-3.1.1-a/VERSION 2012-07-03 15:01:13.000000000 +0000
++++ mpfr-3.1.1-b/VERSION 2012-08-30 09:28:51.000000000 +0000
+@@ -1 +1 @@
+-3.1.1
++3.1.1-p1
+diff -Naurd mpfr-3.1.1-a/src/get_d64.c mpfr-3.1.1-b/src/get_d64.c
+--- mpfr-3.1.1-a/src/get_d64.c 2012-07-03 15:01:18.000000000 +0000
++++ mpfr-3.1.1-b/src/get_d64.c 2012-08-30 09:28:51.000000000 +0000
+@@ -32,6 +32,10 @@
+
+ #ifdef MPFR_WANT_DECIMAL_FLOATS
+
++#ifndef DEC64_MAX
++# define DEC64_MAX 9.999999999999999E384dd
++#endif
++
+ #ifdef DPD_FORMAT
+ static int T[1000] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32,
+@@ -142,26 +146,14 @@
+ static _Decimal64
+ get_decimal64_min (int negative)
+ {
+- union ieee_double_extract x;
+-
+- x.s.sig = (negative) ? 1 : 0;
+- x.s.exp = 0;
+- x.s.manh = 0;
+- x.s.manl = 1;
+- return x.d;
++ return negative ? - 1E-398dd : 1E-398dd;
+ }
+
+ /* construct the decimal64 largest finite number with given sign */
+ static _Decimal64
+ get_decimal64_max (int negative)
+ {
+- union ieee_double_extract x;
+-
+- x.s.sig = (negative) ? 1 : 0;
+- x.s.exp = 1919;
+- x.s.manh = 1048575; /* 2^20-1 */
+- x.s.manl = ~0;
+- return x.d;
++ return negative ? - DEC64_MAX : DEC64_MAX;
+ }
+
+ /* one-to-one conversion:
+@@ -334,7 +326,8 @@
+ /* the largest decimal64 number is just below 10^(385) < 2^1279 */
+ else if (MPFR_UNLIKELY (e > 1279)) /* then src >= 2^1279 */
+ {
+- if (MPFR_RNDZ || (rnd_mode == MPFR_RNDU && negative != 0)
++ if (rnd_mode == MPFR_RNDZ
++ || (rnd_mode == MPFR_RNDU && negative != 0)
+ || (rnd_mode == MPFR_RNDD && negative == 0))
+ return get_decimal64_max (negative);
+ else
+@@ -354,6 +347,15 @@
+ which corresponds to s=[0.]1000...000 and e=-397 */
+ if (e < -397)
+ {
++ if (rnd_mode == MPFR_RNDN && e == -398)
++ {
++ /* If 0.5E-398 < |src| < 1E-398 (smallest subnormal),
++ src should round to +/- 1E-398 in MPFR_RNDN. */
++ mpfr_get_str (s, &e, 10, 1, src, MPFR_RNDA);
++ return e == -398 && s[negative] <= '5' ?
++ get_decimal64_zero (negative) :
++ get_decimal64_min (negative);
++ }
+ if (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDN
+ || (rnd_mode == MPFR_RNDD && negative == 0)
+ || (rnd_mode == MPFR_RNDU && negative != 0))
+@@ -379,7 +381,8 @@
+ which corresponds to s=[0.]9999...999 and e=385 */
+ else if (e > 385)
+ {
+- if (MPFR_RNDZ || (rnd_mode == MPFR_RNDU && negative != 0)
++ if (rnd_mode == MPFR_RNDZ
++ || (rnd_mode == MPFR_RNDU && negative != 0)
+ || (rnd_mode == MPFR_RNDD && negative == 0))
+ return get_decimal64_max (negative);
+ else
+diff -Naurd mpfr-3.1.1-a/src/mpfr.h mpfr-3.1.1-b/src/mpfr.h
+--- mpfr-3.1.1-a/src/mpfr.h 2012-07-03 15:01:19.000000000 +0000
++++ mpfr-3.1.1-b/src/mpfr.h 2012-08-30 09:28:51.000000000 +0000
+@@ -27,7 +27,7 @@
+ #define MPFR_VERSION_MAJOR 3
+ #define MPFR_VERSION_MINOR 1
+ #define MPFR_VERSION_PATCHLEVEL 1
+-#define MPFR_VERSION_STRING "3.1.1"
++#define MPFR_VERSION_STRING "3.1.1-p1"
+
+ /* Macros dealing with MPFR VERSION */
+ #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
+diff -Naurd mpfr-3.1.1-a/src/version.c mpfr-3.1.1-b/src/version.c
+--- mpfr-3.1.1-a/src/version.c 2012-07-03 15:01:18.000000000 +0000
++++ mpfr-3.1.1-b/src/version.c 2012-08-30 09:28:51.000000000 +0000
+@@ -25,5 +25,5 @@
+ const char *
+ mpfr_get_version (void)
+ {
+- return "3.1.1";
++ return "3.1.1-p1";
+ }
+diff -Naurd mpfr-3.1.1-a/tests/tget_set_d64.c mpfr-3.1.1-b/tests/tget_set_d64.c
+--- mpfr-3.1.1-a/tests/tget_set_d64.c 2012-07-03 15:01:24.000000000 +0000
++++ mpfr-3.1.1-b/tests/tget_set_d64.c 2012-08-30 09:28:51.000000000 +0000
+@@ -25,6 +25,10 @@
+ #include <stdlib.h> /* for exit */
+ #include "mpfr-test.h"
+
++#ifndef DEC64_MAX
++# define DEC64_MAX 9.999999999999999E384dd
++#endif
++
+ /* #define DEBUG */
+
+ static void
+@@ -149,6 +153,15 @@
+ mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
+ mpfr_set (y, x, MPFR_RNDZ);
+ d = mpfr_get_decimal64 (x, MPFR_RNDU);
++ ASSERT_ALWAYS (d == DEC64_MAX);
++ mpfr_set_ui (x, 0, MPFR_RNDZ);
++ mpfr_set_decimal64 (x, d, MPFR_RNDZ);
++ ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
++
++ mpfr_set_str (x, "-9.999999999999999E384", 10, MPFR_RNDZ);
++ mpfr_set (y, x, MPFR_RNDZ);
++ d = mpfr_get_decimal64 (x, MPFR_RNDA);
++ ASSERT_ALWAYS (d == -DEC64_MAX);
+ mpfr_set_ui (x, 0, MPFR_RNDZ);
+ mpfr_set_decimal64 (x, d, MPFR_RNDZ);
+ ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
+@@ -225,6 +238,83 @@
+ mpfr_clear (x);
+ }
+
++static void
++check_overflow (void)
++{
++ mpfr_t x;
++ int err = 0, neg, rnd;
++
++ mpfr_init2 (x, 96);
++ for (neg = 0; neg < 2; neg++)
++ RND_LOOP (rnd)
++ {
++ _Decimal64 d, e;
++ mpfr_rnd_t r = (mpfr_rnd_t) rnd;
++ int sign = neg ? -1 : 1;
++
++ e = sign * (MPFR_IS_LIKE_RNDZ (r, neg) ? 1 : 2) * DEC64_MAX;
++ /* This tests the binary exponent e > 1279 case of get_d64.c */
++ mpfr_set_si_2exp (x, sign, 9999, MPFR_RNDN);
++ d = mpfr_get_decimal64 (x, r);
++ if (d != e)
++ {
++ printf ("Error 1 in check_overflow for %s, %s\n",
++ neg ? "negative" : "positive",
++ mpfr_print_rnd_mode (r));
++ err = 1;
++ }
++ /* This tests the decimal exponent e > 385 case of get_d64.c */
++ mpfr_set_si_2exp (x, sign * 31, 1274, MPFR_RNDN);
++ d = mpfr_get_decimal64 (x, r);
++ if (d != e)
++ {
++ printf ("Error 2 in check_overflow for %s, %s\n",
++ neg ? "negative" : "positive",
++ mpfr_print_rnd_mode (r));
++ err = 1;
++ }
++ /* This tests the last else (-382 <= e <= 385) of get_d64.c */
++ mpfr_set_decimal64 (x, e, MPFR_RNDA);
++ d = mpfr_get_decimal64 (x, r);
++ if (d != e)
++ {
++ printf ("Error 3 in check_overflow for %s, %s\n",
++ neg ? "negative" : "positive",
++ mpfr_print_rnd_mode (r));
++ err = 1;
++ }
++ }
++ mpfr_clear (x);
++ if (err)
++ exit (1);
++}
++
++static void
++check_tiny (void)
++{
++ mpfr_t x;
++ _Decimal64 d;
++
++ /* If 0.5E-398 < |x| < 1E-398 (smallest subnormal), x should round
++ to +/- 1E-398 in MPFR_RNDN. Note: the midpoint 0.5E-398 between
++ 0 and 1E-398 is not a representable binary number, so that there
++ are no tests for it. */
++ mpfr_init2 (x, 128);
++ mpfr_set_str (x, "1E-398", 10, MPFR_RNDZ);
++ d = mpfr_get_decimal64 (x, MPFR_RNDN);
++ MPFR_ASSERTN (d == 1.0E-398dd);
++ mpfr_neg (x, x, MPFR_RNDN);
++ d = mpfr_get_decimal64 (x, MPFR_RNDN);
++ MPFR_ASSERTN (d == -1.0E-398dd);
++ mpfr_set_str (x, "0.5E-398", 10, MPFR_RNDU);
++ d = mpfr_get_decimal64 (x, MPFR_RNDN);
++ MPFR_ASSERTN (d == 1.0E-398dd);
++ mpfr_neg (x, x, MPFR_RNDN);
++ d = mpfr_get_decimal64 (x, MPFR_RNDN);
++ MPFR_ASSERTN (d == -1.0E-398dd);
++ mpfr_clear (x);
++}
++
+ int
+ main (void)
+ {
+@@ -241,6 +331,8 @@
+ check_inf_nan ();
+ check_random ();
+ check_native ();
++ check_overflow ();
++ check_tiny ();
+
+ tests_end_mpfr ();
+ return 0;
+diff -Naurd mpfr-3.1.1-a/PATCHES mpfr-3.1.1-b/PATCHES
+--- mpfr-3.1.1-a/PATCHES 2012-08-30 09:35:12.000000000 +0000
++++ mpfr-3.1.1-b/PATCHES 2012-08-30 09:35:12.000000000 +0000
+@@ -0,0 +1 @@
++strtofr-ternary-value
+diff -Naurd mpfr-3.1.1-a/VERSION mpfr-3.1.1-b/VERSION
+--- mpfr-3.1.1-a/VERSION 2012-08-30 09:28:51.000000000 +0000
++++ mpfr-3.1.1-b/VERSION 2012-08-30 09:35:12.000000000 +0000
+@@ -1 +1 @@
+-3.1.1-p1
++3.1.1-p2
+diff -Naurd mpfr-3.1.1-a/src/mpfr.h mpfr-3.1.1-b/src/mpfr.h
+--- mpfr-3.1.1-a/src/mpfr.h 2012-08-30 09:28:51.000000000 +0000
++++ mpfr-3.1.1-b/src/mpfr.h 2012-08-30 09:35:12.000000000 +0000
+@@ -27,7 +27,7 @@
+ #define MPFR_VERSION_MAJOR 3
+ #define MPFR_VERSION_MINOR 1
+ #define MPFR_VERSION_PATCHLEVEL 1
+-#define MPFR_VERSION_STRING "3.1.1-p1"
++#define MPFR_VERSION_STRING "3.1.1-p2"
+
+ /* Macros dealing with MPFR VERSION */
+ #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
+diff -Naurd mpfr-3.1.1-a/src/strtofr.c mpfr-3.1.1-b/src/strtofr.c
+--- mpfr-3.1.1-a/src/strtofr.c 2012-07-03 15:01:16.000000000 +0000
++++ mpfr-3.1.1-b/src/strtofr.c 2012-08-30 09:35:12.000000000 +0000
+@@ -667,6 +667,20 @@
+ /* (z, exp_z) = base^(exp_base-pstr_size) */
+ z = result + 2*ysize + 1;
+ err = mpfr_mpn_exp (z, &exp_z, pstr->base, exp_z, ysize);
++ /* Since we want y/z rounded toward zero, we must get an upper
++ bound of z. If err >= 0, the error on z is bounded by 2^err. */
++ if (err >= 0)
++ {
++ mp_limb_t cy;
++ unsigned long h = err / GMP_NUMB_BITS;
++ unsigned long l = err - h * GMP_NUMB_BITS;
++
++ if (h >= ysize) /* not enough precision in z */
++ goto next_loop;
++ cy = mpn_add_1 (z, z, ysize - h, MPFR_LIMB_ONE << l);
++ if (cy != 0) /* the code below requires z on ysize limbs */
++ goto next_loop;
++ }
+ exact = exact && (err == -1);
+ if (err == -2)
+ goto underflow; /* FIXME: Sure? */
+@@ -730,6 +744,7 @@
+ MPFR_RNDN, rnd, MPFR_PREC(x)))
+ break;
+
++ next_loop:
+ /* update the prec for next loop */
+ MPFR_ZIV_NEXT (loop, prec);
+ } /* loop */
+diff -Naurd mpfr-3.1.1-a/src/version.c mpfr-3.1.1-b/src/version.c
+--- mpfr-3.1.1-a/src/version.c 2012-08-30 09:28:51.000000000 +0000
++++ mpfr-3.1.1-b/src/version.c 2012-08-30 09:35:12.000000000 +0000
+@@ -25,5 +25,5 @@
+ const char *
+ mpfr_get_version (void)
+ {
+- return "3.1.1-p1";
++ return "3.1.1-p2";
+ }
+diff -Naurd mpfr-3.1.1-a/tests/tstrtofr.c mpfr-3.1.1-b/tests/tstrtofr.c
+--- mpfr-3.1.1-a/tests/tstrtofr.c 2012-07-03 15:01:24.000000000 +0000
++++ mpfr-3.1.1-b/tests/tstrtofr.c 2012-08-30 09:35:12.000000000 +0000
+@@ -1105,6 +1105,92 @@
+ mpfr_clear (y);
+ }
+
++/* From a bug reported by Joseph S. Myers
++ https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00005.html */
++static void
++bug20120814 (void)
++{
++ mpfr_exp_t emin = -30, e;
++ mpfr_t x, y;
++ int r;
++ char s[64], *p;
++
++ mpfr_init2 (x, 2);
++ mpfr_set_ui_2exp (x, 3, emin - 2, MPFR_RNDN);
++ mpfr_get_str (s + 1, &e, 10, 19, x, MPFR_RNDD);
++ s[0] = s[1];
++ s[1] = '.';
++ for (p = s; *p != 0; p++) ;
++ *p = 'e';
++ sprintf (p + 1, "%d", (int) e - 1);
++
++ mpfr_init2 (y, 4);
++ r = mpfr_strtofr (y, s, NULL, 0, MPFR_RNDN);
++ if (r <= 0 || ! mpfr_equal_p (x, y))
++ {
++ printf ("Error in bug20120814\n");
++ printf ("mpfr_strtofr failed on string \"%s\"\n", s);
++ printf ("Expected inex > 0 and y = 0.1100E%d\n", (int) emin);
++ printf ("Got inex = %-6d and y = ", r);
++ mpfr_dump (y);
++ exit (1);
++ }
++
++ mpfr_clear (x);
++ mpfr_clear (y);
++}
++
++static void
++bug20120829 (void)
++{
++ mpfr_t x1, x2, e;
++ int inex1, inex2, i, r;
++ char s[48] = "1e-1";
++
++ mpfr_init2 (e, 128);
++ mpfr_inits2 (4, x1, x2, (mpfr_ptr) 0);
++
++ inex1 = mpfr_set_si (e, -1, MPFR_RNDN);
++ MPFR_ASSERTN (inex1 == 0);
++
++ for (i = 1; i <= sizeof(s) - 5; i++)
++ {
++ s[3+i] = '0';
++ s[4+i] = 0;
++ inex1 = mpfr_mul_ui (e, e, 10, MPFR_RNDN);
++ MPFR_ASSERTN (inex1 == 0);
++ RND_LOOP(r)
++ {
++ mpfr_rnd_t rnd = (mpfr_rnd_t) r;
++
++ inex1 = mpfr_exp10 (x1, e, rnd);
++ inex1 = SIGN (inex1);
++ inex2 = mpfr_strtofr (x2, s, NULL, 0, rnd);
++ inex2 = SIGN (inex2);
++ /* On 32-bit machines, for i = 7, r8389, r8391 and r8394 do:
++ strtofr.c:...: MPFR assertion failed: cy == 0
++ r8396 is OK.
++ On 64-bit machines, for i = 15,
++ r8389 does: strtofr.c:678: MPFR assertion failed: err < (64 - 0)
++ r8391 does: strtofr.c:680: MPFR assertion failed: h < ysize
++ r8394 and r8396 are OK.
++ */
++ if (! mpfr_equal_p (x1, x2) || inex1 != inex2)
++ {
++ printf ("Error in bug20120829 for i = %d, rnd = %s\n",
++ i, mpfr_print_rnd_mode (rnd));
++ printf ("Expected inex = %d, x = ", inex1);
++ mpfr_dump (x1);
++ printf ("Got inex = %d, x = ", inex2);
++ mpfr_dump (x2);
++ exit (1);
++ }
++ }
++ }
++
++ mpfr_clears (e, x1, x2, (mpfr_ptr) 0);
++}
++
+ int
+ main (int argc, char *argv[])
+ {
+@@ -1117,6 +1203,8 @@
+ check_retval ();
+ bug20081028 ();
+ test20100310 ();
++ bug20120814 ();
++ bug20120829 ();
+
+ tests_end_mpfr ();
+ return 0;