Should gcc-4.0 apply mathematical associative rules for addition or multiplication

Gabriel Dos Reis gdr@cs.tamu.edu
Mon Oct 4 23:20:00 GMT 2004


Fariborz Jahanian <fjahanian@apple.com> writes:

| 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.

Multiplication on floating point datatypes is *NOT* associative.
Transformations that are predicated on associativity are not
optimizations in that case.  They may be applied only if
-funsafe-transformations. 

-- Gaby

| 
| - Thanks, fariborz (fjahanian@apple.com)

-- 
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
	 Texas A&M University -- Computer Science Department
	301, Bright Building -- College Station, TX 77843-3112



More information about the Gcc mailing list