Optimizing a+b-a-b etc.

Tim Peters uunet!ksr.com!tim
Sun May 10 14:37:49 PDT 1992


>  [jim demmel]
>  ...
>  A typical code sequence is the following, which returns A and B
>  representing the exact sum of C and D, where B is precisely the
>  roundoff committed while computing A.
>
>    A = C+D
>    if (abs(C)<abs(D)) swap C and D
>    B = (A-C)-D

Expect that last line should have been
     B = D-(A-C)
Yes?  Else we compiler writers would have been doing you a favor by
optimizing B to 0 <grin> ...

>  Parentheses must be respected.

The F77 & F90 stds require that parentheses be respected, so if your
compiler doesn't, it's a bug.

The C std requires not only that parens be respected, but also that
evaluation proceed strictly left-to-right even in the absence of parens
(read section 2.1.2.3 & then bounce all over creation trying to flesh out
the implications).  Fortran did this right, while the C folks bounced
from one insane extreme to another ...

the-only-`real'-problem-here-is-getting-std-c-*to*-optimize-ly y'rs
   - tim

Tim Peters   Kendall Square Research Corp
timaksr.com,         ksr!timauunet.uu.net



More information about the Numeric-interest mailing list