Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!gatech!ncsuvx!ece-csc!jnh From: jnh@ece-csc.UUCP (Joseph Nathan Hall) Newsgroups: comp.lang.c Subject: Re: C optimizer Keywords: C Ultrix Message-ID: <3918@ece-csc.UUCP> Date: 15 Feb 89 16:50:47 GMT References: <515@larry.UUCP> <954@philmds.UUCP> Reply-To: jnh@ece-csc.UUCP (Joseph Nathan Hall) Organization: North Carolina State University, Raleigh, NC Lines: 35 In article <954@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes: >In article <515@larry.UUCP> jwp@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? > >Try rand() instead of sin(), cos(). Now, you wouldn't want the compiler >to form only one call to rand(), would you? > Now, you know, this is an interesting point. The functions (call them "pure" or "mathematical") that can be optimized in the fashion of redundant subexpressions have to depend SOLELY on their inputs, and furthermore (this is the point) can't have any "memory" of previous state. I think language support for pure functions would be very useful, personally. No static variables, no references to external variables (other than other pure functions). The function rand() would be OK if it required a seed value as a parameter. -- v v sssss|| joseph hall || 201-1D Hampton Lee Court v v s s || jnh@ece-csc.ncsu.edu (Internet) || Cary, NC 27511 v sss || the opinions expressed herein are not necessarily those of my -----------|| employer, north carolina state university . . . . . . . . . . .