Possible error in _controlfp_s cannot restore control word correctly

I came across a problem with _controlfp_s (Visual Studio 2008) or my understanding of this. I thought the first out parameter returned control flags before changes to other parameters were applied. It seems to return flags after the change.

So, I thought the correct way to use it was like this:

// Chop rounding unsigned int old; _controlfp_s(&old, _RC_CHOP, _MCW_RC); // Do chopped math // Restore unsigned int unused; _controlfp_s(&unused, old, _MCW_RC); 

Unfortunately, I need to do this:

 // Save unsigned int old1; _controlfp_s(&old1, 0, 0); // Chop rounding unsigned int old2; _controlfp_s(&old2, _RC_CHOP, _MCW_RC); // Do chopped math // Restore unsigned int unused; _controlfp_s(&unused, old1, _MCW_RC); 

Did I miss something? It seems pretty silly to do this.

btw: I reported this to MS, who said that they couldn’t understand this, and suggested that I provide a video showing the problem. Yes, right.

Brad

+4
source share
2 answers

According to MSDN :

If the value for the mask is 0 , _controlfp_s receives a floating point control word. If the mask is non-zero, a new value is set for the control word: For any bit that is included (equal to 1) in the mask, the corresponding bit in the new one is used to update the control word. In other words, fpcntrl = ((fpcntrl & ~ mask) | (new and mask)), where fpcntrl is a floating point control word.

(my accent) Thus, the way to reliably store the current control word is the second method that you wrote (the one that you already found working). If you change the control word, then you will not skip 0 for the mask, and in the function documentation it will not receive the current control word.

+3
source

So it looks like it could be by design - it's just plain stupid.

When will you ever want to know a control word after you change it? However, you almost never use the old control word to return it.

It just makes you make an extra call because someone didn’t think when he developed the function.

So, I switched to this approach:

 _controlfp_s(&uiOldCW, _CW_DEFAULT, 0xfffff); 
0
source

All Articles