[PATCH] Optimize (x+2.0)+3.0 with -ffast-math
Roger Sayle
roger@eyesopen.com
Sun Aug 10 20:08:00 GMT 2003
The properties of floating point arithmetic mean that its unsafe to
take advantage of associative operators. For this reason, GCC doesn't
attempt to optimize (x*2.0)*3.0 or (x+2.0)+3.0 by default.
With -ffast-math, however, we're allowed to apply more aggressive
optimizations that may produce slightly different results under some
exceptional inputs values. Under these conditions, GCC does convert
(x*2.0)*3.0 into x*6.0, but currently won't transform (x+2.0)+3.0
into x+5.0. The patch below tweaks the flag_unsafe_math_optimizations
test to allow us to take advantage of other associative floating point
operators than just MULT_EXPR, such as MIN_EXPR and MAX_EXPR.
For comparison, SGI's Pro64 compilers allow reassociation of both
addition and multiplication with their equivalent of -ffast-math.
The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all languages except treelang, and regression tested
with a top-level "make -k check" with no new failures.
Ok for mainline?
2003-08-10 Roger Sayle <roger@eyesopen.com>
* fold-const.c (fold): Optimize any associative floating point
operator with -funsafe-math-optimizations, not just MULT_EXPR.
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.289
diff -c -3 -p -r1.289 fold-const.c
*** fold-const.c 4 Aug 2003 23:46:33 -0000 1.289
--- fold-const.c 10 Aug 2003 14:04:36 -0000
*************** fold (tree expr)
*** 5803,5815 ****
associate:
/* In most languages, can't associate operations on floats through
parentheses. Rather than remember where the parentheses were, we
! don't associate floats at all. It shouldn't matter much. However,
! associating multiplications is only very slightly inaccurate, so do
! that if -funsafe-math-optimizations is specified. */
if (! wins
! && (! FLOAT_TYPE_P (type)
! || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
{
tree var0, con0, lit0, minus_lit0;
tree var1, con1, lit1, minus_lit1;
--- 5803,5813 ----
associate:
/* In most languages, can't associate operations on floats through
parentheses. Rather than remember where the parentheses were, we
! don't associate floats at all, unless the user has specified
! -funsafe-math-optimizations. */
if (! wins
! && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
{
tree var0, con0, lit0, minus_lit0;
tree var1, con1, lit1, minus_lit1;
Roger
--
Roger Sayle, E-mail: roger@eyesopen.com
OpenEye Scientific Software, WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road, Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507. Fax: (+1) 505-473-0833
More information about the Gcc-patches
mailing list