开发者

What Determines the Default Setting of the x87 FPU Control Word?

What determines the default setting of the x87 FPU control word -- specifically, the precision control field? Does the compiler set it based on the target processor? Is there a compiler option to chan开发者_StackOverflow社区ge it?

Using Microsoft Visual C++ 2008 Express Edition on an Intel Core Duo processor, the default setting for the precision control field is "01b", meaning double (53 bit) precision. I'm wondering -- why is the default not "11"b, or extended (64 bit) precision?

(I know I can change it using _controlfp.)


There was a bug in the Microsoft C runtime DLL (MSVCRT.DLL) where it would change the FPCW, which could change application behavior depending on the order of DLL loading.

So if you care about the FPCW setting, it is not only best practice to change it to your desired settings before execution, (even on a function call boundary), but also to change it back when you are done. (And if you call any functions or libaries, beware of callees who might change it and not be so considerate as to change it back for you!)

Also note: the FPCW is expensive to modify, but not very expensive to examine. So if there's a good chance it is already set the way you want, you can save a lot of time by checking it before modifying it.


From a theoretical standpoint: probably the last thing that used it, or barring that, whatever Intel decided would be a good default.

From a practical standpoint: just set it to your desired precision before you begin work. Explicit defaults are usually better than implied defaults.


When the OS sets up a process (or thread) -- say, when your program launches, it is responsible for initializing the control registers to the default state (exactly what the default state is can vary somewhat from platform to platform). It is also responsible for saving and restoring state around context switches, so that your program doesn't bleed state into some other process that is also running on the system.

Beyond that, a compiler or a language runtime might modify the state before your code is executed.

So either: Windows' default FP state is 53 bit precision or VS2008 inserts code to set the processor to 53 bit precision by default. The Windows default process state should be documented in the ABI documentation for the platform.


I've been wondering about the same original question. I've experimented with VC++ 6 & MinGW on Windows 2000 (32-bit).

A simple 3-line program had the FPU control word set for 'Double precision' (0x027f) for VC++ and 'Extended precision' when compiled using MinGW (0x037f). On the same machine, gcc/Linux-32 had the same extended precision (0x37f). i.e. MinGW and gcc/Linux seems to be having the same FPU setting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜