Powerbasic Museum 2020-B

IT-Consultant: Charles Pegge => Discussion => Topic started by: Charles Pegge on July 24, 2008, 12:30:42 AM

Title: Asm: Fine tuning the Floating point processor
Post by: Charles Pegge on July 24, 2008, 12:30:42 AM
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
Title: Re: Asm: Fine tuning the Floating point processor
Post by: Charles Pegge on July 24, 2008, 12:57:02 PM

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


Title: Re: Asm: Fine tuning the Floating point processor
Post by: Petr Schreiber on July 24, 2008, 09:28:24 PM
Thanks Charles,

another really useful thing to know!


Petr