Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uxc!uxc.cso.uiuc.edu!uxg.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald From: mcdonald@uxe.cso.uiuc.edu Newsgroups: comp.lang.c Subject: Re: C optimizer Message-ID: <225800134@uxe.cso.uiuc.edu> Date: 14 Feb 89 15:36:00 GMT References: <515@larry.UUCP> Lines: 26 Nf-ID: #R:larry.UUCP:515:uxe.cso.uiuc.edu:225800134:000:1006 Nf-From: uxe.cso.uiuc.edu!mcdonald Feb 14 09:36:00 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