C optimizer
Doug Gwyn
gwyn at smoke.BRL.MIL
Tue Feb 14 05:14:11 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));
>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.
More information about the Comp.lang.c
mailing list