AMD amd64 architecture User Manual

Page of 336
x87 Floating-Point Programming
291
24592—Rev. 3.15—November 2009
AMD64 Technology
FXSAVE and FXRSTOR Instructions.
Application software can save and restore the 128-bit media
state, 64-bit media state, and x87 floating-point state by executing the FXSAVE and FXRSTOR
instructions. The FXSAVE and FXRSTOR instructions execute faster than FSAVE/FNSAVE and
FRSTOR because they do not save and restore the x87 pointers (last instruction pointer, last data
pointer, and last opcode, described in “Pointers and Opcode State” on page 247) except in the
relatively rare cases in which the exception-summary (ES) bit in the x87 status word (the ES register
image for FXSAVE, or the ES memory image for FXRSTOR) is set to 1, indicating that an unmasked
x87 exception has occurred.
Unlike FSAVE and FNSAVE, however, FXSAVE does not alter the tag bits. The state of the saved x87
data registers is retained, thus indicating that the registers may still be valid (or whatever other value
the tag bits indicated prior to the save). To invalidate the contents of the x87 data registers after
FXSAVE, software must explicitly execute an FINIT instruction. Also, FXSAVE (like FNSAVE) and
FXRSTOR do not check for pending unmasked x87 floating-point exceptions. An FWAIT instruction
can be used for this purpose.
The architecture supports two memory formats for FXSAVE and FXRSTOR, a 512-byte 32-bit legacy
format and a 512-byte 64-bit format, used in 64-bit mode. Selection of the 32-bit or 64-bit format is
determined by the effective operand size for the FXSAVE and FXRSTOR instructions. For details, see
“Media and x87 Processor State” in Volume 2.
6.10
Performance Considerations
In addition to typical code optimization techniques, such as those affecting loops and the inlining of
function calls, the following considerations may help improve the performance of application
programs written with x87 floating-point instructions.
These are implementation-independent performance considerations. Other considerations depend on
the hardware implementation. For information about such implementation-dependent considerations
and for more information about application performance in general, see the data sheets and the
software-optimization guides relating to particular hardware implementations.
6.10.1 Replace x87 Code with 128-Bit Media Code
Code written with 128-bit media floating-point instructions can operate in parallel on four times as
many single-precision floating-point operands as can x87 floating-point code. This achieves
potentially four times the computational work of x87 instructions that use single-precision operands.
Also, the higher density of 128-bit media floating-point operands may make it possible to remove local
temporary variables that would otherwise be needed in x87 floating-point code. 128-bit media code is
easier to write than x87 floating-point code, because the XMM register file is flat rather than stack-
oriented, and, in 64-bit mode there are twice the number of XMM registers as x87 registers.
6.10.2 Use FCOMI-FCMOVBranching
Depending on the hardware implementation of the architecture, the combination of FCOMI and
FCMOVcc is often faster than the classical approach using FxSTSW AX instructions for comparison-