I've just hit into a bug in gcc 4.x, it sucks to have a bug in your toolchain when that's probably the last thing you would ever suspect.
The code goes like this:
#include
struct d {
unsigned int lo, hi;
};
static inline unsigned long long r(struct d x)
{
return *(unsigned long long *)&x;
}
void
g(unsigned long long x)
{
printf("%llx\n", x);
}
int
main(void)
{
struct d c;
c.lo = 0xdeadbeef;
c.hi = 0x2;
g(r(c));
return(0);
}
Compile with gcc -O2 -m32 ...
You'd expect to see 0x2deadbeef on a 32-bit little-endian machine, right? No ... the top 32 bits get garbled.
It all works without -fstrict-aliasing (implicitly set with -O2).
On -Os, you get a big fat zero.
On a 64-bit machine ... you get a zero too.
The key, apparently is the inline. If it's not inlined, then it all works .... sigh.
回應
Should add ... happens under
Should add ... happens under Linux, and on Mac OS X 10.4 (which ships with gcc-4.0 by default, though -fstrict-aliasing has to be explicitly specified in conjunction wtih -O2).