On Fri, Aug 03, 2007 at 06:45:55PM +0100, Dave Korn wrote: > On 03 August 2007 18:35, Nathan Froyd wrote: > > > On Fri, Aug 03, 2007 at 06:24:06PM +0100, Paul Brook wrote: > >> On Friday 03 August 2007, Jonathan S. Shapiro wrote: > >>> Then it seems very curious that the constant folding should fail on this > >>> platform. Any idea what may be going on here? > >> > >> You're exploiting a hole in the C aliasing rules by accessing a 32-bit int > >> as type char. I tested several compilers (4.2, 4.1 and 3.4 x86, 4.2 m68k > >> and 4.2 arm) and the only one that eliminated the comparison was 3.4-x86. > > > > FWIW, rewriting it with the "obvious" union approach seems to give the > > desired results on 4.2 arm with and without -mbig-endian. > > Shouldn't it also work by changing the (char *) cast to (unsigned char *), > since IIUIC the standard guarantees you're allowed to access any object as an > array of unsigned char and not get into aliasing difficulties?
The standard makes the same guarantee about (char *). This is not "exploiting a hole"; it is standard C that you can access anything as (char *).