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: 67 2012-04-17 22:40

>>71
My computer is too slow. If I do benchmarks, it will be embarrassing.

but here is gcc's optimized output if anyone familiar with x86 assembly wants to get a feel for it:


power3longlong:
  pushl %ebp
  movl  %esp, %ebp
  subl  $32, %esp
  movl  8(%ebp), %eax
  movl  %eax, -24(%ebp)
  movl  12(%ebp), %eax
  movl  %eax, -20(%ebp)
  movl  16(%ebp), %eax
  movl  %eax, -32(%ebp)
  movl  20(%ebp), %eax
  movl  %eax, -28(%ebp)
  fldl  -24(%ebp)
  fstpl -8(%ebp)
  fld1
  fstpl -16(%ebp)
  jmp .L15
.L17:
  movl  -32(%ebp), %eax
  andl  $1, %eax
  testb %al, %al
  je  .L16
  fldl  -16(%ebp)
  fmull -8(%ebp)
  fstpl -16(%ebp)
.L16:
  fldl  -8(%ebp)
  fmull -8(%ebp)
  fstpl -8(%ebp)
  movl  -32(%ebp), %eax
  movl  -28(%ebp), %edx
  shrdl $1, %edx, %eax
  shrl  %edx
  movl  %eax, -32(%ebp)
  movl  %edx, -28(%ebp)
.L15:
  movl  -32(%ebp), %eax
  movl  -28(%ebp), %edx
  orl %edx, %eax
  testl %eax, %eax
  jne .L17
  fldl  -16(%ebp)
  leave
  ret

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