problem with alias-safe code
Ulrich Drepper
drepper@cygnus.com
Sat Jun 27 10:26:00 GMT 1998
This is no problem with Mark's alias code, it's a problem with code to
tell the compiler what's going on so that Mark's code can work
correctly.
The following is an implementation of fabs for LE machines using GNU
C. It's not meant to be portable to other compilers.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double
fabs (double x)
{
unsigned int i;
i = (((union { double D; int I[2]; }) {D: x}).I[1]);
(((union { double D; int I[2]; }) {D: x}).I[1]) = i & 0x7fffffff;
return x;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The code gcc generates even with -O1 is this (I added
-momit-leaf-frame-pointer to make it more readable, it doesn't change
the result):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.globl fabs
.type fabs,@function
fabs:
fldl 4(%esp)
ret
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is not exactly what is expected. Somehow gcc assumes that the
second assignment does not change `x' and optimizes all the code away.
I know this code is barely legal but given the gcc extensions of
casting to a union this code should work.
--
---------------. drepper at gnu.org ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com `------------------------
More information about the Gcc-bugs
mailing list