Microsoft KB Archive/102555

= INFO: How Windows handles floating-point calculations =

Article ID: 102555

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Win32 Application Programming Interface, when used with:  Microsoft Windows 95

 Microsoft Windows 98 Standard Edition

 Microsoft Windows Millennium Edition

 Microsoft Windows NT 4.0</li></ul>

 Microsoft Windows 2000 Standard Edition</li></ul>

 Microsoft Windows XP Professional</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q102555

<div class="summary_section">

SUMMARY
All Microsoft Windows platforms provide Win32 applications a common floating-point environment, but they differ in their support for device drivers, and in their support for Win16, Portable Operating System Interface (POSIX), MS-DOS, and OS/2-based applications.

This article describes how each of these platforms provides floating-point support to applications and to device drivers.

<div class="moreinformation_section">

MORE INFORMATION
This section first describes floating-point functionality in Windows NT, Windows 2000, and its successors, and then describes the same in Windows 95, Windows 98, Windows 98 Second Edition, and Windows Millennium Edition (Me)-based systems. Members of each group provide similar floating-point support to each other, but the two groups differ from each other because of underlying architectural differences.

Win32 applications
Each thread in a Win32 application has its own execution state that includes the general-purpose registers and the floating-point registers. Therefore, floating-point execution and exception handling are controlled on a per-thread basis. This permits individual threads in a process to adjust the behavior of the floating-point processor to use specialized rounding or precision control and exception handling.

Floating-point exceptions are handled by the Win32 structured exception handling (SEH) mechanism built into the Win32 kernel. When a thread causes a floating-point exception, the SEH mechanism looks for an exception handler registered by the thread to handle the exception. If the thread does not handle the exception, the process that owns it is closed.

Windows NT and its successors provide full floating-point support for Win32 applications, even on Intel 86 processors that do not have built-in floating-point support. Emulation is provided by the Win32 kernel for computers that do not have floating-point hardware. Therefore, compilers do not have to provide their own floating-point emulation.

Languages may have runtime libraries or runtime systems that provide default floating-point exception handlers. For example, Microsoft Visual C++ runtime library provides default floating-point exception handling and includes functions such as _controlfp for determining and adjusting the floating-point hardware's rounding, precision control, and exception handling behavior.

Win16 applications
Win16 applications are typically compiled to have fixup records in their object code that contain floating-point instructions and calls to the 16-bit floating-point emulator, WIN87EM. If the computer's processor supports floating-point instructions, or if a coprocessor is present, the loader resolves the fixups to the floating-point instructions, and the instructions will be executed. If floating-point hardware is not detected, the loader resolves the fixups to the calls to WIN87EM, and the emulator will be executed.

WIN87EM does not intercept floating-point instructions that applications execute; it runs only when it is explicitly called. Therefore, make sure that Win16 applications are compiled to support emulation, which will tell the linker to put fixup records in the executable.

Windows NT on RISC-based computers use Windows NT's Intel 86 processor emulator code to run Win16 applications. Windows NT 3.5 and Windows NT 3.51 include 80286 and 80287 emulators; Windows NT 4.0 includes an i486 emulator. Win16 applications always run as if the Intel floating-point coprocessors are present. Therefore, WIN87EM does not provide floating-point emulation on RISC computers.

Device drivers
Kernel-mode drivers for Windows 2000 and its successors can use floating-point instructions when IRQL is less than or equal to DISPATCH_LEVEL, but must explicitly preserve the processor's floating-point state so that the caller's floating-point context is not changed. Driver functions must call KeSaveFloatingPointState before performing any floating-point operations, and must call KeRestoreFloatingPointState before returning to the caller. These functions are documented in the Windows 2000 Driver Development Kit (DDK).

Windows NT 3.51 and Windows 4.0 kernel-mode drivers cannot use floating-point instructions, because kernel-mode stack switches do not preserve the floating-point registers. If a kernel-mode driver does use any floating-point instruction, this may cause either corruption of the user-mode application's numeric state or a system failure (crash).

MS-DOS applications
Windows NT and its successors on Intel 86 run MS-DOS applications in the Intel  86 processor's Virtual-8086 mode. MS-DOS applications can access the floating-point hardware just as they would in MS-DOS. If no floating-point hardware is present, no emulation is provided for the application. Therefore, the application must provide its own emulation.

Windows NT on RISC-based computers uses built-in Intel 86 emulation code. Windows NT 3.5 and Windows NT 3.51 include 80286 and 80287 emulators; Windows NT 4.0 includes an i486 emulator. MS-DOS applications always run as if the Intel floating-point coprocessors are present.

POSIX applications
As with Win32 applications, POSIX applications can assume that full floating-point support always exists.

The POSIX standard does not define a way to turn on floating-point exceptions. Therefore, POSIX applications that use floating-point exceptions must rely on system-specific features. A POSIX application compiled with Microsoft Visual C++ can turn on floating exceptions by using the _controlfp function. Floating-point exceptions can then be caught by SIGFPE, or, if the application needs to do more than just catch the exception, by the fpieee_flt function.

OS/2 applications
Floating-point support in this subsystem matches that of OS/2: If floating- point hardware is not installed, the OS/2 application must provide its own emulation. The OS/2 subsystem is supported only on Intel 86 versions of Windows NT and its successors.

Win32 applications
The Windows 95 through Windows Me platforms provide the same floating-point support for Win32 applications as the Windows NT and Windows 2000 platforms.

Win16 applications
Win16 applications are typically compiled to include fixup records in their object code that contain floating-point instructions and calls to the 16-bit floating-point emulator, WIN87EM. If the computer's processor supports floating-point instructions or if a coprocessor is present, the loader resolves the fixups to the floating-point instructions, and they will be executed. If floating-point hardware is not detected, the loader resolves the fixups to the calls to WIN87EM, and the emulator will be executed.

WIN87EM does not intercept floating-point instructions that applications execute; it runs only when it is explicitly called. Therefore, compile Win16 applications to support emulation, which will tell the linker to put fixup records in the executable.

Device drivers
Virtual device drivers (VxDs) can use floating-point instructions but must use the Virtual Math Coprocessor Device interface to save and restore the state of the floating-point coprocessor. The VxD must also register an exception handler for floating-point exceptions.

MS-DOS applications
The Windows 95 through Windows Me platforms run MS-DOS applications in the Intel 86 processor's Virtual-8086 mode. MS-DOS applications can access the floating-point hardware just as they would in MS-DOS. If no floating-point hardware is present, no emulation is provided for the application. Therefore, the application must provide its own emulation, as is typically performed by the MS-DOS application's runtime library.

The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.

Additional query words: FPU floating point math coprocessor

Keywords: kbfloatpoint kbinfo kbkernbase kbprogramming KB102555

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.