News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Fibonacci

Started by EduardoS, January 12, 2006, 10:21:42 PM

Previous topic - Next topic

EduardoS

Its not the best way to calculate the n number of a fibonacci sequence, but it is the only method i found to use xadd in its main loop :toothy


fibonacci proc a:DWORD
    mov eax, 1
    mov ecx, [esp+4]
    mov edx, eax
    sub ecx, 2
    jna fim
    @@:
        xadd eax, edx
    dec ecx
    jnz @B
    fim:
    ret 4
fibonacci endp

zooba

I got identical results and timings with this variation:
    mov eax, 1
    mov ecx, TEST_VALUE
    mov ebx, eax
    sub ecx, 1
    @@:
        mov edx, ebx
        mov ebx, eax
        mov eax, edx
        add ebx, edx
       
    dec ecx
    jnz @B


Note that I've inverted the loop to (hopefully) avoid register dependencies. XADD saves a register and doesn't appear to be any less efficient, at least in this application.

XADD was introduced with the 486, so presumably it is still maintained (unlike the string commands). :U

sheep

I don't know if it's just me but both of those ran frighteningly slow so I'll put up my version:



phi TBYTE [(sqrt(5) + 1)/2]
sqrt5 TBYTE sqrt(5)

fib3:
fild DWORD PTR [esp+4]
fld phi
fyl2x
fld st(0)
frndint
fsub st(1),st(0)
fxch st(1)
f2xm1
fld1
fadd
fscale
fstp st(1)
fld1
fld st(1)
fdiv
fsub
fld sqrt5
fdiv
fistp DWORD PTR [esp+4]
mov eax,DWORD PTR [esp+4]
retn 4



which uses Binet's closed-form solution for fibonacci numbers. It runs much faster for me, runs in the same amount of time for any number, and can work for n > 47 if you want to use tbytes instead of doublewords.

EduardoS

My porpose wasnt create a fast algo (i already have a faster one) but find an algo that uses xadd,
never found a usage for it before.

zooba

Quote from: EduardoS on January 14, 2006, 09:38:01 PM
My porpose wasnt create a fast algo (i already have a faster one) but find an algo that uses xadd,
never found a usage for it before.

Yeah, well. You went and posted it in the Laboratory :P :wink