Path: utzoo!utgpu!watmath!clyde!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: C optimizer Keywords: C Ultrix Message-ID: <11751@haddock.ima.isc.com> Date: 13 Feb 89 20:05:58 GMT References: <515@larry.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 16 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)); 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@haddock.isc.com), The Walking Lint