Asm: Fine tuning the Floating point processor

Started by Charles Pegge, July 24, 2008, 12:30:42 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

The behaviour of the FPU can be modified to round results to the nearest value, or up or down or just truncated. The flags for doing this are located in the 16 bit control register. This is accessible with the instructions FLDCW (load control word) and FSTCW (store control word).

It is also possible to control the precision if necessary. The Extended precision format which is native to the FPU, has 64 bits of precision - this can be reduced if so desired.


This is briefly described in the following article:

http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm#cword

Charles Pegge


This demonstrates rounding control on the FPU in PowerBasic


#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

    DIM f AS DOUBLE
    DIM r AS LONG
    DIM p AS BYTE PTR
    DIM fm AS LONG
    DIM fr AS LONG
   
    ' set test data
   
    f=-33.6
   
    ' save original control word
   
    p=VARPTR(fm)
    ! fstcw [eax]
   
   
    ' set FPU control word for integer rounding
   
    'fr=fm OR &h0000 ' round to nearest                  -34
    'fr=fm OR &h0400 ' round down towards -ininity       -34
    'fr=fm OR &h0800 ' round up   towards + infinity     -33
    fr=fm OR &h0c00 ' truncate towoards zero             -33
   
    ' load control word
   
    p=VARPTR(fr)
    ! fldcw [eax]
   
    ' perform operation
   
    ! fld   qword f
    p=VARPTR(r)
    ! fistp   dword [eax] ' store result
   

    ' restore original control word
   
    p=VARPTR(fm)
    ! fldcw [eax]
 
       
    MSGBOX STR$(r)
   

END FUNCTION



Petr Schreiber

Thanks Charles,

another really useful thing to know!


Petr
AMD Sempron 3400+ | 1GB RAM @ 533MHz | GeForce 6200 / GeForce 9500GT | 32bit Windows XP SP3

psch.thinbasic.com