Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture
D-16 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D.3.3.1 Exception Synchronization: What, Why and When
Exception synchronization means that the exception handler inspects and deals with
the exception in the context in which it occurred. If concurrent execution is allowed,
the state of the processor when it recognizes the exception is often not in the context
in which it occurred. The processor may have changed many of its internal registers
and be executing a totally different program by the time the exception occurs. If the
exception handler cannot recapture the original context, it cannot reliably determine
the cause of the exception or recover successfully from the exception. To handle this
situation, the x87 FPU has special registers updated at the start of each numeric
instruction to describe the state of the numeric program when the failed instruction
was attempted.
This provides tools to help the exception handler recapture the original context, but
the application code must also be written with synchronization in mind. Overall,
exception synchronization must ensure that the x87 FPU and other relevant parts of
the context are in a well defined state when the handler is invoked after an unmasked
numeric exception occurs.
When the x87 FPU signals an unmasked exception condition, it is requesting help.
The fact that the exception was unmasked indicates that further numeric program
execution under the arithmetic and programming rules of the x87 FPU will probably
yield invalid results. Thus the exception must be handled, and with proper synchro-
nization, or the program will not operate reliably.
For programmers using higher-level languages, all required synchronization is auto-
matically provided by the appropriate compiler. However, for assembly language
programmers exception synchronization remains the responsibility of the
programmer. It is not uncommon for a programmer to expect that their numeric
program will not cause numeric exceptions after it has been tested and debugged,
but in a different system or numeric environment, exceptions may occur regularly
nonetheless. An obvious example would be use of the program with some numbers
beyond the range for which it was designed and tested. Example D-1 and Example
D-2 in Section D.3.3.2, “Exception Synchronization Examples,” show a subtle way in
which unexpected exceptions can occur.
As described in Section D.3.1, “Floating-Point Exceptions and Their Defaults,”
depending on options determined by the software system designer, the processor
can perform one of two possible courses of action when a numeric exception occurs.
• The x87 FPU can provide a default fix-up for selected numeric exceptions. If the
x87 FPU performs its default action for all exceptions, then the need for exception
synchronization is not manifest. However, code is often ported to contexts and
operating systems for which it was not originally designed. Example D-1 and
Example D-2, below, illustrate that it is safest to always consider exception
synchronization when designing code that uses the x87 FPU.
• Alternatively, a software exception handler can be invoked to handle the
exception. When a numeric exception is unmasked and the exception occurs, the
x87 FPU stops further execution of the numeric instruction and causes a branch
to a software exception handler. When an x87 FPU exception handler will be