Value of x87 floating point control word can be checked with _control87
. When a new thread starts, on my platform it seems to inherit value of floating point control word from the parent thread.
Is this undefined behavior, or am I guaranteed that if I start a new thread, and the thread library has no bugs, the control word has the same value it had in the parent thread?
In which standard is this behavior defined, and how? If it is not defined in any standard, is it defined in the processor manual, or the operating system documentation?
I am using C++ language, developing on 64-bit Windows 7, compiling for 32-bit Windows target, and executing the code with an x86-compatible processor. I need an answer specifically for this platform, but if the behavior is the same for all languages and processors, then a generic answer would be better.
Each bit in the floating-point control word corresponds to a mode of the floating-point math processor. The IFORT.F90 module file in the ...\INCLUDE folder contains the INTEGER (2) parameters defined for the control word, as shown in the following table:
They do not affect the MXCSR register (the control and status register for the SSE and SSE2 instructions). Each bit in the floating-point control word corresponds to a mode of the floating-point math processor.
On systems based on the IA-32 architecture, the FPU control word includes bits that control the FPU's precision, rounding mode, and whether exceptions generate signals if they occur.
The control word defaults are: 53-bit precision Round to nearest (rounding mode) The denormal, underflow, overflow, divide-by-zero, invalid, and inexact precision exceptions are disabled (do not generate an exception).
The C Standard (ISO/IEC 9899:2011) has this statement in 7.6 paragraph 2:
The floating-point environment has thread storage duration. The initial state for a thread’s floating-point environment is the current state of the floating-point environment of the thread that creates it at the time of creation.
The C++ Standard (ISO/IEC 14882:2014) has this statement in 26.3.1 [cfenv.syn] paragraph 3:
The floating-point environment has thread storage duration (3.7.2). The initial state for a thread’s floatingpoint environment is the state of the floating-point environment of the thread that constructs the corresponding std::thread object (30.3.1) at the time it constructed the object.
That is, both C and C++ specify that the floating point environment is inherited from the creating thread. This floating point environment is the language level representation of any control world. Note, however, that there is no mandate that a floating point environment is supported. This hinted at, e.g., by C's footnote 12 (in 5.1.2.3; the highlighting is mine):
The IEC 60559 standard for binary floating-point arithmetic requires certain user-accessible status flags and control modes. Floating-point operations implicitly set the status flags; modes affect result values of floating-point operations. Implementations that support such floating-point state are required to regard changes to it as side effects — see annex F for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With