Bug 21720

Summary: GCC incorrectly rounds hex floats
Product: gcc Reporter: Neil Booth <neil>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: minor CC: gcc-bugs, jsm28
Priority: P3 Keywords: diagnostic, wrong-code
Version: 4.1.0   
Target Milestone: 4.1.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 16989    

Description Neil Booth 2005-05-23 15:01:11 UTC
Unlike decimal floats, correctly rounding hex floats is easy and cheap, so we
should do it.  Moreover the standard requires it.

With -Wall GCC complains that the following function is missing a return
statement because it has incorrectly folded the comparison to false.

int foo(void) { if (0x1.0000010000000000000000000000000000000001p0f != 1) return
1; }

Remove one of the long line of zeroes and the precision falls within GCC's
limits and it works.
Comment 1 GCC Commits 2005-07-29 01:24:53 UTC
Subject: Bug 21720

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jsm28@gcc.gnu.org	2005-07-29 01:24:39

Modified files:
	gcc            : ChangeLog real.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg: hex-round-1.c 

Log message:
	PR c/21720
	* real.c (real_from_string): Set last bit if there is a nonzero
	hex digit beyond GCC's internal precision.
	
	testsuite:
	* gcc.dg/hex-round-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9593&r2=2.9594
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/real.c.diff?cvsroot=gcc&r1=1.155&r2=1.156
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5847&r2=1.5848
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/hex-round-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 2 Andrew Pinski 2005-07-29 05:35:37 UTC
Fixed.
Comment 3 Joseph S. Myers 2005-07-29 18:31:17 UTC
It turns out my patch actually fixed a similar problem with the code handling
digits before the ".", and my testcase didn't have enough zeroes to show the
problem on 64-bit hosts.  Testing a patch to fix the problem properly (by making
the same change to the code for digits after the "." and expanding the testcase).
Comment 4 GCC Commits 2005-07-29 21:14:32 UTC
Subject: Bug 21720

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jsm28@gcc.gnu.org	2005-07-29 21:14:22

Modified files:
	gcc            : ChangeLog real.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg: hex-round-1.c 
Added files:
	gcc/testsuite/gcc.dg: hex-round-2.c 

Log message:
	PR c/21720
	* real.c (real_from_string): Also set last bit if there is a
	nonzero hex digit beyond GCC's internal precision after ".".
	
	testsuite:
	* gcc.dg/hex-round-1.c: Test more cases.
	* gcc.dg/hex-round-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9606&r2=2.9607
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/real.c.diff?cvsroot=gcc&r1=1.156&r2=1.157
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5852&r2=1.5853
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/hex-round-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/hex-round-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2

Comment 5 Andrew Pinski 2005-07-29 23:24:24 UTC
Fixed.