C optimizer

Earle R. Horton earleh at eleazar.dartmouth.edu
Tue Feb 14 04:06:07 AEST 1989


In article <515 at larry.UUCP> jwp at larry.UUCP (Jeffrey W Percival) writes:
>I have a question about how much optimizing I should worry about when
>writing programs in C.  Suppose I have this code fragment:
>
>	x = (1 + cos(r)) / (cos(r) * sin(r));
>	y = (cos(r) - sin(r)) / (1 + sin(r));
>
>I made this up, but the point is the re-use of the sin() and cos()
>calls.  Now, can I expect the compiler to form only one call to sin and
>cos?

     No.  The compiler has no way of knowing what sin() and cos() do,
besides obviously returning a result.  Either or both might have side
effects, and optimizing out any of the calls prevents the side effects
from happening.

Earle R. Horton. 23 Fletcher Circle, Hanover, NH 03755--Graduate student.
He who puts his hand to the plow and looks back is not fit for the kingdom
of winners.  In any case, 'BACK' doesn't work.



More information about the Comp.lang.c mailing list