This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
problem with alias-safe code
- To: egcs-bugs at cygnus dot com
- Subject: problem with alias-safe code
- From: Ulrich Drepper <drepper at cygnus dot com>
- Date: 27 Jun 1998 10:22:54 -0700
- Reply-To: drepper at cygnus dot com (Ulrich Drepper)
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 `------------------------