Search code examples
visual-c++fpu

Flipping bits in the mxcsr register


I'm attempting to set bits in the mxcsr control register. I know how to do this with gcc (fesetenv), but I haven't been able to get this working MSVC. The possibilities I've considered are:

  1. Assembly, which isn't supported inline on MSVC x64.
  2. The _controlfp function, which doesn't seem to match up one to one (note that I may be completely misunderstanding this function, but all of this is poorly documented).

Specifically, I want to set these bits:

  • "Denormals are zeros"
  • "Flush to zeros"

Any ideas for how I can do this?


Solution

  • The _controlfp routine is a generic routine that works across ARM, x86 and x64 - there is no reason to expect it to use the same bits the actual hardware registers use.

    From the article _controlfp, it appears that _controlfp does not allow for individual control of input and output denormals, but sets them together.

    To set the x64 SSE2 to flush denormal operands and outputs to zero, use

    _controlfp(_DN_FLUSH, _MCW_DN);