• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Asm: Calculating Exponents using the FPU

Started by Charles Pegge, July 25, 2008, 10:37:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge


Tricky but important code, rendered for PB


#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

' CALCULATING EXPONENTS WITH THE FPU

' http://www.website.masmforum.com/tutorials/fptute/fpuchap11.htm

' Calculating x^y

DIM x AS DOUBLE
DIM y AS DOUBLE
DIM r AS DOUBLE

' test values
x=4 : y=3 ' r=64

! fld qword y       ; y
! fld qword x   ;   ; x
! fyl2x             ; y*log2(x)
! fld  st(0)        ; make a second copy
!                   ; ST(0)=y*log2(x), ST(1)=y*log2(x), ST(2)=zzz
! frndint           ; round it to an integer
!                   ; ST(0)=int[y*log2(x)], ST(1)=y*log2(x), ST(2)=zzz
! fsub st(1),st(0)  ; this will leave only a fractional portion in ST(1)
!                   ; ST(0)=int[y*log2(x)], ST(1)=y*log2(x)-int[y*log2(x)], ST(2)=zzz
! fxch st(1)        ; ST(0)=y*log2(x)-int[y*log2(x)], ST(1)=int[y*log2(x)], ST(2)=zzz
! f2xm1             ; get the fractional power of 2 (minus 1)
!                   ; ST(0)=2^ST(0)-1, ST(1)=int[y*log2(x)], ST(2)=zzz
! fld1              ; ST(0)=1, ST(1)=2^ST(0)-1, ST(2)=int[y*log2(x)], ST(3)=zzz
! faddp st(1),st(0) ; add the 1 to ST(1) and POP ST(0)
!                   ; ST(0)=2ST(0), ST(1)=int[y*log2(x)], ST(2)=zzz
! fscale            ; add the integer in ST(1) to the exponent of ST(0)
!                   ; effectively multiplying the content of ST(0) by 2int
!                   ; and yielding the final result of x^y
!                   ; ST(0)=x^y, ST(1)=int[y*log2(x)], ST(2)=zzz
! fstp st(1)        ; the content of ST(1) has become useless
!                   ; overwrite the content of ST(1) with the result and POP ST(0)
!                   ; ST(0)=x^y, ST(1)=zzz
! fstp qword r      ; r

MSGBOX STR$(r)
   

END FUNCTION