This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/26000] New: cast from int to float violates C++ standard in non-optimized mode
- From: "pr2345 at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 28 Jan 2006 02:53:19 -0000
- Subject: [Bug c++/26000] New: cast from int to float violates C++ standard in non-optimized mode
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Platform: i686 Linux 2.6.11
gcc version:
> gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--host=i386-redhat-linux
Thread model: posix
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
Summary:
According to the C++ standard section 5.2.9/2, the result of casting an
expression to float is as if a temporary variable of type float were
declared and initialized with that expression, and then used in place
of the expression.
That seems to be violated in the program below when compiled in non debug mode
(gcc test.c) The program a.out prints 1 instead of 0.
In optimized mode (gcc -O test.c) the program prints 0 correctly. Same thing
with -O2.
Same result when renaming the file test.cpp and compiling with g++ and also
when using static_cast instead of C-style casts.
In the program below, the functions foo1() and foo2() should always produce the
same result. But the assembly code for foo2() differs from foo1() by two extra
lines (fstps and flds) when compiled in non-optimized mode.
test.c:
int foo1(int x)
{
return (int)(float)x;
}
int foo2(int x)
{
float f = x;
return (int)f;
}
int main()
{
int i = 0x1000001; /* i == 16777217 */
int u = foo1(i) - foo2(i);
printf("u=%d\n", u);
}
--
Summary: cast from int to float violates C++ standard in non-
optimized mode
Product: gcc
Version: 4.0.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: pr2345 at gmail dot com
GCC host triplet: ?
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26000