User Guide

290 x87 Floating-Point Programming
AMD64 Technology 24592—Rev. 3.15—November 2009
used externally. It is recommended that system software set NE to 1. This enables optimal
performance in handling x87 floating-point exceptions.
If CR0.NE = 0, internal processor control of x87 floating-point exceptions is disabled and the
external IGNNE# input signal controls whether x87 floating-point exceptions are ignored, as
follows:
- When IGNNE# is 0, x87 floating-point exceptions are reported by asserting the FERR# output
signal, then stopping program execution until an external interrupt is detected. External logic
use the FERR# signal to generate the external interrupt.
- When IGNNE# is 1, x87 floating-point exceptions do not stop program execution. After
FERR# is asserted, instructions continue to execute.
Using NaNs in IE Diagnostic Exceptions. Both SNaNs and QNaNs can be encoded with many
different values to carry diagnostic information. By means of appropriate masking and unmasking of
the invalid-operation exception (IE), software can use signaling NaNs to invoke an exception handler.
Within the constraints imposed by the encoding of SNaNs and QNaNs, software may freely assign the
bits in the significand of a NaN. See the section “Not a Number (NaN)” on page 255 for format details.
For example, software can pre-load each element of an array with a signaling NaN that encodes the
array index. When a n application accesses an uninitialized array element, the invalid-operation
exception is invoked and the service routine can identify that element. A service routine can store
debug information in memory as the exceptions occur. The routine can create a QNaN that references
its associated debug area in memory. As the program runs, the service routine can create a different
QNaN for each error condition, so that a single test-run can identify a collection of errors.
6.9 State-Saving
In general, system software should save and restore x87 state between task switches or other
interventions in the execution of x87 floating-point procedures. Virtually all modern operating systems
running on x86 processors implement preemptive multitasking that handle saving and restoring of
state across task switches, independent of hardware task-switch support. However, application
procedures are also free to save and restore x87 state at any time they deem useful.
6.9.1 State-Saving Instructions
FSAVE/FNSAVE and FRSTOR Instructions. Application software can save and restore the x87
state by executing the FSAVE (or FNSAVE) and FRSTOR instructions. Alternatively, software may
use multiple FxSTx (floating-point store stack top) instructions for saving only the contents of the x87
data registers, rather than the complete x87 state.
The FSAVE instruction stores the state, but only after handling any pending unmasked x87 floating-
point exceptions, whereas the FNSAVE instruction skips the handling of these exceptions. The state of
all x87 data registers is saved, as well as all x87 environment state (the x87 control word register, status
word register, tag word, instruction pointer, data pointer, and last opcode register). After saving this
state, the tag bits for all x87 registers are changed to empty and thus available for a new procedure.