7-45
FLOATING-POINT UNIT
7.6.2.Uses for Signaling NANs
By unmasking the invalid operation exception, the programmer can use signaling NaNs to trap
to the exception handler. The generality of this approach and the large number of NaN values
that are available provide the sophisticated programmer with a tool that can be applied to a
variety of special situations.
For example, a compiler can use signaling NaNs as references to uninitialized (real) array
elements. The compiler can preinitialize each array element with a signaling NaN whose signif-
icand contained the index (relative position) of the element. Then, if an application program
attempts to access an element that it had not initialized, it can use the NaN placed there by the
compiler. If the invalid operation exception is unmasked, an interrupt will occur, and the excep-
tion handler will be invoked. The exception handler can determine which element has been
accessed, since the operand address field of the exception pointers will point to the NaN, and the
NaN will contain the index number of the array element.
7.6.3.Uses for Quiet NANs
Quiet NaNs are often used to speed up debugging. In its early testing phase, a program often
contains multiple errors. An exception handler can be written to save diagnostic information in
memory whenever it was invoked. After storing the diagnostic data, it can supply a quiet NaN
as the result of the erroneous instruction, and that NaN can point to its associated diagnostic area
in memory. The program will then continue, creating a different NaN for each error. When the
program ends, the NaN results can be used to access the diagnostic data saved at the time the
errors occurred. Many errors can thus be diagnosed and corrected in one test run.
In embedded applications which use computed results in further computations, an undetected
QNaN can invalidate all subsequent results. Such applications should therefore periodically
check for QNaNs and provide a recovery mechanism to be used if a QNaN result is detected.
Table 7-19. Results of Operations with NaN Operands
Source Operands
NaN Result
(invalid operation exception is masked)
An SNaN and a QNaN.
Src1 NaN (converted to QNaN if Src1 is an SNaN).
Two SNaNs.
Src1 NaN (converted to QNaN )
Two QNaNs.
Src1 QNaN
An SNaN and a real value.
The SNaN converted into a QNaN.
A QNaN and a real value.
The QNaN source operand.
An SNaN/QNaN value ( for instructions which take
only one operand ie. RCPPS, RCPSS,
RSQRTPS, RSQRTSS)
The SNaN converted into a QNaN/the source QNaN.
Neither source operand is a NaN and a floating-
point invalid operation exception is signaled.
The default QNaN real indefinite.