Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fast positive integer power function

Name: Anonymous 2012-04-15 6:00

I'm trying to figure out how to compute an integer power of a double efficiently with as little overhead as possible (trying to avoid loops and recursion to minimize the amount of calls and jumps). Calculation speed is the priority.
So far I got this trivial piece of code:

double f(double a,char b){
if(b==2)return a*a;
if(b==3)return a*a*a;
if(b==4)return a*a*a*a;
if(b==5)return a*a*a*a*a;
if(b==6)return a*a*a*a*a*a;
if(b==7)return a*a*a*a*a*a*a;
if(b==8)return a*a*a*a*a*a*a*a;
if(b==9)return a*a*a*a*a*a*a*a*a;
        return a*a*a*a*a*a*a*a*a*a;
}


...which of course assumes the exponent is at most 10. The code can easily be expanded if higher exponents are needed.
I'm thinking this is probably optimal, but I know you /prog/riders have some magic tricks up your sleeve, so hit me with them.

Name: Anonymous 2012-04-15 8:13

>>15
nope, just that there is a compiler C_1 running on platform P_1, and a compiler C_2 running on a platform P_2, such that there exists two non tail call optimized implementations, NTC1 and NTC2, and there exists a reasonably small epsilon, such that:

time(C_1, P_1, >>1) > time(C_2, P_2, NTC1)

and

| 2*time(C_1, P_1, >>1) - time(C_2, P_2, NTC2) | < epsilon

Name: Anonymous 2012-04-15 8:14

wait, fixed:

time(C_1, P_1, >>1) > time(C_1, P_1, NTC1)

and

| 2*time(C_2, P_2, >>1) - time(C_2, P_2, NTC2) | < epsilon

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List