------- 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 <stdio.h> inline long long Vgetbytes(double f) { return *reinterpret_cast<const long long *>(&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 4000000000000000 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: movq 8(%rsp), %rsi # ERROR second argument to printf call wrong movq %rbx, 8(%rsp) # ERROR if this line came before prior we would be OK call printf # printf gets rdi and rsi as args. rsi bad. movq 8(%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 call printf # This one will work addq $16, %rsp # Clean up the printf stack xorl %eax, %eax # <result> 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 #, <anonymous> movl $.LC1, %edi #, movq %rbx, %rsi # <anonymous>, <anonymous> xorl %eax, %eax # call printf # movq %rbx, %rsi # <anonymous>, <anonymous> movl $.LC1, %edi #, xorl %eax, %eax # call printf # popq %rbx # xorl %eax, %eax # <result> ret -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28778