Intel 64 and IA-32 Architectures Software Developers Manual - Volume 1 - Basic Architecture
Intel 64 and IA-32 Architectures Software Developers Manual - Volume 1 - Basic Architecture
UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT DESIGNED NOR IN-
TENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL PRODUCT COULD CREATE A SITUA-
TION WHERE PERSONAL INJURY OR DEATH MAY OCCUR.
Intel may make changes to specifications and product descriptions at any time, without notice. Designers
must not rely on the absence or characteristics of any features or instructions marked "reserved" or "unde-
fined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or
incompatibilities arising from future changes to them. The information here is subject to change without no-
tice. Do not finalize a design with this information.
The Intel® 64 architecture processors may contain design defects or errors known as errata. Current char-
acterized errata are available on request.
Intel® Hyper-Threading Technology requires a computer system with an Intel® processor supporting Intel
Hyper-Threading Technology and an Intel® HT Technology enabled chipset, BIOS and operating system.
Performance will vary depending on the specific hardware and software you use. For more information, see
http://www.intel.com/technology/hyperthread/index.htm; including details on which processors support Intel HT
Technology.
Intel® Virtualization Technology requires a computer system with an enabled Intel® processor, BIOS, virtual
machine monitor (VMM) and for some uses, certain platform software enabled for it. Functionality, perfor-
mance or other benefits will vary depending on hardware and software configurations. Intel® Virtualization
Technology-enabled BIOS and VMM applications are currently in development.
64-bit computing on Intel architecture requires a computer system with a processor, chipset, BIOS, oper-
ating system, device drivers and applications enabled for Intel® 64 architecture. Processors will not operate
(including 32-bit operation) without an Intel® 64 architecture-enabled BIOS. Performance will vary de-
pending on your hardware and software configurations. Consult with your system vendor for more infor-
mation.
Enabling Execute Disable Bit functionality requires a PC with a processor with Execute Disable Bit capability
and a supporting operating system. Check with your PC manufacturer on whether your system delivers Ex-
ecute Disable Bit functionality.
Intel, Pentium, Intel Xeon, Intel NetBurst, Intel Core, Intel Core Solo, Intel Core Duo, Intel Core 2 Duo,
Intel Core 2 Extreme, Intel Pentium D, Itanium, Intel SpeedStep, MMX, Intel Atom, and VTune are trade-
marks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other coun-
tries.
*Other names and brands may be claimed as the property of others.
Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing
your product order.
Copies of documents which have an ordering number and are referenced in this document, or other Intel
literature, may be obtained by calling 1-800-548-4725, or by visiting Intel’s website at http://www.intel.com
CHAPTER 2
INTEL® 64 AND IA-32 ARCHITECTURES
2.1 BRIEF HISTORY OF INTEL® 64 AND IA-32 ARCHITECTURE. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1
2.1.1 16-bit Processors and Segmentation (1978) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1
2.1.2 The Intel® 286 Processor (1982) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1
2.1.3 The Intel386™ Processor (1985) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
2.1.4 The Intel486™ Processor (1989) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
2.1.5 The Intel® Pentium® Processor (1993) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
2.1.6 The P6 Family of Processors (1995-1999) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
2.1.7 The Intel® Pentium® 4 Processor Family (2000-2006) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
2.1.8 The Intel® Xeon® Processor (2001- 2007) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
2.1.9 The Intel® Pentium® M Processor (2003-Current). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
2.1.10 The Intel® Pentium® Processor Extreme Edition (2005-2007). . . . . . . . . . . . . . . . . . . . . 2-5
2.1.11 The Intel® Core™ Duo and Intel® Core™ Solo Processors (2006-2007). . . . . . . . . . . . . 2-5
2.1.12 The Intel® Xeon® Processor 5100, 5300 Series and
Intel® Core™2 Processor Family (2006-Current) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
2.1.13 The Intel® Xeon® Processor 5200, 5400, 7400 Series and
Intel® Core™2 Processor Family (2007-Current) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
2.1.14 The Intel® Atom™ Processor Family (2008-Current) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
2.1.15 The Intel® Core™i7 Processor Family (2008-Current) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
2.2 MORE ON SPECIFIC ADVANCES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
2.2.1 P6 Family Microarchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
2.2.2 Intel NetBurst® Microarchitecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10
2.2.2.1 The Front End Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
2.2.2.2 Out-Of-Order Execution Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
2.2.2.3 Retirement Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
2.2.3 Intel® Core™ Microarchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
2.2.3.1 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
2.2.3.2 Execution Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
2.2.4 Intel® Atom™ Microarchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Vol. 1 iii
CONTENTS
PAGE
2.2.5 Intel Microarchitecture (Nehalem) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
2.2.6 SIMD Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
2.2.7 Intel® Hyper-Threading Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
2.2.7.1 Some Implementation Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
2.2.8 Multi-Core Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
2.2.9 Intel® 64 Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26
2.2.10 Intel® Virtualization Technology (Intel® VT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
2.3 INTEL® 64 AND IA-32 PROCESSOR GENERATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
CHAPTER 3
BASIC EXECUTION ENVIRONMENT
3.1 MODES OF OPERATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-1
3.1.1 Intel® 64 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
3.2 OVERVIEW OF THE BASIC EXECUTION ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
3.2.1 64-Bit Mode Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
3.3 MEMORY ORGANIZATION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
3.3.1 IA-32 Memory Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
3.3.2 Paging and Virtual Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
3.3.3 Memory Organization in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
3.3.4 Modes of Operation vs. Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
3.3.5 32-Bit and 16-Bit Address and Operand Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
3.3.6 Extended Physical Addressing in Protected Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
3.3.7 Address Calculations in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
3.3.7.1 Canonical Addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13
3.4 BASIC PROGRAM EXECUTION REGISTERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13
3.4.1 General-Purpose Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
3.4.1.1 General-Purpose Registers in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
3.4.2 Segment Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
3.4.2.1 Segment Registers in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20
3.4.3 EFLAGS Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20
3.4.3.1 Status Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21
3.4.3.2 DF Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22
3.4.3.3 System Flags and IOPL Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23
3.4.3.4 RFLAGS Register in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
3.5 INSTRUCTION POINTER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
3.5.1 Instruction Pointer in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
3.6 OPERAND-SIZE AND ADDRESS-SIZE ATTRIBUTES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
3.6.1 Operand Size and Address Size in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25
3.7 OPERAND ADDRESSING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26
3.7.1 Immediate Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27
3.7.2 Register Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27
3.7.2.1 Register Operands in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28
3.7.3 Memory Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28
3.7.3.1 Memory Operands in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
3.7.4 Specifying a Segment Selector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
3.7.4.1 Segmentation in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30
3.7.5 Specifying an Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30
iv Vol. 1
CONTENTS
PAGE
3.7.5.1 Specifying an Offset in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-32
3.7.6 Assembler and Compiler Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-32
3.7.7 I/O Port Addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-33
CHAPTER 4
DATA TYPES
4.1 FUNDAMENTAL DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1
4.1.1 Alignment of Words, Doublewords, Quadwords, and Double Quadwords . . . . . . . . . . . . 4-2
4.2 NUMERIC DATA TYPES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
4.2.1 Integers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
4.2.1.1 Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5
4.2.1.2 Signed Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5
4.2.2 Floating-Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
4.3 POINTER DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
4.3.1 Pointer Data Types in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
4.4 BIT FIELD DATA TYPE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
4.5 STRING DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
4.6 PACKED SIMD DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
4.6.1 64-Bit SIMD Packed Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-11
4.6.2 128-Bit Packed SIMD Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-12
4.7 BCD AND PACKED BCD INTEGERS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13
4.8 REAL NUMBERS AND FLOATING-POINT FORMATS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
4.8.1 Real Number System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-16
4.8.2 Floating-Point Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-16
4.8.2.1 Normalized Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-18
4.8.2.2 Biased Exponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-18
4.8.3 Real Number and Non-number Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-19
4.8.3.1 Signed Zeros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-20
4.8.3.2 Normalized and Denormalized Finite Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-20
4.8.3.3 Signed Infinities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-21
4.8.3.4 NaNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-21
4.8.3.5 Operating on SNaNs and QNaNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-22
4.8.3.6 Using SNaNs and QNaNs in Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-23
4.8.3.7 QNaN Floating-Point Indefinite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-24
4.8.4 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-24
4.8.4.1 Rounding Control (RC) Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-25
4.8.4.2 Truncation with SSE and SSE2 Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . .4-26
4.9 OVERVIEW OF FLOATING-POINT EXCEPTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
4.9.1 Floating-Point Exception Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-28
4.9.1.1 Invalid Operation Exception (#I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-28
4.9.1.2 Denormal Operand Exception (#D). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-28
4.9.1.3 Divide-By-Zero Exception (#Z) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-29
4.9.1.4 Numeric Overflow Exception (#O) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-29
4.9.1.5 Numeric Underflow Exception (#U) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-30
4.9.1.6 Inexact-Result (Precision) Exception (#P) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-31
4.9.2 Floating-Point Exception Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-32
4.9.3 Typical Actions of a Floating-Point Exception Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-33
Vol. 1 v
CONTENTS
PAGE
CHAPTER 5
INSTRUCTION SET SUMMARY
5.1 GENERAL-PURPOSE INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
5.1.1 Data Transfer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
5.1.2 Binary Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
5.1.3 Decimal Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
5.1.4 Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
5.1.5 Shift and Rotate Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
5.1.6 Bit and Byte Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
5.1.7 Control Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
5.1.8 String Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
5.1.9 I/O Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
5.1.10 Enter and Leave Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
5.1.11 Flag Control (EFLAG) Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
5.1.12 Segment Register Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
5.1.13 Miscellaneous Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
5.2 X87 FPU INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
5.2.1 x87 FPU Data Transfer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
5.2.2 x87 FPU Basic Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
5.2.3 x87 FPU Comparison Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
5.2.4 x87 FPU Transcendental Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
5.2.5 x87 FPU Load Constants Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
5.2.6 x87 FPU Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
5.3 X87 FPU AND SIMD STATE MANAGEMENT INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
5.4 MMX™ INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
5.4.1 MMX Data Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
5.4.2 MMX Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
5.4.3 MMX Packed Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
5.4.4 MMX Comparison Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
5.4.5 MMX Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
5.4.6 MMX Shift and Rotate Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
5.4.7 MMX State Management Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
5.5 SSE INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
5.5.1 SSE SIMD Single-Precision Floating-Point Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
5.5.1.1 SSE Data Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
5.5.1.2 SSE Packed Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17
5.5.1.3 SSE Comparison Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
5.5.1.4 SSE Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
5.5.1.5 SSE Shuffle and Unpack Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
5.5.1.6 SSE Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19
5.5.2 SSE MXCSR State Management Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19
5.5.3 SSE 64-Bit SIMD Integer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19
5.5.4 SSE Cacheability Control, Prefetch, and Instruction Ordering Instructions. . . . . . . . . . 5-20
5.6 SSE2 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20
5.6.1 SSE2 Packed and Scalar Double-Precision Floating-Point Instructions . . . . . . . . . . . . . 5-21
5.6.1.1 SSE2 Data Movement Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
5.6.1.2 SSE2 Packed Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
vi Vol. 1
CONTENTS
PAGE
5.6.1.3 SSE2 Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-22
5.6.1.4 SSE2 Compare Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-22
5.6.1.5 SSE2 Shuffle and Unpack Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-22
5.6.1.6 SSE2 Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-23
5.6.2 SSE2 Packed Single-Precision Floating-Point Instructions . . . . . . . . . . . . . . . . . . . . . . . . .5-23
5.6.3 SSE2 128-Bit SIMD Integer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-24
5.6.4 SSE2 Cacheability Control and Ordering Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-24
5.7 SSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25
5.7.1 SSE3 x87-FP Integer Conversion Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-25
5.7.2 SSE3 Specialized 128-bit Unaligned Data Load Instruction . . . . . . . . . . . . . . . . . . . . . . . .5-25
5.7.3 SSE3 SIMD Floating-Point Packed ADD/SUB Instructions . . . . . . . . . . . . . . . . . . . . . . . . . .5-25
5.7.4 SSE3 SIMD Floating-Point Horizontal ADD/SUB Instructions . . . . . . . . . . . . . . . . . . . . . . .5-26
5.7.5 SSE3 SIMD Floating-Point LOAD/MOVE/DUPLICATE Instructions. . . . . . . . . . . . . . . . . . .5-26
5.7.6 SSE3 Agent Synchronization Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-27
5.8 SUPPLEMENTAL STREAMING SIMD EXTENSIONS 3 (SSSE3) INSTRUCTIONS . . . . . . . . . . 5-27
5.8.1 Horizontal Addition/Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-27
5.8.2 Packed Absolute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-28
5.8.3 Multiply and Add Packed Signed and Unsigned Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-28
5.8.4 Packed Multiply High with Round and Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-28
5.8.5 Packed Shuffle Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-29
5.8.6 Packed Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-29
5.8.7 Packed Align Right. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-29
5.9 SSE4 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
5.10 SSE4.1 INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-30
5.10.1 Dword Multiply Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-30
5.10.2 Floating-Point Dot Product Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-31
5.10.3 Streaming Load Hint Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-31
5.10.4 Packed Blending Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-31
5.10.5 Packed Integer MIN/MAX Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-31
5.10.6 Floating-Point Round Instructions with Selectable Rounding Mode . . . . . . . . . . . . . . . .5-32
5.10.7 Insertion and Extractions from XMM Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-32
5.10.8 Packed Integer Format Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-33
5.10.9 Improved Sums of Absolute Differences (SAD) for 4-Byte Blocks . . . . . . . . . . . . . . . . . .5-33
5.10.10 Horizontal Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-33
5.10.11 Packed Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-34
5.10.12 Packed Qword Equality Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-34
5.10.13 Dword Packing With Unsigned Saturation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-34
5.11 SSE4.2 INSTRUCTION SET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-34
5.11.1 String and Text Processing Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-34
5.11.2 Packed Comparison SIMD integer Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-34
5.11.3 Application-Targeted Accelerator Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-35
5.12 SYSTEM INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-35
5.13 64-BIT MODE INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-36
5.14 VIRTUAL-MACHINE EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-36
5.15 SAFER MODE EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-37
Vol. 1 vii
CONTENTS
PAGE
CHAPTER 6
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
6.1 PROCEDURE CALL TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1
6.2 STACKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1
6.2.1 Setting Up a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
6.2.2 Stack Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
6.2.3 Address-Size Attributes for Stack Accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
6.2.4 Procedure Linking Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
6.2.4.1 Stack-Frame Base Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
6.2.4.2 Return Instruction Pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
6.2.5 Stack Behavior in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
6.3 CALLING PROCEDURES USING CALL AND RET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
6.3.1 Near CALL and RET Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
6.3.2 Far CALL and RET Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6
6.3.3 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
6.3.3.1 Passing Parameters Through the General-Purpose Registers . . . . . . . . . . . . . . . . . . . 6-7
6.3.3.2 Passing Parameters on the Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
6.3.3.3 Passing Parameters in an Argument List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
6.3.4 Saving Procedure State Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
6.3.5 Calls to Other Privilege Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
6.3.6 CALL and RET Operation Between Privilege Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
6.3.7 Branch Functions in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
6.4 INTERRUPTS AND EXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13
6.4.1 Call and Return Operation for Interrupt or Exception Handling Procedures . . . . . . . . 6-14
6.4.2 Calls to Interrupt or Exception Handler Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
6.4.3 Interrupt and Exception Handling in Real-Address Mode. . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
6.4.4 INT n, INTO, INT 3, and BOUND Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18
6.4.5 Handling Floating-Point Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18
6.4.6 Interrupt and Exception Behavior in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
6.5 PROCEDURE CALLS FOR BLOCK-STRUCTURED LANGUAGES . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
6.5.1 ENTER Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20
6.5.2 LEAVE Instruction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25
CHAPTER 7
PROGRAMMING WITH
GENERAL-PURPOSE INSTRUCTIONS
7.1 PROGRAMMING ENVIRONMENT FOR GP INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1
7.2 PROGRAMMING ENVIRONMENT FOR GP INSTRUCTIONS IN 64-BIT MODE . . . . . . . . . . . . . . 7-2
7.3 SUMMARY OF GP INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3
7.3.1 Data Transfer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3
7.3.1.1 General Data Movement Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4
7.3.1.2 Exchange Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
7.3.1.3 Exchange Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7
7.3.1.4 Stack Manipulation Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7
7.3.1.5 Stack Manipulation Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9
7.3.1.6 Type Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10
7.3.1.7 Type Conversion Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
viii Vol. 1
CONTENTS
PAGE
7.3.2 Binary Arithmetic Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12
7.3.2.1 Addition and Subtraction Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12
7.3.2.2 Increment and Decrement Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12
7.3.2.3 Increment and Decrement Instructions in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . .7-12
7.3.2.4 Comparison and Sign Change Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12
7.3.2.5 Multiplication and Divide Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-13
7.3.3 Decimal Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-13
7.3.3.1 Packed BCD Adjustment Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-14
7.3.3.2 Unpacked BCD Adjustment Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-14
7.3.4 Decimal Arithmetic Instructions in 64-Bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-15
7.3.5 Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-15
7.3.6 Shift and Rotate Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-15
7.3.6.1 Shift Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-15
7.3.6.2 Double-Shift Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-17
7.3.6.3 Rotate Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-18
7.3.7 Bit and Byte Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-20
7.3.7.1 Bit Test and Modify Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-20
7.3.7.2 Bit Scan Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-20
7.3.7.3 Byte Set on Condition Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-20
7.3.7.4 Test Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-21
7.3.8 Control Transfer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-21
7.3.8.1 Unconditional Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-21
7.3.8.2 Conditional Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-23
7.3.8.3 Control Transfer Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-25
7.3.8.4 Software Interrupt Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-25
7.3.8.5 Software Interrupt Instructions in 64-bit Mode and Compatibility Mode . . . . . . . .7-26
7.3.9 String Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-26
7.3.9.1 Repeating String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-27
7.3.10 String Operations in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-28
7.3.10.1 Repeating String Operations in 64-bit Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-28
7.3.11 I/O Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-28
7.3.12 I/O Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-29
7.3.13 Enter and Leave Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-29
7.3.14 Flag Control (EFLAG) Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-29
7.3.14.1 Carry and Direction Flag Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-29
7.3.14.2 EFLAGS Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-30
7.3.14.3 Interrupt Flag Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-31
7.3.15 Flag Control (RFLAG) Instructions in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-31
7.3.16 Segment Register Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-31
7.3.16.1 Segment-Register Load and Store Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-31
7.3.16.2 Far Control Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-32
7.3.16.3 Software Interrupt Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-32
7.3.16.4 Load Far Pointer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-32
7.3.17 Miscellaneous Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-32
7.3.17.1 Address Computation Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-33
7.3.17.2 Table Lookup Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-33
7.3.17.3 Processor Identification Instruction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-33
Vol. 1 ix
CONTENTS
PAGE
7.3.17.4 No-Operation and Undefined Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33
CHAPTER 8
PROGRAMMING WITH THE X87 FPU
8.1 X87 FPU EXECUTION ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1
8.1.1 x87 FPU in 64-Bit Mode and Compatibility Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
8.1.2 x87 FPU Data Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
8.1.2.1 Parameter Passing With the x87 FPU Register Stack . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5
8.1.3 x87 FPU Status Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6
8.1.3.1 Top of Stack (TOP) Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6
8.1.3.2 Condition Code Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6
8.1.3.3 x87 FPU Floating-Point Exception Flags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7
8.1.3.4 Stack Fault Flag. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9
8.1.4 Branching and Conditional Moves on Condition Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9
8.1.5 x87 FPU Control Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-10
8.1.5.1 x87 FPU Floating-Point Exception Mask Bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-11
8.1.5.2 Precision Control Field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-11
8.1.5.3 Rounding Control Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
8.1.6 Infinity Control Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
8.1.7 x87 FPU Tag Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12
8.1.8 x87 FPU Instruction and Data (Operand) Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13
8.1.9 Last Instruction Opcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14
8.1.9.1 Fopcode Compatibility Sub-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14
8.1.10 Saving the x87 FPU’s State with FSTENV/FNSTENV and FSAVE/FNSAVE . . . . . . . . . 8-15
8.1.11 Saving the x87 FPU’s State with FXSAVE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-17
8.2 X87 FPU DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-17
8.2.1 Indefinites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-19
8.2.2 Unsupported Double Extended-Precision
Floating-Point Encodings and Pseudo-Denormals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-20
8.3 X86 FPU INSTRUCTION SET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-21
8.3.1 Escape (ESC) Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22
8.3.2 x87 FPU Instruction Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22
8.3.3 Data Transfer Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22
8.3.4 Load Constant Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24
8.3.5 Basic Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24
8.3.6 Comparison and Classification Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-26
8.3.6.1 Branching on the x87 FPU Condition Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-28
8.3.7 Trigonometric Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-29
8.3.8 Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-30
8.3.9 Logarithmic, Exponential, and Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-31
8.3.10 Transcendental Instruction Accuracy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-31
8.3.11 x87 FPU Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-32
8.3.12 Waiting vs. Non-waiting Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-33
8.3.13 Unsupported x87 FPU Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-34
8.4 X87 FPU FLOATING-POINT EXCEPTION HANDLING. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-34
8.4.1 Arithmetic vs. Non-arithmetic Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-35
8.5 X87 FPU FLOATING-POINT EXCEPTION CONDITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-36
x Vol. 1
CONTENTS
PAGE
8.5.1 Invalid Operation Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-36
8.5.1.1 Stack Overflow or Underflow Exception (#IS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-37
8.5.1.2 Invalid Arithmetic Operand Exception (#IA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-38
8.5.2 Denormal Operand Exception (#D). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-39
8.5.3 Divide-By-Zero Exception (#Z) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-40
8.5.4 Numeric Overflow Exception (#O) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-40
8.5.5 Numeric Underflow Exception (#U) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-41
8.5.6 Inexact-Result (Precision) Exception (#P) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-42
8.6 X87 FPU EXCEPTION SYNCHRONIZATION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-43
8.7 HANDLING X87 FPU EXCEPTIONS IN SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-45
8.7.1 Native Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-45
8.7.2 MS-DOS* Compatibility Sub-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-45
8.7.3 Handling x87 FPU Exceptions in Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-47
CHAPTER 9
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
9.1 OVERVIEW OF MMX TECHNOLOGY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1
9.2 THE MMX TECHNOLOGY PROGRAMMING ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
9.2.1 MMX Technology in 64-Bit Mode and Compatibility Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
9.2.2 MMX Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3
9.2.3 MMX Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3
9.2.4 Memory Data Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
9.2.5 Single Instruction, Multiple Data (SIMD) Execution Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
9.3 SATURATION AND WRAPAROUND MODES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5
9.4 MMX INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6
9.4.1 Data Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8
9.4.2 Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8
9.4.3 Comparison Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9
9.4.4 Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9
9.4.5 Unpack Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9
9.4.6 Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-10
9.4.7 Shift Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-10
9.4.8 EMMS Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-10
9.5 COMPATIBILITY WITH X87 FPU ARCHITECTURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10
9.5.1 MMX Instructions and the x87 FPU Tag Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-11
9.6 WRITING APPLICATIONS WITH MMX CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11
9.6.1 Checking for MMX Technology Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-11
9.6.2 Transitions Between x87 FPU and MMX Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-12
9.6.3 Using the EMMS Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-12
9.6.4 Mixing MMX and x87 FPU Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-13
9.6.5 Interfacing with MMX Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-13
9.6.6 Using MMX Code in a Multitasking Operating System Environment . . . . . . . . . . . . . . . .9-14
9.6.7 Exception Handling in MMX Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-14
9.6.8 Register Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-14
9.6.9 Effect of Instruction Prefixes on MMX Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-14
Vol. 1 xi
CONTENTS
PAGE
CHAPTER 10
PROGRAMMING WITH
STREAMING SIMD EXTENSIONS (SSE)
10.1 OVERVIEW OF SSE EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-1
10.2 SSE PROGRAMMING ENVIRONMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3
10.2.1 SSE in 64-Bit Mode and Compatibility Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-4
10.2.2 XMM Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-4
10.2.3 MXCSR Control and Status Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-5
10.2.3.1 SIMD Floating-Point Mask and Flag Bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6
10.2.3.2 SIMD Floating-Point Rounding Control Field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7
10.2.3.3 Flush-To-Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7
10.2.3.4 Denormals-Are-Zeros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7
10.2.4 Compatibility of SSE Extensions with SSE2/SSE3/MMX and the x87 FPU . . . . . . . . . . 10-8
10.3 SSE DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8
10.4 SSE INSTRUCTION SET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9
10.4.1 SSE Packed and Scalar Floating-Point Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9
10.4.1.1 SSE Data Movement Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-11
10.4.1.2 SSE Arithmetic Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-11
10.4.2 SSE Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13
10.4.2.1 SSE Comparison Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13
10.4.2.2 SSE Shuffle and Unpack Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
10.4.3 SSE Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-15
10.4.4 SSE 64-Bit SIMD Integer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-16
10.4.5 MXCSR State Management Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17
10.4.6 Cacheability Control, Prefetch, and Memory Ordering Instructions . . . . . . . . . . . . . . . 10-18
10.4.6.1 Cacheability Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18
10.4.6.2 Caching of Temporal vs. Non-Temporal Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18
10.4.6.3 PREFETCHh Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19
10.4.6.4 SFENCE Instruction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
10.5 FXSAVE AND FXRSTOR INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
10.6 HANDLING SSE INSTRUCTION EXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21
10.7 WRITING APPLICATIONS WITH THE SSE EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21
CHAPTER 11
PROGRAMMING WITH
STREAMING SIMD EXTENSIONS 2 (SSE2)
11.1 OVERVIEW OF SSE2 EXTENSIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-1
11.2 SSE2 PROGRAMMING ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3
11.2.1 SSE2 in 64-Bit Mode and Compatibility Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4
11.2.2 Compatibility of SSE2 Extensions with SSE, MMX
Technology and x87 FPU Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4
11.2.3 Denormals-Are-Zeros Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-4
11.3 SSE2 DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
11.4 SSE2 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-6
11.4.1 Packed and Scalar Double-Precision Floating-Point Instructions . . . . . . . . . . . . . . . . . . . 11-6
11.4.1.1 Data Movement Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-7
11.4.1.2 SSE2 Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-8
xii Vol. 1
CONTENTS
PAGE
11.4.1.3 SSE2 Logical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-9
11.4.1.4 SSE2 Comparison Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-9
11.4.1.5 SSE2 Shuffle and Unpack Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10
11.4.1.6 SSE2 Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12
11.4.2 SSE2 64-Bit and 128-Bit SIMD Integer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-15
11.4.3 128-Bit SIMD Integer Instruction Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-16
11.4.4 Cacheability Control and Memory Ordering Instructions . . . . . . . . . . . . . . . . . . . . . . . . . 11-16
11.4.4.1 FLUSH Cache Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-17
11.4.4.2 Cacheability Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-17
11.4.4.3 Memory Ordering Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-17
11.4.4.4 Pause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
11.4.5 Branch Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
11.5 SSE, SSE2, AND SSE3 EXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
11.5.1 SIMD Floating-Point Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-19
11.5.2 SIMD Floating-Point Exception Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-19
11.5.2.1 Invalid Operation Exception (#I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-20
11.5.2.2 Denormal-Operand Exception (#D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-21
11.5.2.3 Divide-By-Zero Exception (#Z) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-22
11.5.2.4 Numeric Overflow Exception (#O) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-22
11.5.2.5 Numeric Underflow Exception (#U) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-22
11.5.2.6 Inexact-Result (Precision) Exception (#P) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23
11.5.3 Generating SIMD Floating-Point Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23
11.5.3.1 Handling Masked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23
11.5.3.2 Handling Unmasked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-25
11.5.3.3 Handling Combinations of Masked and Unmasked Exceptions . . . . . . . . . . . . . . . . 11-26
11.5.4 Handling SIMD Floating-Point Exceptions in Software. . . . . . . . . . . . . . . . . . . . . . . . . . . 11-26
11.5.5 Interaction of SIMD and x87 FPU Floating-Point Exceptions. . . . . . . . . . . . . . . . . . . . . 11-26
11.6 WRITING APPLICATIONS WITH SSE/SSE2 EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
11.6.1 General Guidelines for Using SSE/SSE2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
11.6.2 Checking for SSE/SSE2 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-28
11.6.3 Checking for the DAZ Flag in the MXCSR Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-28
11.6.4 Initialization of SSE/SE2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29
11.6.5 Saving and Restoring the SSE/SSE2 State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-30
11.6.6 Guidelines for Writing to the MXCSR Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-30
11.6.7 Interaction of SSE/SSE2 Instructions with x87 FPU and MMX Instructions . . . . . . . 11-31
11.6.8 Compatibility of SIMD and x87 FPU Floating-Point Data Types . . . . . . . . . . . . . . . . . . 11-32
11.6.9 Mixing Packed and Scalar Floating-Point and 128-Bit SIMD Integer Instructions and Data
11-32
11.6.10 Interfacing with SSE/SSE2 Procedures and Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 11-34
11.6.10.1 Passing Parameters in XMM Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-34
11.6.10.2 Saving XMM Register State on a Procedure or Function Call. . . . . . . . . . . . . . . . . . 11-34
11.6.10.3 Caller-Save Requirement for Procedure and Function Calls. . . . . . . . . . . . . . . . . . . 11-35
11.6.11 Updating Existing MMX Technology Routines
Using 128-Bit SIMD Integer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-35
11.6.12 Branching on Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-36
11.6.13 Cacheability Hint Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-36
11.6.14 Effect of Instruction Prefixes on the SSE/SSE2 Instructions. . . . . . . . . . . . . . . . . . . . . 11-37
Vol. 1 xiii
CONTENTS
PAGE
CHAPTER 12
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12.1 PROGRAMMING ENVIRONMENT AND DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-1
12.1.1 SSE3, SSSE3, SSE4 in 64-Bit Mode and Compatibility Mode . . . . . . . . . . . . . . . . . . . . . . . 12-1
12.1.2 Compatibility of SSE3/SSSE3 with MMX Technology, the x87 FPU Environment, and
SSE/SSE2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
12.1.3 Horizontal and Asymmetric Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
12.2 OVERVIEW OF SSE3 INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
12.3 SSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
12.3.1 x87 FPU Instruction for Integer Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-4
12.3.2 SIMD Integer Instruction for Specialized 128-bit Unaligned Data Load. . . . . . . . . . . . . 12-4
12.3.3 SIMD Floating-Point Instructions That Enhance LOAD/MOVE/DUPLICATE Performance . .
12-4
12.3.4 SIMD Floating-Point Instructions Provide Packed Addition/Subtraction . . . . . . . . . . . . 12-5
12.3.5 SIMD Floating-Point Instructions Provide Horizontal Addition/Subtraction . . . . . . . . . 12-5
12.3.6 Two Thread Synchronization Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
12.4 WRITING APPLICATIONS WITH SSE3 EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
12.4.1 Guidelines for Using SSE3 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
12.4.2 Checking for SSE3 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
12.4.3 Enable FTZ and DAZ for SIMD Floating-Point Computation. . . . . . . . . . . . . . . . . . . . . . . . 12-8
12.4.4 Programming SSE3 with SSE/SSE2 Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8
12.5 OVERVIEW OF SSSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8
12.6 SSSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-9
12.6.1 Horizontal Addition/Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-9
12.6.2 Packed Absolute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11
12.6.3 Multiply and Add Packed Signed and Unsigned Bytes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11
12.6.4 Packed Multiply High with Round and Scale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11
12.6.5 Packed Shuffle Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
12.6.6 Packed Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
12.6.7 Packed Align Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
12.7 WRITING APPLICATIONS WITH SSSE3 EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
12.7.1 Guidelines for Using SSSE3 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-12
12.7.2 Checking for SSSE3 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
12.8 SSE3/SSSE3 AND SSE4 EXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
12.8.1 Device Not Available (DNA) Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
12.8.2 Numeric Error flag and IGNNE# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-14
12.8.3 Emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-14
12.8.4 IEEE 754 Compliance of SSE4.1 Floating-Point Instructions . . . . . . . . . . . . . . . . . . . . . . 12-14
12.9 SSE4 OVERVIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-15
12.10 SSE4.1 INSTRUCTION SET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-16
12.10.1 Dword Multiply Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-16
12.10.2 Floating-Point Dot Product Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-16
12.10.3 Streaming Load Hint Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17
12.10.4 Packed Blending Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
12.10.5 Packed Integer MIN/MAX Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-22
12.10.6 Floating-Point Round Instructions with Selectable Rounding Mode . . . . . . . . . . . . . . . 12-23
12.10.7 Insertion and Extractions from XMM Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-23
xiv Vol. 1
CONTENTS
PAGE
12.10.8 Packed Integer Format Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-23
12.10.9 Improved Sums of Absolute Differences (SAD) for 4-Byte Blocks . . . . . . . . . . . . . . . . 12-24
12.10.10 Horizontal Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25
12.10.11 Packed Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25
12.10.12 Packed Qword Equality Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26
12.10.13 Dword Packing With Unsigned Saturation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26
12.11 SSE4.2 INSTRUCTION SET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26
12.11.1 String and Text Processing Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26
12.11.1.1 Memory Operand Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-27
12.11.2 Packed Comparison SIMD Integer Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28
12.11.3 Application-Targeted Accelerator Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28
12.12 WRITING APPLICATIONS WITH SSE4 EXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28
12.12.1 Guidelines for Using SSE4 Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28
12.12.2 Checking for SSE4.1 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28
12.12.3 Checking for SSE4.2 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-29
CHAPTER 13
INPUT/OUTPUT
13.1 I/O PORT ADDRESSING. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-1
13.2 I/O PORT HARDWARE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-1
13.3 I/O ADDRESS SPACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
13.3.1 Memory-Mapped I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-2
13.4 I/O INSTRUCTIONS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-3
13.5 PROTECTED-MODE I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
13.5.1 I/O Privilege Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-4
13.5.2 I/O Permission Bit Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-5
13.6 ORDERING I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7
CHAPTER 14
PROCESSOR IDENTIFICATION AND FEATURE DETERMINATION
14.1 USING THE CPUID INSTRUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-1
14.1.1 Notes on Where to Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-1
14.1.2 Identification of Earlier IA-32 Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14-2
APPENDIX A
EFLAGS CROSS-REFERENCE
A.1 EFLAGS AND INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1
APPENDIX B
EFLAGS CONDITION CODES
B.1 CONDITION CODES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-1
APPENDIX C
FLOATING-POINT EXCEPTIONS SUMMARY
C.1 OVERVIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-1
C.2 X87 FPU INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-2
C.3 SSE INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-4
Vol. 1 xv
CONTENTS
PAGE
C.4 SSE2 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-7
C.5 SSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-11
C.6 SSSE3 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-12
C.7 SSE4 INSTRUCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-12
APPENDIX D
GUIDELINES FOR WRITING X87 FPU
EXCEPTION HANDLERS
D.1 MS-DOS COMPATIBILITY SUB-MODE FOR HANDLING X87 FPU EXCEPTIONS . . . . . . . . . . . D-1
D.2 IMPLEMENTATION OF THE MS-DOS* COMPATIBILITY SUB-MODE IN THE INTEL486™,
PENTIUM®, AND P6 PROCESSOR FAMILY, AND PENTIUM® 4 PROCESSORS . . . . . . . . . . . . . D-3
D.2.1 MS-DOS* Compatibility Sub-mode in the Intel486™ and Pentium® Processors . . . . . . . D-3
D.2.1.1 Basic Rules: When FERR# Is Generated. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-4
D.2.1.2 Recommended External Hardware to Support the MS-DOS* Compatibility Sub-mode
D-5
D.2.1.3 No-Wait x87 FPU Instructions Can Get x87 FPU Interrupt in Window . . . . . . . . . . . D-8
D.2.2 MS-DOS* Compatibility Sub-mode in the P6 Family
and Pentium® 4 Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-10
D.3 RECOMMENDED PROTOCOL FOR MS-DOS* COMPATIBILITY HANDLERS. . . . . . . . . . . . . . D-11
D.3.1 Floating-Point Exceptions and Their Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-12
D.3.2 Two Options for Handling Numeric Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-12
D.3.2.1 Automatic Exception Handling: Using Masked Exceptions. . . . . . . . . . . . . . . . . . . . . . D-12
D.3.2.2 Software Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-14
D.3.3 Synchronization Required for Use of x87 FPU Exception Handlers . . . . . . . . . . . . . . . . D-15
D.3.3.1 Exception Synchronization: What, Why, and When . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-16
D.3.3.2 Exception Synchronization Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-17
D.3.3.3 Proper Exception Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-18
D.3.4 x87 FPU Exception Handling Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-18
D.3.5 Need for Storing State of IGNNE# Circuit If Using x87 FPU and SMM. . . . . . . . . . . . . . D-22
D.3.6 Considerations When x87 FPU Shared Between Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . D-23
D.3.6.1 Speculatively Deferring x87 FPU Saves, General Overview . . . . . . . . . . . . . . . . . . . . D-23
D.3.6.2 Tracking x87 FPU Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-24
D.3.6.3 Interaction of x87 FPU State Saves and Floating-Point Exception Association. . D-25
D.3.6.4 Interrupt Routing From the Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-28
D.3.6.5 Special Considerations for Operating Systems that Support Streaming SIMD
Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-28
D.4 DIFFERENCES FOR HANDLERS USING NATIVE MODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-29
D.4.1 Origin with the Intel 286 and Intel 287, and Intel386 and Intel 387 Processors . . . D-29
D.4.2 Changes with Intel486, Pentium and Pentium Pro Processors with CR0.NE[bit 5] = 1 .D-
30
D.4.3 Considerations When x87 FPU Shared Between Tasks Using Native Mode . . . . . . . . D-30
APPENDIX E
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E.1 TWO OPTIONS FOR HANDLING FLOATING-POINT EXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . E-1
E.2 SOFTWARE EXCEPTION HANDLING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-1
E.3 EXCEPTION SYNCHRONIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-3
xvi Vol. 1
CONTENTS
PAGE
E.4 SIMD FLOATING-POINT EXCEPTIONS AND THE IEEE STANDARD 754 . . . . . . . . . . . . . . . . . . E-4
E.4.1 Floating-Point Emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-4
E.4.2 SSE/SSE2/SSE3 Response To Floating-Point Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . E-6
E.4.2.1 Numeric Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-7
E.4.2.2 Results of Operations with NaN Operands or a NaN Result for SSE/SSE2/SSE3
Numeric Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-7
E.4.2.3 Condition Codes, Exception Flags, and Response for Masked and Unmasked Numeric
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-12
E.4.3 Example SIMD Floating-Point Emulation Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . E-22
Vol. 1 xvii
CONTENTS
PAGE
FIGURES
Figure 1-1. Bit and Byte Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Figure 1-2. Syntax for CPUID, CR, and MSR Data Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Figure 2-1. The P6 Processor Microarchitecture with Advanced Transfer Cache Enhancement 2-
9
Figure 2-2. The Intel NetBurst Microarchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
Figure 2-3. The Intel Core Microarchitecture Pipeline Functionality. . . . . . . . . . . . . . . . . . . . . . . . 2-15
Figure 2-4. SIMD Extensions, Register Layouts, and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20
Figure 2-5. Comparison of an IA-32 Processor Supporting Hyper-Threading Technology and a
Traditional Dual Processor System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
Figure 2-6. Intel 64 and IA-32 Processors that Support Dual-Core . . . . . . . . . . . . . . . . . . . . . . . . 2-24
Figure 2-7. Intel 64 Processors that Support Quad-Core. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25
Figure 2-8. Intel Core i7 Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26
Figure 3-1. IA-32 Basic Execution Environment for Non-64-bit Modes. . . . . . . . . . . . . . . . . . . . . . 3-4
Figure 3-2. 64-Bit Mode Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Figure 3-3. Three Memory Management Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9
Figure 3-4. General System and Application Programming Registers . . . . . . . . . . . . . . . . . . . . . . 3-15
Figure 3-5. Alternate General-Purpose Register Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
Figure 3-6. Use of Segment Registers for Flat Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
Figure 3-7. Use of Segment Registers in Segmented Memory Model . . . . . . . . . . . . . . . . . . . . . . 3-19
Figure 3-8. EFLAGS Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21
Figure 3-9. Memory Operand Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28
Figure 3-10. Memory Operand Address in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
Figure 3-11. Offset (or Effective Address) Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31
Figure 4-1. Fundamental Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1
Figure 4-2. Bytes, Words, Doublewords, Quadwords, and Double Quadwords in Memory . . . . 4-2
Figure 4-3. Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Figure 4-4. Pointer Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Figure 4-5. Pointers in 64-Bit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Figure 4-6. Bit Field Data Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Figure 4-7. 64-Bit Packed SIMD Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Figure 4-8. 128-Bit Packed SIMD Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13
Figure 4-9. BCD Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Figure 4-10. Binary Real Number System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Figure 4-11. Binary Floating-Point Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Figure 4-12. Real Numbers and NaNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19
Figure 6-1. Stack Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Figure 6-2. Stack on Near and Far Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Figure 6-3. Protection Rings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9
Figure 6-4. Stack Switch on a Call to a Different Privilege Level. . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
Figure 6-5. Stack Usage on Transfers to Interrupt and Exception Handling Routines . . . . . . . 6-16
Figure 6-6. Nested Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
Figure 6-7. Stack Frame After Entering the MAIN Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
Figure 6-8. Stack Frame After Entering Procedure A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
Figure 6-9. Stack Frame After Entering Procedure B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Figure 6-10. Stack Frame After Entering Procedure C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25
xviii Vol. 1
CONTENTS
PAGE
Figure 7-1. Operation of the PUSH Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8
Figure 7-2. Operation of the PUSHA Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8
Figure 7-3. Operation of the POP Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9
Figure 7-4. Operation of the POPA Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9
Figure 7-5. Sign Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-11
Figure 7-7. SHR Instruction Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-16
Figure 7-6. SHL/SAL Instruction Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-16
Figure 7-8. SAR Instruction Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-17
Figure 7-9. SHLD and SHRD Instruction Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-18
Figure 7-10. ROL, ROR, RCL, and RCR Instruction Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-19
Figure 7-11. Flags Affected by the PUSHF, POPF, PUSHFD, and POPFD Instructions . . . . . . . . .7-30
Figure 8-1. x87 FPU Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3
Figure 8-2. x87 FPU Data Register Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4
Figure 8-3. Example x87 FPU Dot Product Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5
Figure 8-4. x87 FPU Status Word. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6
Figure 8-5. Moving the Condition Codes to the EFLAGS Register . . . . . . . . . . . . . . . . . . . . . . . . . .8-10
Figure 8-6. x87 FPU Control Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-11
Figure 8-7. x87 FPU Tag Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-13
Figure 8-8. Contents of x87 FPU Opcode Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-15
Figure 8-10. Real Mode x87 FPU State Image in Memory, 32-Bit Format . . . . . . . . . . . . . . . . . . . .8-16
Figure 8-9. Protected Mode x87 FPU State Image in Memory, 32-Bit Format . . . . . . . . . . . . . .8-16
Figure 8-12. Real Mode x87 FPU State Image in Memory, 16-Bit Format . . . . . . . . . . . . . . . . . . . .8-17
Figure 8-11. Protected Mode x87 FPU State Image in Memory, 16-Bit Format . . . . . . . . . . . . . .8-17
Figure 8-13. x87 FPU Data Type Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-19
Figure 9-1. MMX Technology Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Figure 9-2. MMX Register Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3
Figure 9-3. Data Types Introduced with the MMX Technology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
Figure 9-4. SIMD Execution Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5
Figure 10-1. SSE Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-3
Figure 10-2. XMM Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-4
Figure 10-3. MXCSR Control/Status Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-6
Figure 10-4. 128-Bit Packed Single-Precision Floating-Point Data Type . . . . . . . . . . . . . . . . . . . . .10-8
Figure 10-5. Packed Single-Precision Floating-Point Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10
Figure 10-6. Scalar Single-Precision Floating-Point Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10
Figure 10-7. SHUFPS Instruction, Packed Shuffle Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
Figure 10-8. UNPCKHPS Instruction, High Unpack and Interleave Operation . . . . . . . . . . . . . . . 10-15
Figure 10-9. UNPCKLPS Instruction, Low Unpack and Interleave Operation. . . . . . . . . . . . . . . . 10-15
Figure 11-1. Steaming SIMD Extensions 2 Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . .11-3
Figure 11-2. Data Types Introduced with the SSE2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-5
Figure 11-3. Packed Double-Precision Floating-Point Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . .11-6
Figure 11-4. Scalar Double-Precision Floating-Point Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-7
Figure 11-5. SHUFPD Instruction, Packed Shuffle Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-11
Figure 11-6. UNPCKHPD Instruction, High Unpack and Interleave Operation . . . . . . . . . . . . . . . 11-11
Figure 11-7. UNPCKLPD Instruction, Low Unpack and Interleave Operation . . . . . . . . . . . . . . . 11-12
Figure 11-8. SSE and SSE2 Conversion Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13
Figure 11-9. Example Masked Response for Packed Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 11-24
Figure 12-1. Asymmetric Processing in ADDSUBPD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-2
Vol. 1 xix
CONTENTS
PAGE
Figure 12-2. Horizontal Data Movement in HADDPD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Figure 12-3. Horizontal Data Movement in PHADDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-10
Figure 12-4. MPSADBW Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25
Figure 13-1. Memory-Mapped I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-3
Figure 13-2. I/O Permission Bit Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6
Figure D-1. Recommended Circuit for MS-DOS Compatibility x87 FPU
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-7
Figure D-2. Behavior of Signals During x87 FPU Exception Handling . . . . . . . . . . . . . . . . . . . . . . . D-8
Figure D-3. Timing of Receipt of External Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-9
Figure D-4. Arithmetic Example Using Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-13
Figure D-5. General Program Flow for DNA Exception Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-26
Figure D-6. Program Flow for a Numeric Exception Dispatch Routine. . . . . . . . . . . . . . . . . . . . . . D-27
Figure E-1. Control Flow for Handling Unmasked Floating-Point Exceptions . . . . . . . . . . . . . . . . .E-6
xx Vol. 1
CONTENTS
PAGE
TABLES
Table 2-1. Key Features of Most Recent IA-32 Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28
Table 2-2. Key Features of Most Recent Intel 64 Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28
Table 2-3. Key Features of Previous Generations of IA-32 Processors . . . . . . . . . . . . . . . . . . . .2-32
Table 3-1. Instruction Pointer Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-12
Table 3-2. Addressable General Purpose Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-17
Table 3-3. Effective Operand- and Address-Size Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-25
Table 3-4. Effective Operand- and Address-Size Attributes in 64-Bit Mode. . . . . . . . . . . . . . . .3-26
Table 3-5. Default Segment Selection Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-29
Table 4-1. Signed Integer Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Table 4-2. Length, Precision, and Range of Floating-Point Data Types . . . . . . . . . . . . . . . . . . . . . 4-7
Table 4-3. Floating-Point Number and NaN Encodings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Table 4-4. Packed Decimal Integer Encodings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-15
Table 4-5. Real and Floating-Point Number Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-18
Table 4-6. Denormalization Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-21
Table 4-7. Rules for Handling NaNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-23
Table 4-8. Rounding Modes and Encoding of Rounding Control (RC) Field . . . . . . . . . . . . . . . . . .4-25
Table 4-10. Masked Responses to Numeric Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-30
Table 4-9. Numeric Overflow Thresholds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-30
Table 4-11. Numeric Underflow (Normalized) Thresholds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-31
Table 5-1. Instruction Groups and IA-32 Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1
Table 6-1. Exceptions and Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-14
Table 7-1. Move Instruction Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4
Table 7-2. Conditional Move Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6
Table 7-3. Bit Test and Modify Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-20
Table 7-4. Conditional Jump Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-23
Table 8-1. Condition Code Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8
Table 8-2. Precision Control Field (PC). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-12
Table 8-3. Unsupported Double Extended-Precision Floating-Point Encodings and Pseudo-
Denormals8-21
Table 8-4. Data Transfer Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-23
Table 8-5. Floating-Point Conditional Move Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-23
Table 8-6. Setting of x87 FPU Condition Code Flags for Floating-Point Number Comparisons. 8-
27
Table 8-7. Setting of EFLAGS Status Flags for Floating-Point Number Comparisons. . . . . . . .8-28
Table 8-8. TEST Instruction Constants for Conditional Branching . . . . . . . . . . . . . . . . . . . . . . . . .8-29
Table 8-9. Arithmetic and Non-arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-35
Table 8-10. Invalid Arithmetic Operations and the
Masked Responses to Them 8-38
Table 8-11. Divide-By-Zero Conditions and the Masked Responses to Them . . . . . . . . . . . . . . . .8-40
Table 9-1. Data Range Limits for Saturation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6
Table 9-2. MMX Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7
Table 9-3. Effect of Prefixes on MMX Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-15
Table 10-1. PREFETCHh Instructions Caching Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20
Table 11-1. Masked Responses of SSE/SSE2/SSE3 Instructions to Invalid Arithmetic Operations
11-20
Vol. 1 xxi
CONTENTS
PAGE
Table 11-2. SSE and SSE2 State Following a Power-up/Reset or INIT . . . . . . . . . . . . . . . . . . . . . 11-30
Table 11-3. Effect of Prefixes on SSE, SSE2, and SSE3 Instructions . . . . . . . . . . . . . . . . . . . . . . 11-37
Table 12-1. SIMD numeric exceptions signaled by SSE4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-15
Table 12-2. Enhanced 32-bit SIMD Multiply Supported by SSE4.1. . . . . . . . . . . . . . . . . . . . . . . . . 12-16
Table 12-3. Blend Field Size and Control Modes Supported by SSE4.1 . . . . . . . . . . . . . . . . . . . . 12-22
Table 12-4. Enhanced SIMD Integer MIN/MAX Instructions Supported by SSE4.1 . . . . . . . . . . 12-22
Table 12-5. New SIMD Integer conversions supported by SSE4.1 . . . . . . . . . . . . . . . . . . . . . . . . . 12-24
Table 12-6. New SIMD Integer Conversions Supported by SSE4.1 . . . . . . . . . . . . . . . . . . . . . . . . 12-24
Table 12-7. Enhanced SIMD Pack support by SSE4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-26
Table 13-1. I/O Instruction Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8
Table A-1. Codes Describing Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1
Table A-2. EFLAGS Cross-Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1
Table B-1. EFLAGS Condition Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-1
Table C-1. x87 FPU and SIMD Floating-Point Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .C-1
Table C-2. Exceptions Generated with x87 FPU Floating-Point Instructions. . . . . . . . . . . . . . . . .C-2
Table C-3. Exceptions Generated with SSE Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .C-4
Table C-4. Exceptions Generated with SSE2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .C-7
Table C-5. Exceptions Generated with SSE3 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-11
Table C-6. Exceptions Generated with SSE4 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-13
Table E-1. ADDPS, ADDSS, SUBPS, SUBSS, MULPS, MULSS, DIVPS, DIVSS, ADDPD, ADDSD,
SUBPD, SUBSD, MULPD, MULSD, DIVPD, DIVSD, ADDSUBPS, ADDSUBPD, HADDPS,
HADDPD, HSUBPS, HSUBPDE-8
Table E-2. CMPPS.EQ, CMPSS.EQ, CMPPS.ORD, CMPSS.ORD,
CMPPD.EQ, CMPSD.EQ, CMPPD.ORD, CMPSD.ORDE-9
Table E-3. CMPPS.NEQ, CMPSS.NEQ, CMPPS.UNORD, CMPSS.UNORD, CMPPD.NEQ, CMPSD.NEQ,
CMPPD.UNORD, CMPSD.UNORDE-9
Table E-4. CMPPS.LT, CMPSS.LT, CMPPS.LE, CMPSS.LE, CMPPD.LT, CMPSD.LT, CMPPD.LE,
CMPSD.LEE-9
Table E-5. CMPPS.NLT, CMPSS.NLT, CMPPS.NLE, CMPSS.NLE, CMPPD.NLT, CMPSD.NLT,
CMPPD.NLE, CMPSD.NLEE-10
Table E-6. COMISS, COMISD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-10
Table E-7. UCOMISS, UCOMISD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-10
Table E-8. CVTPS2PI, CVTSS2SI, CVTTPS2PI, CVTTSS2SI, CVTPD2PI, CVTSD2SI, CVTTPD2PI,
CVTTSD2SI, CVTPS2DQ, CVTTPS2DQ, CVTPD2DQ, CVTTPD2DQE-11
Table E-9. MAXPS, MAXSS, MINPS, MINSS, MAXPD, MAXSD, MINPD, MINSD . . . . . . . . . . . . . . . E-11
Table E-10. SQRTPS, SQRTSS, SQRTPD, SQRTSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-11
Table E-11. CVTPS2PD, CVTSS2SD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-12
Table E-12. CVTPD2PS, CVTSD2SS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-12
Table E-13. #I - Invalid Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-13
Table E-14. #Z - Divide-by-Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-16
Table E-15. #D - Denormal Operand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-17
Table E-16. #O - Numeric Overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-18
Table E-17. #U - Numeric Underflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-20
Table E-18. #P - Inexact Result (Precision) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . E-21
xxii Vol. 1
CHAPTER 1
ABOUT THIS MANUAL
Vol. 1 1-1
ABOUT THIS MANUAL
1-2 Vol. 1
ABOUT THIS MANUAL
Vol. 1 1-3
ABOUT THIS MANUAL
operate on basic data types, general-purpose and segment registers; also describes
system instructions that are executed in protected mode.
Chapter 8 — Programming with the x87 FPU. Describes the x87 floating-point
unit (FPU), including floating-point registers and data types; gives an overview of the
floating-point instruction set and describes the processor's floating-point exception
conditions.
Chapter 9 — Programming with Intel® MMX™ Technology. Describes Intel
MMX technology, including MMX registers and data types; also provides an overview
of the MMX instruction set.
Chapter 10 — Programming with Streaming SIMD Extensions (SSE).
Describes SSE extensions, including XMM registers, the MXCSR register, and packed
single-precision floating-point data types; provides an overview of the SSE instruc-
tion set and gives guidelines for writing code that accesses the SSE extensions.
Chapter 11 — Programming with Streaming SIMD Extensions 2 (SSE2).
Describes SSE2 extensions, including XMM registers and packed double-precision
floating-point data types; provides an overview of the SSE2 instruction set and gives
guidelines for writing code that accesses SSE2 extensions. This chapter also
describes SIMD floating-point exceptions that can be generated with SSE and SSE2
instructions. It also provides general guidelines for incorporating support for SSE and
SSE2 extensions into operating system and applications code.
Chapter 12 — Programming with SSE3, SSSE3 and SSE4. Provides an overview
of the SSE3 instruction set, Supplemental SSE3, SSE4, and guidelines for writing
code that accesses these extensions.
Chapter 13 — Input/Output. Describes the processor’s I/O mechanism, including
I/O port addressing, I/O instructions, and I/O protection mechanisms.
Chapter 14 — Processor Identification and Feature Determination. Describes
how to determine the CPU type and features available in the processor.
Appendix A — EFLAGS Cross-Reference. Summarizes how the IA-32 instructions
affect the flags in the EFLAGS register.
Appendix B — EFLAGS Condition Codes. Summarizes how conditional jump,
move, and ‘byte set on condition code’ instructions use condition code flags (OF, CF,
ZF, SF, and PF) in the EFLAGS register.
Appendix C — Floating-Point Exceptions Summary. Summarizes exceptions
raised by the x87 FPU floating-point and SSE/SSE2/SSE3 floating-point instructions.
Appendix D — Guidelines for Writing x87 FPU Exception Handlers. Describes
how to design and write MS-DOS* compatible exception handling facilities for FPU
exceptions (includes software and hardware requirements and assembly-language
code examples). This appendix also describes general techniques for writing robust
FPU exception handlers.
Appendix E — Guidelines for Writing SIMD Floating-Point Exception
Handlers. Gives guidelines for writing exception handlers for exceptions generated
by SSE/SSE2/SSE3 floating-point instructions.
1-4 Vol. 1
ABOUT THIS MANUAL
Data Structure
Highest
Address 32 24 23 16 15 8 7 0 Bit offset
28
24
20
16
12
8
4
Byte 3 Byte 2 Byte 1 Byte 0 0
Lowest
Address
Byte Offset
Vol. 1 1-5
ABOUT THIS MANUAL
• Do not depend on the ability to retain information written into any reserved bits.
• When loading a register, always load the reserved bits with the values indicated
in the documentation, if any, or reload them with values previously read from the
same register.
NOTE
Avoid any software dependence upon the state of reserved bits in
Intel 64 and IA-32 registers. Depending upon the values of reserved
register bits will make software dependent upon the unspecified
manner in which the processor handles these bits. Programs that
depend upon reserved values risk incompatibility with future
processors.
1-6 Vol. 1
ABOUT THIS MANUAL
Segment-register:Byte-address
For example, the following segment address identifies the byte at address FF79H in
the segment pointed by the DS register:
DS:FF79H
The following segment address identifies an instruction address in the code segment.
The CS register points to the code segment and the EIP register contains the address
of the instruction.
CS:EIP
Vol. 1 1-7
ABOUT THIS MANUAL
&38,',QSXWDQG2XWSXW
&38,'+(&;66(>ELW@
,QSXWYDOXHVIRU($; (&;UHJLVWHUV
,IRQO\RQHYDOXH($;LVLPSOLHG
2XWSXWUHJLVWHUDQGIHDWXUHIODJRUILHOG
QDPHZLWKELWSRVLWLRQV
9DOXHRUUDQJHRIRXWSXW
&RQWURO5HJLVWHU9DOXHV
&526);65>ELW@
([DPSOH&5QDPH
)HDWXUHIODJRUILHOGQDPH
ZLWKELWSRVLWLRQV
9DOXHRUUDQJHRIRXWSXW
0RGHO6SHFLILF5HJLVWHU9DOXHV
,$B0,6&B(1$%/(6(1$%/()23&2'(>ELW@
([DPSOH065QDPH
)HDWXUHIODJRUILHOGQDPHZLWKELWSRVLWLRQV
9DOXHRUUDQJHRIRXWSXW
20
Figure 1-2. Syntax for CPUID, CR, and MSR Data Presentation
1.3.6 Exceptions
An exception is an event that typically occurs when an instruction causes an error.
For example, an attempt to divide by zero generates an exception. However, some
exceptions, such as breakpoints, occur under other conditions. Some types of excep-
tions may provide error codes. An error code reports additional information about the
error. An example of the notation used to show an exception and error code is shown
below:
#PF(fault code)
1-8 Vol. 1
ABOUT THIS MANUAL
This example refers to a page-fault exception under conditions where an error code
naming a type of fault is reported. Under some conditions, exceptions that produce
error codes may not be able to report an accurate code. In this case, the error code
is zero, as shown below for a general-protection exception:
#GP(0)
Vol. 1 1-9
ABOUT THIS MANUAL
1-10 Vol. 1
CHAPTER 2
®
INTEL 64 AND IA-32 ARCHITECTURES
The exponential growth of computing power and ownership has made the computer
one of the most important forces shaping business and society. Intel 64 and IA-32
architectures have been at the forefront of the computer revolution and is today the
preferred computer architecture, as measured by computers in use and the total
computing power available in the world.
Vol. 1 2-1
INTEL® 64 AND IA-32 ARCHITECTURES
2-2 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
• Extensions to make the virtual-8086 mode more efficient and allow for 4-MByte
as well as 4-KByte pages
• Internal data paths of 128 and 256 bits add speed to internal data transfers
• Burstable external data bus was increased to 64 bits
• An APIC to support systems with multiple processors
• A dual processor mode to support glueless two processor systems
A subsequent stepping of the Pentium family introduced Intel MMX technology (the
Pentium Processor with MMX technology). Intel MMX technology uses the single-
instruction, multiple-data (SIMD) execution model to perform parallel computations
on packed integer data contained in 64-bit registers.
See Section 2.2.6, “SIMD Instructions.”
Vol. 1 2-3
INTEL® 64 AND IA-32 ARCHITECTURES
• The Intel Pentium III processor introduced the Streaming SIMD Extensions
(SSE) to the IA-32 architecture. SSE extensions expand the SIMD execution
model introduced with the Intel MMX technology by providing a new set of 128-
bit registers and the ability to perform SIMD operations on packed single-
precision floating-point values. See Section 2.2.6, “SIMD Instructions.”
• The Pentium III Xeon processor extended the performance levels of the IA-32
processors with the enhancement of a full-speed, on-die, and Advanced Transfer
Cache.
2-4 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
2.1.11 The Intel® Core™ Duo and Intel® Core™ Solo Processors
(2006-2007)
The Intel Core Duo processor offers power-efficient, dual-core performance with a
low-power design that extends battery life. This family and the single-core Intel Core
Vol. 1 2-5
INTEL® 64 AND IA-32 ARCHITECTURES
2.1.13 The Intel® Xeon® Processor 5200, 5400, 7400 Series and
Intel® Core™2 Processor Family (2007-Current)
The Intel Xeon processor 5200, 5400, and 7400 series, Intel Core 2 Quad processor
Q9000 Series, Intel Core 2 Duo processor E8000 series support Intel 64 architecture;
they are based on the Enhanced Intel® Core microarchitecture using 45 nm process
2-6 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Vol. 1 2-7
INTEL® 64 AND IA-32 ARCHITECTURES
2-8 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
System Bus
Frequently used
Bus Unit Less frequently used
Front End
Execution
Instruction Execution
Fetch/
Cache Out-of-Order Retirement
Decode
Microcode Core
ROM
OM16520
To ensure a steady supply of instructions and data for the instruction execution pipe-
line, the P6 processor microarchitecture incorporates two cache levels. The Level 1
cache provides an 8-KByte instruction cache and an 8-KByte data cache, both closely
coupled to the pipeline. The Level 2 cache provides 256-KByte, 512-KByte, or
1-MByte static RAM that is coupled to the core processor through a full clock-speed
64-bit cache bus.
The centerpiece of the P6 processor microarchitecture is an out-of-order execution
mechanism called dynamic execution. Dynamic execution incorporates three data-
processing concepts:
• Deep branch prediction allows the processor to decode instructions beyond
branches to keep the instruction pipeline full. The P6 processor family
implements highly optimized branch prediction algorithms to predict the direction
of the instruction.
• Dynamic data flow analysis requires real-time analysis of the flow of data
through the processor to determine dependencies and to detect opportunities for
out-of-order instruction execution. The out-of-order execution core can monitor
Vol. 1 2-9
INTEL® 64 AND IA-32 ARCHITECTURES
many instructions and execute these instructions in the order that best optimizes
the use of the processor’s multiple execution units, while maintaining the data
integrity.
• Speculative execution refers to the processor’s ability to execute instructions
that lie beyond a conditional branch that has not yet been resolved, and
ultimately to commit the results in the order of the original instruction stream. To
make speculative execution possible, the P6 processor microarchitecture
decouples the dispatch and execution of instructions from the commitment of
results. The processor’s out-of-order execution core uses data-flow analysis to
execute all available instructions in the instruction pool and store the results in
temporary registers. The retirement unit then linearly searches the instruction
pool for completed instructions that no longer have data dependencies with other
instructions or unresolved branch predictions. When completed instructions are
found, the retirement unit commits the results of these instructions to memory
and/or the IA-32 registers (the processor’s eight general-purpose registers and
eight x87 FPU data registers) in the order they were originally issued and retires
the instructions from the instruction pool.
1. Intel 64 and IA-32 processors based on the Intel NetBurst microarchitecture at 90 nm process
can handle more than 24 stores in flight.
2-10 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Vol. 1 2-11
INTEL® 64 AND IA-32 ARCHITECTURES
System Bus
Frequently used paths
Front End
Execution
Trace Cache
Fetch/Decode Out-Of-Order Retirement
Microcode ROM
Core
OM16521
2-12 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Vol. 1 2-13
INTEL® 64 AND IA-32 ARCHITECTURES
2-14 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Instruction Q ueue
M icro-
code D ecode
ROM
S hared L2 C ache
R enam e/A lloc U p to 10.7 G B /s
FS B
S cheduler
A LU A LU A LU
B ranch FA dd FM ul Load S tore
M M X /S S E /FP M M X /S S E M M X/S S E
M ove
Vol. 1 2-15
INTEL® 64 AND IA-32 ARCHITECTURES
2-16 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
— 256 bit internal data path between L2 and L1 data cache improves high
bandwidth.
• Efficient Memory Access
— Efficient hardware prefetchers to L1 and L2, speculatively loading data likely
to be requested by processor to reduce cache miss impact.
• Intel® Digital Media Boost
— Two issue ports for dispatching SIMD instructions to execution units.
— Single-cycle throughput for most 128-bit integer SIMD instructions
— Up to six floating-point operations per cycle
— Up to two 128-bit SIMD integer operations per cycle
— Safe Instruction Recognition (SIR) to allow long-latency floating-point
operations to retire out of order with respect to integer instructions.
Vol. 1 2-17
INTEL® 64 AND IA-32 ARCHITECTURES
2-18 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
SSSE3 extensions were introduced with the Intel Xeon processor 5100 series and
Intel Core 2 processor family. SSSE3 offer 32 instructions to accelerate processing of
SIMD integer data.
SSE4 extensions offer 54 instructions. 47 of them are referred to as SSE4.1 instruc-
tions. SSE4.1 are introduced with Intel Xeon processor 5400 series and Intel Core 2
Extreme processor QX9650. The other 7 SSE4 instructions are referred to as SSE4.2
instructions.
Intel 64 architecture allows four generations of 128-bit SIMD extensions to access up
to 16 XMM registers. IA-32 architecture provides 8 XMM registers.
See also:
• Section 5.4, “MMX™ Instructions,” and Chapter 9, “Programming with Intel®
MMX™ Technology”
• Section 5.5, “SSE Instructions,” and Chapter 10, “Programming with Streaming
SIMD Extensions (SSE)”
• Section 5.6, “SSE2 Instructions,” and Chapter 11, “Programming with Streaming
SIMD Extensions 2 (SSE2)”
• Section 5.7, “SSE3 Instructions”, Section 5.8, “Supplemental Streaming SIMD
Extensions 3 (SSSE3) Instructions”, Section 5.9, “SSE4 Instructions”, and
Chapter 12, “Programming with SSE3, SSSE3, AND SSE4”
Vol. 1 2-19
INTEL® 64 AND IA-32 ARCHITECTURES
MMX Registers
MMX Technology 8 Packed Byte Integers
Quadword
MMX Registers
SSE 8 Packed Byte Integers
Quadword
XMM Registers
4 Packed Single-Precision
Floating-Point Values
MMX Registers
SSE2/SSE3/SSSE3 2 Packed Doubleword Integers
Quadword
XMM Registers
2 Packed Double-Precision
Floating-Point Values
4 Packed Doubleword
Integers
2 Quadword Integers
Double Quadword
2-20 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
AS AS AS AS
Vol. 1 2-21
INTEL® 64 AND IA-32 ARCHITECTURES
execution engine and the system bus interface. After power up and initialization,
each logical processor can be independently directed to execute a specified thread,
interrupted, or halted.
Intel HT Technology leverages the process and thread-level parallelism found in
contemporary operating systems and high-performance applications by providing
two or more logical processors on a single chip. This configuration allows two or more
threads1 to be executed simultaneously on each a physical processor. Each logical
processor executes instructions from an application thread using the resources in the
processor core. The core executes these threads concurrently, using out-of-order
instruction scheduling to maximize the use of execution units during each clock cycle.
1. In the remainder of this document, the term “thread” will be used as a general term for the terms
“process” and “thread.”
2-22 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
ware multi-threading support with both two processor cores and Intel Hyper-
Threading Technology. This means that the Intel Pentium processor Extreme Edition
provides four logical processors in a physical package (two logical processors for
each processor core). The Dual-Core Intel Xeon processor features multi-core, Intel
Hyper-Threading Technology and supports multi-processor platforms.
The Intel Pentium D processor also features multi-core technology. This processor
provides hardware multi-threading support with two processor cores but does not
offer Intel Hyper-Threading Technology. This means that the Intel Pentium D
processor provides two logical processors in a physical package, with each logical
processor owning the complete execution resources of a processor core.
The Intel Core 2 processor family, Intel Xeon processor 3000 series, Intel Xeon
processor 5100 series, and Intel Core Duo processor offer power-efficient multi-core
technology. The processor contains two cores that share a smart second level cache.
The Level 2 cache enables efficient data sharing between two cores to reduce
memory traffic to the system bus.
Vol. 1 2-23
INTEL® 64 AND IA-32 ARCHITECTURES
OM19809
System Bus
The Pentium® dual-core processor is based on the same technology as the Intel Core
2 Duo processor family.
The Intel Xeon processor 7300, 5300 and 3200 series, Intel Core 2 Extreme Quad-
Core processor, and Intel Core 2 Quad processors support Intel quad-core tech-
nology. The Quad-core Intel Xeon processors and the Quad-Core Intel Core 2
processor family are also in Figure 2-7.
2-24 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
System Bus
OM19810
Vol. 1 2-25
INTEL® 64 AND IA-32 ARCHITECTURES
IMC
QPI
DDR3
Chipset
OM19810b
2-26 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Vol. 1 2-27
INTEL® 64 AND IA-32 ARCHITECTURES
transfer cache are shown in Table 2-1. Older generation IA-32 processors, which do
not employ on-die Level 2 cache, are shown in Table 2-2.
Table 2-1. Key Features of Most Recent IA-32 Processors
Intel Date Micro- Top-Bin Tran- Register Syste Max. On-Die
Processor Intro- architecture Clock Fre- sistors Sizes1 m Bus Extern. Caches2
duced quency at Band- Addr.
Intro- width Space
duction
Intel Pentium M 2004 Intel Pentium M 2.00 GHz 140 M GP: 32 3.2 GB/s 4 GB L1: 64 KB
Processor 7553 Processor FPU: 80 L2: 2 MB
MMX: 64
XMM: 128
Intel Core Duo 2006 Improved Intel Pentium 2.16 GHz 152M GP: 32 5.3 GB/s 4 GB L1: 64 KB
Processor M Processor FPU: 80 L2: 2 MB (2MB
T26003 Microarchitecture; Dual MMX: 64 Total)
Core; XMM: 128
Intel Smart Cache,
Advanced Thermal
Manager
Intel Atom 2008 Intel Atom 1.86 GHz - 800 47M GP: 32 Up to 4.2 4 GB L1: 56 KB4
Processor Z5xx Microarchitecture; MHz FPU: 80 GB/s L2: 512KB
series Intel Virtualization MMX: 64
Technology. XMM: 128
NOTES:
1. The register size and external data bus size are given in bits.
2. First level cache is denoted using the abbreviation L1, 2nd level cache is denoted as L2. The size
of L1 includes the first-level data cache and the instruction cache where applicable, but
does not include the trace cache.
3. Intel processor numbers are not a measure of performance. Processor numbers differentiate
features within each processor family, not across different processor families.
See http://www.intel.com/products/processor_number for details.
4. In Intel Atom Processor, the size of L1 instruction cache is 32 KBytes, L1 data cache is 24 KBytes.
64-bit Intel Xeon 2005 Intel NetBurst 3.33 GHz 675M GP: 32, 64 5.3 GB/s 1 1024 GB 12K µop
Processor MP Microarchitecture; FPU: 80 (1 TB) Execution
with 8MB L3 Intel Hyper-Threading MMX: 64 Trace Cache;
Technology; Intel 64 XMM: 128 16 KB L1;
Architecture 1 MB L2,
8 MB L3
2-28 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
Intel Pentium 2005 Intel NetBurst 3.20 GHz 230 M GP: 32, 64 6.4 GB/s 64 GB 12K µop
Processor Microarchitecture; FPU: 80 Execution
Extreme Edition Intel Hyper-Threading MMX: 64 Trace Cache;
840 Technology; Intel 64 XMM: 128 16 KB L1;
Architecture; 1MB L2 (2MB
Dual-core 2 Total)
Dual-Core Intel 2005 Intel NetBurst 3.00 GHz 321M GP: 32, 64 6.4 GB/s 64 GB 12K µop
Xeon Microarchitecture; FPU: 80 Execution
Processor 7041 Intel Hyper-Threading MMX: 64 Trace Cache;
Technology; Intel 64 XMM: 128 16 KB L1;
Architecture; 2MB L2 (4MB
Dual-core 3 Total)
Intel Pentium 4 2005 Intel NetBurst 3.80 GHz 164 M GP: 32, 64 6.4 GB/s 64 GB 12K µop
Processor 672 Microarchitecture; FPU: 80 Execution
Intel Hyper-Threading MMX: 64 Trace Cache;
Technology; Intel 64 XMM: 128 16 KB L1;
Architecture; 2MB L2
Intel Virtualization
Technology.
Intel Pentium 2006 Intel NetBurst 3.46 GHz 376M GP: 32, 64 8.5 GB/s 64 GB 12K µop
Processor Microarchitecture; FPU: 80 Execution
Extreme Edition Intel 64 Architecture; MMX: 64 Trace Cache;
955 Dual Core; XMM: 128 16 KB L1;
Intel Virtualization 2MB L2
Technology. (4MB Total)
Intel Core 2 2006 Intel Core 2.93 GHz 291M GP: 32,64 8.5 GB/s 64 GB L1: 64 KB
Extreme Microarchitecture; FPU: 80 L2: 4MB (4MB
Processor Dual Core; MMX: 64 Total)
X6800 Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology.
Intel Xeon 2006 Intel Core 3.00 GHz 291M GP: 32, 64 10.6 GB/s 64 GB L1: 64 KB
Processor 5160 Microarchitecture; FPU: 80 L2: 4MB (4MB
Dual Core; MMX: 64 Total)
Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology.
Intel Xeon 2006 Intel NetBurst 3.40 GHz 1.3 B GP: 32, 64 12.8 GB/s 64 GB L1: 64 KB
Processor 7140 Microarchitecture; FPU: 80 L2: 1MB (2MB
Dual Core; MMX: 64 Total)
Intel 64 Architecture; XMM: 128 L3: 16 MB
Intel Virtualization (16MB Total)
Technology.
Intel Core 2 2006 Intel Core 2.66 GHz 582M GP: 32,64 8.5 GB/s 64 GB L1: 64 KB
Extreme Microarchitecture; FPU: 80 L2: 4MB (4MB
Processor Quad Core; MMX: 64 Total)
QX6700 Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology.
Vol. 1 2-29
INTEL® 64 AND IA-32 ARCHITECTURES
Intel Core 2 Duo 2007 Intel Core 3.00 GHz 291 M GP: 32, 64 10.6 GB/s 64 GB L1: 64 KB
Processor Microarchitecture; FPU: 80 L2: 4MB (4MB
E6850 Dual Core; MMX: 64 Total)
Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology;
Intel Trusted
Execution Technology
Intel Xeon 2007 Intel Core 2.93 GHz 582 M GP: 32, 64 8.5 GB/s 1024 GB L1: 64 KB
Processor 7350 Microarchitecture; FPU: 80 L2: 4MB (8MB
Quad Core; MMX: 64 Total)
Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology.
Intel Xeon 2007 Enhanced Intel Core 3.00 GHz 820 M GP: 32, 64 12.8 GB/s 256 GB L1: 64 KB
Processor 5472 Microarchitecture; FPU: 80 L2: 6MB
Quad Core; MMX: 64 (12MB Total)
Intel 64 Architecture; XMM: 128
Intel Virtualization
Technology.
Intel Atom 2008 Intel Atom 2.0 - 1.60 GHz 47 M GP: 32, 64 Up to 4.2 Up to 64GB L1: 56 KB4
Processor Microarchitecture; FPU: 80 GB/s L2: 512KB
Intel 64 Architecture; MMX: 64
Intel Virtualization XMM: 128
Technology.
Intel Xeon 2008 Enhanced Intel Core 2.67 GHz 1.9 B GP: 32, 64 8.5 GB/s 1024 GB L1: 64 KB
Processor 7460 Microarchitecture; Six FPU: 80 L2: 3MB (9MB
Cores; MMX: 64 Total)
Intel 64 Architecture; XMM: 128 L3: 16MB
Intel Virtualization
Technology.
Intel Atom 2008 Intel Atom 1.60 GHz 94 M GP: 32, 64 Up to 4.2 Up to 64GB L1: 56 KB5
Processor 330 Microarchitecture; FPU: 80 GB/s L2: 512KB
Intel 64 Architecture; MMX: 64 (1MB Total)
Dual core; XMM: 128
Intel Virtualization
Technology.
Intel Core i7-965 2008 Intel microarchitecture 3.20 GHz 731 M GP: 32, 64 QPI: 6.4 64 GB L1: 64 KB
Processor (Nehalem); Quadcore; FPU: 80 GT/s; L2: 256KB
Extreme Edition HyperThreading MMX: 64 Memory: L3: 8MB
Technology; Intel QPI; XMM: 128 25.6 GB/s
Intel 64 Architecture;
Intel Virtualization
Technology.
NOTES:
1. The 64-bit Intel Xeon Processor MP with an 8-MByte L3 supports a multi-processor platform with a
dual system bus; this creates a platform bandwidth with 10.6 GBytes.
2. In Intel Pentium Processor Extreme Edition 840, the size of on-die cache is listed for each core. The
total size of L2 in the physical package in 2 MBytes.
2-30 Vol. 1
INTEL® 64 AND IA-32 ARCHITECTURES
3. In Dual-Core Intel Xeon Processor 7041, the size of on-die cache is listed for each core. The total
size of L2 in the physical package in 4 MBytes.
4. In Intel Atom Processor, the size of L1 instruction cache is 32 KBytes, L1 data cache is 24 KBytes.
5. In Intel Atom Processor, the size of L1 instruction cache is 32 KBytes, L1 data cache is 24 KBytes.
Vol. 1 2-31
INTEL® 64 AND IA-32 ARCHITECTURES
Intel Xeon Processor 2001 1.70 GHz, Intel NetBurst 42 M 32 GP 64 64 GB 12K µop
Microarchitecture 80 FPU Execution Trace
64 MMX Cache; L1: 8KB
128 XMM L2: 512KB
Intel Xeon Processor 2002 2.20 GHz, Intel NetBurst 55 M 32 GP 64 64 GB 12K µop
Microarchitecture, 80 FPU Execution Trace
HyperThreading 64 MMX Cache; L1: 8KB
Technology 128 XMM L2: 512KB
Intel Pentium 4 2004 3.40 GHz, Intel NetBurst 125 M 32 GP 64 64 GB 12K µop
Processor Supporting Microarchitecture, 80 FPU Execution Trace
Hyper-Threading HyperThreading 64 MMX Cache; L1: 16KB
Technology at 90 nm Technology 128 XMM L2: 1 MB
process
NOTE:
1. The register size and external data bus size are given in bits. Note also that each 32-bit general-
purpose (GP) registers can be addressed as an 8- or a 16-bit data registers in all of the processors.
2. Internal data paths are 2 to 4 times wider than the external data bus for each processor.
2-32 Vol. 1
CHAPTER 3
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-1
BASIC EXECUTION ENVIRONMENT
3-2 Vol. 1
BASIC EXECUTION ENVIRONMENT
resources (described briefly in the following paragraphs and shown in Figure 3-1)
make up the basic execution environment for an IA-32 processor.
An Intel 64 processor supports the basic execution environment of an IA-32
processor, and a similar environment under IA-32e mode that can execute 64-bit
programs (64-bit sub-mode) and 32-bit programs (compatibility sub-mode).
The basic execution environment is used jointly by the application programs and the
operating system or executive running on the processor.
• Address space — Any task or program running on an IA-32 processor can
address a linear address space of up to 4 GBytes (232 bytes) and a physical
address space of up to 64 GBytes (236 bytes). See Section 3.3.6, “Extended
Physical Addressing in Protected Mode,” for more information about addressing
an address space greater than 4 GBytes.
• Basic program execution registers — The eight general-purpose registers,
the six segment registers, the EFLAGS register, and the EIP (instruction pointer)
register comprise a basic execution environment in which to execute a set of
general-purpose instructions. These instructions perform basic integer arithmetic
on byte, word, and doubleword integers, handle program flow control, operate on
bit and byte strings, and address memory. See Section 3.4, “Basic Program
Execution Registers,” for more information about these registers.
• x87 FPU registers — The eight x87 FPU data registers, the x87 FPU control
register, the status register, the x87 FPU instruction pointer register, the x87 FPU
operand (data) pointer register, the x87 FPU tag register, and the x87 FPU opcode
register provide an execution environment for operating on single-precision,
double-precision, and double extended-precision floating-point values, word
integers, doubleword integers, quadword integers, and binary coded decimal
(BCD) values. See Section 8.1, “x87 FPU Execution Environment,” for more
information about these registers.
• MMX registers — The eight MMX registers support execution of single-
instruction, multiple-data (SIMD) operations on 64-bit packed byte, word, and
doubleword integers. See Section 9.2, “The MMX Technology Programming
Environment,” for more information about these registers.
• XMM registers — The eight XMM data registers and the MXCSR register support
execution of SIMD operations on 128-bit packed single-precision and double-
precision floating-point values and on 128-bit packed byte, word, doubleword,
and quadword integers. See Section 10.2, “SSE Programming Environment,” for
more information about these registers.
Vol. 1 3-3
BASIC EXECUTION ENVIRONMENT
Six 16-bit
Segment Registers
Registers
FPU Registers
MMX Registers
Eight 64-bit
Registers MMX Registers
XMM Registers
3-4 Vol. 1
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-5
BASIC EXECUTION ENVIRONMENT
• x87 FPU registers — See Chapter 8, “Programming with the x87 FPU.”
• MMX Registers — See Chapter 9, “Programming with Intel® MMX™
Technology.”
• XMM registers — See Chapter 10, “Programming with Streaming SIMD
Extensions (SSE),” Chapter 11, “Programming with Streaming SIMD Extensions 2
(SSE2),” and Chapter 12, “Programming with SSE3, SSSE3, AND SSE4.”
• Stack implementation and procedure calls — See Chapter 6, “Procedure
Calls, Interrupts, and Exceptions.”
3-6 Vol. 1
BASIC EXECUTION ENVIRONMENT
hold a full 64-bit base address. The local descriptor table register (LDTR) and the
task register (TR) also expand to hold a full 64-bit base address.
Six 16-bit
Segment Registers
Registers
FPU Registers
MMX Registers
XMM Registers
Sixteen 128-bit
Registers XMM Registers
Vol. 1 3-7
BASIC EXECUTION ENVIRONMENT
3-8 Vol. 1
BASIC EXECUTION ENVIRONMENT
segment prevents the stack from growing into the code or data space and
overwriting instructions or data, respectively.
• Real-address mode memory model — This is the memory model for the Intel
8086 processor. It is supported to provide compatibility with existing programs
written to run on the Intel 8086 processor. The real-address mode uses a specific
implementation of segmented memory in which the linear address space for the
program and the operating system/executive consists of an array of segments of
up to 64 KBytes in size each. The maximum size of the linear address space in
real-address mode is 220 bytes.
See also: Chapter 15, “8086 Emulation,” Intel® 64 and IA-32 Architectures
Software Developer’s Manual, Volume 3A.
Flat Model
Linear Address
Linear
Address
Space*
Segmented Model
Segments
Vol. 1 3-9
BASIC EXECUTION ENVIRONMENT
3-10 Vol. 1
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-11
BASIC EXECUTION ENVIRONMENT
Generally, displacements and immediates in 64-bit mode are not extended to 64 bits.
They are still limited to 32 bits and sign-extended during effective-address calcula-
tions. In 64-bit mode, however, support is provided for 64-bit displacement and
immediate forms of the MOV instruction.
All 16-bit and 32-bit address calculations are zero-extended in IA-32e mode to form
64-bit addresses. Address calculations are first truncated to the effective address
3-12 Vol. 1
BASIC EXECUTION ENVIRONMENT
size of the current mode (64-bit mode or compatibility mode), as overridden by any
address-size prefix. The result is then zero-extended to the full 64-bit address width.
Because of this, 16-bit and 32-bit applications running in compatibility mode can
access only the low 4 GBytes of the 64-bit mode effective addresses. Likewise, a
32-bit address generated in 64-bit mode can access only the low 4 GBytes of the
64-bit mode effective addresses.
Vol. 1 3-13
BASIC EXECUTION ENVIRONMENT
• EFLAGS (program status and control) register. The EFLAGS register report
on the status of the program being executed and allows limited (application-
program level) control of the processor.
• EIP (instruction pointer) register. The EIP register contains a 32-bit pointer
to the next instruction to be executed.
3-14 Vol. 1
BASIC EXECUTION ENVIRONMENT
General-Purpose Registers
31 0
EAX
EBX
ECX
EDX
ESI
EDI
EBP
ESP
Segment Registers
15 0
CS
DS
SS
ES
FS
GS
Instruction Pointer 0
31
EIP
Vol. 1 3-15
BASIC EXECUTION ENVIRONMENT
General-Purpose Registers
31 16 15 8 7 0 16-bit 32-bit
AH AL AX EAX
BH BL BX EBX
CH CL CX ECX
DH DL DX EDX
BP EBP
SI ESI
DI EDI
SP ESP
3-16 Vol. 1
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-17
BASIC EXECUTION ENVIRONMENT
Linear Address
Space for Program
3-18 Vol. 1
BASIC EXECUTION ENVIRONMENT
Code
Segment
Segment Registers
Data
CS Segment
DS Stack
SS Segment
ES All segments
FS are mapped
GS to the same
linear-address
space
Data
Segment
Data
Segment
Data
Segment
Each of the segment registers is associated with one of three types of storage: code,
data, or stack. For example, the CS register contains the segment selector for the
code segment, where the instructions being executed are stored. The processor
fetches instructions from the code segment, using a logical address that consists of
the segment selector in the CS register and the contents of the EIP register. The EIP
register contains the offset within the code segment of the next instruction to be
executed. The CS register cannot be loaded explicitly by an application program.
Instead, it is loaded implicitly by instructions or internal processor operations that
change program control (such as, procedure calls, interrupt handling, or task
switching).
The DS, ES, FS, and GS registers point to four data segments. The availability of
four data segments permits efficient and secure access to different types of data
structures. For example, four separate data segments might be created: one for the
data structures of the current module, another for the data exported from a higher-
level module, a third for a dynamically created data structure, and a fourth for data
shared with another program. To access additional data segments, the application
program must load segment selectors for these segments into the DS, ES, FS, and
GS registers, as needed.
The SS register contains the segment selector for the stack segment, where the
procedure stack is stored for the program, task, or handler currently being executed.
All stack operations use the SS register to find the stack segment. Unlike the CS
register, the SS register can be loaded explicitly, which permits application programs
to set up multiple stacks and switch among them.
Vol. 1 3-19
BASIC EXECUTION ENVIRONMENT
See Section 3.3, “Memory Organization,” for an overview of how the segment regis-
ters are used in real-address mode.
The four segment registers CS, DS, SS, and ES are the same as the segment regis-
ters found in the Intel 8086 and Intel 286 processors and the FS and GS registers
were introduced into the IA-32 Architecture with the Intel386™ family of processors.
3-20 Vol. 1
BASIC EXECUTION ENVIRONMENT
an interrupt or exception is handled with a task switch, the state of the EFLAGS
register is saved in the TSS for the task being suspended.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I
V V O
I I I A V R 0 N O D I T S Z A P C
0 0 0 0 0 0 0 0 0 0 T F F F F F F 0 F 0 F 1 F
D C M F P
P F
L
X ID Flag (ID)
X Virtual Interrupt Pending (VIP)
X Virtual Interrupt Flag (VIF)
X Alignment Check (AC)
X Virtual-8086 Mode (VM)
X Resume Flag (RF)
X Nested Task (NT)
X I/O Privilege Level (IOPL)
S Overflow Flag (OF)
C Direction Flag (DF)
X Interrupt Enable Flag (IF)
X Trap Flag (TF)
S Sign Flag (SF)
S Zero Flag (ZF)
S Auxiliary Carry Flag (AF)
S Parity Flag (PF)
S Carry Flag (CF)
As the IA-32 Architecture has evolved, flags have been added to the EFLAGS register,
but the function and placement of existing flags have remained the same from one
family of the IA-32 processors to the next. As a result, code that accesses or modifies
these flags for one family of IA-32 processors works as expected when run on later
families of processors.
Vol. 1 3-21
BASIC EXECUTION ENVIRONMENT
3.4.3.2 DF Flag
The direction flag (DF, located in bit 10 of the EFLAGS register) controls string
instructions (MOVS, CMPS, SCAS, LODS, and STOS). Setting the DF flag causes the
string instructions to auto-decrement (to process strings from high addresses to low
addresses). Clearing the DF flag causes the string instructions to auto-increment
(process strings from low addresses to high addresses).
The STD and CLD instructions set and clear the DF flag, respectively.
3-22 Vol. 1
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-23
BASIC EXECUTION ENVIRONMENT
3-24 Vol. 1
BASIC EXECUTION ENVIRONMENT
The operand-size attribute selects the size of operands. When the 16-bit operand-
size attribute is in force, operands can generally be either 8 bits or 16 bits, and when
the 32-bit operand-size attribute is in force, operands can generally be 8 bits or 32
bits.
The address-size attribute selects the sizes of addresses used to address memory:
16 bits or 32 bits. When the 16-bit address-size attribute is in force, segment offsets
and displacements are 16 bits. This restriction limits the size of a segment to 64
KBytes. When the 32-bit address-size attribute is in force, segment offsets and
displacements are 32 bits, allowing up to 4 GBytes to be addressed.
The default operand-size attribute and/or address-size attribute can be overridden
for a particular instruction by adding an operand-size and/or address-size prefix to
an instruction. See Chapter 2, “Instruction Format,” in the Intel® 64 and IA-32 Archi-
tectures Software Developer’s Manual, Volume 3A. The effect of this prefix applies
only to the targeted instruction.
Table 3-4 shows effective operand size and address size (when executing in
protected mode or compatibility mode) depending on the settings of the D flag and
the operand-size and address-size prefixes.
Vol. 1 3-25
BASIC EXECUTION ENVIRONMENT
In the case of SSE/SSE2/SSE3/SSSE3 SIMD instructions: the 66H, F2H, and F3H
prefixes are mandatory for opcode extensions. In such a case, there is no interaction
between a valid REX.W prefix and a 66H opcode extension prefix.
See Chapter 2, “Instruction Format,” in the Intel® 64 and IA-32 Architectures Soft-
ware Developer’s Manual, Volume 3A.
3-26 Vol. 1
BASIC EXECUTION ENVIRONMENT
ADD EAX, 14
All arithmetic instructions (except the DIV and IDIV instructions) allow the source
operand to be an immediate value. The maximum value allowed for an immediate
operand varies among instructions, but can never be greater than the maximum
value of an unsigned doubleword integer (232).
Vol. 1 3-27
BASIC EXECUTION ENVIRONMENT
15 0 31 0
Segment Offset (or Linear Address)
Selector
3-28 Vol. 1
BASIC EXECUTION ENVIRONMENT
15 0 63 0
Segment Offset (or Linear Address)
Selector
Vol. 1 3-29
BASIC EXECUTION ENVIRONMENT
MOV DS, BX
Segment selectors can also be specified explicitly as part of a 48-bit far pointer in
memory. Here, the first doubleword in memory contains the offset and the next word
contains the segment selector.
3-30 Vol. 1
BASIC EXECUTION ENVIRONMENT
EAX
EAX None
EBX 1
EBX
ECX
ECX 2 8-bit
EDX
+ EDX +
ESP * 16-bit
EBP 4
EBP
ESI
ESI 8 32-bit
EDI
EDI
Vol. 1 3-31
BASIC EXECUTION ENVIRONMENT
created when a procedure is entered. Here, the EBP register is the best choice for
the base register, because it automatically selects the stack segment. This is a
compact encoding for this common function.
• (Index ∗ Scale) + Displacement ⎯ This address mode offers an efficient way
to index into a static array when the element size is 2, 4, or 8 bytes. The
displacement locates the beginning of the array, the index register holds the
subscript of the desired array element, and the processor automatically converts
the subscript into an index by applying the scaling factor.
• Base + Index + Displacement ⎯ Using two registers together supports either
a two-dimensional array (the displacement holds the address of the beginning of
the array) or one of several instances of an array of records (the displacement is
an offset to a field within the record).
• Base + (Index ∗ Scale) + Displacement ⎯ Using all the addressing
components together allows efficient indexing of a two-dimensional array when
the elements of the array are 2, 4, or 8 bytes in size.
3-32 Vol. 1
BASIC EXECUTION ENVIRONMENT
Vol. 1 3-33
BASIC EXECUTION ENVIRONMENT
3-34 Vol. 1
CHAPTER 4
DATA TYPES
This chapter introduces data types defined for the Intel 64 and IA-32 architectures.
A section at the end of this chapter describes the real-number and floating-point
concepts used in x87 FPU, SSE, SSE2, SSE3 and SSSE3 extensions.
7 0
Byte
N
15 8 7 0
High Low
Byte Byte Word
N+1 N
31 16 15 0
High Word Low Word Doubleword
N+2 N
63 32 31 0
High Doubleword Low Doubleword Quadword
N+4 N
127 64 63 0
High Quadword Low Quadword Double
Quadword
N+8 N
The quadword data type was introduced into the IA-32 architecture in the Intel486
processor; the double quadword data type was introduced in the Pentium III
processor with the SSE extensions.
Figure 4-2 shows the byte order of each of the fundamental data types when refer-
enced as operands in memory. The low byte (bits 0 through 7) of each data type
occupies the lowest address in memory and that address is also the address of the
operand.
Vol. 1 4-1
DATA TYPES
4EH FH
12H EH
7AH DH
36H AH
Byte at Address 9H
1FH 9H
Contains 1FH Quadword at Address 6H
A4H 8H Contains
7AFE06361FA4230BH
Word at Address 6H 23H 7H
Contains 230BH 0BH 6H
45H 5H
67H 4H
Word at Address 2H
Contains 74CBH 74H 3H
CBH 2H Double quadword at Address 0H
Word at Address 1H Contains
Contains CB31H 31H 1H 4E127AFE06361FA4230B456774CB3112
12H 0H
4-2 Vol. 1
DATA TYPES
exception). However, additional memory bus cycles are required to access unaligned
data from memory.
Vol. 1 4-3
DATA TYPES
Sign
Word Signed Integer
15 14 0
Sign
Doubleword Signed Integer
31 30 0
Sign
Quadword Signed Integer
63 62 0
Sign
Single Precision
Floating Point
31 30 23 22 0
Sign
Double Precision
Floating Point
63 62 52 51 0
4.2.1 Integers
The Intel 64 and IA-32 architectures define two types of integers: unsigned and
signed. Unsigned integers are ordinary binary values ranging from 0 to the maximum
positive number that can be encoded in the selected operand size. Signed integers
4-4 Vol. 1
DATA TYPES
are two’s complement binary values that can be used to represent both positive and
negative integer values.
Some integer instructions (such as the ADD, SUB, PADDB, and PSUBB instructions)
operate on either unsigned or signed integer operands. Other integer instructions
(such as IMUL, MUL, IDIV, DIV, FIADD, and FISUB) operate on only one integer type.
The following sections describe the encodings and ranges of the two types of
integers.
Vol. 1 4-5
DATA TYPES
4-6 Vol. 1
DATA TYPES
Table 4-2 gives the length, precision, and approximate normalized range that can be
represented by each of these data types. Denormal values are also supported in each
of these types.
NOTE
Section 4.8, “Real Numbers and Floating-Point Formats,” gives an
overview of the IEEE Standard 754 floating-point formats and defines
the terms integer bit, QNaN, SNaN, and denormal value.
Table 4-3 shows the floating-point encodings for zeros, denormalized finite numbers,
normalized finite numbers, infinites, and NaNs for each of the three floating-point
data types. It also gives the format for the QNaN floating-point indefinite value. (See
Section 4.8.3.7, “QNaN Floating-Point Indefinite,” for a discussion of the use of the
QNaN floating-point indefinite value.)
For the single-precision and double-precision formats, only the fraction part of the
significand is encoded. The integer is assumed to be 1 for all numbers except 0 and
denormalized finite numbers. For the double extended-precision format, the integer
is contained in bit 63, and the most-significant fraction bit is bit 62. Here, the integer
is explicitly set to 1 for normalized numbers, infinities, and NaNs, and to 0 for zero
and denormalized numbers.
Vol. 1 4-7
DATA TYPES
The exponent of each floating-point data type is encoded in biased format; see
Section 4.8.2.2, “Biased Exponent.” The biasing constant is 127 for the single-
precision format, 1023 for the double-precision format, and 16,383 for the double
extended-precision format.
4-8 Vol. 1
DATA TYPES
Near Pointer
Offset
31 0
Vol. 1 4-9
DATA TYPES
Near Pointer
64-bit Offset
63 0
79 64 63 0
47 32 31 0
31 16 15 0
Bit Field
Field Length
Least
Significant
Bit
4-10 Vol. 1
DATA TYPES
Vol. 1 4-11
DATA TYPES
Packed Bytes
63 0
Packed Words
63 0
Packed Doublewords
63 0
63 0
63 0
63 0
4-12 Vol. 1
DATA TYPES
Packed Bytes
127 0
Packed Words
127 0
Packed Doublewords
127 0
Packed Quadwords
127 0
127 0
127 0
127 0
Vol. 1 4-13
DATA TYPES
BCD Integers
X BCD
7 43 0
Packed BCD Integers
BCD BCD
7 43 0
Sign 80-Bit Packed BCD Decimal Integers
X D17 D16 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
79 78 72 71 0
4 Bits = 1 BCD Digit
When operating on BCD integers in general-purpose registers, the BCD values can be
unpacked (one BCD digit per byte) or packed (two BCD digits per byte). The value of
an unpacked BCD integer is the binary value of the low half-byte (bits 0 through 3).
The high half-byte (bits 4 through 7) can be any value during addition and subtrac-
tion, but must be zero during multiplication and division. Packed BCD integers allow
two BCD digits to be contained in one byte. Here, the digit in the high half-byte is
more significant than the digit in the low half-byte.
When operating on BCD integers in x87 FPU data registers, BCD values are packed in
an 80-bit format and referred to as decimal integers. In this format, the first 9 bytes
hold 18 BCD digits, 2 digits per byte. The least-significant digit is contained in the
lower half-byte of byte 0 and the most-significant digit is contained in the upper half-
byte of byte 9. The most significant bit of byte 10 contains the sign bit (0 = positive
and 1 = negative; bits 0 through 6 of byte 10 are don’t care bits). Negative decimal
integers are not stored in two's complement form; they are distinguished from posi-
tive decimal integers only by the sign bit. The range of decimal integers that can be
encoded in this format is –1018 + 1 to 1018 – 1.
The decimal integer format exists in memory only. When a decimal integer is loaded
in an x87 FPU data register, it is automatically converted to the double-extended-
precision floating-point format. All decimal integers are exactly representable in
double extended-precision format.
Table 4-4 gives the possible encodings of value in the decimal integer data type.
4-14 Vol. 1
DATA TYPES
. . .
. . .
Smallest 0 0000000 0000 0000 0000 0000 ... 0001
Zero 0 0000000 0000 0000 0000 0000 ... 0000
Negative
Zero 1 0000000 0000 0000 0000 0000 ... 0000
Vol. 1 4-15
DATA TYPES
4-16 Vol. 1
DATA TYPES
+10
10.0000000000000000000000
1.11111111111111111111111
Precision 24 Binary Digits
Sign
Exponent Significand
Fraction
Integer or J-Bit
Vol. 1 4-17
DATA TYPES
4-18 Vol. 1
DATA TYPES
NaN NaN
− Denormalized Finite + Denormalized Finite
−∞ − Normalized Finite − 0+ 0 + Normalized Finite + ∞
NOTES:
1. Integer bit of fraction implied for
single-precision floating-point format.
2. Fraction must be non-zero.
3. Sign bit ignored.
Vol. 1 4-19
DATA TYPES
An IA-32 processor can operate on and/or return any of these values, depending on
the type of computation being performed. The following sections describe these
number and non-number classes.
4-20 Vol. 1
DATA TYPES
In the extreme case, all the significant bits are shifted out to the right by leading
zeros, creating a zero result.
The Intel 64 and IA-32 architectures deal with denormal values in the following ways:
• It avoids creating denormals by normalizing numbers whenever possible.
• It provides the floating-point underflow exception to permit programmers to
detect cases when denormals are created.
• It provides the floating-point denormal-operand exception to permit procedures
or programs to detect when denormals are being used as source operands for
computations.
4.8.3.4 NaNs
Since NaNs are non-numbers, they are not part of the real number line. In
Figure 4-12, the encoding space for NaNs in the floating-point formats is shown
Vol. 1 4-21
DATA TYPES
above the ends of the real number line. This space includes any value with the
maximum allowable biased exponent and a non-zero fraction (the sign bit is ignored
for NaNs).
The IA-32 architecture defines two classes of NaNs: quiet NaNs (QNaNs) and
signaling NaNs (SNaNs). A QNaN is a NaN with the most significant fraction bit set;
an SNaN is a NaN with the most significant fraction bit clear. QNaNs are allowed to
propagate through most arithmetic operations without signaling an exception.
SNaNs generally signal a floating-point invalid-operation exception whenever they
appear as operands in arithmetic operations.
SNaNs are typically used to trap or invoke an exception handler. They must be
inserted by software; that is, the processor never generates an SNaN as a result of a
floating-point operation.
4-22 Vol. 1
DATA TYPES
Vol. 1 4-23
DATA TYPES
operand address field of the exception pointer will point to the NaN, and the NaN will
contain the index number of the array element.
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 diag-
nostic 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 that 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.
4.8.4 Rounding
When performing floating-point operations, the processor produces an infinitely
precise floating-point result in the destination format (single-precision, double-preci-
sion, or double extended-precision floating-point) whenever possible. However,
because only a subset of the numbers in the real number continuum can be repre-
sented in IEEE Standard 754 floating-point formats, it is often the case that an infi-
nitely precise result cannot be encoded exactly in the format of the destination
operand.
For example, the following value (a) has a 24-bit fraction. The least-significant bit of
this fraction (the underlined bit) cannot be encoded exactly in the single-precision
format (which has only a 23-bit fraction):
(a) 1.0001 0000 1000 0011 1001 0111E2 101
To round this result (a), the processor first selects two representable fractions b and
c that most closely bracket a in value (b < a < c).
(b) 1.0001 0000 1000 0011 1001 011E2 101
(c) 1.0001 0000 1000 0011 1001 100E2 101
4-24 Vol. 1
DATA TYPES
The processor then sets the result to b or to c according to the selected rounding
mode. Rounding introduces an error in a result that is less than one unit in the last
place (the least significant bit position of the floating-point value) to which the result
is rounded.
The IEEE Standard 754 defines four rounding modes (see Table 4-8): round to
nearest, round up, round down, and round toward zero. The default rounding mode
(for the Intel 64 and IA-32 architectures) is round to nearest. This mode provides the
most accurate and statistically unbiased estimate of the true result and is suitable for
most applications.
Table 4-8. Rounding Modes and Encoding of Rounding Control (RC) Field
Rounding RC Field Description
Mode Setting
Round to 00B Rounded result is the closest to the infinitely precise result. If two
nearest (even) values are equally close, the result is the even value (that is, the
one with the least-significant bit of zero). Default
Round down 01B Rounded result is closest to but no greater than the infinitely
(toward −∞) precise result.
Round up 10B Rounded result is closest to but no less than the infinitely precise
(toward +∞) result.
Round toward 11B Rounded result is closest to but no greater in absolute value than
zero (Truncate) the infinitely precise result.
The round up and round down modes are termed directed rounding and can be
used to implement interval arithmetic. Interval arithmetic is used to determine upper
and lower bounds for the true result of a multistep computation, when the interme-
diate results of the computation are subject to rounding.
The round toward zero mode (sometimes called the “chop” mode) is commonly used
when performing integer arithmetic with the x87 FPU.
The rounded result is called the inexact result. When the processor produces an
inexact result, the floating-point precision (inexact) flag (PE) is set (see Section
4.9.1.6, “Inexact-Result (Precision) Exception (#P)”).
The rounding modes have no effect on comparison operations, operations that
produce exact results, or operations that produce NaN results.
Vol. 1 4-25
DATA TYPES
NOTE
All of the exceptions listed above except the denormal-operand
exception (#D) are defined in IEEE Standard 754.
4-26 Vol. 1
DATA TYPES
Vol. 1 4-27
DATA TYPES
4-28 Vol. 1
DATA TYPES
See the following sections for information regarding the denormal-operand exception
when detected while executing x87 FPU or SSE/SSE2/SSE3 instructions:
• x87 FPU; Section 8.5.2, “Denormal Operand Exception (#D)”
• SIMD floating-point exceptions; Section 11.5.2.2, “Denormal-Operand Exception
(#D)”
Vol. 1 4-29
DATA TYPES
See the following sections for information regarding the numeric overflow exception
when detected while executing x87 FPU instructions or while executing
SSE/SSE2/SSE3 instructions:
• x87 FPU; Section 8.5.4, “Numeric Overflow Exception (#O)”
• SIMD floating-point exceptions; Section 11.5.2.4, “Numeric Overflow Exception
(#O)”
4-30 Vol. 1
DATA TYPES
How the processor handles an underflow condition, depends on two related condi-
tions:
• creation of a tiny result
• creation of an inexact result; that is, a result that cannot be represented exactly
in the destination format
Which of these events causes an underflow exception to be reported and how the
processor responds to the exception condition depends on whether the underflow
exception is masked:
• Underflow exception masked — The underflow exception is reported (the UE
flag is set) only when the result is both tiny and inexact. The processor returns a
denormalized result to the destination operand, regardless of inexactness.
• Underflow exception not masked — The underflow exception is reported
when the result is tiny, regardless of inexactness. The processor leaves the
source and destination operands unaltered or stores a biased result in the
designating operand (depending whether the underflow exception was generated
during an SSE/SSE2/SSE3 floating-point operation or an x87 FPU operation) and
invokes a software exception handler.
See the following sections for information regarding the numeric underflow exception
when detected while executing x87 FPU instructions or while executing
SSE/SSE2/SSE3 instructions:
• x87 FPU; Section 8.5.5, “Numeric Underflow Exception (#U)”
• SIMD floating-point exceptions; Section 11.5.2.5, “Numeric Underflow Exception
(#U)”
Vol. 1 4-31
DATA TYPES
exception occurs frequently and indicates that some (normally acceptable) accuracy
will be lost due to rounding. The exception is supported for applications that need to
perform exact arithmetic only. Because the rounded result is generally satisfactory
for most applications, this exception is commonly masked.
If the inexact-result exception is masked when an inexact-result condition occurs and
a numeric overflow or underflow condition has not occurred, the processor sets the
PE flag and stores the rounded result in the destination operand. The current
rounding mode determines the method used to round the result. See Section 4.8.4,
“Rounding.”
If the inexact-result exception is not masked when an inexact result occurs and
numeric overflow or underflow has not occurred, the PE flag is set, the rounded result
is stored in the destination operand, and a software exception handler is invoked.
If an inexact result occurs in conjunction with numeric overflow or underflow, one of
the following operations is carried out:
• If an inexact result occurs along with masked overflow or underflow, the OE flag
or UE flag and the PE flag are set and the result is stored as described for the
overflow or underflow exceptions; see Section 4.9.1.4, “Numeric Overflow
Exception (#O),” or Section 4.9.1.5, “Numeric Underflow Exception (#U).” If the
inexact result exception is unmasked, the processor also invokes a software
exception handler.
• If an inexact result occurs along with unmasked overflow or underflow and the
destination operand is a register, the OE or UE flag and the PE flag are set, the
result is stored as described for the overflow or underflow exceptions, and a
software exception handler is invoked.
If an unmasked numeric overflow or underflow exception occurs and the destination
operand is a memory location (which can happen only for a floating-point store), the
inexact-result condition is not reported and the C1 flag is cleared.
See the following sections for information regarding the inexact-result exception
when detected while executing x87 FPU or SSE/SSE2/SSE3 instructions:
• x87 FPU; Section 8.5.6, “Inexact-Result (Precision) Exception (#P)”
• SIMD floating-point exceptions; Section 11.5.2.3, “Divide-By-Zero Exception
(#Z)”
4-32 Vol. 1
DATA TYPES
Vol. 1 4-33
DATA TYPES
4-34 Vol. 1
CHAPTER 5
INSTRUCTION SET SUMMARY
Vol. 1 5-1
INSTRUCTION SET SUMMARY
The following sections list instructions in each major group and subgroup. Given for
each instruction is its mnemonic and descriptive names. When two or more
mnemonics are given (for example, CMOVA/CMOVNBE), they represent different
mnemonics for the same instruction opcode. Assemblers support redundant
mnemonics for some instructions to make it easier to read code listings. For instance,
CMOVA (Conditional move if above) and CMOVNBE (Conditional move if not below or
equal) represent the same condition. For detailed information about specific instruc-
tions, see the Intel® 64 and IA-32 Architectures Software Developer’s Manual,
Volumes 3A & 3B.
5-2 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-3
INSTRUCTION SET SUMMARY
5-4 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-5
INSTRUCTION SET SUMMARY
BT Bit test
BTS Bit test and set
BTR Bit test and reset
BTC Bit test and complement
BSF Bit scan forward
BSR Bit scan reverse
SETE/SETZ Set byte if equal/Set byte if zero
SETNE/SETNZ Set byte if not equal/Set byte if not zero
SETA/SETNBE Set byte if above/Set byte if not below or equal
SETAE/SETNB/SETNC Set byte if above or equal/Set byte if not below/Set byte if not
carry
SETB/SETNAE/SETC Set byte if below/Set byte if not above or equal/Set byte if carry
SETBE/SETNA Set byte if below or equal/Set byte if not above
SETG/SETNLE Set byte if greater/Set byte if not less or equal
SETGE/SETNL Set byte if greater or equal/Set byte if not less
SETL/SETNGE Set byte if less/Set byte if not greater or equal
SETLE/SETNG Set byte if less or equal/Set byte if not greater
SETS Set byte if sign (negative)
SETNS Set byte if not sign (non-negative)
SETO Set byte if overflow
SETNO Set byte if not overflow
SETPE/SETP Set byte if parity even/Set byte if parity
SETPO/SETNP Set byte if parity odd/Set byte if not parity
TEST Logical compare
5-6 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-7
INSTRUCTION SET SUMMARY
5-8 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-9
INSTRUCTION SET SUMMARY
5-10 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-11
INSTRUCTION SET SUMMARY
5-12 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-13
INSTRUCTION SET SUMMARY
Set Reference, A-M,” of the Intel® 64 and IA-32 Architectures Software Developer’s
Manual, Volume 2A.
MMX instructions are divided into the following subgroups: data transfer, conversion,
packed arithmetic, comparison, logical, shift and rotate, and state management
instructions. The sections that follow introduce each subgroup.
5-14 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-15
INSTRUCTION SET SUMMARY
5-16 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-17
INSTRUCTION SET SUMMARY
5-18 Vol. 1
INSTRUCTION SET SUMMARY
UNPCKLPS Unpacks and interleaves the two low-order values from two
single-precision floating-point operands
Vol. 1 5-19
INSTRUCTION SET SUMMARY
5-20 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-21
INSTRUCTION SET SUMMARY
5-22 Vol. 1
INSTRUCTION SET SUMMARY
UNPCKLPD Unpacks and interleaves the low values from two packed
double-precision floating-point operands
Vol. 1 5-23
INSTRUCTION SET SUMMARY
5-24 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-25
INSTRUCTION SET SUMMARY
5-26 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-27
INSTRUCTION SET SUMMARY
PHADDSW Adds two adjacent, signed 16-bit integers horizontally from the
source and destination operands and packs the signed, satu-
rated 16-bit results to the destination operand.
PHADDD Adds two adjacent, signed 32-bit integers horizontally from the
source and destination operands and packs the signed 32-bit
results to the destination operand.
PHSUBW Performs horizontal subtraction on each adjacent pair of 16-bit
signed integers by subtracting the most significant word from
the least significant word of each pair in the source and destina-
tion operands. The signed 16-bit results are packed and written
to the destination operand.
PHSUBSW Performs horizontal subtraction on each adjacent pair of 16-bit
signed integers by subtracting the most significant word from
the least significant word of each pair in the source and destina-
tion operands. The signed, saturated 16-bit results are packed
and written to the destination operand.
PHSUBD Performs horizontal subtraction on each adjacent pair of 32-bit
signed integers by subtracting the most significant doubleword
from the least significant double word of each pair in the source
and destination operands. The signed 32-bit results are packed
and written to the destination operand.
5-28 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-29
INSTRUCTION SET SUMMARY
5-30 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-31
INSTRUCTION SET SUMMARY
5-32 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-33
INSTRUCTION SET SUMMARY
5-34 Vol. 1
INSTRUCTION SET SUMMARY
Vol. 1 5-35
INSTRUCTION SET SUMMARY
5-36 Vol. 1
INSTRUCTION SET SUMMARY
the VMCS have been written to the VMCS-data area in the refer-
enced VMCS region.
VMREAD Reads a component from the VMCS (the encoding of that field is
given in a register operand) and stores it into a destination
operand.
VMWRITE Writes a component to the VMCS (the encoding of that field is
given in a register operand) from a source operand.
The behavior of the VMX management instructions is summarized below:
VMCALL Allows a guest in VMX non-root operation to call the VMM for
service. A VM exit occurs, transferring control to the VMM.
VMLAUNCH Launches a virtual machine managed by the VMCS. A VM entry
occurs, transferring control to the VM.
VMRESUME Resumes a virtual machine managed by the VMCS. A VM entry
occurs, transferring control to the VM.
VMXOFF Causes the processor to leave VMX operation.
VMXON Takes a single 64-bit source operand in memory. It causes a
logical processor to enter VMX root operation and to use the
memory referenced by the operand to support VMX operation.
INVEPT Invalidate cached Extended Page Table (EPT) mappings in the
processor to synchronize address translation in virtual machines
with memory-resident EPT pages.
INVVPID Invalidate cached mappings of address translation based on the
Virtual Processor ID (VPID).
Vol. 1 5-37
INSTRUCTION SET SUMMARY
5-38 Vol. 1
CHAPTER 6
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
This chapter describes the facilities in the Intel 64 and IA-32 architectures for
executing calls to procedures or subroutines. It also describes how interrupts and
exceptions are handled from the perspective of an application programmer.
6.2 STACKS
The stack (see Figure 6-1) is a contiguous array of memory locations. It is contained
in a segment and identified by the segment selector in the SS register. When using
the flat memory model, the stack can be located anywhere in the linear address
space for the program. A stack can be up to 4 GBytes long, the maximum size of a
segment.
Items are placed on the stack using the PUSH instruction and removed from the
stack using the POP instruction. When an item is pushed onto the stack, the
processor decrements the ESP register, then writes the item at the new top of stack.
When an item is popped off the stack, the processor reads the item from the top of
stack, then increments the ESP register. In this manner, the stack grows down in
memory (towards lesser addresses) when items are pushed on the stack and shrinks
up (towards greater addresses) when the items are popped from the stack.
A program or operating system/executive can set up many stacks. For example, in
multitasking systems, each task can be given its own stack. The number of stacks in
a system is limited by the maximum number of segments and the available physical
memory.
Vol. 2 6-1
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
When a system sets up many stacks, only one stack—the current stack—is avail-
able at a time. The current stack is the one contained in the segment referenced by
the SS register.
Stack Segment
Bottom of Stack
(Initial ESP Value)
Local Variables
for Calling
Procedure The Stack Can Be
16 or 32 Bits Wide
Parameters
Passed to The EBP register is
Called typically set to point
Procedure to the return
instruction pointer.
Frame Boundary
Return Instruction EBP Register
Pointer
ESP Register
Top of Stack
The processor references the SS register automatically for all stack operations. For
example, when the ESP register is used as a memory address, it automatically points
to an address in the current stack. Also, the CALL, RET, PUSH, POP, ENTER, and
LEAVE instructions all perform operations on the current stack.
6-2 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
3. Load the stack pointer for the stack into the ESP register using a MOV, POP, or
LSS instruction. The LSS instruction can be used to load the SS and ESP registers
in one operation.
See “Segment Descriptors” in of the Intel® 64 and IA-32 Architectures Software
Developer’s Manual, Volume 3A, for information on how to set up a segment
descriptor and segment limits for a stack segment.
Vol. 2 6-3
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
6-4 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Vol. 2 6-5
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
6-6 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Vol. 2 6-7
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
use the stack-frame base pointer (in the EBP register) to make a frame boundary for
easy access to the parameters.
The stack can also be used to pass parameters back from the called procedure to the
calling procedure.
6-8 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Protection Rings
Operating
System
Kernel Level 0
Operating System
Services (Device
Drivers, Etc.) Level 1
Applications Level 2
Level 3
Highest Lowest
0 1 2 3
Privilege Levels
In this example, the highest privilege level 0 (at the center of the diagram) is used for
segments that contain the most critical code modules in the system, usually the
kernel of an operating system. The outer rings (with progressively lower privileges)
are used for segments that contain code modules for less critical software.
Code modules in lower privilege segments can only access modules operating at
higher privilege segments by means of a tightly controlled and protected interface
called a gate. Attempts to access higher privilege segments without going through a
protection gate and without having sufficient access rights causes a general-protec-
tion exception (#GP) to be generated.
If an operating system or executive uses this multilevel protection mechanism, a call
to a procedure that is in a more privileged protection level than the calling procedure
is handled in a similar manner as a far call (see Section 6.3.2, “Far CALL and RET
Operation”). The differences are as follows:
• The segment selector provided in the CALL instruction references a special data
structure called a call gate descriptor. Among other things, the call gate
descriptor provides the following:
— access rights information
— the segment selector for the code segment of the called procedure
— an offset into the code segment (that is, the instruction pointer for the called
procedure)
Vol. 2 6-9
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
• The processor switches to a new stack to execute the called procedure. Each
privilege level has its own stack. The segment selector and stack pointer for the
privilege level 3 stack are stored in the SS and ESP registers, respectively, and
are automatically saved when a call to a more privileged level occurs. The
segment selectors and stack pointers for the privilege level 2, 1, and 0 stacks are
stored in a system segment called the task state segment (TSS).
The use of a call gate and the TSS during a stack switch are transparent to the calling
procedure, except when a general-protection exception is raised.
Calling SS
Calling ESP
Param 1 Param 1
Stack Frame
Param 2 Param 2 Stack Frame
Before Call
Param 3 ESP Before Call Param 3 After Call
Calling CS
ESP After Call Calling EIP
Calling SS
ESP After Return Calling ESP
Param 1 Param 1
Param 2 Param 2
Param 3 Param 3
Calling CS
ESP Before Return Calling EIP
6-10 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
3. Loads the segment selector and stack pointer for the new stack (that is, the stack
for the privilege level being called) from the TSS into the SS and ESP registers
and switches to the new stack.
4. Pushes the temporarily saved SS and ESP values for the calling procedure’s stack
onto the new stack.
5. Copies the parameters from the calling procedure’s stack to the new stack. A
value in the call gate descriptor determines how many parameters to copy to the
new stack.
6. Pushes the temporarily saved CS and EIP values for the calling procedure to the
new stack.
7. Loads the segment selector for the new code segment and the new instruction
pointer from the call gate into the CS and EIP registers, respectively.
8. Begins execution of the called procedure at the new privilege level.
When executing a return from the privileged procedure, the processor performs
these actions:
1. Performs a privilege check.
2. Restores the CS and EIP registers to their values prior to the call.
3. If the RET instruction has an optional n argument, increments the stack pointer
by the number of bytes specified with the n operand to release parameters from
the stack. If the call gate descriptor specifies that one or more parameters be
copied from one stack to the other, a RET n instruction must be used to release
the parameters from both stacks. Here, the n operand specifies the number of
bytes occupied on each stack by the parameters. On a return, the processor
increments ESP by n for each stack to step over (effectively remove) these
parameters from the stacks.
4. Restores the SS and ESP registers to their values prior to the call, which causes a
switch back to the stack of the calling procedure.
5. If the RET instruction has an optional n argument, increments the stack pointer
by the number of bytes specified with the n operand to release parameters from
the stack (see explanation in step 3).
6. Resumes execution of the calling procedure.
See Chapter 4, “Protection,” in the Intel® 64 and IA-32 Architectures Software
Developer’s Manual, Volume 3A, for detailed information on calls to privileged levels
and the call gate descriptor.
Vol. 2 6-11
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
• In 64-bit mode and compatibility mode, 64-bit call-gate descriptors for far calls
are available
In 64-bit mode, the operand size for all near branches (CALL, RET, JCC, JCXZ, JMP,
and LOOP) is forced to 64 bits. These instructions update the 64-bit RIP without the
need for a REX operand-size prefix.
The following aspects of near branches are controlled by the effective operand size:
• Truncation of the size of the instruction pointer
• Size of a stack pop or push, due to a CALL or RET
• Size of a stack-pointer increment or decrement, due to a CALL or RET
• Indirect-branch operand size
In 64-bit mode, all of the above actions are forced to 64 bits regardless of operand
size prefixes (operand size prefixes are silently ignored). However, the displacement
field for relative branches is still limited to 32 bits and the address size for near
branches is not forced in 64-bit mode.
Address sizes affect the size of RCX used for JCXZ and LOOP; they also impact the
address calculation for memory indirect branches. Such addresses are 64 bits by
default; but they can be overridden to 32 bits by an address size prefix.
Software typically uses far branches to change privilege levels. The legacy IA-32
architecture provides the call-gate mechanism to allow software to branch from one
privilege level to another, although call gates can also be used for branches that do
not change privilege levels. When call gates are used, the selector portion of the
direct or indirect pointer references a gate descriptor (the offset in the instruction is
ignored). The offset to the destination’s code segment is taken from the call-gate
descriptor.
64-bit mode redefines the type value of a 32-bit call-gate descriptor type to a 64-bit
call gate descriptor and expands the size of the 64-bit descriptor to hold a 64-bit
offset. The 64-bit mode call-gate descriptor allows far branches that reference any
location in the supported linear-address space. These call gates also hold the target
code selector (CS), allowing changes to privilege level and default size as a result of
the gate transition.
Because immediates are generally specified up to 32 bits, the only way to specify a
full 64-bit absolute RIP in 64-bit mode is with an indirect branch. For this reason,
direct far branches are eliminated from the instruction set in 64-bit mode.
64-bit mode also expands the semantics of the SYSENTER and SYSEXIT instructions
so that the instructions operate within a 64-bit memory space. The mode also intro-
duces two new instructions: SYSCALL and SYSRET (which are valid only in 64-bit
mode). For details, see “SYSENTER—Fast System Call” and “SYSEXIT—Fast Return
from Fast System Call” in Chapter 4, “Instruction Set Reference, N-Z,” of the Intel®
64 and IA-32 Architectures Software Developer’s Manual, Volume 2B.
6-12 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Vol. 2 6-13
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
6-14 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
If the code segment for the handler procedure has the same privilege level as the
currently executing program or task, the handler procedure uses the current stack; if
the handler executes at a more privileged level, the processor switches to the stack
for the handler’s privilege level.
If no stack switch occurs, the processor does the following when calling an interrupt
or exception handler (see Figure 6-5):
1. Pushes the current contents of the EFLAGS, CS, and EIP registers (in that order)
on the stack.
2. Pushes an error code (if appropriate) on the stack.
3. Loads the segment selector for the new code segment and the new instruction
pointer (from the interrupt gate or trap gate) into the CS and EIP registers,
respectively.
4. If the call is through an interrupt gate, clears the IF flag in the EFLAGS register.
5. Begins execution of the handler procedure.
Vol. 2 6-15
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
ESP Before
EFLAGS Transfer to Handler
CS
EIP
Error Code ESP After
Transfer to Handler
ESP Before
Transfer to Handler SS
ESP
EFLAGS
CS
EIP
ESP After Error Code
Transfer to Handler
Figure 6-5. Stack Usage on Transfers to Interrupt and Exception Handling Routines
6-16 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
A return from an interrupt or exception handler is initiated with the IRET instruction.
The IRET instruction is similar to the far RET instruction, except that it also restores
the contents of the EFLAGS register for the interrupted procedure. When executing a
return from an interrupt or exception handler from the same privilege level as the
interrupted procedure, the processor performs these actions:
1. Restores the CS and EIP registers to their values prior to the interrupt or
exception.
2. Restores the EFLAGS register.
3. Increments the stack pointer appropriately.
4. Resumes execution of the interrupted procedure.
When executing a return from an interrupt or exception handler from a different priv-
ilege level than the interrupted procedure, the processor performs these actions:
1. Performs a privilege check.
2. Restores the CS and EIP registers to their values prior to the interrupt or
exception.
3. Restores the EFLAGS register.
4. Restores the SS and ESP registers to their values prior to the interrupt or
exception, resulting in a stack switch back to the stack of the interrupted
procedure.
5. Resumes execution of the interrupted procedure.
Vol. 2 6-17
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
interrupt table contains instruction pointers to the interrupt and exception handler
procedures.
The processor saves the state of the EFLAGS register, the EIP register, the CS
register, and an optional error code on the stack before switching to the handler
procedure.
A return from the interrupt or exception handler is carried out with the IRET
instruction.
See Chapter 15, “8086 Emulation,” in the Intel® 64 and IA-32 Architectures Soft-
ware Developer’s Manual, Volume 3A, for more information on handling interrupts
and exceptions in real-address mode.
6-18 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Vol. 2 6-19
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
ENTER 2048,3
The lexical nesting level determines the number of stack frame pointers to copy into
the new stack frame from the preceding frame. A stack frame pointer is a doubleword
used to access the variables of a procedure. The set of stack frame pointers used by
a procedure to access the variables of other procedures is called the display. The first
doubleword in the display is a pointer to the previous stack frame. This pointer is
used by a LEAVE instruction to undo the effect of an ENTER instruction by discarding
the current stack frame.
After the ENTER instruction creates the display for a procedure, it allocates the
dynamic local variables for the procedure by decrementing the contents of the ESP
register by the number of bytes specified in the first parameter. This new value in the
ESP register serves as the initial top-of-stack for all PUSH and POP operations within
the procedure.
To allow a procedure to address its display, the ENTER instruction leaves the EBP
register pointing to the first doubleword in the display. Because stacks grow down,
this is actually the doubleword with the highest address in the display. Data manipu-
lation instructions that specify the EBP register as a base register automatically
address locations within the stack segment instead of the data segment.
The ENTER instruction can be used in two ways: nested and non-nested. If the lexical
level is 0, the non-nested form is used. The non-nested form pushes the contents of
6-20 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
the EBP register on the stack, copies the contents of the ESP register into the EBP
register, and subtracts the first operand from the contents of the ESP register to allo-
cate dynamic storage. The non-nested form differs from the nested form in that no
stack frame pointers are copied. The nested form of the ENTER instruction occurs
when the second parameter (lexical level) is not zero.
The following pseudo code shows the formal definition of the ENTER instruction.
STORAGE is the number of bytes of dynamic storage to allocate for local variables,
and LEVEL is the lexical nesting level.
PUSH EBP;
FRAME_PTR ← ESP;
IF LEVEL > 0
THEN
DO (LEVEL − 1) times
EBP ← EBP − 4;
PUSH Pointer(EBP); (* doubleword pointed to by EBP *)
OD;
PUSH FRAME_PTR;
FI;
EBP ← FRAME_PTR;
ESP ← ESP − STORAGE;
The main procedure (in which all other procedures are nested) operates at the
highest lexical level, level 1. The first procedure it calls operates at the next deeper
lexical level, level 2. A level 2 procedure can access the variables of the main
program, which are at fixed locations specified by the compiler. In the case of level 1,
the ENTER instruction allocates only the requested dynamic storage on the stack
because there is no previous display to copy.
A procedure that calls another procedure at a lower lexical level gives the called
procedure access to the variables of the caller. The ENTER instruction provides this
access by placing a pointer to the calling procedure's stack frame in the display.
A procedure that calls another procedure at the same lexical level should not give
access to its variables. In this case, the ENTER instruction copies only that part of the
display from the calling procedure which refers to previously nested procedures
operating at higher lexical levels. The new stack frame does not include the pointer
for addressing the calling procedure’s stack frame.
The ENTER instruction treats a re-entrant procedure as a call to a procedure at the
same lexical level. In this case, each succeeding iteration of the re-entrant procedure
can address only its own variables and the variables of the procedures within which it
is nested. A re-entrant procedure always can address its own variables; it does not
require pointers to the stack frames of previous iterations.
By copying only the stack frame pointers of procedures at higher lexical levels, the
ENTER instruction makes certain that procedures access only those variables of
higher lexical levels, not those at parallel lexical levels (see Figure 6-6).
Vol. 2 6-21
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Block-structured languages can use the lexical levels defined by ENTER to control
access to the variables of nested procedures. In Figure 6-6, for example, if procedure
A calls procedure B which, in turn, calls procedure C, then procedure C will have
access to the variables of the MAIN procedure and procedure A, but not those of
procedure B because they are at the same lexical level. The following definition
describes the access to variables for the nested procedures in Figure 6-6.
1. MAIN has variables at fixed locations.
2. Procedure A can access only the variables of MAIN.
3. Procedure B can access only the variables of procedure A and MAIN. Procedure B
cannot access the variables of procedure C or procedure D.
4. Procedure C can access only the variables of procedure A and MAIN. Procedure C
cannot access the variables of procedure B or procedure D.
5. Procedure D can access the variables of procedure C, procedure A, and MAIN.
Procedure D cannot access the variables of procedure B.
In Figure 6-7, an ENTER instruction at the beginning of the MAIN procedure creates
three doublewords of dynamic storage for MAIN, but copies no pointers from other
stack frames. The first doubleword in the display holds a copy of the last value in the
EBP register before the ENTER instruction was executed. The second doubleword
holds a copy of the contents of the EBP register following the ENTER instruction. After
the instruction is executed, the EBP register points to the first doubleword pushed on
the stack, and the ESP register points to the last doubleword in the stack frame.
When MAIN calls procedure A, the ENTER instruction creates a new display (see
Figure 6-8). The first doubleword is the last value held in MAIN's EBP register. The
second doubleword is a pointer to MAIN's stack frame which is copied from the
second doubleword in MAIN's display. This happens to be another copy of the last
value held in MAIN’s EBP register. Procedure A can access variables in MAIN because
MAIN is at level 1.
6-22 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Therefore the base address for the dynamic storage used in MAIN is the current
address in the EBP register, plus four bytes to account for the saved contents of
MAIN’s EBP register. All dynamic variables for MAIN are at fixed, positive offsets from
this value.
Dynamic
Storage
ESP
Old EBP
Main’s EBP
When procedure A calls procedure B, the ENTER instruction creates a new display
(see Figure 6-9). The first doubleword holds a copy of the last value in procedure A’s
EBP register. The second and third doublewords are copies of the two stack frame
pointers in procedure A’s display. Procedure B can access variables in procedure A
and MAIN by using the stack frame pointers in its display.
Vol. 2 6-23
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
When procedure B calls procedure C, the ENTER instruction creates a new display for
procedure C (see Figure 6-10). The first doubleword holds a copy of the last value in
procedure B’s EBP register. This is used by the LEAVE instruction to restore procedure
B’s stack frame. The second and third doublewords are copies of the two stack frame
pointers in procedure A’s display. If procedure C were at the next deeper lexical level
from procedure B, a fourth doubleword would be copied, which would be the stack
frame pointer to procedure B’s local variables.
Note that procedure B and procedure C are at the same level, so procedure C is not
intended to access procedure B’s variables. This does not mean that procedure C is
completely isolated from procedure B; procedure C is called by procedure B, so the
pointer to the returning stack frame is a pointer to procedure B’s stack frame. In
addition, procedure B can pass parameters to procedure C either on the stack or
through variables global to both procedures (that is, variables in the scope of both
procedures).
Old EBP
Main’s EBP
Main’s EBP
Main’s EBP
Procedure A’s EBP
Dynamic
Storage
ESP
6-24 Vol. 2
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
Old EBP
Main’s EBP
Main’s EBP
Main’s EBP
Procedure A’s EBP
Dynamic
Storage
ESP
Vol. 2 6-25
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
6-26 Vol. 2
CHAPTER 7
PROGRAMMING WITH
GENERAL-PURPOSE INSTRUCTIONS
General-purpose (GP) instructions are a subset of the IA-32 instructions that repre-
sent the fundamental instruction set for the Intel IA-32 processors. These instruc-
tions were introduced into the IA-32 architecture with the first IA-32 processors (the
Intel 8086 and 8088). Additional instructions were added to the general-purpose
instruction set in subsequent families of IA-32 processors (the Intel 286, Intel386,
Intel486, Pentium, Pentium Pro, and Pentium II processors).
Intel 64 architecture further extends the capability of most general-purpose instruc-
tions so that they are able to handle 64-bit data in 64-bit mode. A small number of
general-purpose instructions (still supported in non-64-bit modes) are not supported
in 64-bit mode.
General-purpose instructions perform basic data movement, memory addressing,
arithmetic and logical, program flow control, input/output, and string operations on a
set of integer, pointer, and BCD data types. This chapter provides an overview of the
general-purpose instructions. See Intel® 64 and IA-32 Architectures Software
Developer’s Manual, Volumes 3A & 3B, for detailed descriptions of individual instruc-
tions.
Vol. 1 7-1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-2 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Vol. 1 7-3
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-4 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Table 7-2 shows mnemonics for CMOVcc instructions and the conditions being tested
for each instruction. The condition code mnemonics are appended to the letters
“CMOV” to form the mnemonics for CMOVcc instructions. The instructions listed in
Table 7-2 as pairs (for example, CMOVA/CMOVNBE) are alternate names for the
same instruction. The assembler provides these alternate names to make it easier to
read program listings.
CMOVcc instructions are useful for optimizing small IF constructions. They also help
eliminate branching overhead for IF statements and the possibility of branch mispre-
dictions by the processor.
These conditional move instructions are supported in the P6 family, Pentium 4, and
Intel Xeon processors. Software can check if CMOVcc instructions are supported by
checking the processor’s feature information with the CPUID instruction.
Vol. 1 7-5
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The XADD (exchange and add) instruction swaps two operands and then stores the
sum of the two operands in the destination operand. The status flags in the EFLAGS
register indicate the result of the addition. This instruction can be combined with the
LOCK prefix (see “LOCK—Assert LOCK# Signal Prefix” in Chapter 3, “Instruction Set
Reference, A-M,” of the Intel® 64 and IA-32 Architectures Software Developer’s
Manual, Volume 2A) in a multiprocessing system to allow multiple processors to
execute one DO loop.
The CMPXCHG (compare and exchange) and CMPXCHG8B (compare and exchange
8 bytes) instructions are used to synchronize operations in systems that use
multiple processors. The CMPXCHG instruction requires three operands: a source
operand in a register, another source operand in the EAX register, and a destination
operand. If the values contained in the destination operand and the EAX register are
equal, the destination operand is replaced with the value of the other source
operand (the value not in the EAX register). Otherwise, the original value of the
destination operand is loaded in the EAX register. The status flags in the EFLAGS
7-6 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
register reflect the result that would have been obtained by subtracting the destina-
tion operand from the value in the EAX register.
The CMPXCHG instruction is commonly used for testing and modifying semaphores.
It checks to see if a semaphore is free. If the semaphore is free, it is marked allo-
cated; otherwise it gets the ID of the current owner. This is all done in one uninter-
ruptible operation. In a single-processor system, the CMPXCHG instruction
eliminates the need to switch to protection level 0 (to disable interrupts) before
executing multiple instructions to test and modify a semaphore.
For multiple processor systems, CMPXCHG can be combined with the LOCK prefix to
perform the compare and exchange operation atomically. (See “Locked Atomic Oper-
ations” in Chapter 7, “Multiple-Processor Management,” of the Intel® 64 and IA-32
Architectures Software Developer’s Manual, Volume 3A, for more information on
atomic operations.)
The CMPXCHG8B instruction also requires three operands: a 64-bit value in
EDX:EAX, a 64-bit value in ECX:EBX, and a destination operand in memory. The
instruction compares the 64-bit value in the EDX:EAX registers with the destination
operand. If they are equal, the 64-bit value in the ECX:EBX register is stored in the
destination operand. If the EDX:EAX register and the destination are not equal, the
destination is loaded in the EDX:EAX register. The CMPXCHG8B instruction can be
combined with the LOCK prefix to perform the operation atomically.
Vol. 1 7-7
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Stack
Before Pushing Doubleword After Pushing Doubleword
Stack
Growth 31 0 31 0
n ESP
n−4 Doubleword Value ESP
n−8
The PUSHA instruction saves the contents of the eight general-purpose registers on
the stack (see Figure 7-2). This instruction simplifies procedure calls by reducing the
number of instructions required to save the contents of the general-purpose regis-
ters. The registers are pushed on the stack in the following order: EAX, ECX, EDX,
EBX, the initial value of ESP before EAX was pushed, EBP, ESI, and EDI.
Stack
Before Pushing Registers After Pushing Registers
Stack 31 0 31 0
Growth
n
n-4 ESP
n-8 EAX
n - 12 ECX
n - 16 EDX
n - 20 EBX
n - 24 Old ESP
n - 28 EBP
n - 32 ESI
n - 36 EDI ESP
The POP instruction copies the word or doubleword at the current top of stack (indi-
cated by the ESP register) to the location specified with the destination operand. It
then increments the ESP register to point to the new top of stack (see Figure 7-3).
The destination operand may specify a general-purpose register, a segment register,
or a memory location.
7-8 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Stack
Before Popping Doubleword After Popping Doubleword
Stack
Growth 31 0 31 0
n
n-4 ESP
n-8 Doubleword Value ESP
The POPA instruction reverses the effect of the PUSHA instruction. It pops the top
eight words or doublewords from the top of the stack into the general-purpose regis-
ters, except for the ESP register (see Figure 7-4). If the operand-size attribute is 32,
the doublewords on the stack are transferred to the registers in the following order:
EDI, ESI, EBP, ignore doubleword, EBX, EDX, ECX, and EAX. The ESP register is
restored by the action of popping the stack. If the operand-size attribute is 16, the
words on the stack are transferred to the registers in the following order: DI, SI, BP,
ignore word, BX, DX, CX, and AX.
Stack
Before Popping Registers After Popping Registers
Stack 0 31 0 31
Growth
n
n-4 ESP
n-8 EAX
n - 12 ECX
n - 16 EDX
n - 20 EBX
n - 24 Ignored
n - 28 EBP
n - 32 ESI
n - 36 EDI ESP
Vol. 1 7-9
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
pushes and pops are supported by using the 66H operand-size prefix. PUSHA,
PUSHAD, POPA, and POPAD are not supported.
7-10 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
15 0
Before Sign
S N N N N N N N N N N N N N N N
Extension
31 15 0
After Sign
S S S S S S S S S S S S S S S S S N N N N N N N N N N N N N N N
Extension
Simple conversion — The CBW (convert byte to word), CWDE (convert word to
doubleword extended), CWD (convert word to doubleword), and CDQ (convert
doubleword to quadword) instructions perform sign extension to double the size of
the source operand.
The CBW instruction copies the sign (bit 7) of the byte in the AL register into every bit
position of the upper byte of the AX register. The CWDE instruction copies the sign
(bit 15) of the word in the AX register into every bit position of the high word of the
EAX register.
The CWD instruction copies the sign (bit 15) of the word in the AX register into every
bit position in the DX register. The CDQ instruction copies the sign (bit 31) of the
doubleword in the EAX register into every bit position in the EDX register. The CWD
instruction can be used to produce a doubleword dividend from a word before a word
division, and the CDQ instruction can be used to produce a quadword dividend from
a doubleword before doubleword division.
Move with sign or zero extension — The MOVSX (move with sign extension) and
MOVZX (move with zero extension) instructions move the source operand into a
register then perform the sign extension.
The MOVSX instruction extends an 8-bit value to a 16-bit value or an 8-bit or 16-bit
value to a 32-bit value by sign extending the source operand, as shown in Figure 7-5.
The MOVZX instruction extends an 8-bit value to a 16-bit value or an 8-bit or 16-bit
value to a 32-bit value by zero extending the source operand.
Vol. 1 7-11
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-12 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
source operands are not modified, nor is the result saved. The CMP instruction is
commonly used in conjunction with a Jcc (jump) or SETcc (byte set on condition)
instruction, with the latter instructions performing an action based on the result of a
CMP instruction.
The NEG (negate) instruction subtracts a signed integer operand from zero. The
effect of the NEG instruction is to change the sign of a two's complement operand
while keeping its magnitude.
Vol. 1 7-13
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-14 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Vol. 1 7-15
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Initial State
CF Operand
X 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1
0
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 0
0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0
The SHR instruction shifts the source operand right by from 1 to 31 bit positions (see
Figure 7-7). As with the SHL/SAL instruction, the empty bit positions are cleared and
the CF flag is loaded with the last bit shifted out of the operand.
7-16 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The SAR instruction shifts the source operand right by from 1 to 31 bit positions (see
Figure 7-8). This instruction differs from the SHR instruction in that it preserves the
sign of the source operand by clearing empty bit positions if the operand is positive or
setting the empty bits if the operand is negative. Again, the CF flag is loaded with the
last bit shifted out of the operand.
The SAR and SHR instructions can also be used to perform division by powers of
2 (see “SAL/SAR/SHL/SHR—Shift Instructions” in Chapter 4, “Instruction Set Refer-
ence, N-Z,” of the Intel® 64 and IA-32 Architectures Software Developer’s Manual,
Volume 2B).
0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1
1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1
Vol. 1 7-17
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
SHLD Instruction
31 0
CF Destination (Memory or Register)
31 0
Source (Register)
SHRD Instruction
31 0
Source (Register)
31 0
Destination (Memory or Register) CF
The SHLD instruction shifts the bits in the destination operand to the left and fills the
empty bit positions (in the destination operand) with bits shifted out of the source
operand. The destination and source operands must be the same length (either
words or doublewords). The shift count can range from 0 to 31 bits. The result of this
shift operation is stored in the destination operand, and the source operand is not
modified. The CF flag is loaded with the last bit shifted out of the destination operand.
The SHRD instruction operates the same as the SHLD instruction except bits are
shifted to the right in the destination operand, with the empty bit positions filled with
bits shifted out of the source operand.
7-18 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
ROL Instruction
31 0
31 ROR Instruction 0
Destination (Memory or Register) CF
RCL Instruction
31 0
CF Destination (Memory or Register)
RCR Instruction
31 0
Destination (Memory or Register) CF
The ROL instruction rotates the bits in the operand to the left (toward more signifi-
cant bit locations). The ROR instruction rotates the operand right (toward less signif-
icant bit locations).
The RCL instruction rotates the bits in the operand to the left, through the CF flag.
This instruction treats the CF flag as a one-bit extension on the upper end of the
operand. Each bit that exits from the most significant bit location of the operand
moves into the CF flag. At the same time, the bit in the CF flag enters the least signif-
icant bit location of the operand.
The RCR instruction rotates the bits in the operand to the right through the CF flag.
For all the rotate instructions, the CF flag always contains the value of the last bit
rotated out of the operand, even if the instruction does not use the CF flag as an
extension of the operand. The value of this flag can then be tested by a conditional
jump instruction (JC or JNC).
Vol. 1 7-19
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-20 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
“EFLAGS Condition Codes,” lists the conditions it is possible to test for with this
instruction.
Vol. 1 7-21
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-22 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
Vol. 1 7-23
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The destination operand specifies a relative address (a signed offset with respect to
the address in the EIP register) that points to an instruction in the current code
segment. The Jcc instructions do not support far transfers; however, far transfers can
be accomplished with a combination of a Jcc and a JMP instruction (see “Jcc—Jump if
Condition Is Met” in Chapter 3, “Instruction Set Reference, A-M,” of the Intel® 64
and IA-32 Architectures Software Developer’s Manual, Volume 2A).
Table 7-4 shows the mnemonics for the Jcc instructions and the conditions being
tested for each instruction. The condition code mnemonics are appended to the letter
“J” to form the mnemonic for a Jcc instruction. The instructions are divided into two
groups: unsigned and signed conditional jumps. These groups correspond to the
results of operations performed on unsigned and signed integers respectively. Those
instructions listed as pairs (for example, JA/JNBE) are alternate names for the same
instruction. Assemblers provide alternate names to make it easier to read program
listings.
The JCXZ and JECXZ instructions test the CX and ECX registers, respectively, instead
of one or more status flags. See “Jump if zero instructions” on page 7-25 for more
information about these instructions.
Loop instructions — The LOOP, LOOPE (loop while equal), LOOPZ (loop while zero),
LOOPNE (loop while not equal), and LOOPNZ (loop while not zero) instructions are
conditional jump instructions that use the value of the ECX register as a count for the
number of times to execute a loop. All the loop instructions decrement the count in
the ECX register each time they are executed and terminate a loop when zero is
reached. The LOOPE, LOOPZ, LOOPNE, and LOOPNZ instructions also accept the ZF
flag as a condition for terminating the loop before the count reaches zero.
The LOOP instruction decrements the contents of the ECX register (or the CX register,
if the address-size attribute is 16), then tests the register for the loop-termination
condition. If the count in the ECX register is non-zero, program control is transferred
to the instruction address specified by the destination operand. The destination
operand is a relative address (that is, an offset relative to the contents of the EIP
register), and it generally points to the first instruction in the block of code that is to
be executed in the loop. When the count in the ECX register reaches zero, program
control is transferred to the instruction immediately following the LOOP instruc-
tion, which terminates the loop. If the count in the ECX register is zero when the
LOOP instruction is first executed, the register is pre-decremented to FFFFFFFFH,
causing the loop to be executed 232 times.
The LOOPE and LOOPZ instructions perform the same operation (they are
mnemonics for the same instruction). These instructions operate the same as the
LOOP instruction, except that they also test the ZF flag.
If the count in the ECX register is not zero and the ZF flag is set, program control is
transferred to the destination operand. When the count reaches zero or the ZF flag is
clear, the loop is terminated by transferring program control to the instruction imme-
diately following the LOOPE/LOOPZ instruction.
7-24 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The LOOPNE and LOOPNZ instructions (mnemonics for the same instruction) operate
the same as the LOOPE/LOOPPZ instructions, except that they terminate the loop if
the ZF flag is set.
Jump if zero instructions — The JECXZ (jump if ECX zero) instruction jumps to the
location specified in the destination operand if the ECX register contains the value
zero. This instruction can be used in combination with a loop instruction (LOOP,
LOOPE, LOOPZ, LOOPNE, or LOOPNZ) to test the ECX register prior to beginning a
loop. As described in “Loop instructions on page 7-24, the loop instructions decre-
ment the contents of the ECX register before testing for zero. If the value in the ECX
register is zero initially, it will be decremented to FFFFFFFFH on the first loop instruc-
tion, causing the loop to be executed 232 times. To prevent this problem, a JECXZ
instruction can be inserted at the beginning of the code block for the loop, causing a
jump out the loop if the EAX register count is initially zero. When used with repeated
string scan and compare instructions, the JECXZ instruction can determine whether
the loop terminated because the count reached zero or because the scan or compare
conditions were satisfied.
The JCXZ (jump if CX is zero) instruction operates the same as the JECXZ instruction
when the 16-bit address-size attribute is used. Here, the CX register is tested for
zero.
Vol. 1 7-25
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The INT n instruction can raise any of the processor’s interrupts or exceptions by
encoding the vector number or the interrupt or exception in the instruction. This
instruction can be used to support software generated interrupts or to test the oper-
ation of interrupt and exception handlers.
The IRET (return from interrupt) instruction returns program control from an inter-
rupt handler to the interrupted procedure. The IRET instruction performs a similar
operation to the RET instruction.
The CALL (call procedure) and RET (return from procedure) instructions allow a jump
from one procedure to another and a subsequent return to the calling procedure.
EFLAGS register contents are automatically stored on the stack along with the return
instruction pointer when the processor services an interrupt.
The INTO instruction raises the overflow exception if the OF flag is set. If the flag is
clear, execution continues without raising the exception. This instruction allows soft-
ware to access the overflow exception handler explicitly to check for overflow condi-
tions.
The BOUND instruction compares a signed value against upper and lower bounds,
and raises the “BOUND range exceeded” exception if the value is less than the lower
bound or greater than the upper bound. This instruction is useful for operations such
as checking an array index to make sure it falls within the range defined for the array.
7-26 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
strings can be located in the same segment. (This latter condition can also be
achieved by loading the DS and ES segment registers with the same segment
selector and allowing the ESI register to default to the DS register.)
The MOVS instruction moves the string element addressed by the ESI register to the
location addressed by the EDI register. The assembler recognizes three “short forms”
of this instruction, which specify the size of the string to be moved: MOVSB (move
byte string), MOVSW (move word string), and MOVSD (move doubleword string).
The CMPS instruction subtracts the destination string element from the source string
element and updates the status flags (CF, ZF, OF, SF, PF, and AF) in the EFLAGS
register according to the results. Neither string element is written back to memory.
The assembler recognizes three “short forms” of the CMPS instruction: CMPSB
(compare byte strings), CMPSW (compare word strings), and CMPSD (compare
doubleword strings).
The SCAS instruction subtracts the destination string element from the contents of
the EAX, AX, or AL register (depending on operand length) and updates the status
flags according to the results. The string element and register contents are not modi-
fied. The following “short forms” of the SCAS instruction specify the operand length:
SCASB (scan byte string), SCASW (scan word string), and SCASD (scan doubleword
string).
The LODS instruction loads the source string element identified by the ESI register
into the EAX register (for a doubleword string), the AX register (for a word string), or
the AL register (for a byte string). The “short forms” for this instruction are LODSB
(load byte string), LODSW (load word string), and LODSD (load doubleword string).
This instruction is usually used in a loop, where other instructions process each
element of the string after they are loaded into the target register.
The STOS instruction stores the source string element from the EAX (doubleword
string), AX (word string), or AL (byte string) register into the memory location iden-
tified with the EDI register. The “short forms” for this instruction are STOSB (store
byte string), STOSW (store word string), and STOSD (store doubleword string). This
instruction is also normally used in a loop. Here a string is commonly loaded into
the register with a LODS instruction, operated on by other instructions, and then
stored again in memory with a STOS instruction.
The I/O instructions (see Section 7.3.11, “I/O Instructions”) also perform operations
on strings in memory.
Vol. 1 7-27
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
at higher addresses and work toward lower ones, or they can begin at lower
addresses and work toward higher ones. The DF flag in the EFLAGS register controls
whether the registers are incremented (DF = 0) or decremented (DF = 1). The STD
and CLD instructions set and clear this flag, respectively.
The following repeat prefixes can be used in conjunction with a count in the ECX
register to cause a string instruction to repeat:
• REP — Repeat while the ECX register not zero.
• REPE/REPZ — Repeat while the ECX register not zero and the ZF flag is set.
• REPNE/REPNZ — Repeat while the ECX register not zero and the ZF flag is clear.
When a string instruction has a repeat prefix, the operation executes until one of the
termination conditions specified by the prefix is satisfied. The REPE/REPZ and
REPNE/REPNZ prefixes are used only with the CMPS and SCAS instructions. Also,
note that a REP STOS instruction is the fastest way to initialize a large block of
memory.
7-28 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The block I/O instructions (INS and OUTS) instructions move blocks of data (strings)
between an I/O port and memory. These instructions operate similar to the string
instructions (see Section 7.3.9, “String Operations”). The ESI and EDI registers are
used to specify string elements in memory and the repeat prefixes (REP) are used to
repeat the instructions to implement block moves. The assembler recognizes the
following alternate mnemonics for these instructions: INSB (input byte), INSW (input
word), and INSD (input doubleword), and OUTB (output byte), OUTW (output word),
and OUTD (output doubleword).
The INS and OUTS instructions use an address in the DX register to specify the I/O
port to be read or written to.
Vol. 1 7-29
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
uses the flag in an operation is executed. They are also used in conjunction with the
rotate-with-carry instructions (RCL and RCR).
The STD (set direction flag) and CLD (clear direction flag) instructions allow the DF
flag in the EFLAGS register to be modified directly. The DF flag determines the direc-
tion in which index registers ESI and EDI are stepped when executing string
processing instructions. If the DF flag is clear, the index registers are incremented
after each iteration of a string instruction; if the DF flag is set, the registers are
decremented.
PUSHFD/POPFD
PUSHF/POPF
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I
V V
A V R 0 N O O D I T S Z P C
0 0 0 0 0 0 0 0 0 0 I I I A
D C M F T P F F F F F F 0 F 0 F 1 F
P F
L
Figure 7-11. Flags Affected by the PUSHF, POPF, PUSHFD, and POPFD Instructions
The POPF instruction pops a word from the stack into the EFLAGS register. Only bits
11, 10, 8, 7, 6, 4, 2, and 0 of the EFLAGS register are affected with all uses of this
instruction. If the current privilege level (CPL) of the current code segment is 0 (most
privileged), the IOPL bits (bits 13 and 12) also are affected. If the I/O privilege level
(IOPL) is greater than or equal to the CPL, numerically, the IF flag (bit 9) also is
affected.
7-30 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
The POPFD instruction pops a doubleword into the EFLAGS register. This instruction
can change the state of the AC bit (bit 18) and the ID bit (bit 21), as well as the bits
affected by a POPF instruction. The restrictions for changing the IOPL bits and the IF
flag that were given for the POPF instruction also apply to the POPFD instruction.
Vol. 1 7-31
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
segment registers (DS, ES, FS, GS, and SS). The transfers are always made to or
from a segment register and a general-purpose register or memory. Transfers
between segment registers are not supported.
The POP and MOV instructions cannot place a value in the CS register. Only the far
control-transfer versions of the JMP, CALL, and RET instructions (see Section
7.3.16.2, “Far Control Transfer Instructions”) affect the CS register directly.
7-32 Vol. 1
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
• Processor identification
• NOP and undefined instruction entry
Vol. 1 7-33
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7-34 Vol. 1
CHAPTER 8
PROGRAMMING WITH THE X87 FPU
Vol. 1 8-1
PROGRAMMING WITH THE X87 FPU
x87 FPU and MMX instructions, the programmer must explicitly manage the x87 FPU
and MMX state (see Section 9.5, “Compatibility with x87 FPU Architecture”).
8-2 Vol. 1
PROGRAMMING WITH THE X87 FPU
Data Registers
Sign 79 78 64 63 0
R7 Exponent Significand
R6
R5
R4
R3
R2
R1
R0
15 0 47 0
Control Last Instruction Pointer
Register
Tag 10 0
Register
Opcode
The x87 FPU instructions treat the eight x87 FPU data registers as a register stack (see
Figure 8-2). All addressing of the data registers is relative to the register on the top of
the stack. The register number of the current top-of-stack register is stored in the
TOP (stack TOP) field in the x87 FPU status word. Load operations decrement TOP by
one and load a value into the new top-of-stack register, and store operations store
the value from the current TOP register in memory and then increment TOP by one.
(For the x87 FPU, a load operation is equivalent to a push and a store operation is
equivalent to a pop.) Note that load and store operations are also available that do
not push and pop the stack.
Vol. 1 8-3
PROGRAMMING WITH THE X87 FPU
8-4 Vol. 1
PROGRAMMING WITH THE X87 FPU
Computation
Dot Product = (5.6 x 2.4) + (3.8 x 10.3)
Code:
FLD value1 ;(a) value1 = 5.6
FMUL value2 ;(b) value2 = 2.4
FLD value3 ; value3 = 3.8
FMUL value4 ;(c)value4 = 10.3
FADD ST(1) ;(d)
Vol. 1 8-5
PROGRAMMING WITH THE X87 FPU
FPU Busy
Top of Stack Pointer
15 14 13 11 10 9 8 7 6 5 4 3 2 1 0
C C C C E S P U O Z D I
B TOP
3 2 1 0 S F E E E E E E
Condition
Code
Error Summary Status
Stack Fault
Exception Flags
Precision
Underflow
Overflow
Zero Divide
Denormalized Operand
Invalid Operation
The contents of the x87 FPU status register (referred to as the x87 FPU status word)
can be stored in memory using the FSTSW/FNSTSW, FSTENV/FNSTENV,
FSAVE/FNSAVE, and FXSAVE instructions. It can also be stored in the AX register of
the integer unit, using the FSTSW/FNSTSW instructions.
8-6 Vol. 1
PROGRAMMING WITH THE X87 FPU
are used principally for conditional branching and for storage of information used in
exception handling (see Section 8.1.4, “Branching and Conditional Moves on Condi-
tion Codes”).
As shown in Table 8-1, the C1 condition code flag is used for a variety of functions.
When both the IE and SF flags in the x87 FPU status word are set, indicating a stack
overflow or underflow exception (#IS), the C1 flag distinguishes between overflow
(C1 = 1) and underflow (C1 = 0). When the PE flag in the status word is set, indi-
cating an inexact (rounded) result, the C1 flag is set to 1 if the last rounding by the
instruction was upward. The FXAM instruction sets C1 to the sign of the value being
examined.
The C2 condition code flag is used by the FPREM and FPREM1 instructions to indicate
an incomplete reduction (or partial remainder). When a successful reduction has
been completed, the C0, C3, and C1 condition code flags are set to the three least-
significant bits of the quotient (Q2, Q1, and Q0, respectively). See “FPREM1—Partial
Remainder” in Chapter 3, “Instruction Set Reference, A-M,” of the Intel® 64 and
IA-32 Architectures Software Developer’s Manual, Volume 2A, for more information
on how these instructions use the condition code flags.
The FPTAN, FSIN, FCOS, and FSINCOS instructions set the C2 flag to 1 to indicate
that the source operand is beyond the allowable range of ±263 and clear the C2 flag
if the source operand is within the allowable range.
Where the state of the condition code flags are listed as undefined in Table 8-1, do
not rely on any specific value in these flags.
Vol. 1 8-7
PROGRAMMING WITH THE X87 FPU
8-8 Vol. 1
PROGRAMMING WITH THE X87 FPU
Vol. 1 8-9
PROGRAMMING WITH THE X87 FPU
SAHF Instruction
31 EFLAGS Register 7 0
Z P C
F F 1 F
The new mechanism is available beginning with the P6 family processors. Using this
mechanism, the new floating-point compare and set EFLAGS instructions (FCOMI,
FCOMIP, FUCOMI, and FUCOMIP) compare two floating-point values and set the ZF,
PF, and CF flags in the EFLAGS register directly. A single instruction thus replaces the
three instructions required by the old mechanism.
Note also that the FCMOVcc instructions (also new in the P6 family processors) allow
conditional moves of floating-point values (values in the x87 FPU data registers)
based on the setting of the status flags (ZF, PF, and CF) in the EFLAGS register. These
instructions eliminate the need for an IF statement to perform conditional moves of
floating-point values.
8-10 Vol. 1
PROGRAMMING WITH THE X87 FPU
Infinity Control
Rounding Control
Precision Control
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
P U O Z D I
X RC PC M M M M M M
Exception Masks
Precision
Underflow
Overflow
Zero Divide
Denormal Operand
Invalid Operation
Reserved
Vol. 1 8-11
PROGRAMMING WITH THE X87 FPU
The double precision and single precision settings reduce the size of the significand to
53 bits and 24 bits, respectively. These settings are provided to support IEEE Stan-
dard 754 and to provide compatibility with the specifications of certain existing
programming languages. Using these settings nullifies the advantages of the double
extended-precision floating-point format's 64-bit significand length. When reduced
precision is specified, the rounding of the significand value clears the unused bits on
the right to zeros.
The precision-control bits only affect the results of the following floating-point
instructions: FADD, FADDP, FIADD, FSUB, FSUBP, FISUB, FSUBR, FSUBRP, FISUBR,
FMUL, FMULP, FIMUL, FDIV, FDIVP, FIDIV, FDIVR, FDIVRP, FIDIVR, and FSQRT.
8-12 Vol. 1
PROGRAMMING WITH THE X87 FPU
15 0
TAG Values
00 — Valid
01 — Zero
10 — Special: invalid (NaN, unsupported), infinity, or denormal
11 — Empty
Each tag in the x87 FPU tag word corresponds to a physical register (numbers 0
through 7). The current top-of-stack (TOP) pointer stored in the x87 FPU status word
can be used to associate tags with registers relative to ST(0).
The x87 FPU uses the tag values to detect stack overflow and underflow conditions
(see Section 8.5.1.1, “Stack Overflow or Underflow Exception (#IS)”).
Application programs and exception handlers can use this tag information to check
the contents of an x87 FPU data register without performing complex decoding of the
actual data in the register. To read the tag register, it must be stored in memory using
either the FSTENV/FNSTENV or FSAVE/FNSAVE instructions. The location of the tag
word in memory after being saved with one of these instructions is shown in Figures
8-9 through 8-12.
Software cannot directly load or modify the tags in the tag register. The FLDENV and
FRSTOR instructions load an image of the tag register into the x87 FPU; however, the
x87 FPU uses those tag values only to determine if the data registers are empty
(11B) or non-empty (00B, 01B, or 10B).
If the tag register image indicates that a data register is empty, the tag in the tag
register for that data register is marked empty (11B); if the tag register image indi-
cates that the data register is non-empty, the x87 FPU reads the actual value in the
data register and sets the tag for the register accordingly. This action prevents a
program from setting the values in the tag register to incorrectly represent the actual
contents of non-empty data registers.
Vol. 1 8-13
PROGRAMMING WITH THE X87 FPU
Note that the value in the x87 FPU data pointer register is always a pointer to a
memory operand, If the last non-control instruction that was executed did not have
a memory operand, the value in the data pointer register is undefined (reserved).
The contents of the x87 FPU instruction and data pointer registers remain unchanged
when any of the control instructions (FINIT/FNINIT, FCLEX/FNCLEX, FLDCW,
FSTCW/FNSTCW, FSTSW/FNSTSW, FSTENV/FNSTENV, FLDENV, FSAVE/FNSAVE,
FRSTOR, and WAIT/FWAIT) are executed.
The pointers stored in the x87 FPU instruction and data pointer registers consist of an
offset (stored in bits 0 through 31) and a segment selector (stored in bits 32
through 47).
These registers can be accessed by the FSTENV/FNSTENV, FLDENV, FINIT/FNINIT,
FSAVE/FNSAVE, FRSTOR, FXSAVE, and FXRSTOR instructions. The FINIT/FNINIT and
FSAVE/FNSAVE instructions clear these registers.
For all the x87 FPUs and NPXs except the 8087, the x87 FPU instruction pointer points
to any prefixes that preceded the instruction. For the 8087, the x87 FPU instruction
pointer points only to the actual opcode.
8-14 Vol. 1
PROGRAMMING WITH THE X87 FPU
10 8 7 0
The fopcode compatibility mode should be enabled only when x87 FPU floating-point
exception handlers are designed to use the fopcode to analyze program performance
or restart a program after an exception has been handled.
Vol. 1 8-15
PROGRAMMING WITH THE X87 FPU
For instructions that also store x87 FPU data registers, the eight
80-bit registers (R0-R7) follow the above structure in sequence.
Figure 8-9. Protected Mode x87 FPU State Image in Memory, 32-Bit Format
For instructions that also store x87 FPU data registers, the eight
80-bit registers (R0-R7) follow the above structure in sequence.
Figure 8-10. Real Mode x87 FPU State Image in Memory, 32-Bit Format
8-16 Vol. 1
PROGRAMMING WITH THE X87 FPU
Figure 8-11. Protected Mode x87 FPU State Image in Memory, 16-Bit Format
Figure 8-12. Real Mode x87 FPU State Image in Memory, 16-Bit Format
Vol. 1 8-17
PROGRAMMING WITH THE X87 FPU
8-18 Vol. 1
PROGRAMMING WITH THE X87 FPU
Single-Precision Floating-Point
Sign Exp. Fraction
3130 23 22 Implied Integer 0
Double-Precision Floating-Point
Sign Exponent Fraction
63 62 52 51 Implied Integer 0
Sign
Double Extended-Precision Floating-Point
Exponent Fraction
79 78 6463 62 Integer 0
Word Integer
Sign
15 14 0
Doubleword Integer
Sign
31 30 0
Quadword Integer
Sign
Sign 63 62 0
Packed BCD Integers
X D17 D16 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
79 78 72 71 4 Bits = 1 BCD Digit 0
8.2.1 Indefinites
For each x87 FPU data type, one unique encoding is reserved for representing the
special value indefinite. The x87 FPU produces indefinite values as responses to
some masked floating-point invalid-operation exceptions. See Tables 4-1, 4-3, and
Vol. 1 8-19
PROGRAMMING WITH THE X87 FPU
4-4 for the encoding of the integer indefinite, QNaN floating-point indefinite, and
packed BCD integer indefinite, respectively.
The binary integer encoding 100..00B represents either of two things, depending on
the circumstances of its use:
• The largest negative number supported by the format (–215, –231, or –263)
• The integer indefinite value
If this encoding is used as a source operand (as in an integer load or integer arith-
metic instruction), the x87 FPU interprets it as the largest negative number repre-
sentable in the format being used. If the x87 FPU detects an invalid operation when
storing an integer value in memory with an FIST/FISTP instruction and the invalid-
operation exception is masked, the x87 FPU stores the integer indefinite encoding in
the destination operand as a masked response to the exception. In situations where
the origin of a value with this encoding may be ambiguous, the invalid-operation
exception flag can be examined to see if the value was produced as a response to an
exception.
8-20 Vol. 1
PROGRAMMING WITH THE X87 FPU
Vol. 1 8-21
PROGRAMMING WITH THE X87 FPU
8-22 Vol. 1
PROGRAMMING WITH THE X87 FPU
The FILD (load integer) instruction converts an integer operand in memory into
double extended-precision floating-point format and pushes the value onto the top of
the register stack. The FBLD (load packed decimal) instruction performs the same
load operation for a packed BCD operand in memory.
The FST (store floating point) and FIST (store integer) instructions store the value in
register ST(0) in memory in the destination format (floating point or integer, respec-
tively). Again, the format conversion is carried out automatically.
The FSTP (store floating point and pop), FISTP (store integer and pop), and FBSTP
(store packed decimal and pop) instructions store the value in the ST(0) registers
into memory in the destination format (floating point, integer, or packed BCD), then
performs a pop operation on the register stack. A pop operation causes the ST(0)
register to be marked empty and the stack pointer (TOP) in the x87 FPU control work
to be incremented by 1. The FSTP instruction can also be used to copy the value in
the ST(0) register to another x87 FPU register [ST(i)].
The FXCH (exchange register contents) instruction exchanges the value in a selected
register in the stack [ST(i)] with the value in ST(0).
The FCMOVcc (conditional move) instructions move the value in a selected register in
the stack [ST(i)] to register ST(0) if a condition specified with a condition code (cc) is
satisfied (see Table 8-5). The condition being tested for is represented by the status
flags in the EFLAGS register. The condition code mnemonics are appended to the
letters “FCMOV” to form the mnemonic for a FCMOVcc instruction.
Vol. 1 8-23
PROGRAMMING WITH THE X87 FPU
Like the CMOVcc instructions, the FCMOVcc instructions are useful for optimizing
small IF constructions. They also help eliminate branching overhead for IF operations
and the possibility of branch mispredictions by the processor.
Software can check if the FCMOVcc instructions are supported by checking the
processor’s feature information with the CPUID instruction.
8-24 Vol. 1
PROGRAMMING WITH THE X87 FPU
The add, subtract, multiply and divide instructions operate on the following types of
operands:
• Two x87 FPU data registers
• An x87 FPU data register and a floating-point or integer value in memory
See Section 8.1.2, “x87 FPU Data Registers,” for a description of how operands are
referenced on the data register stack.
Operands in memory can be in single-precision floating-point, double-precision
floating-point, word-integer, or doubleword-integer format. They are converted to
double extended-precision floating-point format automatically.
Reverse versions of the subtract (FSUBR) and divide (FDIVR) instructions enable effi-
cient coding. For example, the following options are available with the FSUB and
FSUBR instructions for operating on values in a specified x87 FPU data register ST(i)
and the ST(0) register:
FSUB:
ST(0) ← ST(0) − ST(i)
ST(i) ← ST(i) − ST(0)
FSUBR:
ST(0) ← ST(i) − ST(0)
ST(i) ← ST(0) − ST(i)
These instructions eliminate the need to exchange values between the ST(0) register
and another x87 FPU register to perform a subtraction or division.
Vol. 1 8-25
PROGRAMMING WITH THE X87 FPU
The pop versions of the add, subtract, multiply, and divide instructions offer the
option of popping the x87 FPU register stack following the arithmetic operation.
These instructions operate on values in the ST(i) and ST(0) registers, store the result
in the ST(i) register, and pop the ST(0) register.
The FPREM instruction computes the remainder from the division of two operands in
the manner used by the Intel 8087 and Intel 287 math coprocessors; the FPREM1
instruction computes the remainder in the manner specified in IEEE Standard 754.
The FSQRT instruction computes the square root of the source operand.
The FRNDINT instruction returns a floating-point value that is the integral value
closest to the source value in the direction of the rounding mode specified in the RC
field of the x87 FPU control word.
The FABS, FCHS, and FXTRACT instructions perform convenient arithmetic opera-
tions. The FABS instruction produces the absolute value of the source operand. The
FCHS instruction changes the sign of the source operand. The FXTRACT instruction
separates the source operand into its exponent and fraction and stores each value in
a register in floating-point format.
8-26 Vol. 1
PROGRAMMING WITH THE X87 FPU
The FCOM, FCOMP, and FCOMPP instructions compare the value in register ST(0) with
a floating-point source operand and set the condition code flags (C0, C2, and C3) in
the x87 FPU status word according to the results (see Table 8-6).
If an unordered condition is detected (one or both of the values are NaNs or in an
undefined format), a floating-point invalid-operation exception is generated.
The pop versions of the instruction pop the x87 FPU register stack once or twice after
the comparison operation is complete.
The FUCOM, FUCOMP, and FUCOMPP instructions operate the same as the FCOM,
FCOMP, and FCOMPP instructions. The only difference is that with the FUCOM,
FUCOMP, and FUCOMPP instructions, if an unordered condition is detected because
one or both of the operands are QNaNs, the floating-point invalid-operation excep-
tion is not generated.
Table 8-6. Setting of x87 FPU Condition Code Flags for Floating-Point Number
Comparisons
Condition C3 C2 C0
ST(0) > Source Operand 0 0 0
ST(0) < Source Operand 0 0 1
ST(0) = Source Operand 1 0 0
Unordered 1 1 1
The FICOM and FICOMP instructions also operate the same as the FCOM and FCOMP
instructions, except that the source operand is an integer value in memory. The
integer value is automatically converted into an double extended-precision floating-
point value prior to making the comparison. The FICOMP instruction pops the x87
FPU register stack following the comparison operation.
The FTST instruction performs the same operation as the FCOM instruction, except
that the value in register ST(0) is always compared with the value 0.0.
The FCOMI and FCOMIP instructions were introduced into the IA-32 architecture in
the P6 family processors. They perform the same comparison as the FCOM and
FCOMP instructions, except that they set the status flags (ZF, PF, and CF) in the
EFLAGS register to indicate the results of the comparison (see Table 8-7) instead of
the x87 FPU condition code flags. The FCOMI and FCOMIP instructions allow condition
branch instructions (Jcc) to be executed directly from the results of their comparison.
Vol. 1 8-27
PROGRAMMING WITH THE X87 FPU
Table 8-7. Setting of EFLAGS Status Flags for Floating-Point Number Comparisons
Comparison Results ZF PF CF
ST0 > ST(i) 0 0 0
ST0 < ST(i) 0 0 1
ST0 = ST(i) 1 0 0
Unordered 1 1 1
Software can check if the FCOMI and FCOMIP instructions are supported by checking
the processor’s feature information with the CPUID instruction.
The FUCOMI and FUCOMIP instructions operate the same as the FCOMI and FCOMIP
instructions, except that they do not generate a floating-point invalid-operation
exception if the unordered condition is the result of one or both of the operands being
a QNaN. The FCOMIP and FUCOMIP instructions pop the x87 FPU register stack
following the comparison operation.
The FXAM instruction determines the classification of the floating-point value in the
ST(0) register (that is, whether the value is zero, a denormal number, a normal finite
number, ∞, a NaN, or an unsupported format) or that the register is empty. It sets the
x87 FPU condition code flags to indicate the classification (see “FXAM—Examine” in
Chapter 3, “Instruction Set Reference, A-M,” of the Intel® 64 and IA-32 Architec-
tures Software Developer’s Manual, Volume 2A). It also sets the C1 flag to indicate
the sign of the value.
8-28 Vol. 1
PROGRAMMING WITH THE X87 FPU
2. Check ordered comparison result. Use the constants given in Table 8-8 in the
TEST instruction to test for a less than, equal to, or greater than result, then use
the corresponding conditional branch instruction to transfer program control to
the appropriate procedure or section of code.
If a program or procedure has been thoroughly tested and it incorporates periodic
checks for QNaN results, then it is not necessary to check for the unordered result
every time a comparison is made.
See Section 8.1.4, “Branching and Conditional Moves on Condition Codes,” for
another technique for branching on x87 FPU condition codes.
Some non-comparison x87 FPU instructions update the condition code flags in the
x87 FPU status word. To ensure that the status word is not altered inadvertently,
store it immediately following a comparison operation.
FSIN Sine
FCOS Cosine
FSINCOS Sine and cosine
FPTAN Tangent
FPATAN Arctangent
These instructions operate on the top one or two registers of the x87 FPU register
stack and they return their results to the stack. The source operands for the FSIN,
FCOS, FSINCOS, and FPTAN instructions must be given in radians; the source
operand for the FPATAN instruction is given in rectangular coordinate units.
The FSINCOS instruction returns both the sine and the cosine of a source operand
value. It operates faster than executing the FSIN and FCOS instructions in succes-
sion.
The FPATAN instruction computes the arctangent of ST(1) divided by ST(0),
returning a result in radians. It is useful for converting rectangular coordinates to
polar coordinates.
Vol. 1 8-29
PROGRAMMING WITH THE X87 FPU
8.3.8 Pi
When the argument (source operand) of a trigonometric function is within the range
of the function, the argument is automatically reduced by the appropriate multiple of
2π through the same reduction mechanism used by the FPREM and FPREM1 instruc-
tions. The internal value of π that the x87 FPU uses for argument reduction and other
computations is as follows:
π = 0.f ∗ 22
where:
f = C90FDAA2 2168C234 C
(The spaces in the fraction above indicate 32-bit boundaries.)
This internal π value has a 66-bit mantissa, which is 2 bits more than is allowed in the
significand of an double extended-precision floating-point value. (Since 66 bits is not
an even number of hexadecimal digits, two additional zeros have been added to the
value so that it can be represented in hexadecimal format. The least-significant
hexadecimal digit (C) is thus 1100B, where the two least-significant bits represent
bits 67 and 68 of the mantissa.)
This value of π has been chosen to guarantee no loss of significance in a source
operand, provided the operand is within the specified range for the instruction.
If the results of computations that explicitly use π are to be used in the FSIN, FCOS,
FSINCOS, or FPTAN instructions, the full 66-bit fraction of π should be used. This
insures that the results are consistent with the argument-reduction algorithms that
these instructions use. Using a rounded version of π can cause inaccuracies in result
values, which if propagated through several calculations, might result in meaningless
results.
A common method of representing the full 66-bit fraction of π is to separate the value
into two numbers (highπ and lowπ) that when added together give the value for π
shown earlier in this section with the full 66-bit fraction:
π = highπ + lowπ
For example, the following two values (given in scientific notation with the fraction in
hexadecimal and the exponent in decimal) represent the 33 most-significant and the
33 least-significant bits of the fraction:
highπ (unnormalized) = 0.C90FDAA20 * 2+2
lowπ (unnormalized) = 0.42D184698 * 2− 31
These values encoded in the IEEE double-precision floating-point format are as
follows:
highπ = 400921FB 54400000
lowπ = 3DE0B461 1A600000
(Note that in the IEEE double-precision floating-point format, the exponents are
biased (by 1023) and the fractions are normalized.)
Similar versions of π can also be written in double extended-precision floating-point
format.
8-30 Vol. 1
PROGRAMMING WITH THE X87 FPU
FYL2X Logarithm
FYL2XP1 Logarithm epsilon
F2XM1 Exponential
FSCALE Scale
The FYL2X and FYL2XP1 instructions perform two different base 2 logarithmic opera-
tions. The FYL2X instruction computes (y ∗ log2x). This operation permits the calcu-
lation of the log of any base using the following equation:
logb x = (1/log2 b) ∗ log2 x
The FYL2XP1 instruction computes (y ∗ log2(x + 1)). This operation provides
optimum accuracy for values of x that are close to 0.
The F2XM1 instruction computes (2x − 1). This instruction only operates on source
values in the range −1.0 to +1.0.
The FSCALE instruction multiplies the source operand by a power of 2.
( x ) – F ( x )-
error = f--------------------------
k – 63
2
Vol. 1 8-31
PROGRAMMING WITH THE X87 FPU
–k
1≤2 f ( x ) < 2.
With the Pentium processor and later IA-32 processors, the worst case error on
transcendental functions is less than 1 ulp when rounding to the nearest (even) and
less than 1.5 ulps when rounding in other modes. The functions are guaranteed to be
monotonic, with respect to the input operands, throughout the domain supported by
the instruction.
The instructions FYL2X and FYL2XP1 are two operand instructions and are guaran-
teed to be within 1 ulp only when y equals 1. When y is not equal to 1, the maximum
ulp error is always within 1.35 ulps in round to nearest mode. (For the two operand
functions, monotonicity was proved by holding one of the operands constant.)
8-32 Vol. 1
PROGRAMMING WITH THE X87 FPU
NOTES
When operating a Pentium or Intel486 processor in MS-DOS compat-
ibility mode, it is possible (under unusual circumstances) for a non-
waiting instruction to be interrupted prior to being executed to
handle a pending x87 FPU exception. The circumstances where this
can happen and the resulting action of the processor are described in
Vol. 1 8-33
PROGRAMMING WITH THE X87 FPU
Section D.2.1.3, “No-Wait x87 FPU Instructions Can Get x87 FPU
Interrupt in Window.”
When operating a P6 family, Pentium 4, or Intel Xeon processor in
MS-DOS compatibility mode, non-waiting instructions can not be
interrupted in this way (see Section D.2.2, “MS-DOS* Compatibility
Sub-mode in the P6 Family and Pentium® 4 Processors”).
NOTE
Section 4.9.1, “Floating-Point Exception Conditions,” provides a
general overview of how the IA-32 processor detects and handles the
8-34 Vol. 1
PROGRAMMING WITH THE X87 FPU
Vol. 1 8-35
PROGRAMMING WITH THE X87 FPU
8-36 Vol. 1
PROGRAMMING WITH THE X87 FPU
NOTES
The term stack overflow originates from the situation where the
program has loaded (pushed) eight values from memory onto the
x87 FPU register stack and the next value pushed on the stack causes
a stack wraparound to a register that already contains a value.
The term stack underflow originates from the opposite situation.
Here, a program has stored (popped) eight values from the x87 FPU
register stack to memory and the next value popped from the stack
causes stack wraparound to an empty register.
When the x87 FPU detects stack overflow or underflow, it sets the IE flag (bit 0) and
the SF flag (bit 6) in the x87 FPU status word to 1. It then sets condition-code flag C1
(bit 9) in the x87 FPU status word to 1 if stack overflow occurred or to 0 if stack
underflow occurred.
If the invalid-operation exception is masked, the x87 FPU returns the floating point,
integer, or packed decimal integer indefinite value to the destination operand,
depending on the instruction being executed. This value overwrites the destination
register or memory location specified by the instruction.
If the invalid-operation exception is not masked, a software exception handler is
invoked (see Section 8.7, “Handling x87 FPU Exceptions in Software”) and the top-
of-stack pointer (TOP) and source operands remain unchanged.
Vol. 1 8-37
PROGRAMMING WITH THE X87 FPU
8-38 Vol. 1
PROGRAMMING WITH THE X87 FPU
Normally, when one or both of the source operands is a QNaN (and neither is an
SNaN or in an unsupported format), an invalid-operand exception is not generated.
An exception to this rule is most of the compare instructions (such as the FCOM and
FCOMI instructions) and the floating-point to integer conversion instructions
(FIST/FISTP and FBSTP). With these instructions, a QNaN source operand will
generate an invalid-operand exception.
Vol. 1 8-39
PROGRAMMING WITH THE X87 FPU
8-40 Vol. 1
PROGRAMMING WITH THE X87 FPU
The action that the x87 FPU takes when numeric overflow occurs and the numeric-
overflow exception is not masked, depends on whether the instruction is supposed to
store the result in memory or on the register stack.
• Destination is a memory location — The OE flag is set and a software
exception handler is invoked (see Section 8.7, “Handling x87 FPU Exceptions in
Software”). The top-of-stack pointer (TOP) and source and destination operands
remain unchanged. Because the data in the stack is in double extended-precision
format, the exception handler has the option either of re-executing the store
instruction after proper adjustment of the operand or of rounding the significand
on the stack to the destination's precision as the standard requires. The
exception handler should ultimately store a value into the destination location in
memory if the program is to continue.
• Destination is the register stack — The significand of the result is rounded
according to current settings of the precision and rounding control bits in the x87
FPU control word and the exponent of the result is adjusted by dividing it by
224576. (For instructions not affected by the precision field, the significand is
rounded to double-extended precision.) The resulting value is stored in the
destination operand. Condition code bit C1 in the x87 FPU status word (called in
this situation the “round-up bit”) is set if the significand was rounded upward and
cleared if the result was rounded toward 0. After the result is stored, the OE flag
is set and a software exception handler is invoked. The scaling bias value 24,576
is equal to 3 ∗ 213. Biasing the exponent by 24,576 normally translates the
number as nearly as possible to the middle of the double extended-precision
floating-point exponent range so that, if desired, it can be used in subsequent
scaled operations with less risk of causing further exceptions.
When using the FSCALE instruction, massive overflow can occur, where the result
is too large to be represented, even with a bias-adjusted exponent. Here, if
overflow occurs again, after the result has been biased, a properly signed ∞ is
stored in the destination operand.
Vol. 1 8-41
PROGRAMMING WITH THE X87 FPU
The flag (UE) for the numeric-underflow exception is bit 4 of the x87 FPU status
word, and the mask bit (UM) is bit 4 of the x87 FPU control word.
When a numeric-underflow condition occurs and the exception is masked, the x87
FPU performs the operation described in Section 4.9.1.5, “Numeric Underflow Excep-
tion (#U).”
When the exception is not masked, the action of the x87 FPU depends on whether the
instruction is supposed to store the result in a memory location or on the x87 FPU
resister stack.
• Destination is a memory location — (Can occur only with a store instruction.)
The UE flag is set and a software exception handler is invoked (see Section 8.7,
“Handling x87 FPU Exceptions in Software”). The top-of-stack pointer (TOP) and
source and destination operands remain unchanged, and no result is stored in
memory.
Because the data in the stack is in double extended-precision format, the
exception handler has the option either of re-exchanges the store instruction
after proper adjustment of the operand or of rounding the significand on the
stack to the destination's precision as the standard requires. The exception
handler should ultimately store a value into the destination location in memory if
the program is to continue.
• Destination is the register stack — The significand of the result is rounded
according to current settings of the precision and rounding control bits in the x87
FPU control word and the exponent of the result is adjusted by multiplying it by
224576. (For instructions not affected by the precision field, the significand is
rounded to double extended precision.) The resulting value is stored in the
destination operand. Condition code bit C1 in the x87 FPU status register (acting
here as a “round-up bit”) is set if the significand was rounded upward and cleared
if the result was rounded toward 0. After the result is stored, the UE flag is set
and a software exception handler is invoked. The scaling bias value 24,576 is the
same as is used for the overflow exception and has the same effect, which is to
translate the result as nearly as possible to the middle of the double extended-
precision floating-point exponent range.
When using the FSCALE instruction, massive underflow can occur, where the
result is too tiny to be represented, even with a bias-adjusted exponent. Here, if
underflow occurs again after the result has been biased, a properly signed 0 is
stored in the destination operand.
8-42 Vol. 1
PROGRAMMING WITH THE X87 FPU
The inexact-result exception flag (PE) is bit 5 of the x87 FPU status word, and the
mask bit (PM) is bit 5 of the x87 FPU control word.
If the inexact-result exception is masked when an inexact-result condition occurs and
a numeric overflow or underflow condition has not occurred, the x87 FPU handles the
exception as describe in Section 4.9.1.6, “Inexact-Result (Precision) Exception (#P),”
with one additional action. The C1 (round-up) bit in the x87 FPU status word is set to
indicate whether the inexact result was rounded up (C1 is set) or “not rounded up”
(C1 is cleared). In the “not rounded up” case, the least-significant bits of the inexact
result are truncated so that the result fits in the destination format.
If the inexact-result exception is not masked when an inexact result occurs and
numeric overflow or underflow has not occurred, the x87 FPU handles the exception
as described in the previous paragraph and, in addition, invokes a software exception
handler.
If an inexact result occurs in conjunction with numeric overflow or underflow, the x87
FPU carries out one of the following operations:
• If an inexact result occurs in conjunction with masked overflow or underflow, the
OE or UE flag and the PE flag are set and the result is stored as described for the
overflow or underflow exceptions (see Section 8.5.4, “Numeric Overflow
Exception (#O),” or Section 8.5.5, “Numeric Underflow Exception (#U)”). If the
inexact result exception is unmasked, the x87 FPU also invokes a software
exception handler.
• If an inexact result occurs in conjunction with unmasked overflow or underflow
and the destination operand is a register, the OE or UE flag and the PE flag are
set, the result is stored as described for the overflow or underflow exceptions
(see Section 8.5.4, “Numeric Overflow Exception (#O),” or Section 8.5.5,
“Numeric Underflow Exception (#U)”) and a software exception handler is
invoked.
If an unmasked numeric overflow or underflow exception occurs and the destination
operand is a memory location (which can happen only for a floating-point store), the
inexact-result condition is not reported and the C1 flag is cleared.
Vol. 1 8-43
PROGRAMMING WITH THE X87 FPU
When a floating-point exception is unmasked and the exception condition occurs, the
x87 FPU stops further execution of the floating-point instruction and signals the
exception event. On the next occurrence of a floating-point instruction or a
WAIT/FWAIT instruction in the instruction stream, the processor checks the ES flag in
the x87 FPU status word for pending floating-point exceptions. If floating-point
exceptions are pending, the x87 FPU makes an implicit call (traps) to the floating-
point software exception handler. The exception handler can then execute recovery
procedures for selected or all floating-point exceptions.
Synchronization problems occur in the time between the moment when the excep-
tion is signaled and when it is actually handled. Because of concurrent execution,
integer or system instructions can be executed during this time. It is thus possible for
the source or destination operands for a floating-point instruction that faulted to be
overwritten in memory, making it impossible for the exception handler to analyze or
recover from the exception.
To solve this problem, an exception synchronizing instruction (either a floating-point
instruction or a WAIT/FWAIT instruction) can be placed immediately after any
floating-point instruction that might present a situation where state information
pertaining to a floating-point exception might be lost or corrupted. Floating-point
instructions that store data in memory are prime candidates for synchronization. For
example, the following three lines of code have the potential for exception synchro-
nization problems:
FILD COUNT ;Floating-point instruction
INC COUNT ;Integer instruction
FSQRT ;Subsequent floating-point instruction
In this example, the INC instruction modifies the source operand of the floating-point
instruction, FILD. If an exception is signaled during the execution of the FILD instruc-
tion, the INC instruction would be allowed to overwrite the value stored in the COUNT
memory location before the floating-point exception handler is called. With the
COUNT variable modified, the floating-point exception handler would not be able to
recover from the error.
Rearranging the instructions, as follows, so that the FSQRT instruction follows the
FILD instruction, synchronizes floating-point exception handling and eliminates the
possibility of the COUNT variable being overwritten before the floating-point excep-
tion handler is invoked.
FILD COUNT ;Floating-point instruction
FSQRT ;Subsequent floating-point instruction synchronizes
;any exceptions generated by the FILD instruction.
INC COUNT ;Integer instruction
The FSQRT instruction does not require any synchronization, because the results of
this instruction are stored in the x87 FPU data registers and will remain there, undis-
turbed, until the next floating-point or WAIT/FWAIT instruction is executed. To abso-
lutely insure that any exceptions emanating from the FSQRT instruction are handled
(for example, prior to a procedure call), a WAIT instruction can be placed directly
after the FSQRT instruction.
8-44 Vol. 1
PROGRAMMING WITH THE X87 FPU
Note that some floating-point instructions (non-waiting instructions) do not check for
pending unmasked exceptions (see Section 8.3.11, “x87 FPU Control Instructions”).
They include the FNINIT, FNSTENV, FNSAVE, FNSTSW, FNSTCW, and FNCLEX instruc-
tions. When an FNINIT, FNSTENV, FNSAVE, or FNCLEX instruction is executed, all
pending exceptions are essentially lost (either the x87 FPU status register is cleared
or all exceptions are masked). The FNSTSW and FNSTCW instructions do not check
for pending interrupts, but they do not modify the x87 FPU status and control regis-
ters. A subsequent “waiting” floating-point instruction can then handle any pending
exceptions.
Vol. 1 8-45
PROGRAMMING WITH THE X87 FPU
8-46 Vol. 1
PROGRAMMING WITH THE X87 FPU
Vol. 1 8-47
PROGRAMMING WITH THE X87 FPU
8-48 Vol. 1
CHAPTER 9
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
The Intel MMX technology was introduced into the IA-32 architecture in the
Pentium II processor family and Pentium processor with MMX technology. The exten-
sions introduced in MMX technology support a single-instruction, multiple-data
(SIMD) execution model that is designed to accelerate the performance of advanced
media and communications applications.
This chapter describes MMX technology.
Vol. 1 9-1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Address Space
232 -1
MMX Registers
Eight 64-Bit
General-Purpose
Registers
Eight 32-Bit
9-2 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
63 0
MM7
MM6
MM5
MM4
MM3
MM2
MM1
MM0
Vol. 1 9-3
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
63 0
63 0
63 0
9-4 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
The SIMD execution model supported in the MMX technology directly addresses the
needs of modern media, communications, and graphics applications, which often use
sophisticated algorithms that perform the same operations on a large number of
small data types (bytes, words, and doublewords). For example, most audio data is
represented in 16-bit (word) quantities. The MMX instructions can operate on 4
words simultaneously with one instruction. Video and graphics information is
commonly represented as palletized 8-bit (byte) quantities. In Figure 9-4, one MMX
instruction operates on 8 bytes simultaneously.
Source 1 X3 X2 X1 X0
Source 2 Y3 Y2 Y1 Y0
OP OP OP OP
Destination X3 OP Y3 X2 OP Y2 X1 OP Y1 X0 OP Y0
Vol. 1 9-5
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Saturation arithmetic provides an answer for many overflow situations. For example,
in color calculations, saturation causes a color to remain pure black or pure white
without allowing inversion. It also prevents wraparound artifacts from entering into
computations when range checking of source operands it not used.
MMX instructions do not indicate overflow or underflow occurrence by generating
exceptions or setting flags in the EFLAGS register.
9-6 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
NOTES
The MMX instructions described in this chapter are those instructions
that are available in an IA-32 processor when
CPUID.01H:EDX.MMX[bit 23] = 0.
Section 10.4.4, “SSE 64-Bit SIMD Integer Instructions,” and Section
11.4.2, “SSE2 64-Bit and 128-Bit SIMD Integer Instructions,” list
additional instructions included with SSE/SSE2 extensions that
operate on the MMX registers but are not considered part of the MMX
instruction set.
Vol. 1 9-7
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
9-8 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Vol. 1 9-9
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
can be used to convert byte integers to word integers, word integers to doubleword
integers, or doubleword integers to quadword integers.
9-10 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Example 9-1. Partial Routine for Detecting MMX Technology with the CPUID Instruction
... ; identify existence of CPUID instruction
... ; identify Intel processor
mov EAX, 1 ; request for feature flags
CPUID ; 0FH, 0A2H CPUID instruction
test EDX, 00800000H ; Is IA MMX technology bit (Bit 23 of EDX) set?
jnz ; MMX_Technology_Found
Vol. 1 9-11
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
9-12 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Vol. 1 9-13
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
9-14 Vol. 1
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
Vol. 1 9-15
PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY
9-16 Vol. 1
CHAPTER 10
PROGRAMMING WITH
STREAMING SIMD EXTENSIONS (SSE)
The streaming SIMD extensions (SSE) were introduced into the IA-32 architecture in
the Pentium III processor family. These extensions enhance the performance of IA-32
processors for advanced 2-D and 3-D graphics, motion video, image processing,
speech recognition, audio synthesis, telephony, and video conferencing.
This chapter describes SSE. Chapter 11, “Programming with Streaming SIMD Exten-
sions 2 (SSE2),” provides information to assist in writing application programs that
use SSE2 extensions. Chapter 12, “Programming with SSE3, SSSE3, AND SSE4,”
provides this information for SSE3 extensions.
Vol. 1 10-1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
10-2 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
Address Space
MMX Registers
Eight 64-Bit
General-Purpose
Registers
Eight 32-Bit
0
EFLAGS Register 32 Bits
• MXCSR register — This 32-bit register (see Figure 10-3 and Section 10.2.3,
“MXCSR Control and Status Register”) provides status and control bits used in
SIMD floating-point operations.
• MMX registers — These eight registers (see Figure 9-2) are used to perform
operations on 64-bit packed integer data. They are also used to hold operands for
some operations performed between the MMX and XMM registers. MMX registers
are referenced by the names MM0 through MM7.
• General-purpose registers — The eight general-purpose registers (see
Figure 3-5) are used along with the existing IA-32 addressing modes to address
operands in memory. (MMX and XMM registers cannot be used to address
memory). The general-purpose registers are also used to hold operands for some
Vol. 1 10-3
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
SSE instructions and are referenced as EAX, EBX, ECX, EDX, EBP, ESI, EDI, and
ESP.
• EFLAGS register — This 32-bit register (see Figure 3-8) is used to record result
of some compare operations.
127 0
XMM7
XMM6
XMM5
XMM4
XMM3
XMM2
XMM1
XMM0
SSE instructions use the XMM registers only to operate on packed single-precision
floating-point operands. SSE2 extensions expand the functions of the XMM registers
to operand on packed or scalar double-precision floating-point operands and packed
10-4 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
integer operands (see Section 11.2, “SSE2 Programming Environment,” and Section
12.1, “Programming Environment and Data types”).
XMM registers can only be used to perform calculations on data; they cannot be used
to address memory. Addressing memory is accomplished by using the general-
purpose registers.
Data can be loaded into XMM registers or written from the registers to memory in
32-bit, 64-bit, and 128-bit increments. When storing the entire contents of an XMM
register in memory (128-bit store), the data is stored in 16 consecutive bytes, with
the low-order byte of the register being stored in the first byte in memory.
Vol. 1 10-5
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
31 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
F R P U O Z D I D P U O Z D I
Reserved A
Z C M M M M M M E E E E E E
Z
Flush to Zero
Rounding Control
Precision Mask
Underflow Mask
Overflow Mask
Divide-by-Zero Mask
Denormal Operation Mask
Invalid Operation Mask
Denormals Are Zeros*
Precision Flag
Underflow Flag
Overflow Flag
Divide-by-Zero Flag
Denormal Flag
Invalid Operation Flag
* The denormals-are-zeros flag was introduced in the Pentium 4 and Intel Xeon processor.
10-6 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
10.2.3.3 Flush-To-Zero
Bit 15 (FZ) of the MXCSR register enables the flush-to-zero mode, which controls the
masked response to a SIMD floating-point underflow condition. When the underflow
exception is masked and the flush-to-zero mode is enabled, the processor performs
the following operations when it detects a floating-point underflow condition:
• Returns a zero result with the sign of the true result
• Sets the precision and underflow exception flags
If the underflow exception is not masked, the flush-to-zero bit is ignored.
The flush-to-zero mode is not compatible with IEEE Standard 754. The IEEE-
mandated masked response to underflow is to deliver the denormalized result (see
Section 4.8.3.2, “Normalized and Denormalized Finite Numbers”). The flush-to-zero
mode is provided primarily for performance reasons. At the cost of a slight precision
loss, faster execution can be achieved for applications where underflows are common
and rounding the underflow result to zero can be tolerated.
The flush-to-zero bit is cleared upon a power-up or reset of the processor, disabling
the flush-to-zero mode.
10.2.3.4 Denormals-Are-Zeros
Bit 6 (DAZ) of the MXCSR register enables the denormals-are-zeros mode, which
controls the processor’s response to a SIMD floating-point denormal operand condi-
tion. When the denormals-are-zeros flag is set, the processor converts all denormal
source operands to a zero with the sign of the original operand before performing any
computations on them. The processor does not set the denormal-operand exception
flag (DE), regardless of the setting of the denormal-operand exception mask bit
(DM); and it does not generate a denormal-operand exception if the exception is
unmasked.
The denormals-are-zeros mode is not compatible with IEEE Standard 754 (see
Section 4.8.3.2, “Normalized and Denormalized Finite Numbers”). The denormals-
are-zeros mode is provided to improve processor performance for applications such
as streaming media processing, where rounding a denormal operand to zero does
not appreciably affect the quality of the processed data.
The denormals-are-zeros flag is cleared upon a power-up or reset of the processor,
disabling the denormals-are-zeros mode.
The denormals-are-zeros mode was introduced in the Pentium 4 and Intel Xeon
processor with the SSE2 extensions; however, it is fully compatible with the SSE
Vol. 1 10-7
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
SIMD floating-point instructions (that is, the denormals-are-zeros flag affects the
operation of the SSE SIMD floating-point instructions). In earlier IA-32 processors
and in some models of the Pentium 4 processor, this flag (bit 6) is reserved. See
Section 11.6.3, “Checking for the DAZ Flag in the MXCSR Register,” for instructions
for detecting the availability of this feature.
Attempting to set bit 6 of the MXCSR register on processors that do not support the
DAZ flag will cause a general-protection exception (#GP). See Section 11.6.6,
“Guidelines for Writing to the MXCSR Register,” for instructions for preventing such
general-protection exceptions by using the MXCSR_MASK value returned by the
FXSAVE instruction.
Contains 4 Single-Precision
Floating-Point Values
127 96 95 64 63 32 31 0
10-8 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
Vol. 1 10-9
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
operand contains the results of the operation (OP) performed in parallel on the corre-
sponding values (X0 and Y0, X1 and Y1, X2 and Y2, and X3 and Y3) in each operand.
X3 X2 X1 X0
Y3 Y2 Y1 Y0
OP OP OP OP
X3 OP Y3 X2 OP Y2 X1 OP Y1 X0 OP Y0
X3 X2 X1 X0
Y3 Y2 Y1 Y0
OP
X3 X2 X1 X0 OP Y0
10-10 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
Vol. 1 10-11
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
The ADDPS (add packed single-precision floating-point values) and SUBPS (subtract
packed single-precision floating-point values) instructions add and subtract, respec-
tively, two packed single-precision floating-point operands.
The ADDSS (add scalar single-precision floating-point values) and SUBSS (subtract
scalar single-precision floating-point values) instructions add and subtract, respec-
tively, the low single-precision floating-point values of two operands and store the
result in the low doubleword of the destination operand.
The MULPS (multiply packed single-precision floating-point values) instruction multi-
plies two packed single-precision floating-point operands.
The MULSS (multiply scalar single-precision floating-point values) instruction multi-
plies the low single-precision floating-point values of two operands and stores the
result in the low doubleword of the destination operand.
The DIVPS (divide packed, single-precision floating-point values) instruction divides
two packed single-precision floating-point operands.
The DIVSS (divide scalar single-precision floating-point values) instruction divides
the low single-precision floating-point values of two operands and stores the result in
the low doubleword of the destination operand.
The RCPPS (compute reciprocals of packed single-precision floating-point values)
instruction computes the approximate reciprocals of values in a packed single-preci-
sion floating-point operand.
The RCPSS (compute reciprocal of scalar single-precision floating-point values)
instruction computes the approximate reciprocal of the low single-precision floating-
point value in the source operand and stores the result in the low doubleword of the
destination operand.
The SQRTPS (compute square roots of packed single-precision floating-point values)
instruction computes the square roots of the values in a packed single-precision
floating-point operand.
The SQRTSS (compute square root of scalar single-precision floating-point values)
instruction computes the square root of the low single-precision floating-point value
in the source operand and stores the result in the low doubleword of the destination
operand.
The RSQRTPS (compute reciprocals of square roots of packed single-precision
floating-point values) instruction computes the approximate reciprocals of the
square roots of the values in a packed single-precision floating-point operand.
The RSQRTSS (reciprocal of square root of scalar single-precision floating-point
value) instruction computes the approximate reciprocal of the square root of the low
single-precision floating-point value in the source operand and stores the result in
the low doubleword of the destination operand.
The MAXPS (return maximum of packed single-precision floating-point values)
instruction compares the corresponding values from two packed single-precision
floating-point operands and returns the numerically greater value from each compar-
ison to the destination operand.
10-12 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
Vol. 1 10-13
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
DEST X3 X2 X1 X0
SRC Y3 Y2 Y1 Y0
10-14 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
DEST X3 X2 X1 X0
SRC Y3 Y2 Y1 Y0
DEST Y3 X3 Y2 X2
DEST X3 X2 X1 X0
SRC Y3 Y2 Y1 Y0
DEST Y1 X1 Y0 X0
Vol. 1 10-15
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
NOTE
When SSE2 extensions are present in an IA-32 processor, these
instructions are extended to operate on 128-bit operands in XMM
registers and 128-bit memory locations.
The PAVGB (compute average of packed unsigned byte integers) and PAVGW
(compute average of packed unsigned word integers) instructions compute a SIMD
average of two packed unsigned byte or word integer operands, respectively. For
each corresponding pair of data elements in the packed source operands, the
elements are added together, a 1 is added to the temporary sum, and that result is
shifted right one bit position.
10-16 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
The PEXTRW (extract word) instruction copies a selected word from an MMX register
into a general-purpose register.
The PINSRW (insert word) instruction copies a word from a general-purpose register
or from memory into a selected word location in an MMX register.
The PMAXUB (maximum of packed unsigned byte integers) instruction compares the
corresponding unsigned byte integers in two packed operands and returns the
greater of each comparison to the destination operand.
The PMINUB (minimum of packed unsigned byte integers) instruction compares the
corresponding unsigned byte integers in two packed operands and returns the lesser
of each comparison to the destination operand.
The PMAXSW (maximum of packed signed word integers) instruction compares the
corresponding signed word integers in two packed operands and returns the greater
of each comparison to the destination operand.
The PMINSW (minimum of packed signed word integers) instruction compares the
corresponding signed word integers in two packed operands and returns the lesser of
each comparison to the destination operand.
The PMOVMSKB (move byte mask) instruction creates an 8-bit mask from the packed
byte integers in an MMX register and stores the result in the low byte of a general-
purpose register. The mask contains the most significant bit of each byte in the MMX
register. (When operating on 128-bit operands, a 16-bit mask is created.)
The PMULHUW (multiply packed unsigned word integers and store high result)
instruction performs a SIMD unsigned multiply of the words in the two source oper-
ands and returns the high word of each result to an MMX register.
The PSADBW (compute sum of absolute differences) instruction computes the SIMD
absolute differences of the corresponding unsigned byte integers in two source oper-
ands, sums the differences, and stores the sum in the low word of the destination
operand.
The PSHUFW (shuffle packed word integers) instruction shuffles the words in the
source operand according to the order specified by an 8-bit immediate operand and
returns the result to the destination operand.
Vol. 1 10-17
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
10-18 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
See also: Chapter 10, “Memory Cache Control,” in the Intel® 64 and IA-32 Architec-
tures Software Developer’s Manual, Volume 3A.
Using the WC semantics, the store transaction will be weakly ordered, meaning that
the data may not be written to memory in program order, and the store will not write
allocate (that is, the processor will not fetch the corresponding cache line into the
cache hierarchy, prior to performing the store). Also, different processor implemen-
tations may choose to collapse and combine these stores.
The memory type of the region being written to can override the non-temporal hint,
if the memory address specified for the non-temporal store is in uncacheable
memory. Uncacheable as referred to here means that the region being written to has
been mapped with either an uncacheable (UC) or write protected (WP) memory type.
In general, WC semantics require software to ensure coherence, with respect to
other processors and other system agents (such as graphics cards). Appropriate use
of synchronization and fencing must be performed for producer-consumer usage
models. Fencing ensures that all system agents have global visibility of the stored
data; for instance, failure to fence may result in a written cache line staying within a
processor and not being visible to other agents.
For processors that implement non-temporal stores by updating data in-place that
already resides in the cache hierarchy, the destination region should also be mapped
as WC. If mapped as WB or WT, there is the potential for speculative processor reads
to bring the data into the caches; in this case, non-temporal stores would then
update in place, and data would not be flushed from the processor by a subsequent
fencing operation.
The memory type visible on the bus in the presence of memory type aliasing is imple-
mentation specific. As one possible example, the memory type written to the bus
may reflect the memory type for the first store to this line, as seen in program order;
other alternatives are possible. This behavior should be considered reserved, and
dependence on the behavior of any particular implementation risks future incompat-
ibility.
Vol. 1 10-19
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
10-20 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
The FXSAVE and FXRSTOR instructions can be used in place of the FSAVE/FNSAVE
and FRSTOR instructions; however, the operation of the FXSAVE and FXRSTOR
instructions are not identical to the operation of FSAVE/FNSAVE and FRSTOR.
NOTE
The FXSAVE and FXRSTOR instructions are not considered part
of the SSE instruction group. They have a separate CPUID
feature bit to indicate whether they are present (if
CPUID.01H:EDX.FXSR[bit 24] = 1).
The CPUID feature bit for SSE extensions does not indicate the
presence of FXSAVE and FXRSTOR.
Vol. 1 10-21
PROGRAMMING WITH STREAMING SIMD EXTENSIONS (SSE)
10-22 Vol. 1
CHAPTER 11
PROGRAMMING WITH
STREAMING SIMD EXTENSIONS 2 (SSE2)
The streaming SIMD extensions 2 (SSE2) were introduced into the IA-32 architecture
in the Pentium 4 and Intel Xeon processors. These extensions enhance the perfor-
mance of IA-32 processors for advanced 3-D graphics, video decoding/encoding,
speech recognition, E-commerce, Internet, scientific, and engineering applications.
This chapter describes the SSE2 extensions and provides information to assist in
writing application programs that use these and the SSE extensions.
Vol. 1 11-1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
These new features extend the IA-32 architecture’s SIMD programming model in
three important ways:
• They provide the ability to perform SIMD operations on pairs of packed double-
precision floating-point values. This permits higher precision computations to be
carried out in XMM registers, which enhances processor performance in scientific
and engineering applications and in applications that use advanced 3-D geometry
techniques (such as ray tracing). Additional flexibility is provided with instruc-
tions that operate on single (scalar) double-precision floating-point values
located in the low quadword of an XMM register.
• They provide the ability to operate on 128-bit packed integers (bytes, words,
doublewords, and quadwords) in XMM registers. This provides greater flexibility
and greater throughput when performing SIMD operations on packed integers.
The capability is particularly useful for applications such as RSA authentication
and RC5 encryption. Using the full set of SIMD registers, data types, and instruc-
tions provided with the MMX technology and SSE/SSE2 extensions, programmers
can develop algorithms that finely mix packed single- and double-precision
floating-point data and 64- and 128-bit packed integer data.
• SSE2 extensions enhance the support introduced with SSE extensions for
controlling the cacheability of SIMD data. SSE2 cache control instructions provide
the ability to stream data in and out of the XMM registers without polluting the
caches and the ability to prefetch data before it is actually used.
SSE2 extensions are fully compatible with all software written for IA-32 processors.
All existing software continues to run correctly, without modification, on processors
that incorporate SSE2 extensions, as well as in the presence of applications that
incorporate these extensions. Enhancements to the CPUID instruction permit detec-
tion of the SSE2 extensions. Also, because the SSE2 extensions use the same regis-
ters as the SSE extensions, no new operating-system support is required for saving
and restoring program state during a context switch beyond that provided for the
SSE extensions.
SSE2 extensions are accessible from all IA-32 execution modes: protected mode,
real address mode, virtual 8086 mode.
The following sections in this chapter describe the programming environment for
SSE2 extensions including: the 128-bit XMM floating-point register set, data types,
and SSE2 instructions. It also describes exceptions that can be generated with the
SSE and SSE2 instructions and gives guidelines for writing applications with SSE and
SSE2 extensions.
For additional information about SSE2 extensions, see:
• Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volumes
2A & 2B, provide a detailed description of individual SSE3 instructions.
• Chapter 12, “System Programming for Streaming SIMD Instruction Sets,” in the
Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A,
gives guidelines for integrating the SSE and SSE2 extensions into an operating-
system environment.
11-2 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Address Space
MMX Registers
Eight 64-Bit
General-Purpose
Registers
Eight 32-Bit
0
EFLAGS Register 32 Bits
• MXCSR register — This 32-bit register (see Figure 10-3) provides status and
control bits used in floating-point operations. The denormals-are-zeros and
flush-to-zero flags in this register provide a higher performance alternative for
the handling of denormal source operands and denormal (underflow) results. For
more information on the functions of these flags see Section 10.2.3.4,
“Denormals-Are-Zeros,” and Section 10.2.3.3, “Flush-To-Zero.”
• MMX registers — These eight registers (see Figure 9-2) are used to perform
operations on 64-bit packed integer data. They are also used to hold operands for
some operations performed between MMX and XMM registers. MMX registers are
referenced by the names MM0 through MM7.
Vol. 1 11-3
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-4 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
All of these data types are operated on in XMM registers or memory. Instructions are
provided to convert between these 128-bit data types and the 64-bit and 32-bit data
types.
The address of a 128-bit packed memory operand must be aligned on a 16-byte
boundary, except in the following cases:
• a MOVUPD instruction which supports unaligned accesses
• scalar instructions that use an 8-byte memory operand that is not subject to
alignment requirements
Figure 4-2 shows the byte order of 128-bit (double quadword) and 64-bit (quad-
word) data types in memory.
Vol. 1 11-5
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
X1 X0
Y1 Y0
OP OP
X1 OP Y1 X0 OP Y0
11-6 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
X1 X0
Y1 Y0
OP
X1 X0 OP Y0
Vol. 1 11-7
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
an XMM register or vice versa, or between XMM registers. Alignment of the memory
address is not required, unless alignment checking is enabled.
The MOVHPD (move high packed double-precision floating-point) instruction trans-
fers a 64-bit double-precision floating-point operand from memory to the high quad-
word of an XMM register or vice versa. The low quadword of the register is left
unchanged. Alignment of the memory address is not required, unless alignment
checking is enabled.
The MOVLPD (move low packed double-precision floating-point) instruction transfers
a 64-bit double-precision floating-point operand from memory to the low quadword
of an XMM register or vice versa. The high quadword of the register is left unchanged.
Alignment of the memory address is not required, unless alignment checking is
enabled.
The MOVMSKPD (move packed double-precision floating-point mask) instruction
extracts the sign bit of each of the two packed double-precision floating-point
numbers in an XMM register and saves them in a general-purpose register. This 2-bit
value can then be used as a condition to perform branching.
11-8 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-9
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-10 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
DEST X1 X0
SRC Y1 Y0
DEST Y1 or Y0 X1 or X0
DEST X1 X0
SRC Y1 Y0
DEST Y1 X1
Vol. 1 11-11
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
DEST X1 X0
SRC Y1 Y0
DEST Y0 X0
11-12 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Single-Precision
Floating Point
SI I (XMM/mem)
SS2 2S
T SS CV
CV TT CV TPS
TT 2D
CV S PI PS Q
2S S2 2PI 2D
SI
C
T P
T S Q
VT
CV CV TTP
D
Q
CV
2P
S
2P
S
PI
4 Doubleword
VT
Integer
C
CVTSD2SS
CVTPD2PS
CVTPS2PD
CVTSS2SD
(XMM/mem)
Doubleword 2 Doubleword
Integer Integer 2 Doubleword
(r32/mem) (MMX/mem) Integer
(XMM/mem)
C
VT
D
2P
PI
Q
2P
D
D
VT
D DQ
C TT
VT S
C
Q
C
C
TP D2
V
2D
CV VT
SD D2
VT P
TT PD
C VT
2S SI
PD 2P
C
I
C
2P I
VT
I
SI
2S
Double-Precision
D
Floating-Point
(XMM/mem)
Vol. 1 11-13
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-14 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
operands in XMM registers or memory (the latter for at most one source operand).
When the conversion is inexact, the rounded value according to the rounding mode
selected in the MXCSR register is returned.
Vol. 1 11-15
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
The PSHUFD (shuffle packed doubleword integers) instruction shuffles the double-
word integers packed into the source operand and stores the shuffled result in the
destination operand. An 8-bit immediate operand specifies the shuffle order.
The PSLLDQ (shift double quadword left logical) instruction shifts the contents of the
source operand to the left by the amount of bytes specified by an immediate
operand. The empty low-order bytes are cleared (set to 0).
The PSRLDQ (shift double quadword right logical) instruction shifts the contents of
the source operand to the right by the amount of bytes specified by an immediate
operand. The empty high-order bytes are cleared (set to 0).
The PUNPCKHQDQ (Unpack high quadwords) instruction interleaves the high quad-
word of the source operand and the high quadword of the destination operand and
writes them to the destination register.
The PUNPCKLQDQ (Unpack low quadwords) instruction interleaves the low quad-
words of the source operand and the low quadwords of the destination operand and
writes them to the destination register.
Two additional SSE instructions enable data movement from the MMX registers to the
XMM registers.
The MOVQ2DQ (move quadword integer from MMX to XMM registers) instruction
moves the quadword integer from an MMX source register to an XMM destination
register.
The MOVDQ2Q (move quadword integer from XMM to MMX registers) instruction
moves the low quadword integer from an XMM source register to an MMX destination
register.
11-16 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
NOTE
CLFLUSH was introduced with the SSE2 extensions. However, the
instruction can be implemented in IA-32 processors that do not
implement the SSE2 extensions. Detect CLFLUSH using the feature
bit (if CPUID.01H:EDX.CLFSH[bit 19] = 1).
Vol. 1 11-17
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11.4.4.4 Pause
The PAUSE instruction is provided to improve the performance of “spin-wait loops”
executed on a Pentium 4 or Intel Xeon processor. On a Pentium 4 processor, it also
provides the added benefit of reducing processor power consumption while executing
a spin-wait loop. It is recommended that a PAUSE instruction always be included in
the code sequence for a spin-wait loop.
1. The FISTTP instruction in SSE3 does not generate SIMD floating-point exceptions, but it can gen-
erate x87 FPU floating-point exceptions.
11-18 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-19
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-20 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-21
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-22 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-23
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
and continuing program execution. The masked result may be a rounded normalized
value, signed infinity, a denormal finite number, zero, a QNaN floating-point indefi-
nite, or a QNaN depending on the exception condition detected. In most cases, the
corresponding exception flag bit in MXCSR is also set. The one situation where an
exception flag is not set is when an underflow condition is detected and it is not
accompanied by an inexact result.
When operating on packed floating-point operands, the processor returns a masked
result for each of the sub-operand computations and sets a separate set of internal
exception flags for each computation. It then performs a logical-OR on the internal
exception flag settings and sets the exception flags in the MXCSR register according
to the results of OR operations.
For example, Figure 11-9 shows the results of an MULPS instruction. In the example,
all SIMD floating-point exceptions are masked. Assume that a denormal exception
condition is detected prior to the multiplication of sub-operands X0 and Y0, no excep-
tion condition is detected for the multiplication of X1 and Y1, a numeric overflow
exception condition is detected for the multiplication of X2 and Y2, and another
denormal exception is detected prior to the multiplication of sub-operands X3 and
Y3. Because denormal exceptions are masked, the processor uses the denormal
source values in the multiplications of (X0 and Y0) and of (X3 and Y3) passing the
results of the multiplications through to the destination operand. With the denormal
operand, the result of the X0 and Y0 computation is a normalized finite value, with no
exceptions detected. However, the X3 and Y3 computation produces a tiny and
inexact result. This causes the corresponding internal numeric underflow and
inexact-result exception flags to be set.
X3 X2 X1 X0 (Denormal)
Y3 (Denormal) Y2 Y1 Y0
For the multiplication of X2 and Y2, the processor stores the floating-point ∞ in the
destination operand, and sets the corresponding internal sub-operand numeric over-
flow flag. The result of the X1 and Y1 multiplication is passed through to the destina-
tion operand, with no internal sub-operand exception flags being set. Following the
11-24 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-25
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
exception for overflow and underflow (no inexact result exception would be gener-
ated because the multiplications of X0 and Y0 and of X1 and Y1 are exact).
11-26 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
• An application that expects to detect x87 FPU exceptions that occur during the
execution of x87 FPU instructions will not be notified if exceptions occurs during
the execution of corresponding SSE/SSE2/SSE31 instructions, unless the
exception masks that are enabled in the x87 FPU control word have also been
enabled in the MXCSR register and the application is capable of handling SIMD
floating-point exceptions (#XM).
— Masked exceptions that occur during an SSE/SSE2/SSE3 library call cannot
be detected by unmasking the exceptions after the call (in an attempt to
generate the fault based on the fact that an exception flag is set). A SIMD
floating-point exception flag that is set when the corresponding exception is
unmasked will not generate a fault; only the next occurrence of that
unmasked exception will generate a fault.
— An application which checks the x87 FPU status word to determine if any
masked exception flags were set during an x87 FPU library call will also need
to check the MXCSR register to detect a similar occurrence of a masked
exception flag being set during an SSE/SSE2/SSE3 library call.
1. SSE3 refers to ADDSUBPD, ADDSUBPS, HADDPD, HADDPS, HSUBPD and HSUBPS; the only other
SSE3 instruction that can raise floating-point exceptions is FISTTP: it can generate x87 FPU
invalid operation and inexact result exceptions.
Vol. 1 11-27
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
• Use stack and data alignment techniques to keep data properly aligned for
efficient memory use.
• Use the non-temporal store instructions offered with the SSE and SSE2
extensions.
• Employ the optimization and scheduling techniques described in the Intel
Pentium 4 Optimization Reference Manual (see Section 1.4, “Related Literature,”
for the order number for this manual).
11-28 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
— If the value of the MXCSR_MASK field is non-zero and bit 6 is set, the DAZ
flag and denormals-are-zero mode are supported.
If the DAZ flag is not supported, then it is a reserved bit and attempting to write a 1
to it will cause a general-protection exception (#GP). See Section 11.6.6, “Guidelines
for Writing to the MXCSR Register,” for general guidelines for preventing general-
protection exceptions when writing to the MXCSR register.
Vol. 1 11-29
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
If the processor is reset by asserting the INIT# pin, the SSE and SSE2 state is not
changed.
11-30 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Developer’s Manual, Volume 2A, for a description of FXSAVE and the layout of the
FXSAVE image.
4. Check the value in the MXCSR_MASK field in the FXSAVE image (bytes 28
through 31).
— If the value of the MXCSR_MASK field is 00000000H, then the MXCSR_MASK
value is the default value of 0000FFBFH. Note that this value indicates that bit
6 of the MXCSR register is reserved; this setting indicates that the
denormals-are-zero mode is not supported on the processor.
— If the value of the MXCSR_MASK field is non-zero, the MXCSR_MASK value
should be used as the MXCSR_MASK.
All bits set to 0 in the MXCSR_MASK value indicate reserved bits in the MXCSR
register. Thus, if the MXCSR_MASK value is AND’d with a value to be written into the
MXCSR register, the resulting value will be assured of having all its reserved bits set
to 0, preventing the possibility of a general-protection exception being generated
when the value is written to the MXCSR register.
For example, the default MXCSR_MASK value when 00000000H is returned in the
FXSAVE image is 0000FFBFH. If software AND’s a value to be written to MXCSR
register with 0000FFBFH, bit 6 of the result (the DAZ flag) will be ensured of being
set to 0, which is the required setting to prevent general-protection exceptions on
processors that do not support the denormals-are-zero mode.
To prevent general-protection exceptions, the MXCSR_MASK value should be AND’d
with the value to be written into the MXCSR register in the following situations:
• Operating system routines that receive a parameter from an application program
and then write that value to the MXCSR register (either with an FXRSTOR or
LDMXCSR instruction)
• Any application program that writes to the MXCSR register and that needs to run
robustly on several different IA-32 processors
Note that all bits in the MXCSR_MASK value that are set to 1 indicate features that
are supported by the MXCSR register; they can be treated as feature flags for identi-
fying processor capabilities.
Vol. 1 11-31
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
majority of its floating-point computations in the XMM registers, using the packed
and scalar floating-point instructions, and at the same time use the x87 FPU to
perform trigonometric and other transcendental computations. Likewise, an
application can perform packed 64-bit and 128-bit SIMD integer operations
together without restrictions.
• Those SSE and SSE2 instructions that operate on MMX registers (such as the
CVTPS2PI, CVTTPS2PI, CVTPI2PS, CVTPD2PI, CVTTPD2PI, CVTPI2PD,
MOVDQ2Q, MOVQ2DQ, PADDQ, and PSUBQ instructions) can also be executed in
the same instruction stream as 64-bit SIMD integer or x87 FPU instructions,
however, here they are subject to the restrictions on the simultaneous use of
MMX technology and x87 FPU instructions, which include:
— Transition from x87 FPU to MMX technology instructions or to SSE or SSE2
instructions that operate on MMX registers should be preceded by saving the
state of the x87 FPU.
— Transition from MMX technology instructions or from SSE or SSE2 instruc-
tions that operate on MMX registers to x87 FPU instructions should be
preceded by execution of the EMMS instruction.
11-32 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
arithmetic operation on the data in an XMM register, it does not check that the data
being operated on matches the data type specified in the instruction.
As a general rule, because data typing of SIMD floating-point and integer data types
is not enforced at the architectural level, it is the responsibility of the programmer,
assembler, or compiler to insure that code enforces data typing. Failure to enforce
correct data typing can lead to computations that return unexpected results.
For example, in the following code sample, two packed single-precision floating-point
operands are moved from memory into XMM registers (using MOVAPS instructions);
then a double-precision packed add operation (using the ADDPD instruction) is
performed on the operands:
movaps xmm0, [eax] ; EAX register contains pointer to packed
; single-precision floating-point operand
movaps xmm1, [ebx]
addpd xmm0, xmm1
Pentium 4 and Intel Xeon processors execute these instructions without generating
an invalid-operand exception (#UD) and will produce the expected results in register
XMM0 (that is, the high and low 64-bits of each register will be treated as a double-
precision floating-point value and the processor will operate on them accordingly).
Because the data types operated on and the data type expected by the ADDPD
instruction were inconsistent, the instruction may result in a SIMD floating-point
exception (such as numeric overflow [#O] or invalid operation [#I]) being gener-
ated, but the actual source of the problem (inconsistent data types) is not detected.
The ability to operate on an operand that contains a data type that is inconsistent
with the typing of the instruction being executed, permits some valid operations to be
performed. For example, the following instructions load a packed double-precision
floating-point operand from memory to register XMM0, and a mask to register
XMM1; then they use XORPD to toggle the sign bits of the two packed values in
register XMM0.
movapd xmm0, [eax] ; EAX register contains pointer to packed
; double-precision floating-point operand
movaps xmm1, [ebx] ; EBX register contains pointer to packed
; double-precision floating-point mask
xorpd xmm0, xmm1 ; XOR operation toggles sign bits using
; the mask in xmm1
In this example: XORPS or PXOR can be used in place of XORPD and yield the same
correct result. However, because of the type mismatch between the operand data
type and the instruction data type, a latency penalty will be incurred due to imple-
mentations of the instructions at the microarchitecture level.
Latency penalties can also be incurred by using move instructions of the wrong type.
For example, MOVAPS and MOVAPD can both be used to move a packed single-preci-
Vol. 1 11-33
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-34 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Vol. 1 11-35
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
• Loop counters need to be updated, since each 128-bit SIMD integer instruction
operates on twice the amount of data as its 64-bit SIMD integer counterpart.
11-36 Vol. 1
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
Besides reducing cache pollution, the use of weakly-ordered memory types can be
important under certain data sharing relationships, such as a producer-consumer
relationship. The use of weakly ordered memory can make the assembling of data
more efficient; but care must be taken to ensure that the consumer obtains the data
that the producer intended. Some common usage models that may be affected in this
way by weakly-ordered stores are:
• Library functions that use weakly ordered memory to write results
• Compiler-generated code that writes weakly-ordered results
• Hand-crafted code
The degree to which a consumer of data knows that the data is weakly ordered can
vary for these cases. As a result, the SFENCE or MFENCE instruction should be used
to ensure ordering between routines that produce weakly-ordered data and routines
that consume the data. SFENCE and MFENCE provide a performance-efficient way to
ensure ordering by guaranteeing that every store instruction that precedes
SFENCE/MFENCE in program order is globally visible before a store instruction that
follows the fence.
See also “Instruction Prefixes” in Chapter 2 of the Intel® 64 and IA-32 Architectures
Software Developer’s Manual, Volume 2A, for complete description of instruction
prefixes.
NOTE
Some SSE/SSE2/SSE3 instructions have two-byte opcodes that are
either 2 bytes or 3 bytes in length. Two-byte opcodes that are 3 bytes
in length consist of: a mandatory prefix (F2H, F3H, or 66H), 0FH, and
an opcode byte. See Table 11-3.
Vol. 1 11-37
PROGRAMMING WITH STREAMING SIMD EXTENSIONS 2 (SSE2)
11-38 Vol. 1
CHAPTER 12
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
X1 X0
Y1 Y0
ADD SUB
X1 + Y1 X0 -Y0
12-2 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
X1 X0
Y1 Y0
ADD ADD
Y0 + Y1 X0 + X1
Vol. 1 12-3
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12-4 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-5
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
elements of the second operand; and the fourth by adding the third and fourth
elements of the second operand.
• HADDPS OperandA, OperandB
— OperandA (128 bits, four data elements): 3a, 2a, 1a, 0a
— OperandB (128 bits, four data elements): 3b, 2b, 1b, 0b
— Result (Stored in OperandA): 3b+2b, 1b+0b, 3a+2a, 1a+0a
The HSUBPS instruction performs a single-precision subtraction on contiguous data
elements. The first data element of the result is obtained by subtracting the second
element of the first operand from the first element of the first operand; the second
element by subtracting the fourth element of the first operand from the third element
of the first operand; the third by subtracting the second element of the second
operand from the first element of the second operand; and the fourth by subtracting
the fourth element of the second operand from the third element of the second
operand.
• HSUBPS OperandA, OperandB
— OperandA (128 bits, four data elements): 3a, 2a, 1a, 0a
— OperandB (128 bits, four data elements): 3b, 2b, 1b, 0b
— Result (Stored in OperandA): 2b-3b, 0b-1b, 2a-3a, 0a-1a
The HADDPD instruction performs a double-precision addition on contiguous data
elements. The first data element of the result is obtained by adding the first and
second elements of the first operand; the second element by adding the first and
second elements of the second operand.
• HADDPD OperandA, OperandB
— OperandA (128 bits, two data elements): 1a, 0a
— OperandB (128 bits, two data elements): 1b, 0b
— Result (Stored in OperandA): 1b+0b, 1a+0a
The HSUBPD instruction performs a double-precision subtraction on contiguous data
elements. The first data element of the result is obtained by subtracting the second
element of the first operand from the first element of the first operand; the second
element by subtracting the second element of the second operand from the first
element of the second operand.
• HSUBPD OperandA OperandB
— OperandA (128 bits, two data elements): 1a, 0a
— OperandB (128 bits, two data elements): 1b, 0b
— Result (Stored in OperandA): 0b-1b, 0a-1a
12-6 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-7
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
application attempts to use the MONITOR and MWAIT instructions, the application
should use the following steps:
1. Check that the processor supports MONITOR and MWAIT. If
CPUID.01H:ECX.MONITOR[bit 3] = 1, MONITOR and MWAIT are available at
ring 0.
2. Query the smallest and largest line size that MONITOR uses. Use
CPUID.05H:EAX.smallest[bits 15:0];EBX.largest[bits15:0]. Values are returned
in bytes in EAX and EBX.
3. Ensure the memory address range(s) that will be supplied to MONITOR meets
memory type requirements.
MONITOR and MWAIT are targeted for system software that supports efficient thread
synchronization, See Chapter 12 in the Intel® 64 and IA-32 Architectures Software
Developer’s Manual, Volume 3A for details.
12-8 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-9
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
X3 X2 X1 X0
Y3 Y2 Y1 Y0
Y2 + Y3 Y0 + Y1 X2 + X3 X0 + X1
There are six horizontal add instructions (represented by three mnemonics); three
operate on 128-bit operands and three operate on 64-bit operands. The width of
each data element is either 16 bits or 32 bits. The mnemonics are listed below.
• PHADDW adds two adjacent, signed 16-bit integers horizontally from the source
and destination operands and packs the signed 16-bit results to the destination
operand.
• PHADDSW adds two adjacent, signed 16-bit integers horizontally from the source
and destination operands and packs the signed, saturated 16-bit results to the
destination operand.
• PHADDD adds two adjacent, signed 32-bit integers horizontally from the source
and destination operands and packs the signed 32-bit results to the destination
operand.
There are six horizontal subtract instructions (represented by three mnemonics);
three operate on 128-bit operands and three operate on 64-bit operands. The width
of each data element is either 16 bits or 32 bits. These are listed below.
• PHSUBW performs horizontal subtraction on each adjacent pair of 16-bit signed
integers by subtracting the most significant word from the least significant word
of each pair in the source and destination operands. The signed 16-bit results are
packed and written to the destination operand.
• PHSUBSW performs horizontal subtraction on each adjacent pair of 16-bit signed
integers by subtracting the most significant word from the least significant word
of each pair in the source and destination operands. The signed, saturated 16-bit
results are packed and written to the destination operand.
• PHSUBD performs horizontal subtraction on each adjacent pair of 32-bit signed
integers by subtracting the most significant doubleword from the least significant
12-10 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
double word of each pair in the source and destination operands. The signed
32-bit results are packed and written to the destination operand.
Vol. 1 12-11
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12-12 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-13
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12.8.3 Emulation
CR0.EM is used by some software to emulate x87 floating-point instructions,
CR0.EM[bit 2] cannot be used for emulation of SSE, SSE2, SSE3, SSSE3, and SSE4.
If an SSE3, SSSE3, and SSE4 instruction executes with CR0.EM[bit 2] set, an invalid
opcode exception (INT 6) is generated instead of a device not available exception
(INT 7).
12-14 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-15
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12-16 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-17
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
mov eax, $0
mov [u_dev_status], eax
producerStart:
mov eax, [u_dev_status] # poll status flag to see if consumer is requestion data
cmp eax, $0 #
jne done # I no longer need to produce
commence PCI writes to WC region..
mov eax, $1 # producer ready to notify the consumer via status flag
mov [u_dev_status], eax
# now wait for consumer to signal its status
spinloop:
cmp [u_dev_status], $1 # did I get a signal from the consumer ?
jne producerStart # yes I did
jmp spinloop # check again
done:
// producer is finished at this point
12-18 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Example 12-1. Sketch of MOVNTDQA Usage of a Consumer and a PCI Producer (Contd.)
// P1: consumer check PCI status flag to consume WC data
mov eax, $0 # request to the producer
mov [u_dev_status], eax
consumerStart:
mov; eax, [u_dev_status] # reads the value of the PCI status
cmp eax, $1 # has producer written
jne consumerStart # tight loop; make it more efficient with pause, etc.
mfence # producer finished device writes to WC, ensure WC region is coherent
ntread:
movntdqa xmm0, [addr]
movntdqa xmm1, [addr + 16]
movntdqa xmm2, [addr + 32]
movntdqa xmm3, [addr + 48]
… # do any more NT reads as needed
mfence # ensure PCI device reads the correct value of [u_dev_status]
# now decide whether we are done or we need the producer to produce more data
# if we are done write a 2 into the variable, otherwise write a 0 into the variable
mov eax, $0/$2 # end or continue producing
mov [u_dev_status], eax
# if I want to consume again I will jump back to consumerStart after storing a 0 into eax
# otherwise I am done
Vol. 1 12-19
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
producerStart:
# We use a locked operation to prevent any races between the producer and the consumer
# updating this variable. Assume initial value is 0
mov eax, $0
xchg eax, [signalVariable] # signalVariable is used for communicating
cmp eax, $0 # am I supposed to be writing for the consumer
jne done # I no longer need to produce
movntdq [addr1], xmm0 # producer writes the data
movntdq [addr2], xmm1 # ..
.
# We will again use a locked instruction. Serves 2 purposes. Updated value signals to the consumer
and
# the serialization of the lock flushes all the WC stores to memory
mov eax, $1
xchg [signalVariable], eax # signal to the consumer
# For simplicity, we show a spin loop, more efficient spin loop can be done using PAUSE
spinloop:
cmp [signalVariable], $1 # did I get a signal from the consumer ?
jne producerStart # yes I did
jmp spinloop # check again
done:
// producer is finished at this point
12-20 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
# now decide whether we are done or we need the producer to produce more data
# if we are done write a 2 into the variable, otherwise write a 0 into the variable
mov eax, $0/$2 # end or continue producing
xchg [signalVariable], eax
# if I want to consume again I will jump back to consumerStart after storing a 0 into eax
# otherwise I am done
Vol. 1 12-21
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Table 12-3. Blend Field Size and Control Modes Supported by SSE4.1
Packed Packed
Double Single Packed Packed Packed Packed Blend
Instructions FP FP QWord DWord Word Byte Control
BLENDPS X Imm8
BLENDPD X Imm8
BLENDVPS X X(1) XMM0
BLENDVPD X X(1) XMM0
(2) (2) (2)
PBLENDVB X XMM0
PBLENDW X X X Imm8
NOTE:
1. Use of floating-point SIMD instructions on integer data types may incur performance penalties.
2. Byte variable blend can be used for larger sized fields by reformatting (or shuffling) the blend
control.
12-22 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-23
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
The source operand is from either an XMM register or memory; the destination is an
XMM register. See Table 12-5.
When accessing memory, no alignment is required for any of the instructions unless
alignment checking is enabled. In which case, all conversions must be aligned to the
width of the memory reference. The number of elements converted (and width of
memory reference) is illustrated in Table 12-6. The alignment requirement is shown
in parenthesis.
12-24 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
overlapping pairs of 4 bytes in the destination with the 4-byte field from the source
operand. MPSADBW uses eleven consecutive bytes in the destination operand, its
offset is specified by a control bit in the immediate byte (i.e. the offset can be from
byte 0 or from byte 4). Figure 12-4 illustrates the operation of MPSADBW. MPSADBW
can simplify coding of dense motion estimation by providing source and destination
offset control, higher throughput of SAD operations, and the smaller chunk size.
Imm[1:0]*32
127 96 64 0
Destination
Sum
127 16 0
Vol. 1 12-25
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12-26 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-27
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
instructions accessing the XMM registers. The absence of an alignment check for
these four instructions does not imply any modification to the existing definitions of
other instructions.
12-28 Vol. 1
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
Vol. 1 12-29
PROGRAMMING WITH SSE3, SSSE3, AND SSE4
12-30 Vol. 1
CHAPTER 13
INPUT/OUTPUT
In addition to transferring data to and from external memory, IA-32 processors can
also transfer data to and from input/output ports (I/O ports). I/O ports are created in
system hardware by circuity that decodes the control, data, and address pins on the
processor. These I/O ports are then configured to communicate with peripheral
devices. An I/O port can be an input port, an output port, or a bidirectional port.
Some I/O ports are used for transmitting data, such as to and from the transmit and
receive registers, respectively, of a serial interface device. Other I/O ports are used
to control peripheral devices, such as the control registers of a disk controller.
This chapter describes the processor’s I/O architecture. The topics discussed include:
• I/O port addressing
• I/O instructions
• I/O protection mechanism
Vol. 1 13-1
INPUT/OUTPUT
M/IO# pin indicates a memory address (1) or an I/O address (0). When the separate
I/O address space is selected, it is the responsibility of the hardware to decode the
memory-I/O bus transaction to select I/O ports rather than memory. Data is trans-
mitted between the processor and an I/O device through the data lines.
13-2 Vol. 1
INPUT/OUTPUT
ters (MTRRs) to map the address space used for the memory-mapped I/O as
uncacheable (UC). See Chapter 10, “Memory Cache Control,” in the Intel® 64 and
IA-32 Architectures Software Developer’s Manual, Volume 3A, for a complete discus-
sion of the MTRRs.
The Pentium and Intel486 processors do not support MTRRs. Instead, they provide
the KEN# pin, which when held inactive (high) prevents caching of all addresses sent
out on the system bus. To use this pin, external address decoding logic is required to
block caching in specific address spaces.
Physical Memory
FFFF
EPROM
I/O Port
I/O Port
I/O Port
RAM
0
Figure 13-1. Memory-Mapped I/O
All the IA-32 processors that have on-chip caches also provide the PCD (page-level
cache disable) flag in page table and page directory entries. This flag allows caching
to be disabled on a page-by-page basis. See “Page-Directory and Page-Table Entries”
in Chapter 3 of in the Intel® 64 and IA-32 Architectures Software Developer’s
Manual, Volume 3A.
Vol. 1 13-3
INPUT/OUTPUT
13-4 Vol. 1
INPUT/OUTPUT
ilege level needed to perform I/O. In a typical protection ring model, access to the
I/O address space is restricted to privilege levels 0 and 1. Here, kernel and the device
drivers are allowed to perform I/O, while less privileged device drivers and applica-
tion programs are denied access to the I/O address space. Application programs
must then make calls to the operating system to perform I/O.
The following instructions can be executed only if the current privilege level (CPL) of
the program or task currently executing is less than or equal to the IOPL: IN, INS,
OUT, OUTS, CLI (clear interrupt-enable flag), and STI (set interrupt-enable flag).
These instructions are called I/O sensitive instructions, because they are sensitive
to the IOPL field. Any attempt by a less privileged program or task to use an I/O
sensitive instruction results in a general-protection exception (#GP) being signaled.
Because each task has its own copy of the EFLAGS register, each task can have a
different IOPL.
The I/O permission bit map in the TSS can be used to modify the effect of the IOPL
on I/O sensitive instructions, allowing access to some I/O ports by less privileged
programs or tasks (see Section 13.5.2, “I/O Permission Bit Map”).
A program or task can change its IOPL only with the POPF and IRET instructions;
however, such changes are privileged. No procedure may change the current IOPL
unless it is running at privilege level 0. An attempt by a less privileged procedure to
change the IOPL does not result in an exception; the IOPL simply remains
unchanged.
The POPF instruction also may be used to change the state of the IF flag (as can the
CLI and STI instructions); however, the POPF instruction in this case is also I/O sensi-
tive. A procedure may use the POPF instruction to change the setting of the IF flag
only if the CPL is less than or equal to the current IOPL. An attempt by a less privi-
leged procedure to change the IF flag does not result in an exception; the IF flag
simply remains unchanged.
Vol. 1 13-5
INPUT/OUTPUT
base must
not exceed
DFFFH.
0
Because each task has its own TSS, each task has its own I/O permission bit map.
Access to individual I/O ports can thus be granted to individual tasks.
If in protected mode and the CPL is less than or equal to the current IOPL, the
processor allows all I/O operations to proceed. If the CPL is greater than the IOPL or
if the processor is operating in virtual-8086 mode, the processor checks the I/O
permission bit map to determine if access to a particular I/O port is allowed. Each bit
in the map corresponds to an I/O port byte address. For example, the control bit for
I/O port address 29H in the I/O address space is found at bit position 1 of the sixth
byte in the bit map. Before granting I/O access, the processor tests all the bits corre-
sponding to the I/O port being addressed. For a doubleword access, for example, the
processors tests the four bits corresponding to the four adjacent 8-bit port
addresses. If any tested bit is set, a general-protection exception (#GP) is signaled.
If all tested bits are clear, the I/O operation is allowed to proceed.
Because I/O port addresses are not necessarily aligned to word and doubleword
boundaries, the processor reads two bytes from the I/O permission bit map for every
access to an I/O port. To prevent exceptions from being generated when the ports
with the highest addresses are accessed, an extra byte needs to included in the TSS
immediately after the table. This byte must have all of its bits set, and it must be
within the segment limit.
It is not necessary for the I/O permission bit map to represent all the I/O addresses.
I/O addresses not spanned by the map are treated as if they had set bits in the map.
For example, if the TSS segment limit is 10 bytes past the bit-map base address, the
map has 11 bytes and the first 80 I/O ports are mapped. Higher addresses in the I/O
address space generate exceptions.
13-6 Vol. 1
INPUT/OUTPUT
If the I/O bit map base address is greater than or equal to the TSS segment limit,
there is no I/O permission map, and all I/O instructions generate exceptions when
the CPL is greater than the current IOPL.
Vol. 1 13-7
INPUT/OUTPUT
• The processor never buffers I/O writes. Therefore, strict ordering of I/O
operations is enforced by the processor. (As with memory-mapped I/O, it is
possible for a chip set to post writes in certain I/O ranges.)
• The processor synchronizes I/O instruction execution with external bus activity
(see Table 13-1).
13-8 Vol. 1
CHAPTER 14
PROCESSOR IDENTIFICATION AND
FEATURE DETERMINATION
Vol. 1 14-1
PROCESSOR IDENTIFICATION AND FEATURE DETERMINATION
• Test feature identification flags individually and do not make assumptions about
undefined bits.
14-2 Vol. 1
APPENDIX A
EFLAGS CROSS-REFERENCE
Vol. 1 A-1
EFLAGS CROSS-REFERENCE
A-2 Vol. 1
EFLAGS CROSS-REFERENCE
Vol. 1 A-3
EFLAGS CROSS-REFERENCE
A-4 Vol. 1
EFLAGS CROSS-REFERENCE
Vol. 1 A-5
EFLAGS CROSS-REFERENCE
A-6 Vol. 1
APPENDIX B
EFLAGS CONDITION CODES
Vol. 1 B-1
EFLAGS CONDITION CODES
Many of the test conditions are described in two different ways. For example, LE (less
or equal) and NG (not greater) describe the same test condition. Alternate
mnemonics are provided to make code more intelligible.
The terms “above” and “below” are associated with the CF flag and refer to the rela-
tion between two unsigned integer values. The terms “greater” and “less” are asso-
ciated with the SF and OF flags and refer to the relation between two signed integer
values.
B-2 Vol. 1
APPENDIX C
FLOATING-POINT EXCEPTIONS SUMMARY
C.1 OVERVIEW
This appendix shows which of the floating-point exceptions can be generated for:
• x87 FPU instructions — see Table C-2
• SSE instructions — see Table C-3
• SSE2 instructions — see Table C-4
• SSE3 instructions — see Table C-5
• SSE4 instructions — see Table C-6
Table C-1 lists types of floating-point exceptions that potentially can be generated by
the x87 FPU and by SSE/SSE2/SSE3 instructions.
The floating point exceptions shown in Table C-1 (except for #D and #IS) are defined
in IEEE Standard 754-1985 for Binary Floating-Point Arithmetic. See Section 4.9.1,
“Floating-Point Exception Conditions,” for a detailed discussion of floating-point
exceptions.
Vol. 1 C-1
FLOATING-POINT EXCEPTIONS SUMMARY
C-2 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Table C-2. Exceptions Generated with x87 FPU Floating-Point Instructions (Contd.)
Mnemonic Instruction #IS #IA #D #Z #O #U #P
FLD extended or stack Load floating-point Y
FLD single or double Load floating-point Y Y Y
FLD1 Load + 1.0 Y
FLDCW Load Control word Y Y Y Y Y Y Y
FLDENV Load environment Y Y Y Y Y Y Y
FLDL2E Load log2e Y
FLDL2T Load log210 Y
FLDLG2 Load log102 Y
FLDLN2 Load loge2 Y
FLDPI Load π Y
FLDZ Load + 0.0 Y
FMUL(P) Multiply floating-point Y Y Y Y Y Y
FNOP No operation
FPATAN Partial arctangent Y Y Y Y Y
FPREM Partial remainder Y Y Y Y
FPREM1 IEEE partial remainder Y Y Y Y
FPTAN Partial tangent Y Y Y Y Y
FRNDINT Round to integer Y Y Y Y
FRSTOR Restore state Y Y Y Y Y Y Y
FSAVE Save state
FSCALE Scale Y Y Y Y Y Y
FSIN Sine Y Y Y Y Y
FSINCOS Sine and cosine Y Y Y Y Y
FSQRT Square root Y Y Y Y
FST(P) stack or extended Store floating-point Y
FST(P) single or double Store floating-point Y Y Y Y Y
FSTCW Store control word
FSTENV Store environment
FSTSW (AX) Store status word
FSUB(R)(P) Subtract floating-point Y Y Y Y Y Y
FTST Test Y Y Y
Vol. 1 C-3
FLOATING-POINT EXCEPTIONS SUMMARY
Table C-2. Exceptions Generated with x87 FPU Floating-Point Instructions (Contd.)
Mnemonic Instruction #IS #IA #D #Z #O #U #P
FUCOM(P)(P) Unordered compare floating- Y Y Y
point
FWAIT CPU Wait
FXAM Examine
FXCH Exchange registers Y
FXTRACT Extract Y Y Y Y
FYL2X Logarithm Y Y Y Y Y Y Y
FYL2XP1 Logarithm epsilon Y Y Y Y Y Y
C-4 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Vol. 1 C-5
FLOATING-POINT EXCEPTIONS SUMMARY
C-6 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Vol. 1 C-7
FLOATING-POINT EXCEPTIONS SUMMARY
C-8 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Vol. 1 C-9
FLOATING-POINT EXCEPTIONS SUMMARY
C-10 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Vol. 1 C-11
FLOATING-POINT EXCEPTIONS SUMMARY
C-12 Vol. 1
FLOATING-POINT EXCEPTIONS SUMMARY
Vol. 1 C-13
FLOATING-POINT EXCEPTIONS SUMMARY
C-14 Vol. 1
APPENDIX D
GUIDELINES FOR WRITING X87 FPU
EXCEPTION HANDLERS
As described in Chapter 8, “Programming with the x87 FPU,” the IA-32 Architecture
supports two mechanisms for accessing exception handlers to handle unmasked x87
FPU exceptions: native mode and MS-DOS compatibility mode. The primary purpose
of this appendix is to provide detailed information to help software engineers design
and write x87 FPU exception-handling facilities to run on PC systems that use the
MS-DOS compatibility mode1 for handling x87 FPU exceptions. Some of the informa-
tion in this appendix will also be of interest to engineers who are writing native-mode
x87 FPU exception handlers. The information provided is as follows:
• Discussion of the origin of the MS-DOS x87 FPU exception handling mechanism
and its relationship to the x87 FPU’s native exception handling mechanism.
• Description of the IA-32 flags and processor pins that control the MS-DOS x87
FPU exception handling mechanism.
• Description of the external hardware typically required to support MS-DOS
exception handling mechanism.
• Description of the x87 FPU’s exception handling mechanism and the typical
protocol for x87 FPU exception handlers.
• Code examples that demonstrate various levels of x87 FPU exception handlers.
• Discussion of x87 FPU considerations in multitasking environments.
• Discussion of native mode x87 FPU exception handling.
The information given is oriented toward the most recent generations of IA-32
processors, starting with the Intel486. It is intended to augment the reference infor-
mation given in Chapter 8, “Programming with the x87 FPU.”
A more extensive version of this appendix is available in the application note AP-578,
Software and Hardware Considerations for x87 FPU Exception Handlers for Intel
Architecture Processors (Order Number 243291), which is available from Intel.
1 Microsoft Windows* 95 and Windows 3.1 (and earlier versions) operating systems use almost
the same x87 FPU exception handling interface as MS-DOS. The recommendations in this appen-
dix for a MS-DOS compatible exception handler thus apply to all three operating systems.
Vol. 1 D-1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D-2 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Vol. 1 D-3
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Thus, the external circuitry necessary to support the MS-DOS compatibility mode for
Intel 487 SX processors is the same as for standard Intel486 DX processors.
The Pentium, P6 family, and Pentium 4 processors offer the same mechanism (the NE
bit and the FERR# and IGNNE# pins) as the Intel486 processors for generating x87
FPU exceptions in MS-DOS compatibility mode. The actions of these mechanisms are
slightly different and more straightforward for the P6 family and Pentium 4 proces-
sors, as described in Section D.2.2, “MS-DOS* Compatibility Sub-mode in the P6
Family and Pentium® 4 Processors.”
For Pentium, P6 family, and Pentium 4 processors, it is important to note that the
special DP (Dual Processing) mode for Pentium processors and also the more general
Intel MultiProcessor Specification for systems with multiple Pentium, P6 family, or
Pentium 4 processors support x87 FPU exception handling only in the native mode.
Intel does not recommend using the MS-DOS compatibility x87 FPU mode for
systems using more than one processor.
D-4 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
immediate error reporting will cause the processor to freeze just before executing
the next WAIT or x87 FPU instruction if the error condition has not been cleared by
that time.
Note that in general, whether deferred or immediate error reporting is used for an
x87 FPU exception depends both on which exception occurred and which instruction
caused that exception. A complete specification of these cases, which applies to both
the Pentium and the Intel486 processors, is given in Section 5.1.21 in the Pentium
Processor Family Developer’s Manual: Volume 1.
If NE = 0 but the IGNNE# input is active while an unmasked x87 FPU exception is in
effect, the processor disregards the exception, does not assert FERR#, and
continues. If IGNNE# is then de-asserted and the x87 FPU exception has not been
cleared, the processor will respond as described above. (That is, an immediate
exception case will assert FERR# immediately. A deferred exception case will assert
FERR# and freeze just before the next x87 FPU or WAIT instruction.) The assertion of
IGNNE# is intended for use only inside the x87 FPU exception handler, where it is
needed if one wants to execute non-control x87 FPU instructions for diagnosis,
before clearing the exception condition. When IGNNE# is asserted inside the excep-
tion handler, a preceding x87 FPU exception has already caused FERR# to be
asserted, and the external interrupt hardware has responded, but IGNNE# assertion
still prevents the freeze at x87 FPU instructions. Note that if IGNNE# is left active
outside of the x87 FPU exception handler, additional x87 FPU instructions may be
executed after a given instruction has caused an x87 FPU exception. In this case, if
the x87 FPU exception handler ever did get invoked, it could not determine which
instruction caused the exception.
To properly manage the interface between the processor’s FERR# output, its IGNNE#
input, and the IRQ13 input of the PIC, additional external hardware is needed. A
recommended configuration is described in the following section.
Vol. 1 D-5
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
2. During the x87 FPU interrupt service routine (exception handler) the processor
will need to clear the interrupt request latch (Flip Flop #1). It may also want to
execute non-control x87 FPU instructions before the exception is cleared from the
x87 FPU. For this purpose the IGNNE# must be driven low. Typically in the PC
environment an I/O access to Port 0F0H clears the external x87 FPU exception
interrupt request (FP_IRQ). In the recommended circuit, this access also is used
to activate IGNNE#. With IGNNE# active, the x87 FPU exception handler may
execute any x87 FPU instruction without being blocked by an active x87 FPU
exception.
3. Clearing the exception within the x87 FPU will cause the FERR# signal to be
deactivated and then there is no further need for IGNNE# to be active. In the
recommended circuit, the deactivation of FERR# is used to deactivate IGNNE#. If
another circuit is used, the software and circuit together must assure that
IGNNE# is deactivated no later than the exit from the x87 FPU exception handler.
D-6 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
5(6(7
,23RUW)+
$GGUHVV'HFRGH
9
))
)(55
35
3HQWLXP3UR3URFHVVRU
9
,QWHO3URFHVVRU
3HQWLXP3URFHVVRU
9 &/5
))
35
9
,*11(
,175
,QWHUUXSW
)3B,54
&RQWUROOHU
/(*(1'
))Q)OLS)ORSQ
&/5&OHDURU5HVHW
In the circuit in Figure D-1, when the x87 FPU exception handler accesses I/O port
0F0H it clears the IRQ13 interrupt request output from Flip Flop #1 and also clocks
out the IGNNE# signal (active) from Flip Flop #2. So the handler can activate
IGNNE#, if needed, by doing this 0F0H access before clearing the x87 FPU exception
condition (which de-asserts FERR#).
However, the circuit does not depend on the order of actions by the x87 FPU excep-
tion handler to guarantee the correct hardware state upon exit from the handler. Flip
Flop #2, which drives IGNNE# to the processor, has its CLEAR input attached to the
inverted FERR#. This ensures that IGNNE# can never be active when FERR# is inac-
Vol. 1 D-7
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
tive. So if the handler clears the x87 FPU exception condition before the 0F0H
access, IGNNE# does not get activated and left on after exit from the handler.
0F0H Address
Decode
D.2.1.3 No-Wait x87 FPU Instructions Can Get x87 FPU Interrupt in
Window
The Pentium and Intel486 processors implement the “no-wait” floating-point instruc-
tions (FNINIT, FNCLEX, FNSTENV, FNSAVE, FNSTSW, FNSTCW, FNENI, FNDISI or
FNSETPM) in the MS-DOS compatibility mode in the following manner. (See Section
8.3.11, “x87 FPU Control Instructions,” and Section 8.3.12, “Waiting vs. Non-waiting
Instructions,” for a discussion of the no-wait instructions.)
If an unmasked numeric exception is pending from a preceding x87 FPU instruction,
a member of the no-wait class of instructions will, at the beginning of its execution,
assert the FERR# pin in response to that exception just like other x87 FPU instruc-
tions, but then, unlike the other x87 FPU instructions, FERR# will be de-asserted.
This de-assertion was implemented to allow the no-wait class of instructions to
proceed without an interrupt due to any pending numeric exception. However, the
brief assertion of FERR# is sufficient to latch the x87 FPU exception request into most
hardware interface implementations (including Intel’s recommended circuit).
All the x87 FPU instructions are implemented such that during their execution, there
is a window in which the processor will sample and accept external interrupts. If
there is a pending interrupt, the processor services the interrupt first before
resuming the execution of the instruction. Consequently, it is possible that the no-
wait floating-point instruction may accept the external interrupt caused by it’s own
assertion of the FERR# pin in the event of a pending unmasked numeric exception,
D-8 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Exception Generating
Floating-Point
Instruction
Assertion of FERR#
by the Processor Start of the “No-Wait”
Floating-Point
Instruction
System
Dependent
Delay
Case 1 External Interrupt
Sampling Window
Assertion of INTR Pin
by the System
Case 2
Window Closed
Vol. 1 D-9
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
There are two other ways, in addition to Case 1 above, in which a no-wait floating-
point instruction can service a numeric exception inside its interrupt window. First,
the first floating-point error condition could be of the “immediate” category (as
defined in Section D.2.1.1, “Basic Rules: When FERR# Is Generated”) that asserts
FERR# immediately. If the system delay before asserting INTR is long enough, rela-
tive to the time elapsed before the no-wait floating-point instruction, INTR can be
asserted inside the interrupt window for the latter. Second, consider two no-wait x87
FPU instructions in close sequence, and assume that a previous x87 FPU instruction
has caused an unmasked numeric exception. Then if the INTR timing is too long for
an FERR# signal triggered by the first no-wait instruction to hit the first instruction’s
interrupt window, it could catch the interrupt window of the second.
The possible malfunction of a no-wait x87 FPU instruction explained above cannot
happen if the instruction is being used in the manner for which Intel originally
designed it. The no-wait instructions were intended to be used inside the x87 FPU
exception handler, to allow manipulation of the x87 FPU before the error condition is
cleared, without hanging the processor because of the x87 FPU error condition, and
without the need to assert IGNNE#. They will perform this function correctly, since
before the error condition is cleared, the assertion of FERR# that caused the x87 FPU
error handler to be invoked is still active. Thus the logic that would assert FERR#
briefly at a no-wait instruction causes no change since FERR# is already asserted.
The no-wait instructions may also be used without problem in the handler after the
error condition is cleared, since now they will not cause FERR# to be asserted at all.
If a no-wait instruction is used outside of the x87 FPU exception handler, it may
malfunction as explained above, depending on the details of the hardware interface
implementation and which particular processor is involved. The actual interrupt
inside the window in the no-wait instruction may be blocked by surrounding it with
the instructions: PUSHFD, CLI, no-wait, then POPFD. (CLI blocks interrupts, and the
push and pop of flags preserves and restores the original value of the interrupt flag.)
However, if FERR# was triggered by the no-wait, its latched value and the PIC
response will still be in effect. Further code can be used to check for and correct such
a condition, if needed. Section D.3.6, “Considerations When x87 FPU Shared
Between Tasks,” discusses an important example of this type of problem and gives a
solution.
D-10 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
FERR# is asserted as soon as the x87 FPU detects an unmasked exception; there are
no cases in which error reporting is deferred to the next x87 FPU or WAIT instruction.
(As is discussed in Section D.2.1.1, “Basic Rules: When FERR# Is Generated,” most
exception cases in the Intel486 and Pentium processors are of the deferred type.)
Although FERR# is asserted immediately upon detection of an unmasked x87 FPU
error, this certainly does not mean that the requested interrupt will always be
serviced before the next instruction in the code sequence is executed. To begin with,
the P6 family and Pentium 4 processors execute several instructions simultaneously.
There also will be a delay, which depends on the external hardware implementation,
between the FERR# assertion from the processor and the responding INTR assertion
to the processor. Further, the interrupt request to the PICs (IRQ13) may be tempo-
rarily blocked by the operating system, or delayed by higher priority interrupts, and
processor response to INTR itself is blocked if the operating system has cleared the
IF bit in EFLAGS. Note that Streaming SIMD Extensions numeric exceptions will not
cause assertion of FERR# (independent of the value of CR0.NE). In addition, they
ignore the assertion/deassertion of IGNNE#).
However, just as with the Intel486 and Pentium processors, if the IGNNE# input is
inactive, a floating-point exception which occurred in the previous x87 FPU instruc-
tion and is unmasked causes the processor to freeze immediately when encountering
the next WAIT or x87 FPU instruction (except for no-wait instructions). This means
that if the x87 FPU exception handler has not already been invoked due to the earlier
exception (and therefore, the handler not has cleared that exception state from the
x87 FPU), the processor is forced to wait for the handler to be invoked and handle the
exception, before the processor can execute another WAIT or x87 FPU instruction.
As explained in Section D.2.1.3, “No-Wait x87 FPU Instructions Can Get x87 FPU
Interrupt in Window,” if a no-wait instruction is used outside of the x87 FPU exception
handler, in the Intel486 and Pentium processors, it may accept an unmasked excep-
tion from a previous x87 FPU instruction which happens to fall within the external
interrupt sampling window that is opened near the beginning of execution of all x87
FPU instructions. This will not happen in the P6 family and Pentium 4 processors,
because this sampling window has been removed from the no-wait group of x87 FPU
instructions.
Vol. 1 D-11
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D-12 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
is masked (the corresponding mask bit in the control word = 1), the processor takes
an appropriate default action and continues with the computation.
The processor has a default fix-up activity for every possible exception condition it
may encounter. These masked-exception responses are designed to be safe and are
generally acceptable for most numeric applications.
For example, if the Inexact result (Precision) exception is masked, the system can
specify whether the x87 FPU should handle a result that cannot be represented
exactly by one of four modes of rounding: rounding it normally, chopping it toward
zero, always rounding it up, or always down. If the Underflow exception is masked,
the x87 FPU will store a number that is too small to be represented in normalized
form as a denormal (or zero if it’s smaller than the smallest denormal). Note that
when exceptions are masked, the x87 FPU may detect multiple exceptions in a single
instruction, because it continues executing the instruction after performing its
masked response. For example, the x87 FPU could detect a denormalized operand,
perform its masked response to this exception, and then detect an underflow.
As an example of how even severe exceptions can be handled safely and automati-
cally using the default exception responses, consider a calculation of the parallel
resistance of several values using only the standard formula (see Figure D-4). If R1
becomes zero, the circuit resistance becomes zero. With the divide-by-zero and
precision exceptions masked, the processor will produce the correct result. FDIV of
R1 into 1 gives infinity, and then FDIV of (infinity +R2 +R3) into 1 gives zero.
R1 R2 R3
1
Equivalent Resistance =
1 1 1
+ +
R1 R2 R3
By masking or unmasking specific numeric exceptions in the x87 FPU control word,
programmers can delegate responsibility for most exceptions to the processor,
reserving the most severe exceptions for programmed exception handlers. Excep-
tion-handling software is often difficult to write, and the masked responses have
been tailored to deliver the most reasonable result for each condition. For the
majority of applications, masking all exceptions yields satisfactory results with the
Vol. 1 D-13
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
least programming effort. Certain exceptions can usefully be left unmasked during
the debugging phase of software development, and then masked when the clean
software is actually run. An invalid-operation exception for example, typically indi-
cates a program error that must be corrected.
The exception flags in the x87 FPU status word provide a cumulative record of excep-
tions that have occurred since these flags were last cleared. Once set, these flags can
be cleared only by executing the FCLEX/FNCLEX (clear exceptions) instruction, by
reinitializing the x87 FPU with FINIT/FNINIT or FSAVE/FNSAVE, or by overwriting the
flags with an FRSTOR or FLDENV instruction. This allows a programmer to mask all
exceptions, run a calculation, and then inspect the status word to see if any excep-
tions were detected at any point in the calculation.
D-14 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Intel386 processor using the ERROR# status line between the processor and the
coprocessor. See Section D.1, “MS-DOS Compatibility Sub-mode for Handling x87
FPU Exceptions,” in this appendix, and Chapter 17, “IA-32 Architecture Compati-
bility,” in the Intel® 64 and IA-32 Architectures Software Developer’s Manual,
Volume 3A, for differences in x87 FPU exception handling.
The exception-handling routine is normally a part of the systems software. The
routine must clear (or disable) the active exception flags in the x87 FPU status word
before executing any floating-point instructions that cannot complete execution
when there is a pending floating-point exception. Otherwise, the floating-point
instruction will trigger the x87 FPU interrupt again, and the system will be caught in
an endless loop of nested floating-point exceptions, and hang. In any event, the
routine must clear (or disable) the active exception flags in the x87 FPU status word
after handling them, and before IRET(D). Typical exception responses may include:
• Incrementing an exception counter for later display or printing.
• Printing or displaying diagnostic information (e.g., the x87 FPU environment and
registers).
• Aborting further execution, or using the exception pointers to build an instruction
that will run without exception and executing it.
Applications programmers should consult their operating system's reference
manuals for the appropriate system response to numerical exceptions. For systems
programmers, some details on writing software exception handlers are provided in
Chapter 5, “Interrupt and Exception Handling,” in the Intel® 64 and IA-32 Architec-
tures Software Developer’s Manual, Volume 3A, as well as in Section D.3.4, “x87 FPU
Exception Handling Examples,” in this appendix.
As discussed in Section D.2.1.2, “Recommended External Hardware to Support the
MS-DOS* Compatibility Sub-mode,” some early FERR# to INTR hardware interface
implementations are less robust than the recommended circuit. This is because they
depended on the exception handler to clear the x87 FPU exception interrupt request
to the PIC (by accessing port 0F0H) before the handler causes FERR# to be de-
asserted by clearing the exception from the x87 FPU itself. To eliminate the chance of
a problem with this early hardware, Intel recommends that x87 FPU exception
handlers always access port 0F0H before clearing the error condition from the x87
FPU.
Vol. 1 D-15
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D-16 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
In some operating systems supporting the x87 FPU, the numeric register stack is
extended to memory. To extend the x87 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 D-1. The problem
is that the value of COUNT increments before the exception handler is invoked, so
that the recovery routine will load an incorrect value of COUNT, causing the program
to fail or behave unreliably.
Vol. 1 D-17
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D-18 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
The body of the exception handler examines the diagnostic information and makes a
response that is necessarily application-dependent. This response may range from
halting execution, to displaying a message, to attempting to repair the problem and
proceed with normal execution. The epilogue essentially reverses the actions of the
prologue, restoring the processor so that normal execution can be resumed. The
epilogue must not load an unmasked exception flag into the x87 FPU or another
exception will be requested immediately.
The following code examples show the ASM386/486 coding of three skeleton excep-
tion handlers, with the save spaces given as correct for 32-bit protected mode. They
show how prologues and epilogues can be written for various situations, but the
application-dependent exception handling body is just indicated by comments
showing where it should be placed.
The first two are very similar; their only substantial difference is their choice of
instructions to save and restore the x87 FPU. The trade-off here is between the
increased diagnostic information provided by FNSAVE and the faster execution of
FNSTENV. (Also, after saving the original contents, FNSAVE re-initializes the x87 FPU,
while FNSTENV only masks all x87 FPU exceptions.) For applications that are sensi-
tive to interrupt latency or that do not need to examine register contents, FNSTENV
reduces the duration of the “critical region,” during which the processor does not
recognize another interrupt request. (See the Section 8.1.10, “Saving the x87 FPU’s
State with FSTENV/FNSTENV and FSAVE/FNSAVE,” for a complete description of the
x87 FPU save image.) If the processor supports Streaming SIMD Extensions and the
operating system supports it, the FXSAVE instruction should be used instead of
FNSAVE. If the FXSAVE instruction is used, the save area should be increased to 512
bytes and aligned to 16 bytes to save the entire state. These steps will ensure that
the complete context is saved.
After the exception handler body, the epilogues prepare the processor to resume
execution from the point of interruption (for example, the instruction following the
one that generated the unmasked exception). Notice that the exception flags in the
memory image that is loaded into the x87 FPU are cleared to zero prior to reloading
(in fact, in these examples, the entire status word image is cleared).
Example D-3 and Example D-4 assume that the exception handler itself will not
cause an unmasked exception. Where this is a possibility, the general approach
shown in Example D-5 can be employed. The basic technique is to save the full x87
FPU state and then to load a new control word in the prologue. Note that considerable
care should be taken when designing an exception handler of this type to prevent the
handler from being reentered endlessly.
SAVE_ALL PROC
;
;SAVE REGISTERS, ALLOCATE STACK SPACE FOR x87 FPU STATE IMAGE
PUSH EBP
.
Vol. 1 D-19
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
.
MOV EBP, ESP
SUB ESP, 108 ; ALLOCATES 108 BYTES (32-bit PROTECTED MODE SIZE)
;SAVE FULL x87 FPU STATE, RESTORE INTERRUPT ENABLE FLAG (IF)
FNSAVE [EBP-108]
PUSH [EBP + OFFSET_TO_EFLAGS] ; COPY OLD EFLAGS TO STACK TOP
POPFD ;RESTORE IF TO VALUE BEFORE x87 FPU EXCEPTION
;
;APPLICATION-DEPENDENT EXCEPTION HANDLING CODE GOES HERE
;
;CLEAR EXCEPTION FLAGS IN STATUS WORD (WHICH IS IN MEMORY)
;RESTORE MODIFIED STATE IMAGE
MOV BYTE PTR [EBP-104], 0H
FRSTOR [EBP-108]
;DE-ALLOCATE STACK SPACE, RESTORE REGISTERS
MOV ESP, EBP
.
.
POP EBP
;
;RETURN TO INTERRUPTED CALCULATION
IRETD
SAVE_ALL ENDP
SAVE_ENVIRONMENTPROC
;
;SAVE REGISTERS, ALLOCATE STACK SPACE FOR x87 FPU ENVIRONMENT
PUSH EBP
.
.
MOV EBP, ESP
SUB ESP, 28 ;ALLOCATES 28 BYTES (32-bit PROTECTED MODE SIZE)
;SAVE ENVIRONMENT, RESTORE INTERRUPT ENABLE FLAG (IF)
FNSTENV [EDP - 28]
PUSH [EBP + OFFSET_TO_EFLAGS] ; COPY OLD EFLAGS TO STACK TOP
POPFD ;RESTORE IF TO VALUE BEFORE x87 FPU EXCEPTION
;
;APPLICATION-DEPENDENT EXCEPTION HANDLING CODE GOES HERE
;
;CLEAR EXCEPTION FLAGS IN STATUS WORD (WHICH IS IN MEMORY)
;RESTORE MODIFIED ENVIRONMENT IMAGE
D-20 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
.
.
LOCAL_CONTROL DW ?; ASSUME INITIALIZED
.
.
REENTRANTPROC
;
;SAVE REGISTERS, ALLOCATE STACK SPACE FOR x87 FPU STATE IMAGE
PUSH EBP
.
.
MOV EBP, ESP
SUB ESP, 108 ;ALLOCATES 108 BYTES (32-bit PROTECTED MODE SIZE)
;SAVE STATE, LOAD NEW CONTROL WORD, RESTORE INTERRUPT ENABLE FLAG (IF)
FNSAVE [EBP-108]
FLDCW LOCAL_CONTROL
PUSH [EBP + OFFSET_TO_EFLAGS] ;COPY OLD EFLAGS TO STACK TOP
POPFD ;RESTORE IF TO VALUE BEFORE x87 FPU EXCEPTION
.
.
;
;APPLICATION-DEPENDENT EXCEPTION HANDLING CODE
;GOES HERE - AN UNMASKED EXCEPTION
;GENERATED HERE WILL CAUSE THE EXCEPTION HANDLER TO BE REENTERED
;IF LOCAL STORAGE IS NEEDED, IT MUST BE ALLOCATED ON THE STACK
.
;CLEAR EXCEPTION FLAGS IN STATUS WORD (WHICH IS IN MEMORY)
;RESTORE MODIFIED STATE IMAGE
Vol. 1 D-21
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D.3.5 Need for Storing State of IGNNE# Circuit If Using x87 FPU
and SMM
The recommended circuit (see Figure D-1) for MS-DOS compatibility x87 FPU excep-
tion handling for Intel486 processors and beyond contains two flip flops. When the
x87 FPU exception handler accesses I/O port 0F0H it clears the IRQ13 interrupt
request output from Flip Flop #1 and also clocks out the IGNNE# signal (active) from
Flip Flop #2.
The assertion of IGNNE# may be used by the handler if needed to execute any x87
FPU instruction while ignoring the pending x87 FPU errors. The problem here is that
the state of Flip Flop #2 is effectively an additional (but hidden) status bit that can
affect processor behavior, and so ideally should be saved upon entering SMM, and
restored before resuming to normal operation. If this is not done, and also the SMM
code saves the x87 FPU state, AND an x87 FPU error handler is being used which
relies on IGNNE# assertion, then (very rarely) the x87 FPU handler will nest inside
itself and malfunction. The following example shows how this can happen.
Suppose that the x87 FPU exception handler includes the following sequence:
D-22 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
The problem will only occur if the processor enters SMM between the OUT and the
FLDCW instructions. But if that happens, AND the SMM code saves the x87 FPU state
using FNSAVE, then the IGNNE# Flip Flop will be cleared (because FNSAVE clears the
x87 FPU errors and thus de-asserts FERR#). When the processor returns from SMM it
will restore the x87 FPU state with FRSTOR, which will re-assert FERR#, but the
IGNNE# Flip Flop will not get set. Then when the x87 FPU error handler executes the
FLDCW instruction, the active error condition will cause the processor to re-enter the
x87 FPU error handler from the beginning. This may cause the handler to malfunction.
To avoid this problem, Intel recommends two measures:
1. Do not use the x87 FPU for calculations inside SMM code. (The normal power
management, and sometimes security, functions provided by SMM have no need
for x87 FPU calculations; if they are needed for some special case, use scaling or
emulation instead.) This eliminates the need to do FNSAVE/FRSTOR inside SMM
code, except when going into a 0 V suspend state (in which, in order to save
power, the CPU is turned off completely, requiring its complete state to be saved).
2. The system should not call upon SMM code to put the processor into 0 V suspend
while the processor is running x87 FPU calculations, or just after an interrupt has
occurred. Normal power management protocol avoids this by going into power
down states only after timed intervals in which no system activity occurs.
Vol. 1 D-23
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
“Saving the x87 FPU’s State with FSTENV/FNSTENV and FSAVE/FNSAVE,” for a
complete description of the x87 FPU save image.) If the processor and the operating
system support Streaming SIMD Extensions, the save area should be large enough
and aligned correctly to hold x87 FPU and Streaming SIMD Extensions state.
On a task switch, the general-purpose registers are swapped out to their save area
for the suspending thread, and the registers of the resuming thread are loaded. The
x87 FPU state does not need to be saved at this point. If the resuming thread does
not use the x87 FPU before it is itself suspended, then both a save and a load of the
x87 FPU state has been avoided. It is often the case that several threads may be
executed without any usage of the x87 FPU.
The processor supports speculative deferral of x87 FPU saves via interrupt 7 “Device
Not Available” (DNA), used in conjunction with CR0 bit 3, the “Task Switched” bit
(TS). (See “Control Registers” in Chapter 2 of the Intel® 64 and IA-32 Architectures
Software Developer’s Manual, Volume 3A.) Every task switch via the hardware
supported task switching mechanism (see “Task Switching” in Chapter 6 of the Intel®
64 and IA-32 Architectures Software Developer’s Manual, Volume 3A) sets TS. Multi-
threaded kernels that use software task switching1 can set the TS bit by reading CR0,
ORing a “1” into2 bit 3, and writing back CR0. Any subsequent floating-point instruc-
tions (now being executed in a new thread context) will fault via interrupt 7 before
execution.
This allows a DNA handler to save the old floating-point context and reload the x87
FPU state for the current thread. The handler should clear the TS bit before exit using
the CLTS instruction. On return from the handler the faulting thread will proceed with
its floating-point computation.
Some operating systems save the x87 FPU context on every task switch, typically
because they also change the linear address space between tasks. The problem and
solution discussed in the following sections apply to these operating systems also.
1 In a software task switch, the operating system uses a sequence of instructions to save the sus-
pending thread’s state and restore the resuming thread’s state, instead of the single long non-
interruptible task switch operation provided by the IA-32 architecture.
2 Although CR0, bit 2, the emulation flag (EM), also causes a DNA exception, do not use the EM bit as
a surrogate for TS. EM means that no x87 FPU is available and that floating-point instructions
must be emulated. Using EM to trap on task switches is not compatible with the MMX technology.
If the EM flag is set, MMX instructions raise the invalid opcode exception.
D-24 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
handler, and is used by the DNA exception handler to find the x87 FPU save areas of
the old and new threads. A simplified flow for a DNA exception handler is then:
1. Use the “x87 FPU Owner” variable to find the x87 FPU save area of the last thread
to use the x87 FPU.
2. Save the x87 FPU contents to the old thread’s save area, typically using an
FNSAVE or FXSAVE instruction.
3. Set the x87 FPU Owner variable to the identify the currently executing thread.
4. Reload the x87 FPU contents from the new thread’s save area, typically using an
FRSTOR or FXSTOR instruction.
5. Clear TS using the CLTS instruction and exit the DNA exception handler.
While this flow covers the basic requirements for speculatively deferred x87 FPU
state swaps, there are some additional subtleties that need to be handled in a robust
implementation.
Vol. 1 D-25
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Current Thread
same as
FPU Owner? Yes
No
FPU Owner := Kernel
D-26 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Is Kernel
FPU Owner? Yes
No
Normal Dispatch to
Numeric Exception Handler Exit
Case #2: x87 FPU State Swap with Discarded Numeric Exception
Again, assume two threads A and B, both using the floating-point unit. Let A be the
thread to have most recently executed a floating-point instruction, but this time let
there be a pending numeric exception. Let B be the currently executing thread. When
B starts to execute a floating-point instruction the instruction will fault with the DNA
exception and enter the DNA handler. (If both numeric and DNA exceptions are
pending, the DNA exception takes precedence, in order to support handling the
numeric exception in its own context.)
When the FNSAVE starts, it will trigger an interrupt via FERR# because of the
pending numeric exception. After some system dependent delay, the numeric excep-
tion handler is entered. It may be entered before the FNSAVE starts to execute, or it
may be entered shortly after execution of the FNSAVE. Since the x87 FPU Owner is
the kernel, the numeric exception handler simply exits, discarding the exception. The
Vol. 1 D-27
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
DNA handler resumes execution, completing the FNSAVE of the old floating-point
context of thread A and the FRSTOR of the floating-point context for thread B.
Thread A eventually gets an opportunity to handle the exception that was discarded
during the task switch. After some time, thread B is suspended, and thread A
resumes execution. When thread A starts to execute an floating-point instruction,
once again the DNA exception handler is entered. B’s x87 FPU state is Finessed, and
A’s x87 FPU state is Frustrate. Note that in restoring the x87 FPU state from A’s save
area, the pending numeric exception flags are reloaded into the floating-point status
word. Now when the DNA exception handler returns, thread A resumes execution of
the faulting floating-point instruction just long enough to immediately generate a
numeric exception, which now gets handled in the normal way. The net result is that
the task switch and resulting x87 FPU state swap via the DNA exception handler
causes an extra numeric exception which can be safely discarded.
D-28 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D.4.1 Origin with the Intel 286 and Intel 287, and Intel386
and Intel 387 Processors
The Intel 286 and Intel 287, and Intel386 and Intel 387 processor/coprocessor pairs
are each provided with ERROR# pins that are recommended to be connected
between the processor and x87 FPU. If this is done, when an unmasked x87 FPU
exception occurs, the x87 FPU records the exception, and asserts its ERROR# pin.
The processor recognizes this active condition of the ERROR# status line immediately
before execution of the next WAIT or x87 FPU instruction (except for the no-wait
type) in its instruction stream, and branches to the routine at interrupt vector 16.
Thus an x87 FPU exception will be handled before any other x87 FPU instruction
(after the one causing the error) is executed (except for no-wait instructions, which
will be executed without triggering the x87 FPU exception interrupt, but it will remain
pending).
Vol. 1 D-29
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
Using the dedicated INT 16 for x87 FPU exception handling is referred to as the
native mode. It is the simplest approach, and the one recommended most highly by
Intel.
D-30 Vol. 1
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
discussed above, FERR# gets asserted independent of the value of the NE bit, but
when NE = 1, the operating system should not enable its path through the PIC.)
Another possible (very rare) way a floating-point exception interrupt could occur
while the kernel is executing is by an x87 FPU immediate exception case having its
interrupt delayed by the external hardware until execution has switched to the
kernel. This also cannot happen in native mode because there is no delay through
external hardware.
Thus the native mode x87 FPU exception handler can omit the test to see if the kernel
is the x87 FPU owner, and the DNA handler for a native mode system can omit the
step of setting the kernel as the x87 FPU owner at the handler’s beginning. Since
however these simplifications are minor and save little code, it would be a reasonable
and conservative habit (as long as the MS-DOS compatibility mode is widely used) to
include these steps in all systems.
Note that the special DP (Dual Processing) mode for Pentium processors, and also
the more general Intel MultiProcessor Specification for systems with multiple
Pentium, P6 family, or Pentium 4 processors, support x87 FPU exception handling
only in the native mode. Intel does not recommend using the MS-DOS compatibility
mode for systems using more than one processor.
Vol. 1 D-31
GUIDELINES FOR WRITING X87 FPU EXCEPTION HANDLERS
D-32 Vol. 1
APPENDIX E
GUIDELINES FOR WRITING SIMD FLOATING-POINT
EXCEPTION HANDLERS
See Section 11.5, “SSE, SSE2, and SSE3 Exceptions,” for a detailed discussion of
SIMD floating-point exceptions.
This appendix considers only SSE/SSE2/SSE3 instructions that can generate numeric
(SIMD floating-point) exceptions, and gives an overview of the necessary support for
handling such exceptions. This appendix does not address instructions that do not
generate floating-point exceptions (such as RSQRTSS, RSQRTPS, RCPSS, or RCPPS),
any x87 instructions, or any unlisted instruction.
For detailed information on which instructions generate numeric exceptions, and a
listing of those exceptions, refer to Appendix C, “Floating-Point Exceptions
Summary.” Non-numeric exceptions are handled in a way similar to that for the stan-
dard IA-32 instructions.
Vol. 1 E-1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
5, “Interrupt and Exception Handling,” in the Intel® 64 and IA-32 Architectures Soft-
ware Developer’s Manual, Volume 3A). Some compilers use specific run-time
libraries to assist in floating-point exception handling. If any x87 FPU floating-point
operations are going to be performed that might raise floating-point exceptions, then
the exception handling routine must either disable all floating-point exceptions (for
example, loading a local control word with FLDCW), or it must be implemented as re-
entrant (for the case of x87 FPU exceptions, refer to Example D-1 in Appendix D,
“Guidelines for Writing x87 FPU Exception Handlers”). If this is not the case, the
routine has to clear the status flags for x87 FPU exceptions or to mask all x87 FPU
floating-point exceptions. For SIMD floating-point exceptions though, the exception
flags in MXCSR do not have to be cleared, even if they remain unmasked (but they
may still be cleared). Exceptions are in this case precise and occur immediately, and
a SIMD floating-point exception status flag that is set when the corresponding excep-
tion is unmasked will not generate an exception.
Typical actions performed by this low-level exception handling routine are:
• Incrementing an exception counter for later display or printing
• Printing or displaying diagnostic information (e.g. the MXCSR and XMM registers)
• Aborting further execution, or using the exception pointers to build an instruction
that will run without exception and executing it
• Storing information about the exception in a data structure that will be passed to
a higher level user exception handler
In most cases (and this applies also to SSE/SSE2/SSE3 instructions), there will be
three main components of a low-level floating-point exception handler: a prologue, a
body, and an epilogue.
The prologue performs functions that must be protected from possible interruption
by higher-priority sources - typically saving registers and transferring diagnostic
information from the processor to memory. When the critical processing has been
completed, the prologue may re-enable interrupts to allow higher-priority interrupt
handlers to preempt the exception handler (assuming that the interrupt handler was
called through an interrupt gate, meaning that the processor cleared the interrupt
enable (IF) flag in the EFLAGS register - refer to Section 6.4.1, “Call and Return
Operation for Interrupt or Exception Handling Procedures”).
The body of the exception handler examines the diagnostic information and makes a
response that is application-dependent. It may range from halting execution, to
displaying a message, to attempting to fix the problem and then proceeding with
normal execution, to setting up a data structure, calling a higher-level user exception
handler and continuing execution upon return from it. This latter case will be
assumed in Section E.4, “SIMD Floating-Point Exceptions and the IEEE Standard
754” below.
Finally, the epilogue essentially reverses the actions of the prologue, restoring the
processor state so that normal execution can be resumed.
The following example represents a typical exception handler. To link it with Example
E-2 that will follow in Section E.4.3, “Example SIMD Floating-Point Emulation Imple-
E-2 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
mentation,” assume that the body of the handler (not shown here in detail) passes
the saved state to a routine that will examine in turn all the sub-operands of the
excepting instruction, invoking a user floating-point exception handler if a particular
set of sub-operands raises an unmasked (enabled) exception, or emulating the
instruction otherwise.
;PROLOGUE
;SAVE REGISTERS THAT MIGHT BE USED BY THE EXCEPTION HANDLER
PUSH EBP ;SAVE EBP
PUSH EAX ;SAVE EAX
...
MOV EBP, ESP ;SAVE ESP in EBP
SUB ESP, 512 ;ALLOCATE 512 BYTES
AND ESP, 0fffffff0h ;MAKE THE ADDRESS 16-BYTE ALIGNED
FXSAVE [ESP] ;SAVE FP, MMX, AND SIMD FP STATE
PUSH [EBP+EFLAGS_OFFSET] ;COPY OLD EFLAGS TO STACK TOP
POPFD ;RESTORE THE INTERRUPT ENABLE FLAG IF
;TO VALUE BEFORE SIMD FP EXCEPTION
;BODY
;APPLICATION-DEPENDENT EXCEPTION HANDLING CODE GOES HERE
LDMXCSR LOCAL_MXCSR ;LOAD LOCAL MXCSR VALUE IF NEEDED
...
...
;EPILOGUE
FXRSTOR [ESP] ;RESTORE MODIFIED STATE IMAGE
MOV ESP, EBP ;DE-ALLOCATE STACK SPACE
...
POP EAX ;RESTORE EAX
POP EBP ;RESTORE EBP
IRET ;RETURN TO INTERRUPTED CALCULATION
SIMD_FP_EXC_HANDLER ENDP
Vol. 1 E-3
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
occur immediately and are not delayed until a subsequent floating-point instruction
is executed. However, floating-point emulation may be necessary when unmasked
floating-point exceptions are generated.
E-4 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
operands into up to four sets of sub-operands, and will submit them one set at a time
to an emulation function (See Example E-2 in Section E.4.3, “Example SIMD
Floating-Point Emulation Implementation”). The emulation function will examine the
sub-operands, and will possibly redo the necessary calculation.
Two cases are possible:
• If an unmasked (enabled) exception would occur in this process, the emulation
function will return to its caller (the filter function) with the appropriate infor-
mation. The filter will invoke a (previously registered) user floating-point
exception handler for this set of sub-operands, and will record the result upon
return from the user handler (provided the user handler allows continuation of
the execution).
• If no unmasked (enabled) exception would occur, the emulation function will
determine and will return to its caller the result of the operation for the current
set of sub-operands (it has to be IEEE Standard 754 compliant). The filter
function will record the result (plus any new flag settings).
The user level filter function will then call the emulation function for the next set of
sub-operands (if any). When done with all the operand sets, the partial results will be
packed (if the excepting instruction has a packed floating-point result, which is true
for most SSE/SSE2/SSE3 numeric instructions) and the filter will return to the low-
level exception handler, which in turn will return from the interruption, allowing
execution to continue. Note that the instruction pointer (EIP) has to be altered to
point to the instruction following the excepting instruction, in order to continue
execution correctly.
If a user mode floating-point exception filter is not provided, then all the work for
decoding the excepting instruction, reading its operands, emulating the instruction
for the components of the result that do not correspond to unmasked floating-point
exceptions, and providing the compounded result will have to be performed by the
user-provided floating-point exception handler.
Actual emulation might have to take place for one operand or pair of operands for
scalar operations, and for all sub-operands or pairs of sub-operands for packed oper-
ations. The steps to perform are the following:
• The excepting instruction has to be decoded and the operands have to be read
from the saved context.
• The instruction has to be emulated for each (pair of) sub-operand(s); if no
floating-point exception occurs, the partial result has to be saved; if a masked
floating-point exception occurs, the masked result has to be produced through
emulation and saved, and the appropriate status flags have to be set; if an
unmasked floating-point exception occurs, the result has to be generated by the
user provided floating-point exception handler, and the appropriate status flags
have to be set.
• The partial results have to be combined and written to the context that will be
restored upon application program resumption.
Vol. 1 E-5
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
User Application
From the user-level floating-point filter, Example E-2 in Section E.4.3, “Example
SIMD Floating-Point Emulation Implementation,” will present only the floating-point
emulation part. In order to understand the actions involved, the expected response
to exceptions has to be known for all SSE/SSE2/SSE3 numeric instructions in two
situations: with exceptions enabled (unmasked result), and with exceptions disabled
(masked result). The latter can be found in Section 6.4, “Interrupts and Exceptions.”
The response to NaN operands that do not raise an exception is specified in Section
4.8.3.4, “NaNs.” Operations on NaNs are explained in the same source. This response
is also discussed in more detail in the next subsection, along with the unmasked and
masked responses to floating-point exceptions.
E-6 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-7
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Table E-1. ADDPS, ADDSS, SUBPS, SUBSS, MULPS, MULSS, DIVPS, DIVSS, ADDPD,
ADDSD, SUBPD, SUBSD, MULPD, MULSD, DIVPD, DIVSD, ADDSUBPS, ADDSUBPD,
HADDPS, HADDPD, HSUBPS, HSUBPD
Source Operands Masked Result Unmasked Result
1
SNaN1 op SNaN2 SNaN1 | 00400000H or None
SNaN1 |
0008000000000000H2
SNaN1 op QNaN2 SNaN1 | 00400000H or None
SNaN1 |
0008000000000000H2
QNaN1 op SNaN2 QNaN1 None
QNaN1 op QNaN2 QNaN1 QNaN1 (not an exception)
SNaN op real value SNaN | 00400000H or None
SNaN1 |
0008000000000000H2
Real value op SNaN SNaN | 00400000H or None
SNaN1 |
0008000000000000H2
QNaN op real value QNaN QNaN (not an exception)
Real value op QNaN QNaN QNaN (not an exception)
Neither source operand is Single precision or double None
SNaN, precision QNaN Indefinite
but #I is signaled (e.g. for Inf -
Inf,
Inf ∗ 0, Inf / Inf, 0/0)
NOTES:
1. For Tables E-1 to E-12: op denotes the operation to be performed.
2. SNaN | 0x00400000 is a quiet NaN in single precision format (if SNaN is in single precision) and
SNaN | 0008000000000000H is a quiet NaN in double precision format (if SNaN is in double
precision), obtained from the signaling NaN given as input.
3. Operations involving only quiet NaNs do not raise floating-point exceptions.
E-8 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-9
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-10 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Table E-9. MAXPS, MAXSS, MINPS, MINSS, MAXPD, MAXSD, MINPD, MINSD
Source Operands Masked Result Unmasked Result
Opd1 op NaN2 (any Opd1) NaN2 None
NaN1 op Opd2 (any Opd2) Opd2 None
NOTE:
1. SNaN and QNaN operands raise an Invalid Operation fault.
Vol. 1 E-11
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E.4.2.3 Condition Codes, Exception Flags, and Response for Masked and
Unmasked Numeric Exceptions
In the following, the masked response is what the processor provides when a masked
exception is raised by an SSE/SSE2/SSE3 numeric instruction. The same response is
provided by the floating-point emulator for SSE/SSE2/SSE3 numeric instructions,
when certain components of the quadruple input operands generate exceptions that
are masked (the emulator also generates the correct answer, as specified by IEEE
Standard 754 wherever applicable, in the case when no floating-point exception
occurs). The unmasked response is what the emulator provides to the user handler
for those components of the packed operands of SSE/SSE2/SSE3 instructions that
raise unmasked exceptions. Note that for pre-computation exceptions (floating-point
E-12 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-13
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-14 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-15
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-16 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-17
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-18 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-19
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-20 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
Vol. 1 E-21
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
typedef struct {
unsigned int operation; //SSE or SSE2 operation: ADDPS, ADDSS, ...
unsigned int operand1_uint32; //first operand value
unsigned int operand2_uint32; //second operand value (if any)
float result_fval; // result value (if any)
unsigned int rounding_mode; //rounding mode
unsigned int exc_masks; //exception masks, in the order P,U,O,Z,D,I
unsigned int exception_cause; //exception cause
unsigned int status_flag_inexact; //inexact status flag
unsigned int status_flag_underflow; //underflow status flag
unsigned int status_flag_overflow; //overflow status flag
unsigned int status_flag_divide_by_zero;
//divide by zero status flag
unsigned int status_flag_denormal_operand;
//denormal operand status flag
unsigned int status_flag_invalid_operation;
//invalid operation status flag
unsigned int ftz; // flush-to-zero flag
unsigned int daz; // denormals-are-zeros flag
} EXC_ENV;
E-22 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
1. If the denormals-are-zeros mode is enabled (the DAZ bit in MXCSR is set to 1),
replace all the denormal inputs with zeroes of the same sign (the denormal flag is
not affected by this change).
2. Perform the operation using x87 FPU instructions, with exceptions disabled, the
original user rounding mode, and single precision. This reveals invalid, denormal,
or divide-by-zero exceptions (if there are any) and stores the result in memory as
a double precision value (whose exponent range is large enough to look like
“unbounded” to the result of the single precision computation).
3. If no unmasked exceptions were detected, determine if the result is less than the
smallest normal number (tiny) that can be represented in single precision
format, or greater than the largest normal number that can be represented in
single precision format (huge). If an unmasked overflow or underflow occurs,
calculate the scaled result that will be handed to the user exception handler, as
specified by IEEE Standard 754.
4. If no exception was raised, calculate the result with a “bounded” exponent. If the
result is tiny, it requires denormalization (shifting the significand right while
incrementing the exponent to bring it into the admissible range of [-126,+127]
for single precision floating-point numbers).
The result obtained in step 2 cannot be used because it might incur a double
rounding error (it was rounded to 24 bits in step 2, and might have to be rounded
again in the denormalization process). To overcome this is, calculate the result as
a double precision value, and store it to memory in single precision format.
Rounding first to 53 bits in the significand, and then to 24 never causes a double
rounding error (exact properties exist that state when double-rounding error
occurs, but for the elementary arithmetic operations, the rule of thumb is that if
an infinitely precise result is rounded to 2p+1 bits and then again to p bits, the
result is the same as when rounding directly to p bits, which means that no
double-rounding error occurs).
5. If the result is inexact and the inexact exceptions are unmasked, the calculated
result will be delivered to the user floating-point exception handler.
6. The flush-to-zero case is dealt with if the result is tiny.
7. The emulation function returns RAISE_EXCEPTION to the filter function if an
exception has to be raised (the exception_cause field indicates the cause).
Otherwise, the emulation function returns DO_NOT_ RAISE_EXCEPTION. In the
first case, the result is provided by the user exception handler called by the filter
function. In the second case, it is provided by the emulation function. The filter
function has to collect all the partial results, and to assemble the scalar or packed
result that is used if execution is to continue.
Vol. 1 E-23
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
// 32-bit constants
static unsigned ZEROF_ARRAY[] = {0x00000000};
#define ZEROF *(float *) ZEROF_ARRAY
// +0.0
static unsigned NZEROF_ARRAY[] = {0x80000000};
#define NZEROF *(float *) NZEROF_ARRAY
// -0.0
static unsigned POSINFF_ARRAY[] = {0x7f800000};
#define POSINFF *(float *)POSINFF_ARRAY
// +Inf
static unsigned NEGINFF_ARRAY[] = {0xff800000};
#define NEGINFF *(float *)NEGINFF_ARRAY
// -Inf
// 64-bit constants
static unsigned MIN_SINGLE_NORMAL_ARRAY [] = {0x00000000, 0x38100000};
#define MIN_SINGLE_NORMAL *(double *)MIN_SINGLE_NORMAL_ARRAY
// +1.0 * 2^-126
static unsigned MAX_SINGLE_NORMAL_ARRAY [] = {0x70000000, 0x47efffff};
#define MAX_SINGLE_NORMAL *(double *)MAX_SINGLE_NORMAL_ARRAY
// +1.1...1*2^127
static unsigned TWO_TO_192_ARRAY[] = {0x00000000, 0x4bf00000};
#define TWO_TO_192 *(double *)TWO_TO_192_ARRAY
// +1.0 * 2^192
static unsigned TWO_TO_M192_ARRAY[] = {0x00000000, 0x33f00000};
#define TWO_TO_M192 *(double *)TWO_TO_M192_ARRAY
// +1.0 * 2^-192
// auxiliary functions
static int isnanf (unsigned int ); // returns 1 if f is a NaN, and 0 otherwise
static float quietf (unsigned int ); // converts a signaling NaN to a quiet
// NaN, and leaves a quiet NaN unchanged
static unsigned int check_for_daz (unsigned int ); // converts denormals
// to zeros of the same sign;
// does not affect any status flags
unsigned int
simd_fp_emulate (EXC_ENV *exc_env)
E-24 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
// have to check first for faults (V, D, Z), and then for traps (O, U, I)
result_tiny = 0;
result_huge = 0;
switch (exc_env->operation) {
case ADDPS:
case ADDSS:
case SUBPS:
case SUBSS:
case MULPS:
case MULSS:
case DIVPS:
case DIVSS:
Vol. 1 E-25
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
switch (exc_env->rounding_mode) {
case ROUND_TO_NEAREST:
cw = 0x003f; // round to nearest, single precision, exceptions masked
break;
case ROUND_DOWN:
cw = 0x043f; // round down, single precision, exceptions masked
break;
case ROUND_UP:
cw = 0x083f; // round up, single precision, exceptions masked
break;
case ROUND_TO_ZERO:
cw = 0x0c3f; // round to zero, single precision, exceptions masked
break;
default:
;
}
__asm {
fldcw WORD PTR cw;
}
case ADDPS:
case ADDSS:
// perform the addition
__asm {
fnclex;
// load input operands
fld DWORD PTR uiopd1; // may set denormal or invalid status flags
fld DWORD PTR uiopd2; // may set denormal or invalid status flags
faddp st(1), st(0); // may set inexact or invalid status flags
// store result
fstp QWORD PTR dbl_res24; // exact
}
break;
case SUBPS:
case SUBSS:
// perform the subtraction
__asm {
fnclex;
// load input operands
fld DWORD PTR uiopd1; // may set denormal or invalid status flags
fld DWORD PTR uiopd2; // may set denormal or invalid status flags
fsubp st(1), st(0); // may set the inexact or invalid status flags
// store result
fstp QWORD PTR dbl_res24; // exact
}
break;
E-26 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
case MULPS:
case MULSS:
// perform the multiplication
__asm {
fnclex;
// load input operands
fld DWORD PTR uiopd1; // may set denormal or invalid status flags
fld DWORD PTR uiopd2; // may set denormal or invalid status flags
fmulp st(1), st(0); // may set inexact or invalid status flags
// store result
fstp QWORD PTR dbl_res24; // exact
}
break;
case DIVPS:
case DIVSS:
// perform the division
__asm {
fnclex;
// load input operands
fld DWORD PTR uiopd1; // may set denormal or invalid status flags
fld DWORD PTR uiopd2; // may set denormal or invalid status flags
fdivp st(1), st(0); // may set the inexact, divide by zero, or
// invalid status flags
// store result
fstp QWORD PTR dbl_res24; // exact
}
break;
default:
; // will never occur
// if invalid flag is set, and invalid exceptions are enabled, take trap
if (!(exc_env->exc_masks & INVALID_MASK) && (sw & INVALID_MASK)) {
exc_env->status_flag_invalid_operation = 1;
exc_env->exception_cause = INVALID_OPERATION;
return (RAISE_EXCEPTION);
}
Vol. 1 E-27
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
// if denormal flag set, and denormal exceptions are enabled, take trap
if (!(exc_env->exc_masks & DENORMAL_MASK) && (sw & DENORMAL_MASK)) {
exc_env->status_flag_denormal_operand = 1;
exc_env->exception_cause = DENORMAL_OPERAND;
return (RAISE_EXCEPTION);
}
E-28 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
switch (exc_env->operation) {
case ADDPS:
case ADDSS:
// perform the addition
__asm {
Vol. 1 E-29
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
case SUBPS:
case SUBSS:
// perform the subtraction
__asm {
// load input operands
fld DWORD PTR uiopd1; // may set the denormal status flag
fld DWORD PTR uiopd2; // may set the denormal status flag
fsubp st(1), st(0); // rounded to 53 bits, may set the inexact
// status flag
// store result
fstp QWORD PTR dbl_res; // exact, will not set any flag
}
break;
case MULPS:
case MULSS:
// perform the multiplication
__asm {
// load input operands
fld DWORD PTR uiopd1; // may set the denormal status flag
fld DWORD PTR uiopd2; // may set the denormal status flag
fmulp st(1), st(0); // rounded to 53 bits, exact
// store result
fstp QWORD PTR dbl_res; // exact, will not set any flag
}
break;
case DIVPS:
case DIVSS:
// perform the division
__asm {
// load input operands
fld DWORD PTR uiopd1; // may set the denormal status flag
fld DWORD PTR uiopd2; // may set the denormal status flag
fdivp st(1), st(0); // rounded to 53 bits, may set the inexact
// status flag
// store result
fstp QWORD PTR dbl_res; // exact, will not set any flag
}
break;
default:
E-30 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
// if inexact traps are enabled and result is inexact, take inexact trap
if (!(exc_env->exc_masks & PRECISION_MASK) &&
((sw & PRECISION_MASK) || (exc_env->ftz && result_tiny))) {
exc_env->status_flag_inexact = 1;
exc_env->exception_cause = INEXACT;
if (result_tiny) {
exc_env->status_flag_underflow = 1;
Vol. 1 E-31
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
exc_env->status_flag_inexact = 1;
exc_env->status_flag_underflow = 1;
}
exc_env->result_fval = res;
if (sw & ZERODIVIDE_MASK) exc_env->status_flag_divide_by_zero = 1;
if (sw & DENORMAL_MASK) exc_env->status_flag_denormal= 1;
if (sw & INVALID_MASK) exc_env->status_flag_invalid_operation = 1;
return (DO_NOT_RAISE_EXCEPTION);
break;
case CMPPS:
case CMPSS:
...
break;
case COMISS:
case UCOMISS:
...
break;
case CVTPI2PS:
case CVTSI2SS:
...
break;
case CVTPS2PI:
E-32 Vol. 1
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
case CVTSS2SI:
case CVTTPS2PI:
case CVTTSS2SI:
...
break;
case MAXPS:
case MAXSS:
case MINPS:
case MINSS:
...
break;
case SQRTPS:
case SQRTSS:
...
break;
...
case UNSPEC:
...
break;
default:
...
Vol. 1 E-33
GUIDELINES FOR WRITING SIMD FLOATING-POINT EXCEPTION HANDLERS
E-34 Vol. 1
INDEX
Vol. 1 INDEX-1
INDEX
INDEX-2 Vol. 1
INDEX
Vol. 1 INDEX-3
INDEX
INDEX-4 Vol. 1
INDEX
Vol. 1 INDEX-5
INDEX
INDEX-6 Vol. 1
INDEX
Vol. 1 INDEX-7
INDEX
INDEX-8 Vol. 1
INDEX
Vol. 1 INDEX-9
INDEX
INDEX-10 Vol. 1
INDEX
Vol. 1 INDEX-11
INDEX
INDEX-12 Vol. 1
INDEX
Vol. 1 INDEX-13
INDEX
INDEX-14 Vol. 1
INDEX
Vol. 1 INDEX-15
INDEX
INDEX-16 Vol. 1
INDEX
Vol. 1 INDEX-17
INDEX
Z
ZE (divide by zero exception) flag
x87 FPU status word, 8-7, 8-40
ZE (divide by zero exception) flag bit
MXCSR register, 11-22
Zero, floating-point format, 4-7, 4-20
ZF (zero) flag, EFLAGS register, 3-21, A-1
ZM (divide by zero exception) mask bit
MXCSR register, 11-22
x87 FPU control word, 8-11, 8-40
INDEX-18 Vol. 1