Setting the FPU control word?

Bill Baxter dnewsgroup at billbaxter.com
Mon Mar 10 19:04:18 PDT 2008


Here's a more cleaned up version.  If you see anything that could be 
improved, let me know.  This is my first asm{}.

module fpctrl;
//import std.c.fenv;
import std.stdio;

enum FPPrecision : short
{
     Single = 0x0000,
     Double = 0x0200,
     Real = 0x0300,
     Mask = 0x0300,
     InvMask = ~Mask
}

version(X86) { version = DO_FPU_CONTROL; }
version(X86_64) { version = DO_FPU_CONTROL; }


FPPrecision setFPControlWord(FPPrecision precision)
{
     FPPrecision oldcw;

     version(DO_FPU_CONTROL) {
         FPPrecision newcw;
         asm
         {
             fstcw oldcw;
             fwait;
             mov AX, oldcw;
             and AX,FPPrecision.InvMask;
             or AX,precision;
             mov newcw,AX;
             fldcw newcw;
         }
         debug
         {
             writefln("oldcw was: 0x%x", oldcw);
             asm
             {
                 fstcw newcw;
             }
             writefln("new is: 0x%x", newcw);
         }

         oldcw &= FPPrecision.Mask;
     }

     return oldcw;
}

void main()
{
     auto orig = setFPControlWord(FPPrecision.Double);

     setFPControlWord(FPPrecision.Single);

     setFPControlWord(orig);
}


More information about the Digitalmars-d-learn mailing list