E-15
GUIDELINES FOR WRITING FPU EXCEPTIONS HANDLERS
As described in Section E.3.1., Floating-Point Exceptions and Their Defaults, depending on
options determined by the software system designer, the processor can perform one of two pos-
sible courses of action when a numeric exception occurs.
The FPU can provide a default fix-up for selected numeric exceptions. If the 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 E-1 and Example E-2, below, illustrate that it is safest to
always consider exception synchronization when designing code that uses the FPU.
Alternatively, a software exception handler can be invoked to handle the exception. When
a numeric exception is unmasked and the exception occurs, the FPU stops further
execution of the numeric instruction and causes a branch to a software exception handler.
When an FPU exception handler will be invoked, synchronization must always be
considered to assure reliable performance.
Example E-1 and Example E-2, below, illustrate the need to always consider exception synchro-
nization when writing numeric code, even when the code is initially intended for execution with
exceptions masked.
E.3.3.2.EXCEPTION SYNCHRONIZATION EXAMPLES
In the following examples, three instructions are shown to load an integer, calculate its square
root, then increment the integer. The synchronous execution of the FPU will allow both of these
programs to execute correctly, with INC COUNT being executed in parallel in the processor, as
long as no exceptions occur on the FILD instruction. However, if the code is later moved to an
environment where exceptions are unmasked, the code in Example E-1 will not work correctly:
Example E-1. Incorrect Error Synchronization
FILD COUNT; FPU instruction
INC COUNT; integer instruction alters operand
FSQRT; subsequent FPU instruction -- error
; from previous FPU instruction detected here
Example E-2. Proper Error Synchronization
FILD COUNT; FPU instruction
FSQRT; subsequent FPU instruction -- error from
; previous FPU instruction detected here
INC COUNT; integer instruction alters operand
In some operating systems supporting the FPU, the numeric register stack is extended to mem-
ory. To extend the FPU stack to memory, the invalid exception is unmasked. A push to a full
register or pop from an empty register sets SF (Stack Fault flag) and causes an invalid operation
exception. The recovery routine for the exception must recognize this situation, fix up the stack,
then perform the original operation. The recovery routine will not work correctly in Example
E-1. The problem is that the value of COUNT is incremented before the exception handler is