optimizatinon bug?

Isao Aburatani aburatan@nippon-control-system.co.jp
Mon Oct 28 00:26:00 GMT 2002


Hello,

I found a gcc bug.

Please compile an attached file and run.

$ LANG=C gcc -v
Reading specs from /usr/lib/gcc-lib/i386-linux/3.2.1/specs
Configured with: /mnt/data/gcc-3.1/gcc-3.2-3.2.1ds3/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.2 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-java-gc=boehm --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.2.1 20021020 (Debian prerelease)

$ uname -a 
Linux dual-linux 2.4.18 #1 SMP Sat Jun 15 16:18:03 JST 2002 i686 unknown unknown GNU/Linux

$ gcc -O2  gccbug.c

$ ./a.out
a.out: gccbug.c:63: main: Assertion `dy == 3.8' failed.
Aborted.

I'm sorry if you have already known it.
-------
Isao Aburatani <aburatan@nippon-control-system.co.jp>

-------------- next part --------------
#include <stdio.h>
#include <assert.h>

#define cast64(CASTBUFF) \
        (((unsigned long long)(*(CASTBUFF))      << 56) \
          + ((unsigned long long)(*(CASTBUFF+1)) << 48) \
          + ((unsigned long long)(*(CASTBUFF+2)) << 40) \
          + ((unsigned long long)(*(CASTBUFF+3)) << 32) \
          + ((unsigned long long)(*(CASTBUFF+4)) << 24) \
          + ((unsigned long long)(*(CASTBUFF+5)) << 16) \
          + ((unsigned long long)(*(CASTBUFF+6)) << 8 ) \
          +  (unsigned long long)(*(CASTBUFF+7)))

#define	store64(BUFF, LONGLONG)	\
do {									\
    (BUFF)[0] = ((unsigned char) ((LONGLONG) >> 56));			\
    (BUFF)[1] = ((unsigned char)(((LONGLONG) >> 48) & 0x00000000000000ff));\
    (BUFF)[2] = ((unsigned char)(((LONGLONG) >> 40) & 0x00000000000000ff));\
    (BUFF)[3] = ((unsigned char)(((LONGLONG) >> 32) & 0x00000000000000ff));\
    (BUFF)[4] = ((unsigned char)(((LONGLONG) >> 24) & 0x00000000000000ff));\
    (BUFF)[5] = ((unsigned char)(((LONGLONG) >> 16) & 0x00000000000000ff));\
    (BUFF)[6] = ((unsigned char)(((LONGLONG) >> 8)  & 0x00000000000000ff));\
    (BUFF)[7] = ((unsigned char) ((LONGLONG)        & 0x00000000000000ff));\
} while(0)
void
encode(
	unsigned char	*buff,
	double		dx,
	double		dy)
{
	unsigned long long tmp;
	tmp = *(unsigned long long *)&dx;
	store64(&buff[0],  tmp);
	tmp = *(unsigned long long *)&dy;
	store64(&buff[8],  tmp);
}
void
decode(
	unsigned char	*buff,
	double		*dx,
	double		*dy)
{
	unsigned long long  tmp;
	tmp = cast64(&buff[0]);
	*dx = *(double *)&tmp;
	tmp = cast64(&buff[8]);
	*dy = *(double *)&tmp;
}
int
main(
	int	argc,
	char	*argv[])
{
	unsigned char	buff[16];
	double		dx, dy;

	dx = 3.5;
	dy = 3.8;

	encode(buff, dx, dy);
	decode(buff, &dx, &dy);
	assert(dx == 3.5);
	assert(dy == 3.8);
	return 0;
}


More information about the Gcc-bugs mailing list