C optimizer

Tim Olson tim at crackle.amd.com
Tue Feb 14 06:10:02 AEST 1989


In article <515 at larry.UUCP> jwp at larry.UUCP (Jeffrey W Percival) writes:
| 
| 	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?  Specifically, I am using ULTRIX 2.1 on a VS2000, but my real
| question is, would *most* compilers optimize this as I'd hope, or
| *some* compilers, or hardly any?

I think that very few compilers currently perform this optimization. 
The problem is that (pre-ANSI) compilers usually have no knowledge about
standard library routines -- they look just like other function calls. 
Therefore, the compiler does not know that sin() or cos() are "true
functions", in that they will return the same values for the same
inputs, and have no side-effects.

ANSI-conforming C compilers will be able to perform this optimization,
since the standard library routine names are reserved, and the compiler
can have specific knowledge about them.

I know of at least one C compiler that is tracking the proposed ANSI
standard that will perform this optimization.

	-- Tim Olson
	Advanced Micro Devices
	(tim at crackle.amd.com)



More information about the Comp.lang.c mailing list