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

Fariborz Jahanian fjahanian@apple.com
Mon Oct 4 23:14:00 GMT 2004


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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1734 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20041004/f3525d33/attachment.bin>


More information about the Gcc mailing list