Patch for bug 21720

Joseph S. Myers joseph@codesourcery.com
Fri Jul 29 00:18:00 GMT 2005


This patch fixes bug 21720 (incorrect rounding of hex floats).

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  OK to
commit?

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2005-07-29  Joseph S. Myers  <joseph@codesourcery.com>

	PR c/21720
	* real.c (real_from_string): Set last bit if there is a nonzero
	hex digit beyond GCC's internal precision.

testsuite:
2005-07-29  Joseph S. Myers  <joseph@codesourcery.com>

	PR c/21720
	* gcc.dg/hex-round-1.c: New test.

diff -rupN GCC.orig/gcc/real.c GCC/gcc/real.c
--- GCC.orig/gcc/real.c	2005-06-25 09:54:03.000000000 +0000
+++ GCC/gcc/real.c	2005-07-28 22:34:43.000000000 +0000
@@ -1789,6 +1789,10 @@ real_from_string (REAL_VALUE_TYPE *r, co
 		|= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
 	      pos -= 4;
 	    }
+	  else if (d)
+	    /* Ensure correct rounding by setting last bit if there is
+	       a subsequent nonzero digit.  */
+	    r->sig[0] |= 1ULL;
 	  exp += 4;
 	  str++;
 	}
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/hex-round-1.c GCC/gcc/testsuite/gcc.dg/hex-round-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/hex-round-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/hex-round-1.c	2005-07-28 22:41:06.000000000 +0000
@@ -0,0 +1,31 @@
+/* Test for hexadecimal float rounding: bug 21720.  */
+/* { dg-do link } */
+/* { dg-options "-O -std=gnu99" } */
+
+#include <float.h>
+
+extern void link_failure (void);
+
+int
+main (void)
+{
+#if FLT_RADIX == 2 && FLT_MANT_DIG == 24
+  if (0x1.000001000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.0000010000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.00000100000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.000001000000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.0000010000000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.00000100000000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.000001000000000000000000000000000000000001p0f == 1)
+    link_failure ();
+  if (0x1.0000010000000000000000000000000000000000001p0f == 1)
+    link_failure ();
+#endif
+  return 0;
+}



More information about the Gcc-patches mailing list