c++/1833: [2003-01-01] inlining sometimes causes incorrect behavior

Nigel Stewart nigels@nigels.com
Thu Apr 3 17:23:00 GMT 2003


http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=1833

Perhaps the bug/problem/issue I've found in gcc is related to this one
already in the database: Problem Report 1833

-------

#include <iostream>
using namespace std;

float foo()
{
	assert(sizeof(unsigned int)==sizeof(float));
	
	float tmp = 0.0;
	*((unsigned int *) &tmp) = (127<<23) | (1<<22);
	
	return tmp;
}

int main(int argc,char *argv[])
{
	cout << "Output should be 1.5: " << foo() << endl;
}

-------

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.2/specs
Configured with: /netrel/src/gcc-3.2-3/configure --enable-languages=c,c++,f77,java --enable-libgcj --enable-threads=posix --with-system-zlib --enable-nls --without-included-gettext --enable-interpreter --disable-sjlj-exceptions --disable-version-specific-runtime-libs --enable-shared --build=i686-pc-linux --host=i686-pc-cygwin --target=i686-pc-cygwin --enable-haifa --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --includedir=/nonexistent/include --libexecdir=/usr/sbin
Thread model: posix
gcc version 3.2 20020927 (prerelease)

$ g++ test.cpp -o test && ./test
Output should be 1.5: 1.5

$ g++ -O2 test.cpp -o test && ./test
Output should be 1.5: 0

--------

The following version of foo does not exhibit the same problem:

float foo()
{
	assert(sizeof(unsigned int)==sizeof(float));

	unsigned int tmp = (127<<23) | (1<<22);
	return *((float *) &tmp);
}

Regards,

Nigel Stewart
nigels@nigels.com



More information about the Gcc-bugs mailing list