Bug 21360 - wrong result of 'if' statement with comparing of floating point with gcc.
Summary: wrong result of 'if' statement with comparing of floating point with gcc.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-05-03 15:05 UTC by Dinar Temirbulatov
Modified: 2024-01-25 23:41 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.3.5
Known to fail: 3.4.0, 4.0.0, 4.1.0
Last reconfirmed: 2006-01-29 17:52:26


Attachments
testcase (193 bytes, text/plain)
2005-05-03 15:09 UTC, Dinar Temirbulatov
Details
proposed patch (605 bytes, patch)
2005-05-03 15:11 UTC, Dinar Temirbulatov
Details | Diff
propesed patch (492 bytes, patch)
2005-05-04 12:48 UTC, Dinar Temirbulatov
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dinar Temirbulatov 2005-05-03 15:05:29 UTC
Following code produces wrong results:

#include <stdio.h>

float f = -1.0f ;

int main( void )
{

        if ( (unsigned int)f != (unsigned int)-1.0f ) {
                printf( "%-12s %04d:NG...[%u]--->[%u]\n",
                        __FILE__, __LINE__, (unsigned int)-1.0f, (unsigned int)f ) ;
        } else {
                printf( "[%u]--->[%u] :OK\n", (unsigned int)-1.0f, (unsigned
int)f ) ;
        }

        return( 0 ) ;
}

[dinar@dinar tmp]$ /home/dinar/work/gcc-builds/gnu/gcc-i686-pc-linux-gnu/bin/gcc
sample.c -o sample
[dinar@dinar tmp]$ ./sample
sample.c     0010:NG...[0]--->[4294967295]
[dinar@dinar tmp]$ /home/dinar/work/gcc-builds/gnu/gcc-i686-pc-linux-gnu/bin/gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure
--prefix=/home/dinar/work/gcc-builds/gnu/gcc-i686-pc-linux-gnu/
--enable-languages=c,c++
Thread model: posix
gcc version 4.1.0 20050429 (experimental)
Comment 1 Dinar Temirbulatov 2005-05-03 15:09:59 UTC
Created attachment 8806 [details]
testcase
Comment 2 Dinar Temirbulatov 2005-05-03 15:11:07 UTC
Created attachment 8807 [details]
proposed patch
Comment 3 Andrew Pinski 2005-05-03 15:21:13 UTC
Confirmed.
Comment 4 Joseph S. Myers 2005-05-03 16:33:08 UTC
Conversion of out-of-range floating point values to integers yields undefined
behavior in both C and C++.  There is no need for it to be consistent between
compile-time and runtime conversions.

However, the ISO C decimal floating point proposals (DTR 24732 / WG14 N1107)
make it well-defined for binary as well as decimal floating-point types.  So we
do want to support well-defined semantics (namely, those in that proposal) for
both compile-time and runtime conversions, at least under a command-line option
to enable them.  I don't know whether dfp-branch has such support yet. 
Performance measurements would be needed to ascertain whether there is any
benefit to having both versions or whether we might as well just always use the
DTR 24732 semantics for these conversions (and document that we are doing so,
and seek out and eliminate any optimizations in the compiler which depend on
such conversions being undefined - a search for such optimizations being a
necessary part of the decimal floating point work anyway if not already done).
Comment 5 Dinar Temirbulatov 2005-05-04 12:48:42 UTC
Created attachment 8816 [details]
propesed patch
Comment 6 Mark Mitchell 2005-05-10 01:51:53 UTC
To the extent this is a bug, it's a failure to comply to a future standard.  As
such, it's certainly not release-critical, and it's not even really a
regression.  Removed target milestone and regression markers.
Comment 7 Joseph S. Myers 2005-06-29 21:59:22 UTC
It's come to my attention that C99 Annex F.4#1 specifies that the result is an
unspecified value, with the "invalid" exception raised at runtime, rather than
undefined behavior (in the absence of defining __STDC_IEC_559__, it's still
undefined behavior).  We don't currently implement FENV_ACCESS at all so the
exception is irrelevant and I don't think we currently have any optimizations
relying on the conversion being undefined rather than unspecified, but in any
case the decimal fp work will answer the questions of what optimizations we do
here and whether there is any performance cost to defining the results the way
the decimal fp proposals do unconditionally.
Comment 8 Richard Biener 2007-09-23 12:47:06 UTC
6.3.1.4/1 says this is undefined.