--- Comment #46 from chuck at vertica dot com 2006-11-05 22:17 ---
Folks, can anyone please tell me if this is the same problem as I am seeing
here using gcc 4.0.2 for x86_64:
#include
inline long long Vgetbytes(double f) {
return *reinterpret_cast(&f);
}
int main (int argc, char **argv)
{
double dd = 2.0;
printf ("%llx\n", Vgetbytes(dd));
printf ("%llx\n", Vgetbytes(dd));
}
When compiled with -02 I get:
build0:/home/cbear $ g++4 --version
g++4 (GCC) 4.0.2 20051130 (Red Hat 4.0.2-14.EL4)
build0:/home/cbear $ g++4 -O2 ftp.cpp
build0:/home/cbear $ ./a.out
0
4000
When I look at the disassemby I see the problem:
main:
.LFB14:
pushq %rbx# Save EBX as required
.LCFI0:
movl$.LC1, %edi #, Load format string for printf. edi = arg 0
movabsq $4611686018427387904, %rbx # rbx gets 2.0.
xorl%eax, %eax # irrelevant
subq$16, %rsp # Stack frame for calling printf
.LCFI1:
movq8(%rsp), %rsi # ERROR second argument to printf call wrong
movq%rbx, 8(%rsp) # ERROR if this line came before prior we would
be OK
callprintf # printf gets rdi and rsi as args. rsi bad.
movq8(%rsp), %rsi # Better luck this time, rsi set properly
movl$.LC1, %edi # rdi initialized againg
xorl%eax, %eax # irrelevant
movq%rbx, 8(%rsp) # extraneous, rbx need not be preserved
callprintf # This one will work
addq$16, %rsp # Clean up the printf stack
xorl%eax, %eax #
popq%rbx# restore rbx for caller
ret # done
If I compile with -fno-strict-aliasing I do get valid code and right answers.
However I like the assembly code produced by gcc 3.4.4 much better because it
doesn't bother to allocate stack space, and just uses rbx:
build0:/home/cbear $ g++ --version
g++ (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)
build0:/home/cbear $ g++ -O2 -S -fverbose-asm ftp.cpp
main:
.LFB14:
pushq %rbx#
.LCFI0:
movabsq $4611686018427387904, %rbx #,
movl$.LC1, %edi #,
movq%rbx, %rsi # ,
xorl%eax, %eax #
callprintf #
movq%rbx, %rsi # ,
movl$.LC1, %edi #,
xorl%eax, %eax #
callprintf #
popq%rbx#
xorl%eax, %eax #
ret
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28778