more thoughts on caching pow

David G. Hough at validgh dgh
Tue Apr 12 13:12:37 PDT 1994


One way that compilers can help with pow
is to attempt to propagate constants to
determine when, in x**y or pow(x,y), x has a value 0, 1, 2, or 10, in order
to call appropriate special functions for 0**y, 1**y, 2**y, or 10**y.
In some cases SunPro Fortran compilers already
exploit the 2**y and 10**y functions 
in libsunmath.    One possible objection to
such an approach is that, regarded as a function of x, x**y may not be
monotonic when special versions are selected in this way, but this 
argument seems
to be of limited force when the compiler can determine x.    Similar special
cases to be detected include x**{-1, 0, 0.5, 1, 2, 3, 4}.    SunPro
Fortran and probably others
already detect some of these for the case when y is an explicit 
integer constant.

Perhaps more importantly, a library could be created which included both
a normal pow and a caching pow, perhaps with four entries in its cache.
Then the compiler could detect some cases
when x**y is invoked in a loop with invariant
x, even if the invariant x is unknowable at compile time, and call the caching
pow in those cases and the conventional one otherwise.

I forgot to mention previously that steveraeng.sun.com wrote a report
on a somewhat similar theme, "Caching Function Results:
Faster Arithmetic by Avoiding Unnecessary Computation," which
investigates the merits of caching computed values of functions like
the integer multiply
routine used in previous-generation SPARCs, and the SI function of 015.doduc

Another application of caching is the large tables of powers of two and ten
that are part of the optional base conversion in SunOS 4.x and the standard
base conversion in SunOS 5.x.    These were precomputed, and have an enormous
impact on the size of staticly-compiled executables.    A better idea might
have been to keep only the moderate powers in precomputed tables, and cache
larger values when computed as needed.    The main programs that would suffer
from this approach would be those like

main(){
#include <float.h>
printf(" Hello max %Lg min %Lg \n",LDBL_MAX,LDBL_MIN);}




More information about the Numeric-interest mailing list