C optimizer

Karl Heuer karl at haddock.ima.isc.com
Tue Feb 14 06:05:58 AEST 1989


In article <515 at larry.UUCP> jwp at larry.UUCP (Jeffrey W Percival) writes:
>	x = (1 + cos(r)) / (cos(r) * sin(r));
>	y = (cos(r) - sin(r)) / (1 + sin(r));

Most compilers will *not* optimize this, because they don't recognize that
sin() and cos() are pure functions.  (Clearly it would be an invalid
optimization to do this in general, because of the existence of impure
functions such as getchar() and rand().)

In this case, the optimization would be legal, if the compiler takes the
trouble to observe that the library functions sin() and cos() are pure.  It
could do the same thing with any user-defined functions that are visible and
whose purity can be proven at compile-time (or, perhaps, whose purity is
hinted at with a #pragma).  I don't know of any compilers that are this smart.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list