C optimizer

mcdonald at uxe.cso.uiuc.edu mcdonald at uxe.cso.uiuc.edu
Wed Feb 15 01:36:00 AEST 1989


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

Doug Gwyn has already posted the answer to part of this: according
to ANSI C, the compiler "can" do it to save time. (In general
we can assume his answers supercede most others, at least for 
ANSI compilers).
 
According to my just-performed test, the Microsoft C 5.1 DOES do it. 

In fact, if the compiler had an 80386/80387 mode 
it probably should just inline them, as there are machine
instructions for sin and cosine, as well as sin AND cos (in one 
instruction) in an 80387 (but NOT 8087, which does have tan). The
book says FSINCOS may not be as accurate as FSIN or FCOS, though.
This answers the query in Gwyn's posting.

Doug McDonald



More information about the Comp.lang.c mailing list