Author Topic: Asm: Fine tuning the Floating point processor  (Read 7898 times)

0 Members and 1 Guest are viewing this topic.

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 704
    • Charles Pegge
Asm: Fine tuning the Floating point processor
« 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

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 704
    • Charles Pegge
Re: Asm: Fine tuning the Floating point processor
« Reply #1 on: July 24, 2008, 12:57:02 PM »

This demonstrates rounding control on the FPU in PowerBasic

Code: [Select]
#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


Offline Petr Schreiber

  • Sr. Member
  • ****
  • Posts: 254
Re: Asm: Fine tuning the Floating point processor
« Reply #2 on: July 24, 2008, 09:28:24 PM »
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