Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!ucsd!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: C optimizer Keywords: C pure function optimization Message-ID: <9648@smoke.BRL.MIL> Date: 13 Feb 89 19:14:11 GMT References: <515@larry.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 28 In article <515@larry.UUCP> jwp@larry.UUCP (Jeffrey W Percival) writes: > x = (1 + cos(r)) / (cos(r) * sin(r)); > y = (cos(r) - sin(r)) / (1 + sin(r)); >Now, can I expect the compiler to form only one call to sin and cos? This is an interesting issue. In traditional C implementations, since there was no guarantee that the functions wouldn't have side effects (after all, you might have provided your own definitions for them), the functions had to be called multiple times to make sure that any side effects were properly performed. In a (proposed) ANSI C hosted environment, these functions are known to be provided by the implementation and to conform to the Standard; therefore a sufficiently clever optimizer could take advantage of the fact that they're known to be so-called "pure" functions to avoid calling them multiple times with the same argument. I don't know of any implementations that perform this particular optimization, but I suspect the supercomputer folks will be doing it. There is no standard mechanism for an application to declare its own functions as "pure"; however, if the compiler has access to all files it may be able to make such a determination itself. Note also that ANSI C implementations can #define cos(x) __intrinsic_cos(x) or something along those lines, to allow the compiler to generate in-line code rather than an actual function call for standard functions. There actually is floating-point hardware with SQRT support; I don't know about COS.