User Guide

188 128-Bit Media and Scientific Programming
AMD64 Technology 24592—Rev. 3.15—November 2009
4.11 Saving, Clearing, and Passing State
4.11.1 Saving and Restoring State
In general, system software should save and restore 128-bit media state between task switches or other
interventions in the execution of 128-bit media 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 128-bit media state at any time they deem useful.
Software running at any privilege level may save and restore 128-bit media state by executing the
FXSAVE instruction, which saves not only 128-bit media state but also x87 floating-point state.
Alternatively, software may use multiple move instructions for saving only the contents of selected
128-bit media data registers, or the STMXCSR instruction for saving the MXCSR register state. For
details, see “Save and Restore State” on page 156.
4.11.2 Parameter Passing
128-bit media procedures can use MOVx instructions to pass data to other such procedures. This can
be done directly, via the XMM registers, or indirectly by storing data on the procedure stack. When
storing to the stack, software should use the rSP register for the memory address and, after the save,
explicitly decrement rSP by 16 for each 128-bit XMM register parameter stored on the stack.
Likewise, to load a 128-bit XMM register from the stack, software should increment rSP by 16 after
the load. There is a choice of MOVx instructions designed for aligned and unaligned moves, as
described in “Data Transfer” on page 135 and “Data Transfer” on page 157.
The processor does not check the data type of instruction operands prior to executing instructions. It
only checks them at the point of execution. For example, if the processor executes an arithmetic
instruction that takes double-precision operands but is provided with single-precision operands by
MOVx instructions, the processor will first convert the operands from single precision to double
precision prior to executing the arithmetic operation, and the result will be correct. However, the
required conversion may cause degradation of performance.
Because of this possibility of data-type mismatching between MOVx instructions used to pass
parameters and the instructions in the called procedure that s ubsequently operate on the moved data,
the calling procedure should save its own state prior to the call. The called procedure cannot determine
the caller’s data types, and thus it cannot optimize its choice of instructions for storing a caller’s state.
For further information, see the software optimization documentation for particular hardware
implementations.
4.11.3 Accessing Operands in MMX™ Registers
Software may freely mix 128-bit media instructions (integer or floating-point) with 64-bit media
instructions (integer or floating-point) and general-purpose instructions in a single procedure. There
are no restrictions on transitioning from 128-bit media procedures to x87 procedures, except when a