Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the initial value of x87 floating point control word defined?

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.

like image 919
VLL Avatar asked Aug 29 '16 11:08

VLL


People also ask

What is the integer(2) parameter in the floating point control word?

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:

Does the floating point control word affect the MXCSR Register?

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.

What is the FPU control word?

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.

What are the control word defaults for precision?

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).


1 Answers

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.

like image 133
Dietmar Kühl Avatar answered Oct 24 '22 09:10

Dietmar Kühl