The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Ficko on January 21, 2011, 08:10:44 AM

Title: FPU Rounding mode preservation ?
Post by: Ficko on January 21, 2011, 08:10:44 AM
Does someone knows something about windows preservation of FPU rounding mode after returning from a API call ?

Does it or doesn't ? ::)
Title: Re: FPU Rounding mode preservation ?
Post by: MichaelW on January 21, 2011, 10:44:50 AM
Per Agner Fog's calling_conventions.pdf (available  here (http://www.agner.org/optimize/#manuals)), under Register Usage:

"The floating point control word and bit 6-15 of the MXCSR register must be saved and restored before any call or return by any procedure that needs to modify them, except for procedures that have the purpose of changing these."

So by convention, the RC field should be preserved around an API call. I do, however, know that unless you tell it not to Direct3D will set the PC field for a 24-bit precision, and in my test the change appeared to occur in a call to ShowWindow. So apparently not the entire API follows the convention.
Title: Re: FPU Rounding mode preservation ?
Post by: BogdanOntanu on January 21, 2011, 11:10:46 AM
You need to save your rounding mode when making  any kind of API call if your code must use a certain rounding mode.
In other words the API is free to change the rounding mode in anyway it wants. Rounding mode is not saved by API.

The only guarantee is that during a context switch Windows will save and restore the current FPU registers and rounding mode  but this is standard / expected or else big errors would occur.
Title: Re: FPU Rounding mode preservation ?
Post by: Ficko on January 21, 2011, 11:20:18 AM
Thank you guys.
Title: Re: FPU Rounding mode preservation ?
Post by: raymond on January 22, 2011, 01:42:44 AM
And, do NOT rely on Windows preserving any of the FPU data registers during API calls requiring mathematical computations (such as sizing a window). It seems that ALL their math operations (even adding 3+3) are done with the use of MMX instructions which trash the FPU registers.