This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch gcc-4.0 should not apply mathematical associative rules for addition or multiplication


In the following code the repeated multiplication is folded into a single operation (multiplication by Infinity),
when compiled for apple-ppc-darwin target.
For different values of "x" this leads to undeserved or absent floating point exceptions, and breaks some of
the elementary math functions in Libm. Occurs at optimization O1 and higher.

/* test */
static const double C = 0x1.0p1023;

double foo(double x)
{
return ( ( (x * C) * C ) * C );
}

This is done in routine: simplify_rhs_and_lookup_avail_expr(...) which is preceded with this comment:

/* If we have z = (x OP C1), see if we earlier had x = y OP C2.
If OP is associative, create and fold (y OP C2) OP C1 which
should result in (y OP C3), use that as the RHS for the
assignment. Add minus to this, as we handle it specially below. */

But in:

Section 5.1.2.3 Program execution of the Programming languages — C standard we read:

13 EXAMPLE 5 Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, eveninthe absence of overflowand underflow. Likewise, implementations cannot generally replace decimal constants in order to rearrange expressions. Inthe following fragment, rearrangements suggested by mathematical rules for real numbers are often not valid (see F.8).
double x, y, z;
/*... */
x=(x*y)*z;
//not equivalent to
x*=y*z;

Any comments from math. experts on this alias is appreciated.

- Thanks, fariborz (fjahanian@apple.com)


Here is the patch. It has been bootstrapped, dejagnu tested on apple-ppc-darwin.

OK for fsf mainline?

- thanks, fariborz (fjahanian@apple.com)

ChangeLog:

2004-10-05 Fariborz Jahanian <fjahanian@apple.com>

* tree-ssa-dom.c (associative_fp_binop): New function.
(simplify_rhs_and_lookup_avail_expr): Disallow associativity
and constant folding of floating point MULT_EXPR/PLUS_EXPR
expressions.

Attachment: fsf-patch-tree-ssa-dom.txt
Description: Text document




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]