Platform: i686 Linux 2.6.11
gcc version:
>)
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);
}
this is a dup of bug 323. The issue is more complex than you think, even though in this case you get the same answer for the optimized case and not for the unoptimized case. Using -ffloat-store will get you a zero for the unoptimized case.
*** This bug has been marked as a duplicate of 323 ***