Bug 29991 - double: conversion to bits
Summary: double: conversion to bits
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-11-27 09:00 UTC by Hans Buchmann
Modified: 2006-11-27 09:51 UTC (History)
7 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hans Buchmann 2006-11-27 09:00:08 UTC
Compiling the Code

---------------------------------------
#include <iostream>
#include <iomanip>
typedef long long unsigned Significant;

Significant conv(double v)
{
 return *(Significant*)&v;
}

double v=1.0;
int main(unsigned argc,char* args[])
{
 std::cerr<<std::hex<<conv(v)<<"\n";
}
---------------------------------------

with -O1 gives the correct result:

  0x3ff0000000000000

however compiling the same code with -O2 gives (for example):

  0xb7f7c8d8b7f7fff4

The assembler output:
---------------------------------------
_Z4convd:
.LFB1453:
	pushl	%ebp
.LCFI2:
	movl	%esp, %ebp
.LCFI3:
	subl	$8, %esp        ;dont read the value form stack
.LCFI4:
	movl	-8(%ebp), %eax
	movl	-4(%ebp), %edx
	leave
	ret
---------------------------------------

Output of:
 g++ -v


Target: i686-pc-linux-gnu
Configured with: /var/tmp/portage/gcc-4.1.1/work/gcc-4.1.1/configure 
 --prefix=/usr 
 --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.1.1 
 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/include 
 --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1 
 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1/man 
 --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1/info 
 --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/include/g++-v4 
 --host=i686-pc-linux-gnu 
 --build=i686-pc-linux-gnu 
 --disable-altivec 
 --enable-nls 
 --without-included-gettext 
 --with-system-zlib 
 --disable-checking 
 --disable-werror 
 --disable-libunwind-exceptions 
 --disable-multilib 
 --disable-libmudflap 
 --disable-libssp 
 --disable-libgcj 
 --enable-languages=c,c++,fortran 
 --enable-shared 
 --enable-threads=posix 
 --enable-__cxa_atexit 
 --enable-clocale=gnu
Thread model: posix
gcc version 4.1.1 (Gentoo 4.1.1)

Best regards

Hans Buchmann
Comment 1 Pawel Sikora 2006-11-27 09:07:15 UTC
did you ever read the gcc warnings? `-Wall' tells you what's wrong.
use -fno-strict-aliasing or memcpy() for such conversions.
Comment 2 Andreas Schwab 2006-11-27 09:51:33 UTC

*** This bug has been marked as a duplicate of 21920 ***