Setting the FPU control word?

Neil Vice sardonicpresence at gmail.com
Mon Mar 10 21:15:17 PDT 2008


Well short of the method return value being undefined if DO_FPU_CONTROL is 
not set and the fact that storing the new control word in a variable is 
unnecessary with the exception of the debug, output it looks fine. If you 
didn't require the debug output I believe you could simply use AX as the 
operand to fldcw.

Happy to help =) Incidently it was my first use of asm in D also.

Neil


"Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
news:fr4pb3$1f97$1 at digitalmars.com...
> 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