Intel® Architecture Optimization Reference Manual Copyright © 1998, 1999 Intel Corporation All Rights Reserved Issued in U.S.A. Order Number: 245127-001 Intel® Architecture Optimization Reference Manual Order Number: 730795-001 Revision Revision History Date 001 Documents Streaming SIMD Extensions optimization 02/99 techniques for Pentium® II and Pentium III processors. Information in this document is provided in connection with Intel products. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted by this document. Except as provided in Intel’s Terms and Con- ditions of Sale for such products, Intel assumes no liability whatsoever, and Intel disclaims any express or implied war- ranty, relating to sale and/or use of Intel products including liability or warranties relating to fitness for a particular purpose, merchantability, or infringement of any patent, copyright or other intellectual property right. Intel products are not intended for use in medical, life saving, or life sustaining applications. This Intel® Architecture Optimization manual as well as the software described in it is furnished under license and may only be used or copied in accordance with the terms of the license. The information in this manual is furnished for infor- mational use only, is subject to change without notice, and should not be construed as a commitment by Intel Corpora- tion. Intel Corporation assumes no responsibility or liability for any errors or inaccuracies that may appear in this document or any software that may be provided in association with this document. Except as permitted by such license, no part of this document may be reproduced, stored in a retrieval system, or trans- mitted in any form or by any means without the express written consent of Intel Corporation. Intel may make changes to specifications and product descriptions at any time, without notice. * Third-party brands and names are the property of their respective owners. Copyright © Intel Corporation 1998, 1999. Contents Introduction Tuning Your Application ......................................................... xvii About This Manual................................................................ xviii Related Documentation .......................................................... xix Notational Conventions............................................................ xx Chapter 1 Processor Architecture Overview The Processors’ Execution Architecture................................ 1-1 The Pentium® II and Pentium III Processors Pipeline....... 1-2 The In-order Issue Front End ....................................... 1-2 The Out-of-order Core.................................................. 1-3 In-Order Retirement Unit.............................................. 1-3 Front-End Pipeline Detail .................................................. 1-4 Instruction Prefetcher ................................................... 1-4 Decoders...................................................................... 1-4 Branch Prediction Overview ......................................... 1-5 Dynamic Prediction ...................................................... 1-6 Static Prediction ........................................................... 1-6 Execution Core Detail ....................................................... 1-7 Execution Units and Ports ............................................ 1-9 Caches of the Pentium II and Pentium III Processors ............................................................... 1-10 Store Buffers .............................................................. 1-11 iii Intel Architecture Optimization Reference Manual Streaming SIMD Extensions of the Pentium III Processor... 1-12 Single-Instruction, Multiple-Data (SIMD)......................... 1-13 New Data Types .............................................................. 1-13 Streaming SIMD Extensions Registers........................... 1-14 MMX™ Technology.............................................................. 1-15 Chapter 2 General Optimization Guidelines Integer Coding Guidelines ..................................................... 2-1 Branch Prediction .................................................................. 2-2 Dynamic Branch Prediction............................................... 2-2 Static Prediction ................................................................ 2-3 Eliminating and Reducing the Number of Branches ......... 2-5 Performance Tuning Tip for Branch Prediction.................. 2-8 Partial Register Stalls ............................................................ 2-8 Performance Tuning Tip for Partial Stalls ........................ 2-10 Alignment Rules and Guidelines.......................................... 2-11 Code ............................................................................... 2-11 Data ................................................................................ 2-12 Data Cache Unit (DCU) Split...................................... 2-12 Performance Tuning Tip for Misaligned Accesses...... 2-13 Instruction Scheduling ......................................................... 2-14 Scheduling Rules for Pentium II and Pentium III Processors.................................................................... 2-14 Prefixed Opcodes............................................................ 2-16 Performance Tuning Tip for Instruction Scheduling......... 2-16 Instruction Selection ............................................................ 2-16 The Use of lea Instruction ............................................... 2-17 Complex Instructions ...................................................... 2-17 Short Opcodes ................................................................ 2-17 8/16-bit Operands ........................................................... 2-18 Comparing Register Values ............................................ 2-19 Address Calculations ...................................................... 2-19 iv Contents Clearing a Register......................................................... 2-19 Integer Divide ................................................................. 2-20 Comparing with Immediate Zero .................................... 2-20 Prolog Sequences .......................................................... 2-20 Epilog Sequences .......................................................... 2-20 Improving the Performance of Floating-point Applications ...................................................................... 2-20 Guidelines for Optimizing Floating-point Code ............... 2-21 Improving Parallelism ..................................................... 2-21 Rules and Regulations of the fxch Instruction ................ 2-23 Memory Operands.......................................................... 2-24 Memory Access Stall Information................................... 2-24 Floating-point to Integer Conversion .............................. 2-25 Loop Unrolling ................................................................ 2-28 Floating-Point Stalls........................................................ 2-29 Hiding the One-Clock Latency of a Floating-Point Store................................................. 2-29 Integer and Floating-point Multiply............................. 2-30 Floating-point Operations with Integer Operands ...... 2-30 FSTSW Instructions................................................... 2-31 Transcendental Functions .......................................... 2-31 Chapter 3 Coding for SIMD Architectures Checking for Processor Support of Streaming SIMD Extensions and MMX Technology....................................... 3-2 Checking for MMX Technology Support ........................... 3-2 Checking for Streaming SIMD Extensions Support.......... 3-3 Considerations for Code Conversion to SIMD Programming ...................................................................... 3-4 Identifying Hotspots .......................................................... 3-6 Determine If Code Benefits by Conversion to Streaming SIMD Extensions .......................................... 3-7 Coding Techniques................................................................ 3-7 v Intel Architecture Optimization Reference Manual Coding Methodologies ...................................................... 3-8 Assembly.................................................................... 3-10 Intrinsics ..................................................................... 3-11 Classes ...................................................................... 3-12 Automatic Vectorization .............................................. 3-13 Stack and Data Alignment ................................................... 3-15 Alignment of Data Access Patterns ................................ 3-15 Stack Alignment For Streaming SIMD Extensions.......... 3-16 Data Alignment for MMX Technology .............................. 3-17 Data Alignment for Streaming SIMD Extensions ............ 3-18 Compiler-Supported Alignment .................................. 3-18 Improving Memory Utilization .............................................. 3-20 Data Structure Layout ..................................................... 3-21 Strip Mining ..................................................................... 3-23 Loop Blocking ................................................................. 3-25 Tuning the Final Application ............................................ 3-28 Chapter 4 Using SIMD Integer Instructions General Rules on SIMD Integer Code ................................... 4-1 Planning Considerations........................................................ 4-2 CPUID Usage for Detection of Pentium III Processor SIMD Integer Instructions.................................................... 4-2 Using SIMD Integer, Floating-Point, and MMX Technology Instructions .......................................................................... 4-2 Using the EMMS Instruction ............................................. 4-3 Guidelines for Using EMMS Instruction ............................ 4-5 Data Alignment ...................................................................... 4-6 SIMD Integer and SIMD Floating-point Instructions .............. 4-6 SIMD Instruction Port Assignments .................................. 4-7 Coding Techniques for MMX Technology SIMD Integer Instructions .......................................................................... 4-7 Unsigned Unpack.............................................................. 4-8 Signed Unpack.................................................................. 4-8 vi Contents Interleaved Pack without Saturation ............................... 4-11 Non-Interleaved Unpack ................................................. 4-12 Complex Multiply by a Constant ..................................... 4-14 Absolute Difference of Unsigned Numbers .................... 4-14 Absolute Difference of Signed Numbers ........................ 4-15 Absolute Value................................................................ 4-17 Clipping to an Arbitrary Signed Range [high, low] .......... 4-17 Clipping to an Arbitrary Unsigned Range [high, low] ...... 4-19 Generating Constants..................................................... 4-20 Coding Techniques for Integer Streaming SIMD Extensions ........................................................................ 4-21 Extract Word ................................................................... 4-22 Insert Word ..................................................................... 4-22 Packed Signed Integer Word Maximum ......................... 4-23 Packed Unsigned Integer Byte Maximum....................... 4-23 Packed Signed Integer Word Minimum .......................... 4-23 Packed Unsigned Integer Byte Minimum........................ 4-24 Move Byte Mask to Integer............................................. 4-24 Packed Multiply High Unsigned ...................................... 4-25 Packed Shuffle Word ...................................................... 4-25 Packed Sum of Absolute Differences ............................. 4-26 Packed Average (Byte/Word).......................................... 4-27 Memory Optimizations ........................................................ 4-27 Partial Memory Accesses............................................... 4-28 Instruction Selection to Reduce Memory Access Hits.... 4-30 Increasing Bandwidth of Memory Fills and Video Fills ... 4-32 Increasing Memory Bandwidth Using the MOVQ Instruction................................................................ 4-32 Increasing Memory Bandwidth by Loading and Storing to and from the Same DRAM Page............. 4-32 Increasing the Memory Fill Bandwidth by Using Aligned Stores ......................................................... 4-33 vii Intel Architecture Optimization Reference Manual Use 64-Bit Stores to Increase the Bandwidth to Video.................................................................... 4-33 Increase the Bandwidth to Video Using Aligned Stores....................................................................... 4-33 Scheduling for the SIMD Integer Instructions ...................... 4-34 Scheduling Rules ............................................................ 4-34 Chapter 5 Optimizing Floating-point Applications Rules and Suggestions.......................................................... 5-1 Planning Considerations........................................................ 5-2 Which Part of the Code Benefits from SIMD Floating-point Instructions? ............................................ 5-3 MMX Technology and Streaming SIMD Extensions Floating-point Code ....................................................... 5-3 Scalar Code Optimization ................................................. 5-3 EMMS Instruction Usage Guidelines ................................ 5-4 CPUID Usage for Detection of SIMD Floating-point Support ........................................................................... 5-5 Data Alignment ................................................................. 5-5 Data Arrangement............................................................. 5-6 Vertical versus Horizontal Computation ....................... 5-6 Data Swizzling............................................................ 5-10 Data Deswizzling........................................................ 5-13 Using MMX Technology Code for Copy or Shuffling Functions ................................................................. 5-17 Horizontal ADD .......................................................... 5-18 Scheduling ..................................................................... 5-22 Scheduling with the Triple-Quadruple Rule..................... 5-24 Modulo Scheduling (or Software Pipelining) ................... 5-25 Scheduling to Avoid Register Allocation Stalls................ 5-31 Forwarding from Stores to Loads.................................... 5-31 Conditional Moves and Port Balancing ................................ 5-31 Conditional Moves........................................................... 5-31 viii Contents Port Balancing ................................................................ 5-33 Streaming SIMD Extension Numeric Exceptions ................ 5-36 Exception Priority ........................................................... 5-37 Automatic Masked Exception Handling .......................... 5-38 Software Exception Handling - Unmasked Exceptions .. 5-39 Interaction with x87 Numeric Exceptions ....................... 5-41 Use of CVTTPS2PI/CVTTSS2SI Instructions ................. 5-42 Flush-to-Zero Mode ........................................................ 5-42 Chapter 6 Optimizing Cache Utilization for Pentium III Processors Prefetch and Cacheability Instructions .................................. 6-2 The Prefetching Concept.................................................. 6-2 The Prefetch Instructions.................................................. 6-3 Prefetch and Load Instructions......................................... 6-4 The Non-temporal Store Instructions ............................... 6-5 The sfence Instruction ...................................................... 6-6 Streaming Non-temporal Stores ....................................... 6-6 Coherent Requests...................................................... 6-8 Non-coherent Requests............................................... 6-8 Other Cacheability Control Instructions............................ 6-9 Memory Optimization Using Prefetch.................................. 6-10 Prefetching Usage Checklist .......................................... 6-12 Prefetch Scheduling Distance ........................................ 6-12 Prefetch Concatenation .................................................. 6-13 Minimize Number of Prefetches ..................................... 6-15 Mix Prefetch with Computation Instructions ................... 6-16 Prefetch and Cache Blocking Techniques ...................... 6-18 Single-pass versus Multi-pass Execution ....................... 6-23 Memory Bank Conflicts .................................................. 6-25 Non-temporal Stores and Software Write-Combining .... 6-25 Cache Management ....................................................... 6-26 Video Encoder ........................................................... 6-27 ix Intel Architecture Optimization Reference Manual Video Decoder ........................................................... 6-27 Conclusions from Video Encoder and Decoder Implementation ........................................................ 6-28 Using Prefetch and Streaming-store for a Simple Memory Copy............................................... 6-28 TLB Priming ............................................................... 6-29 Optimizing the 8-byte Memory Copy .......................... 6-29 Chapter 7 Application Performance Tools VTune™ Performance Analyzer............................................. 7-2 Using Sampling Analysis for Optimization ........................ 7-2 Time-based Sampling .................................................. 7-2 Event-based Sampling ................................................. 7-4 Sampling Performance Counter Events ....................... 7-4 Call Graph Profiling ........................................................... 7-7 Call Graph Window ...................................................... 7-7 Static Code Analysis ......................................................... 7-9 Static Assembly Analysis ........................................... 7-10 Dynamic Assembly Analysis ...................................... 7-10 Code Coach Optimizations......................................... 7-11 Assembly Coach Optimization Techniques ................ 7-13 Intel Compiler Plug-in .......................................................... 7-14 Code Optimization Options ........................................ 7-14 Interprocedural and Profile-Guided Optimizations ..... 7-17 Intel Performance Library Suite ........................................... 7-18 Benefits Summary........................................................... 7-19 Libraries Architecture ..................................................... 7-19 Optimizations with Performance Library Suite ................ 7-20 Register Viewing Tool (RVT)................................................ 7-21 Register Data .................................................................. 7-21 Disassembly Data ........................................................... 7-21 x Contents Appendix A Optimization of Some Key Algorithms for the Pentium III Processors Newton-Raphson Method with the Reciprocal Instructions... A-2 Performance Improvements ............................................. A-3 Newton-Raphson Method for Reciprocal Square Root .... A-3 Newton-Raphson Inverse Reciprocal Approximation ....... A-5 3D Transformation Algorithms ............................................... A-7 Aos and SoA Data Structures .......................................... A-8 Performance Improvements ............................................. A-8 SoA .............................................................................. A-8 Prefetching................................................................... A-9 Avoiding Dependency Chains...................................... A-9 Implementation ................................................................. A-9 Assembly Code for SoA Transformation......................... A-13 Motion Estimation................................................................ A-14 Performance Improvements ........................................... A-14 Sum of Absolute Differences ..................................... A-15 Prefetching................................................................. A-15 Implementation ............................................................... A-15 Upsample ............................................................................ A-15 Performance Improvements ........................................... A-16 Streaming SIMD Extensions Implementation of the Upsampling Algorithm .................................................. A-16 FIR Filter Algorithm Using Streaming SIMD Extensions ..... A-17 Performance Improvements for Real FIR Filter .............. A-17 Parallel Multiplication and Interleaved Additions........ A-17 Reducing Data Dependency and Register Pressure . A-17 Scheduling for the Reorder Buffer and the Reservation Station ................................................. A-18 Wrapping the Loop Around (Software Pipelining)...... A-18 Advancing Memory Loads ......................................... A-19 Separating Memory Accesses from Operations ........ A-19 xi Intel Architecture Optimization Reference Manual Unrolling the Loop ...................................................... A-19 Minimizing Pointer Arithmetic/Eliminating Unnecessary Micro-ops ........................................... A-20 Prefetch Hints............................................................. A-20 Minimizing Cache Pollution on Write .......................... A-20 Performance Improvements for the Complex FIR Filter .. A-21 Unrolling the Loop ...................................................... A-21 Reducing Non-Value-Added Instructions ................... A-21 Complex FIR Filter Using a SIMD Data Structure ...... A-21 Code Samples ................................................................ A-22 Appendix B Performance-Monitoring Events and Counters Performance-affecting Events................................................ B-1 Programming Notes ........................................................ B-13 RDPMC Instruction ......................................................... B-13 Instruction Specification ............................................. B-13 Appendix C Instruction to Decoder Specification Appendix D Streaming SIMD Extensions Throughput and Latency Appendix E Stack Alignment for Streaming SIMD Extensions Stack Frames......................................................................... E-1 Aligned esp-Based Stack Frames ..................................... E-4 Aligned ebp-Based Stack Frames..................................... E-6 Stack Frame Optimizations ............................................... E-9 Inlined Assembly and ebx...................................................... E-9 Appendix F The Mathematics of Prefetch Scheduling Distance Simplified Equation ................................................................ F-1 Mathematical Model for PSD ................................................. F-2 No Preloading or Prefetch................................................. F-5 Compute Bound (Case:Tc >= Tl + Tb) .............................. F-7 xii Contents Compute Bound (Case: Tl + Tb > Tc > Tb) ...................... F-8 Memory Throughput Bound (Case: Tb >= Tc) ................. F-9 Example ......................................................................... F-10 Examples 2-1 Prediction Algorithm ..................................................... 2-4 2-2 Misprediction Example ................................................. 2-5 2-3 Assembly Equivalent of Conditional C Statement ........ 2-6 2-4 Code Optimization to Eliminate Branches .................... 2-6 2-5 Eliminating Branch with CMOV Instruction ................... 2-7 2-6 Partial Register Stall ..................................................... 2-9 2-7 Partial Register Stall with Pentium II and Pentium III Processors ................................................................... 2-9 2-8 Simplifying the Blending of Code in Pentium II and Pentium III Processors .............................................. 2-10 2-9 Scheduling Instructions for the Decoder ..................... 2-15 2-10 Scheduling Floating-Point Instructions ....................... 2-22 2-11 Coding for a Floating-Point Register File .................... 2-22 2-12 Using the FXCH Instruction ........................................ 2-23 2-13 Large and Small Load Stalls ...................................... 2-25 2-14 Algorithm to Avoid Changing the Rounding Mode ...... 2-26 2-15 Loop Unrolling ............................................................ 2-28 2-16 Hiding One-Clock Latency .......................................... 2-29 3-1 Identification of MMX Technology with cpuid ................ 3-2 3-2 Identification of Streaming SIMD Extensions with cpuid 3-3 3-3 Identification of Streaming SIMD Extensions by the OS .......................................................................... 3-4 3-4 Simple Four-Iteration Loop ........................................... 3-9 3-5 Streaming SIMD Extensions Using Inlined Assembly Encoding .................................................................... 3-10 3-6 Simple Four-Iteration Loop Coded with Intrinsics ....... 3-11 3-7 C++ Code Using the Vector Classes .......................... 3-13 xiii Intel Architecture Optimization Reference Manual 3-8 Automatic Vectorization for a Simple Loop ................. 3-14 3-9 C Algorithm for 64-bit Data Alignment ........................ 3-17 3-10 AoS data structure ...................................................... 3-22 3-11 SoA data structure ..................................................... 3-22 3-12 Pseudo-code Before Strip Mining ............................... 3-24 3-13 A Strip Mining Code .................................................... 3-25 3-14 Loop Blocking ............................................................. 3-26 4-1 Resetting the Register between __m64 and FP Data Types .................................................................... 4-5 4-2 Unsigned Unpack Instructions ...................................... 4-8 4-3 Signed Unpack Instructions .......................................... 4-9 4-4 Interleaved Pack with Saturation ................................. 4-11 4-5 Interleaved Pack without Saturation ............................ 4-12 4-6 Unpacking Two Packed-word Sources in a Non-interleaved Way ................................................... 4-13 4-7 Complex Multiply by a Constant .................................. 4-14 4-8 Absolute Difference of Two Unsigned Numbers .......... 4-15 4-9 Absolute Difference of Signed Numbers ..................... 4-16 4-10 Computing Absolute Value .......................................... 4-17 4-11 Clipping to an Arbitrary Signed Range [high, low] ...... 4-18 4-12 Simplified Clipping to an Arbitrary Signed Range ....... 4-19 4-13 Clipping to an Arbitrary Unsigned Range [high, low] .. 4-20 4-14 Generating Constants ................................................. 4-20 4-15 pextrw Instruction Code .............................................. 4-22 4-16 pinsrw Instruction Code .............................................. 4-23 4-17 pmovmskb Instruction Code ....................................... 4-24 4-18 pshuf Instruction Code ................................................ 4-26 4-19 A Large Load after a Series of Small Stalls ................ 4-28 4-20 Accessing Data without Delay ..................................... 4-29 4-21 A Series of Small Loads after a Large Store ............... 4-29 4-22 Eliminating Delay for a Series of Small Loads after a Large Store .............................................................. 4-30 5-1 Pseudocode for Horizontal (xyz, AoS) Computation ..... 5-9 xiv Contents 5-2 Pseudocode for Vertical (xxxx, yyyy, zzzz, SoA) Computation ................................................................. 5-9 5-3 Swizzling Data ............................................................ 5-10 5-4 Swizzling Data Using Intrinsics .................................. 5-12 5-5 Deswizzling Data ........................................................ 5-14 5-6 Deswizzling Data Using the movlhps and shuffle Instructions ..................................................... 5-15 5-7 Deswizzling Data Using Intrinsics with the movlhps and shuffle Instructions .............................................. 5-16 5-8 Using MMX Technology Code for Copying or Shuffling ..................................................................... 5-18 5-9 Horizontal Add Using movhlps/movlhps ..................... 5-20 5-10 Horizontal Add Using Intrinsics with movhlps/movlhps 5-21 5-11 Scheduling Instructions that Use the Same Register . 5-22 5-12 Scheduling with the Triple/Quadruple Rule ................ 5-25 5-13 Proper Scheduling for Performance Increase ............. 5-29 5-14 Scheduling with Emulated Conditional Branch ........... 5-32 5-15 Replacing the Streaming SIMD Extensions Code with the MMX Technology Code ................................. 5-34 5-16 Typical Dot Product Implementation ........................... 5-35 6-1 Prefetch Scheduling Distance .................................... 6-13 6-2 Using Prefetch Concatenation .................................... 6-14 6-3 Concatenation and Unrolling the Last Iteration of Inner Loop .................................................................. 6-15 6-4 Prefetch and Loop Unrolling ....................................... 6-16 6-5 Spread Prefetch Instructions ...................................... 6-17 6-6 Data Access of a 3D Geometry Engine without Strip-mining ................................................................ 6-21 6-7 Data Access of a 3D Geometry Engine with Strip-mining ................................................................ 6-22 6-8 Basic Algorithm of a Simple Memory Copy ................ 6-28 6-9 An Optimized 8-byte Memory Copy ............................ 6-30 xv Intel Architecture Optimization Reference Manual A-1 Newton-Raphson Method for Reciprocal Square Root Approximation ...............................................................A-4 A-2 Newton-Raphson Inverse Reciprocal Approximation ....A-5 A-3 Transform SoA Functions, C Code ..............................A-10 E-1 Aligned esp-Based Stack Frames ................................E-5 E-2 Aligned ebp-based Stack Frames ................................E-6 F-1 Calculating Insertion for Scheduling Distance of 3 ......F-3 Figures 1-1 The Complete Pentium II and Pentium III Processors Architecture ................................................ 1-2 1-2 TExecution Units and Ports in the Out-Of-Order Core 1-10 1-3 TStreaming SIMD Extensions Data Type .................... 1-14 1-4 TStreaming SIMD Extensions Register Set ................ 1-14 1-5 TMMX Technology 64-bit Data Type ........................... 1-15 1-6 TMMX Technology Register Set .................................. 1-16 2-1 TPentium II Processor Static Branch Prediction Algorithm ....................................................................... 2-4 2-2 DCU Split in the Data Cache ...................................... 2-13 3-1 Converting to Streaming SIMD Extensions Chart ......... 3-5 3-2 Hand-Coded Assembly and High-Level Compiler Performance Tradeoffs .................................................. 3-9 3-3 Loop Blocking Access Pattern .................................... 3-27 4-1 Using EMMS to Reset the Tag after an MMX Instruction ............................................................ 4-4 4-2 PACKSSDW mm, mm/mm64 Instruction Example ..... 4-10 4-3 Interleaved Pack with Saturation ................................. 4-10 4-4 Result of Non-Interleaved Unpack in MM0 ................. 4-12 4-5 Result of Non-Interleaved Unpack in MM1 ................. 4-13 4-6 pextrw Instruction ........................................................ 4-22 4-7 pinsrw Instruction ........................................................ 4-23 4-8 pmovmskb Instruction Example .................................. 4-24 xvi Contents 4-9 pshuf Instruction Example .......................................... 4-25 4-10 PSADBW Instruction Example ................................... 4-26 5-1 Dot Product Operation .................................................. 5-8 5-2 Horizontal Add Using movhlps/movlhps ..................... 5-19 5-3 Modulo Scheduling Dependency Graph ..................... 5-26 6-1 Memory Access Latency and Execution Without Prefetch ...................................................................... 6-11 6-2 Memory Access Latency and Execution With Prefetch ...................................................................... 6-11 6-3 Cache Blocking - Temporally Adjacent and Non-adjacent Passes ................................................. 6-19 6-4 Examples of Prefetch and Strip-mining for Temporally Adjacent and Non-adjacent Passes Loops ................. 6-20 6-5 Benefits of Incorporating Prefetch into Code .............. 6-23 6-6 Single-Pass vs. Multi-Pass 3D Geometry Engines ..... 6-24 7-1 Sampling Analysis of Hotspots by Location .................. 7-3 7-2 Processor Events List ................................................... 7-5 7-3 Call Graph Window ....................................................... 7-8 7-4 Code Coach Optimization Advice ............................... 7-12 7-5 The RVT: Registers and Disassembly Window .......... 7-22 E-1 Stack Frames Based on Alignment Type ...................... E-3 F-1 Pentium II and Pentium III Processors Memory Pipeline Sketch ............................................................. F-4 F-2 Execution Pipeline, No Preloading or Prefetch ............. F-6 F-3 Compute Bound Execution Pipeline ............................. F-7 F-4 Compute Bound Execution Pipeline ............................. F-8 F-5 Memory Throughput Bound Pipeline ............................ F-9 F-6 Accesses per Iteration, Example 1 ............................. F-11 F-7 Accesses per Iteration, Example 2 ............................. F-12 Tables 1-1 Pentium II and Pentium III Processors Execution Units ............................................................................. 1-8 xvii Intel Architecture Optimization Reference Manual 4-1 Port Assignments .......................................................... 4-7 5-1 EMMS Instruction Usage Guidelines ............................ 5-4 5-2 SoA Form of Representing Vertices Data ..................... 5-7 5-3 EMMS Modulo Scheduling .......................................... 5-27 5-4 EMMS Schedule – Overlapping Iterations .................. 5-27 5-5 Modulo Scheduling with Interval MRT (II=4) ............... 5-28 B-1 Performance Monitoring Events ....................................B-2 C-1 Pentium II and Pentium III Processors Instruction to Decoder Specification .............................................. C-1 C-2 MMX Technology Instruction to Decoder Specification ............................................................... C-17 D-1 Streaming SIMD Extensions Throughput and Latency ................................................................. D-1 xviii Introduction Developing high-performance applications for Intel® architecture (IA)-based processors can be more efficient with better understanding of the newest IA. Even though the applications developed for the 8086/8088, 80286, Intel386™ (DX or SX), and Intel486™ processors will execute on the Pentium®, Pentium Pro, Pentium II and Pentium III processors without any modification or recomputing, the code optimization techniques combined with the advantages of the newest processors can help you tune your application to its greatest potential. This manual provides information on Intel architecture as well as describes code optimization techniques to enable you to tune your application for best results, specifically when run on Pentium II and Pentium III processors. Tuning Your Application Tuning an application to high performance across Intel architecture-based processors requires background information about the following: • the Intel architecture. • critical stall situations that may impact the performance of your application and other performance setbacks within your application • your compiler optimization capabilities • monitoring the application’s performance To help you understand your application and where to begin tuning, you can use Intel’s VTune™ Performance Analyzer. This tool helps you see the performance event counters data of your code provided by the Pentium II and Pentium III processors. This manual informs you about appropriate xvii Intel Architecture Optimization Reference Manual performance counter for measurement. For VTune Performance Analyzer order information, see its web home page at http://developer.intel.com/vtune. About This Manual This manual assumes that you are familiar with IA basics, as well as with C or C++ and assembly language programming. The manual consists of the following parts: Introduction. Defines the purpose and outlines the contents of this manual. Chapter 1—Processor Architecture Overview. Overviews the architectures of the Pentium II and Pentium III processors. Chapter 2—General Optimization Guidelines. Describes the code development techniques to utilize the architecture of Pentium II and Pentium III processors as well as general strategies of efficient memory utilization. Chapter 3—Coding for SIMD Architectures. Describes the following coding methodologies: assembly, inlined-assembly, intrinsics, vector classes, auto-vectorization, and libraries. Also discusses strategies for altering data layout and restructuring algorithms for SIMD-style coding. Chapter 4—Using SIMD Integer Instructions. Describes optimization rules and techniques for high-performance integer and MMX™ technology applications. Chapter 5—Optimizing Floating-Point Applications. Describes rules and optimization techniques, and provides code examples specific to floating-point code, including SIMD-floating point code for Streaming SIMD Extensions. Chapter 6—Optimizing Cache Utilization for Pentium III Processors. Describes the memory hierarchy of Pentium II and Pentium III processor architectures, and how to best use it. The prefetch instruction and cache control management instructions for Streaming SIMD Extensions are also described. xviii Introduction Chapter 7— Application Performance Tools. Describes application performance tools: VTune analyzer, Intel® Compiler plug-ins, and Intel® Performance Libraries Suite. For each tool, techniques and code optimization strategies that help you to take advantage of the Intel architecture are described. Appendix A—Optimization of Some Key Algorithms for the Pentium II and Pentium III Processors. Describes how to optimize the following common algorithms using the Streaming SIMD Extensions: 3D lighting and transform, image compression, audio decomposition, and others. Appendix B—Performance Monitoring Events and Counters. Describes performance-monitoring events and counters and their functions. Appendix C—Instruction to Decoder Specification. Summarizes the IA macro instructions with Pentium II and Pentium III processor decoding information to enable scheduling. Appendix D—Streaming SIMD Extensions Throughput and Latency. Summarizes in a table the instructions’ throughput and latency characteristics. Appendix E—Stack Alignment for Streaming SIMD Extensions. Details on the alignment of the stacks of data for Streaming SIMD Extensions. Appendix F—The Mathematics of Prefetch Scheduling Distance. Discusses how far away prefetch instructions should be inserted. Related Documentation For more information on the Intel architecture, specific techniques and processor architecture terminology referenced in this manual, see the following documentation: Intel Architecture MMX™ Technology Programmer's Reference Manual, order number 243007 Pentium Processor Family Developer’s Manual, Volumes 1, 2, and 3, order numbers 241428, 241429, and 241430 Pentium Pro Processor Family Developer’s Manual, Volumes 1, 2, and 3, order numbers 242690, 242691, and 242692 Pentium II Processor Developer’s Manual, order number 243502 Intel C/C++ Compiler for Win32* Systems User’s Guide, order number 718195 xix Intel Architecture Optimization Reference Manual Notational Conventions This manual uses the following conventions: This type style Indicates an element of syntax, a reserved word, a keyword, a filename, instruction, computer output, or part of a program example. The text appears in lowercase unless uppercase is significant. THIS TYPE STYLE Indicates a value, for example, TRUE, CONST1, or a variable, for example, A, B, or register names MMO through MM7. l indicates lowercase letter L in examples. 1 is the number 1 in examples. O is the uppercase O in examples. 0 is the number 0 in examples. This type style Indicates a placeholder for an identifier, an expression, a string, a symbol, or a value. Substitute one of these items for the placeholder. ... (ellipses) Indicate that a few lines of the code are omitted. This type style Indicates a hypertext link. xx Processor Architecture Overview 1 This chapter provides an overview of the architectural features of the Pentium® II and Pentium III processors and explains the new capabilities of the Pentium III processor. The Streaming SIMD Extensions of the Pentium III processor introduce new general purpose integer and floating-point SIMD instructions, which accelerate applications performance over the Pentium II processors. The Processors’ Execution Architecture The Pentium II and Pentium III processors are aggressive microarchitectural implementations of the 32-bit Intel® architecture (IA). They are designed with a dynamic execution architecture that provides the following features: • out-of-order speculative execution to expose parallelism • superscalar issue to exploit parallelism • hardware register renaming to avoid register name space limitations • pipelined execution to enable high clock speeds • branch prediction to avoid pipeline delays The microarchitecture is designed to execute legacy 32-bit Intel architecture code as quickly as possible, without additional effort from the programmer. This optimization manual assists the developer in leveraging the features of the microarchitecture to attain greater performance by understanding and working with these features to maximally enhance performance. 1-1 1 Intel Architecture Optimization Reference Manual The Pentium® II and Pentium III Processors Pipeline The Pentium II and Pentium III processors’ pipelines contain three parts: • the in-order issue front end • the out-of-order core • the in-order retirement unit. Figure 1-1 gives an overview of the Pentium II and Pentium III processors architecture. Figure 1-1 The Complete Pentium II and Pentium III Processors Architecture System bus L2 cache Bus Interface Unit L1 instruction L1 data cache cache Fetch Load Store Fetch & Decode Unit Dispatch / Execute Unit Retirement Unit (In order unit) (out of order unit) (In order unit) •Fetches instructions •Schedules and executes µOPs •Retires instructions in order •Decodes instructions to µOPs •Contains 5 execution ports •Writes results to registers/memory •Performs branch prediction Instruction Pool/reorder buffer •Buffer of µOPs waiting for execution The In-order Issue Front End The front end supplies instructions in program order to the out-of-order core. It fetches and decodes Intel architecture-based processor macroinstructions, and breaks them down into simple operations called micro-ops (µops). It can issue multiple µops per cycle, in original program order, to the out-of-order core. Since the core aggressively reorders and executes instructions out of program order, the most important consideration in performance tuning is to ensure that enough µops are ready 1-2 Processor Architecture Overview 1 for execution. Accurate branch prediction, instruction prefetch, and fast decoding are essential to getting the most performance out of the in-order front end. The Out-of-order Core The core’s ability to execute instructions out of order is a key factor in exploiting parallelism. This feature enables the processor to reorder instructions so that if one µop is delayed while waiting for data or a contended resource, other µops that are later in program order may proceed around it. The processor employs several buffers to smooth the flow of µops. This implies that when one portion of the pipeline experiences a delay, that delay may be covered by other operations executed in parallel or by executing µops which were previously queued up in a buffer. The delays described in this chapter are treated in this manner. The out-of-order core buffers µops in a Reservation Station (RS) until their operands are ready and resources are available. Each cycle, the core may dispatch up to five µops, as explained in more detail later in the chapter. The core is designed to facilitate parallel execution. Load and store instructions may be issued simultaneously. Most simple operations, such as integer operations, floating-point add, and floating-point multiply, can be pipelined with a throughput of one or two operations per clock cycle. Long latency operations can proceed in parallel with short latency operations. In-Order Retirement Unit For semantically-correct execution, the results of instructions must be processed in original program order. Likewise, any exceptions that occur must be processed in program order. When a µop completes and writes its result, it is retired. Up to three µops may be retired per cycle. The unit in the processor which buffers completed µops is the reorder buffer (ROB). ROB updates the architectural state in order, that is, updates the state of instructions and registers in the program semantics order. ROB also manages the ordering of exceptions. 1-3 1 Intel Architecture Optimization Reference Manual Front-End Pipeline Detail For better understanding operation of the Pentium II and Pentium III processors, this section explains the main processing units of their front-end pipelines: instruction prefetcher, decoders, and branch prediction. Instruction Prefetcher The instruction prefetcher performs aggressive prefetch of straight line code. The Pentium II and Pentium III processors read in instructions from 16-byte-aligned boundaries. For example, if the modulo 16 branch target address (the address of a label) is equal to 14, only two useful instruction bytes are fetched in the first cycle. The rest of the instruction bytes are fetched in subsequent cycles. NOTE. Instruction fetch is always intended for an aligned 16-byte block. Decoders Pentium II and Pentium III processors have three decoders. In each clock cycle, the first decoder is capable of decoding one macroinstruction made up of four or fewer µops. It can handle any number of bytes up to the maximum of 15, but nine- or more-byte instructions require additional cycles. In each clock cycle, the other two decoders can each decode an instruction of one µop, and up to eight bytes. Instructions composed of more than four µops take multiple cycles to decode. Simple instructions have one to four µops; complex instructions (for example, cmpxcg) generally have more than four µops. Complex instructions require multiple cycles to decode. During every clock cycle, up to three macroinstructions are decoded. However, if the instructions are complex or are over seven bytes long, the decoder is limited to decoding fewer instructions. The decoders can decode: • up to three macroinstructions per clock cycle • up to six µops per clock cycle 1-4 Processor Architecture Overview 1 When programming in assembly language, try to schedule your instructions in a 4-1-1 µop sequence, which means instruction with four µops followed by two instructions each with one µop. Scheduling the instructions in a 4-1-1 µop sequence increases the number of instructions that can be decoded during one clock cycle. Most commonly used instructions have the following µop numbers: • Simple instructions of the register-register form have only one µop. • Load instructions are only one µop. • Store instructions have two µops. • Simple read-modify instructions are two µops. • Simple instructions of the register-memory form have two to three µops. • Simple read-modify-write instructions have four µops. See Appendix C, “Instruction to Decoder Specification” for a table specifying the number of µops required by each instruction in the Intel architecture instruction set. Branch Prediction Overview Pentium II and Pentium III processors use a branch target buffer (BTB) to predict the direction and target of branches based on an instruction’s address. The address of the branch instruction is available before the branch has been decoded, so a BTB-based prediction can be made as early as possible to avoid delays caused by going the wrong direction on a branch. The 512-entry BTB stores the history of previously-seen branches and their targets. When a branch is prefetched, the BTB feeds the target address directly into the instruction fetch unit (IFU). Once the branch is executed, the BTB is updated with the target address. Using the branch target buffer allows dynamic prediction of previously seen branches. Once the branch instruction is decoded, the direction of the branch (forward or backward) is known. If there was not a valid entry in the BTB for the branch, the static predictor makes a prediction based on the direction of the branch. 1-5 1 Intel Architecture Optimization Reference Manual Dynamic Prediction The branch target buffer prediction algorithm includes pattern matching and can track up to the last four branch directions per branch address. For example, a loop with four or fewer iterations should have about 100% correct prediction. Additionally, Pentium II and Pentium III processors have a return stack buffer (RSB) that can predict return addresses for procedures that are called from different locations in succession. This increases the benefit of unrolling loops containing function calls. It also mitigates the need to put certain procedures in-line since the return penalty portion of the procedure call overhead is reduced. Pentium II and Pentium III processors have three levels of branch support that can be quantified in the number of cycles lost: 1. Branches that are not taken suffer no penalty. This applies to those branches that are correctly predicted as not taken by the BTB, and to forward branches that are not in the BTB and are predicted as not taken by default. 2. Branches that are correctly predicted as taken by the BTB suffer a minor penalty of losing one cycle of instruction fetch. As with any taken branch, the decode of the rest of the µops after the branch is wasted. 3. Mispredicted branches suffer a significant penalty. The penalty for mispredicted branches is at least nine cycles (the length of the in-order issue pipeline) of lost instruction fetch, plus additional time spent waiting for the mispredicted branch instruction to retire. This penalty is dependent upon execution circumstances. Typically, the average number of cycles lost because of a mispredicted branch is between 10 and 15 cycles and possibly as many as 26 cycles. Static Prediction Branches that are not in the BTB, but are correctly predicted by the static prediction mechanism, suffer a small penalty of about five or six cycles (the length of the pipeline to this point). This penalty applies to unconditional direct branches that have never been seen before. 1-6 Processor Architecture Overview 1 The static prediction mechanism predicts backward conditional branches (those with negative displacement), such as loop-closing branches, as taken. They suffer only a small penalty of approximately six cycles the first time the branch is encountered and a minor penalty of approximately one cycle on subsequent iterations when the negative branch is correctly predicted by the BTB. Forward branches are predicted as not taken. The small penalty for branches that are not in the BTB but are correctly predicted by the decoder is approximately five cycles of lost instruction fetch. This compares to 10-15 cycles for a branch that is incorrectly predicted or that has no prediction. In order to take advantage of the forward-not-taken and backward-taken static predictions, the code should be arranged so that the likely target of the branch immediately follows forward branches. See examples on branch prediction in “Branch Prediction” in Chapter 2. Execution Core Detail To successfully implement parallelism, information on execution units’ latency is required. Also important is the information on the execution units layout in the pipelines and on the µops that execute in pipelines. This section details on the execution core operation including the discussion on instruction latency and throughput, execution units and ports, caches, and store buffers. Instruction Latency and Throughput The core’s ability to exploit parallelism can be enhanced by ordering instructions so that their operands are ready and their corresponding execution units are free when they reach the reservation stations. Knowing instructions’ latencies helps in scheduling instructions appropriately. Some execution units are not pipelined, such that µops cannot be dispatched in consecutive cycles and the throughput is less than one per cycle. Table 1-1 lists Pentium II and Pentium III processors execution units, their latency, and their issue throughput. 1-7 1 Intel Architecture Optimization Reference Manual Table 1-1 Pentium II and Pentium III Processors Execution Units Port Execution Units Latency/Throughput 0 Integer ALU Unit: Latency 1, Throughput 1/cycle LEA instructions Latency 1, Throughput 1/cycle Shift instructions Latency 1, Throughput 1/cycle Integer Multiplication Latency 4, Throughput 1/cycle instruction Floating-Point Unit: FADD instruction Latency 3, Throughput 1/cycle (horizontal align with FADD) FMUL instruction Latency 5, Throughput 1/2cycle1 (align with FMULL) FDIV instruction Latency: single-precision 18 cycles, double-precision 32 cycles, extended-precision 38 cycles. Throughput non-pipelined (align with FDIV) MMX™ technology ALU Unit Latency 1, Throughput 1/cycle MMX technology Multiplier Latency 3, Throughput 1/cycle Unit Streaming SIMD Extensions See Appendix D, “Streaming SIMD Extensions Floating Point Unit: Multiply, Throughput and Latency” Divide, Square Root, Move instructions 1 Integer ALU Unit Latency 1, Throughput 1/cycle MMX technology ALU Unit Latency 1, Throughput 1/cycle MMX technology Shift Unit Latency 1, Throughput 1/cycle Streaming SIMD Extensions: See Appendix D, “Streaming SIMD Extensions Adder, Reciprocal and Throughput and Latency” Reciprocal Square Root, Shuffle/Move instructions continued 1-8 Processor Architecture Overview 1 Table 1-1 Pentium II and Pentium III Processors Execution Units (continued) Port Execution Units Latency/Throughput 2 Load Unit Latency 3 on a cache hit, Throughput 1/cycle Streaming SIMD Extensions See Appendix D, “Streaming SIMD Extensions Load instructions Throughput and Latency” 3 Store Address Unit Latency 0 or 3 (not on critical path), Throughput 1/cycle2 Streaming SIMD Extensions See Appendix D, “Streaming SIMD Extensions Store instruction Throughput and Latency” 4 Store Data Unit Latency 1, Throughput 1/cycle Streaming SIMD Extensions See Appendix D, “Streaming SIMD Extensions Store instruction Throughput and Latency” 1. The FMUL unit cannot accept a second FMUL in the cycle after it has accepted the first. This is NOT the same as only being able to do FMULs on even clock cycles. FMUL is pipelined once every two clock cycles. 2. A load that gets its data from a store to the same address can dispatch in the same cycle as the store, so in that sense the latency of the store is 0. The store itself takes three cycles to complete, but that latency affects only how soon a store buffer entry is freed for use by another µop. Execution Units and Ports Each cycle, the core may dispatch zero or one µop on a port to any of the five pipelines (shown in Figure 1-2) for a maximum issue bandwidth of five µops per cycle. Each pipeline contains several execution units. The µops are dispatched to the pipeline that corresponds to its type of operation. For example, an integer arithmetic logic unit (ALU) and the floating-point execution units (adder, multiplier, and divider) share a pipeline. Knowledge of which µops are executed in the same pipeline can be useful in ordering instructions to avoid resource conflicts. 1-9 1 Intel Architecture Optimization Reference Manual Figure 1-2 Execution Units and Ports in the Out-Of-Order Core Store Address Load Store Calculation Unit Unit Data Unit (16-entry buffer) (12-entry buffer) (12-entry buffer) Port 2 Port 3 Port 4 Reservation Station Port 1 Port 0 Integer Integer Unit Unit FP Unit MMX™ technology Address Pentium(R) III processor Generation FP Unit Unit MMX™ technology Pentium(R) III processor FP Unit Caches of the Pentium II and Pentium III Processors The on-chip cache subsystem of Pentium II and Pentium III processors consists of two 16-Kbyte four-way set associative caches with a cache line length of 32 bytes. The caches employ a write-back mechanism and a pseudo-LRU (least recently used) replacement algorithm. The data cache consists of eight banks interleaved on four-byte boundaries. 1-10 Processor Architecture Overview 1 Level two (L2) caches have been off chip but in the same package. They are 128K or more in size. L2 latencies are in the range of 4 to 10 cycles. An L2 miss initiates a transaction across the bus to memory chips. Such an access requires on the order of at least 11 additional bus cycles, assuming a DRAM page hit. A DRAM page miss incurs another three bus cycles. Each bus cycle equals several processor cycles, for example, one bus cycle for a 100 MHz bus is equal to four processor cycles on a 400 MHz processor. The speed of the bus and sizes of L2 caches are implementation dependent, however. Check the specifications of a given system to understand the precise characteristics of the L2 cache. Store Buffers Pentium II and Pentium III processors have twelve store buffers. These processors temporarily store each write (store) to memory in a store buffer. The store buffer improves processor performance by allowing the processor to continue executing instructions without having to wait until a write to memory and/or cache is complete. It also allows writes to be delayed for more efficient use of memory-access bus cycles. Writes stored in the store buffer are always written to memory in program order. Pentium II and Pentium III processors use processor ordering to maintain consistency in the order in which data is read (loaded) and written (stored) in a program and the order in which the processor actually carries out the reads and writes. With this type of ordering, reads can be carried out speculatively; and in any order, reads can pass buffered writes, while writes to memory are always carried out in program order. Write hits cannot pass write misses, so performance of critical loops can be improved by scheduling the writes to memory. When you expect to see write misses, schedule the write instructions in groups no larger than twelve, and schedule other instructions before scheduling further write instructions. 1-11 1 Intel Architecture Optimization Reference Manual Streaming SIMD Extensions of the Pentium III Processor The Streaming SIMD Extensions of the Pentium III processor accelerate performance of applications over the Pentium II processors, for example, 3D graphics. The programming model is similar to the MMX™ technology model except that instructions now operate on new packed floating-point data types, which contain four single-precision floating-point numbers. The Streaming SIMD Extensions of the Pentium III processor introduce new general purpose floating-point instructions, which operate on a new set of eight 128-bit Streaming SIMD Extensions registers. This gives the programmer the ability to develop algorithms that can mix packed single-precision floating-point and integer using both Streaming SIMD Extensions and MMX instructions respectively. In addition to these instructions, Streaming SIMD Extensions technology also provide new instructions to control cacheability of all data types. These include ability to stream data into the processor while minimizing pollution of the caches and the ability to prefetch data before it is actually used. Both 64-bit integer and packed floating point data can be streamed to memory. The main focus of packed floating-point instruction is the acceleration of 3D geometry. The new definition also contains additional SIMD integer instructions to accelerate 3D rendering and video encoding and decoding. Together with the cacheability control instructions, this combination enables the development of new algorithms that can significantly accelerate 3D graphics and other applications that involve intensive computation. The new Streaming SIMD Extensions state requires operating system support for saving and restoring the new state during a context switch. A new set of extended fsave/frstor (called fxsave/fxrstor) permits saving/restoring new and existing state for applications and operating systems. To make use of these new instructions, an application must verify that the processor and operating system support Streaming SIMD Extensions. If both do, then the software application can use the new features. 1-12 Processor Architecture Overview 1 The Streaming SIMD Extensions are fully compatible with all software written for Intel architecture microprocessors. All existing software continues to run correctly, without modification, on microprocessors that incorporate the Streaming SIMD Extensions, as well as in the presence of existing and new applications that incorporate this technology. Single-Instruction, Multiple-Data (SIMD) The Streaming SIMD Extensions support operations on packed single-precision floating-point data types, and the additional SIMD integer instructions support operations on packed quadword data types (byte, word, or double-word). This approach was chosen because most 3D graphics and digital signal processing (DSP) applications have the following characteristics: • inherently parallel • wide dynamic range, hence floating-point based • regular and re-occurring memory access patterns • localized re-occurring operations performed on the data • data-independent control flow. Streaming SIMD Extensions fully support the IEEE Standard 754 for Binary Floating-Point Architecture. The Streaming SIMD Extensions are accessible from all IA execution modes: protected mode, real-address mode, and Virtual 8086 mode. New Data Types The principal data type of the Streaming SIMD Extensions are a packed single-precision floating-point operand, specifically four 32-bit single-precision (SP) floating-point numbers shown in Figure 1-3. The SIMD integer instructions operate on the packed byte, word, or double-word data types. The prefetch instructions work on a cache line granularity regardless of type. 1-13 1 Intel Architecture Optimization Reference Manual Figure 1-3 Streaming SIMD Extensions Data Type 127 96 95 64 63 32 31 0 Packed, single-precision FP Streaming SIMD Extensions Registers The Streaming SIMD Extensions provide eight 128-bit general-purpose registers, each of which can be directly addressed. These registers are a new state, requiring support from the operating system to use them. They can hold packed, 128-bit data, and are accessed directly by the Streaming SIMD Extensions using the register names XMM0 to XMM7, see Figure 1-4. Figure 1-4 Streaming SIMD Extensions Register Set 127 0 XMM7 XMM6 XMM5 XMM4 XMM3 XMM2 XMM1 XMM0 1-14 Processor Architecture Overview 1 MMX™ Technology Intel’s MMX™ technology is an extension to the Intel architecture (IA) instruction set. The technology uses a single instruction, multiple data (SIMD) technique to speed up multimedia and communications software by processing data elements in parallel. The MMX instruction set adds 57 opcodes and a 64-bit quadword data type. The 64-bit data type, illustrated in Figure 1-5, holds packed integer values upon which MMX instructions operate. In addition, there are eight 64-bit MMX technology registers, each of which can be directly addressed using the register names MM0 to MM7. Figure 1-6 shows the layout of the eight MMX technology registers. Figure 1-5 MMX Technology 64-bit Data Type Packed Byte: 8 bytes packed into 64-bits 63 32 31 16 15 87 0 Packed Word: Four words packed into 64-bits 63 32 31 16 15 0 Packed Double-word: Two doublewords packed into 64-bits 63 32 31 0 1-15 1 Intel Architecture Optimization Reference Manual Figure 1-6 MMX Technology Register Set Tag Field 10 63 0 MM7 MM0 The MMX technology is operating-system-transparent and 100% compatible with all existing Intel architecture software. Therefore all applications will continue to run on processors with MMX technology. Additional information and details about the MMX instructions, data types, and registers can be found in the Intel Architecture MMX™ Technology Programmer’s Reference Manual, order number 243007. 1-16 General Optimization Guidelines 2 This chapter discusses general optimization techniques that can improve the performance of applications for the Pentium® II and Pentium III processor architectures. It discusses general guidelines as well as specifics of each guideline and provides examples of how to improve your code. Integer Coding Guidelines The following guidelines will help you optimize your code: • Use a current generation of compiler, such as the Intel® C/C++ Compiler that will produce an optimized application. • Write code so that Intel compiler can optimize it for you: — Minimize use of global variables, pointers, and complex control flow — Use the const modifier, avoid register modifier — Avoid indirect calls and use the type system — Use minimum sizes for integer and floating-point data types to enable SIMD parallelism • Improve branch predictability by using the branch prediction algorithm. This is one of the most important optimizations for Pentium II processors. Improving branch predictability allows the code to spend fewer cycles fetching instructions due to fewer mispredicted branches. • Take advantage of the SIMD capabilities of MMX™ technology and Streaming SIMD Extensions. • Avoid partial register stalls. • Ensure proper data alignment. 2-1 2 Intel Architecture Optimization Reference Manual • Arrange code to minimize instruction cache misses and optimize prefetch. • Avoid prefixed opcodes other than 0F. • Avoid small loads after large stores to the same area of memory. Avoid large loads after small stores to the same area of memory. Load and store data to the same area of memory using the same data sizes and address alignments. • Use software pipelining. • Avoid self-modifying code. • Avoid placing data in the code segment. • Calculate store addresses as early as possible. • Avoid instructions that contain four or more µops or instructions that are more than seven bytes long. If possible, use instructions that require one µop. • Cleanse partial registers before calling callee-save procedures. Branch Prediction Branch optimizations are one of the most important optimizations for Pentium II processors. Understanding the flow of branches and improving the predictability of branches can increase the speed of your code significantly. Dynamic Branch Prediction Dynamic prediction is always attempted first by checking the branch target buffer (BTB) for a valid entry. If one is not there, static prediction is used. Three elements of dynamic branch prediction are important: • If the instruction address is not in the BTB, execution is predicted to continue without branching. This is known as “fall-through” meaning that the branch is not taken and the subsequent instruction is executed. • Predicted taken branches have a one clock delay. • The Pentium II and Pentium III processors’ BTB pattern matches on the direction of the last four branches to dynamically predict whether a branch will be taken. 2-2 General Optimization Guidelines 2 During the process of instruction prefetch the address of a conditional instruction is checked with the entries in the BTB. When the address is not in the BTB, execution is predicted to fall through to the next instruction. This suggests that branches should be followed by code that will be executed. The code following the branch will be fetched, and in the case of Pentium Pro, Pentium II processors, and Pentium III processor the fetched instructions will be speculatively executed. Therefore, never follow a branch instruction with data. Additionally, when an instruction address for a branch instruction is in the BTB and it is predicted taken, it suffers a one-clock delay on Pentium II processors. To avoid the delay of one clock for taken branches, simply insert additional work between branches that are expected to be taken. This delay restricts the minimum duration of loops to two clock cycles. If you have a very small loop that takes less than two clock cycles, unroll it to remove the one-clock overhead of the branch instruction. The branch predictor on Pentium II processors correctly predicts regular patterns of branches—up to a length of four. For example, it correctly predicts a branch within a loop that is taken on odd iterations, and not taken on even iterations. Static Prediction On Pentium II and Pentium III processors, branches that do not have a history in the BTB are predicted using a static prediction algorithm as follows: • Predict unconditional branches to be taken. • Predict backward conditional branches to be taken. This rule is suitable for loops. • Predict forward conditional branches to be NOT taken. A branch that is statically predicted can lose, at most, six cycles of instruction prefetch. An incorrect prediction suffers a penalty of greater than twelve clocks. Example 2-1 provides the static branch prediction algorithm. 2-3 2 Intel Architecture Optimization Reference Manual Figure 2-1 Pentium® II Processor Static Branch Prediction Algorithm Forward cond itional br anches n ot tak en (fal l throu gh) If < cond ition> { ... } U n cond itional Branch es tak en JMP for { ... } Back w ard Cond itional Br anches ar e tak en loop { } Example 2-1 and Example 2-2 illustrate the basic rules for the static prediction algorithm. Example 2-1 Prediction Algorithm Begin: mov eax, mem32 and eax, ebx imul eax, edx shld eax, 7 JC Begin In the above example, the backward branch (JC Begin) is not in the BTB the first time through, therefore, the BTB does not issue a prediction. The static predictor, however, will predict the branch to be taken, so a misprediction will not occur. 2-4 General Optimization Guidelines 2 Example 2-2 Misprediction Example mov eax, mem32 and eax, ebx imul eax, edx shld eax, 7 JC Begin mov eax, 0 Begin Call Convert The first branch instruction (JC Begin) in Example 2-3 segment is a conditional forward branch. It is not in the BTB the first time through, but the static predictor will predict the branch to fall through. The Call Convert instruction will not be predicted in the BTB the first time it is seen by the BTB, but the call will be predicted as taken by the static prediction algorithm. This is correct for an unconditional branch. In these examples, the conditional branch has only two alternatives: taken and not taken. Indirect branches, such as switch statements, computed GOTOs or calls through pointers, can jump to an arbitrary number of locations. Assuming the branch has a skewed target destination, and most of the time it branches to the same address, then the BTB will predict accurately most of the time. If, however, the target destination is not predictable, performance can degrade quickly. Performance can be improved by changing the indirect branches to conditional branches that can be predicted. Eliminating and Reducing the Number of Branches Eliminating branches improves performance due to: • Reducing the possibility of mispredictions. • Reducing the number of required BTB entries. Using the setcc instruction, or using the Pentium II and Pentium III processors’ conditional move (cmov or fcmov) instructions can eliminate branches. 2-5 2 Intel Architecture Optimization Reference Manual Following is a C code line with a condition that is dependent upon one of the constants: X = (A < B) ? C1 : C2; This code conditionally compares two values, A and B. If the condition is true, X is set to C1; otherwise it is set to C2. The assembly equivalent is shown in the Example 2-3: Example 2-3 Assembly Equivalent of Conditional C Statement cmp A, B ; condition jge L30 ; conditional branch mov ebx, CONST1 ; ebx holds X jmp L31 ; unconditional branch L30: mov ebx, CONST2 L31: If you replace the jge instruction in the previous example with a setcc instruction, this code can be optimized to eliminate the branches as shown in the Example 2-4: Example 2-4 Code Optimization to Eliminate Branches xor ebx, ebx ;clear ebx (X in the C code) cmp A, B setge ebx ;When ebx = 0 or 1 ;OR the complement condition dec ebx ;ebx=00...00 or 11...11 and ebx, (CONST1-CONST2);ebx=0 or(CONST1-CONST2) add ebx, CONST2 ;ebx=CONST1 or CONST2 The optimized code sets ebx to zero, then compares A and B. If A is greater than or equal to B, ebx is set to one. Then ebx is decreased and “and-ed” with the difference of the constant values. This sets ebx to either zero or the 2-6 General Optimization Guidelines 2 difference of the values. By adding CONST2 back to ebx, the correct value is written to ebx. When CONST1 is equal to zero, the last instruction can be deleted. Another way to remove branches on Pentium II and Pentium III processors is to use the cmov and fcmov instructions. Example 2-5 shows changing a test and branch instruction sequence using cmov and eliminating a branch. If the test sets the equal flag, the value in ebx will be moved to eax. This branch is data-dependent, and is representative of an unpredictable branch. Example 2-5 Eliminating Branch with CMOV Instruction test ecx, ecx jne 1h mov eax, ebx 1h: ; To change the code, the jne and the mov instructions ; are combined into one cmovcc instruction that checks ; the equal flag. The optimized code is: test ecx, ecx ; test the flags cmoveq eax, ebx ; if the equal flag is set, move ; ebx to eax 1h: The label 1h: is no longer needed unless it is the target of another branch instruction. The cmov and fcmov instructions are available on the Pentium Pro, Pentium II and Pentium III processors, but not on Pentium processors and earlier 32-bit Intel architecture-based processors. Be sure to check whether a processor supports these instructions with the cpuid instruction if an application needs to run on older processors as well. 2-7 2 Intel Architecture Optimization Reference Manual Performance Tuning Tip for Branch Prediction Intel C/C++ Compiler has a -Qxi switch which turns on Pentium II or Pentium III processor-specific code generation so that the compiler will generate cmov-/fcmov instruction sequences when possible, saving you the effort of doing it by hand. For information on branch elimination, see the Pentium II Processor Computer Based Training (CBT), which is available with the VTune™ Performance Enhancement Environment CD at http://developer.intel.com/vtune. In addition to eliminating branches, the following guidelines improve branch predictability: • Ensure that each call has a matching return. • Don’t intermingle data and instructions. • Unroll very short loops. • Follow static prediction algorithm. When a misprediction occurs the entire pipeline is flushed up to the branch instruction and the processor waits for the mispredicted branch to retire. Branch Misprediction Ratio = BR_Miss_Pred_Ret / BR_Inst_Ret If the branch misprediction ratio is less than about 5% then branch prediction is within normal range. Otherwise, identify the branches that cause significant mispredictions and try to remedy the situation using the techniques described in the “Eliminating and Reducing the Number of Branches” earlier in this chapter. Partial Register Stalls On Pentium II and Pentium III processors, when a 32-bit register (for example, eax) is read immediately after a 16- or 8-bit register (for example, al, ah, ax) is written, the read is stalled until the write retires, after a minimum of seven clock cycles. Consider Example 2-6. The first instruction moves the value 8 into the ax register. The following instruction accesses the register eax. This code sequence results in a partial register stall as shown in Example 2-6. 2-8 General Optimization Guidelines 2 Example 2-6 Partial Register Stall MOV ax, 8 ADD ecx, eax ; Partial stall occurs on access ; of the EAX register This applies to all of the 8- and 16-bit/32-bit register pairs, listed below: Small Registers Large Registers: al ah ax eax bl bh bx ebx cl ch cx ecx dl dh dx edx sp esp bp ebp di edi si esi Pentium processors do not exhibit this penalty. Because Pentium II and Pentium III processors can execute code out of order, the instructions need not be immediately adjacent for the stall to occur. Example 2-7 also contains a partial stall. Example 2-7 Partial Register Stall with Pentium II and Pentium III Processors MOV al, 8 MOV edx, 0x40 MOV edi, new_value ADD edx, eax ; Partial stall accessing EAX In addition, any µops that follow the stalled µop also wait until the clock cycle after the stalled µop continues through the pipe. In general, to avoid stalls, do not read a large (32-bit) register (eax) after writing a small (8- or 16-bit) register (al or ax) which is contained in the large register. Special cases of reading and writing small and large register pairs are implemented in Pentium II and Pentium III processors in order to simplify the blending of code across processor generations. The special cases are implemented for xor and sub when using eax, ebx, ecx, edx, ebp, esp, 2-9 2 Intel Architecture Optimization Reference Manual edi, and esi as shown in the A. through E. series in. Generally, when implementing this sequence, always zero the large register and then write to the lower half of the register. Example 2-8 Simplifying the Blending of Code in Pentium II and Pentium III Processors A. xor eax, eax movb al, mem8 add eax, mem32 ; no partial stall B. xor eax, eax movw ax, mem16 add eax, mem32 ; no partial stall C. sub ax, ax movb al, mem8 add ax, mem16 ; no partial stall D. sub eax, eax movb al, mem8 or ax, mem16 ; no partial stall E. xor ah, ah movb al, mem8 sub ax, mem16 ; no partial stall Performance Tuning Tip for Partial Stalls Partial stalls can be measured by selecting the Partial Stall Events or Partial Stall Cycles events in the VTune Performance Analyzer and running a sampling on your application. Partial Stall Events show the number of events and Partial Stall Cycles show the number of cycles for partial stalls, respectively. To select the events, in the VTune analyzer, click on Configure menu\Options command\Processor Events for EBS for the list of all processor events, select one of the above events and double click on it. The 2-10 General Optimization Guidelines 2 Events Customization window opens where you can set the Counter Mask for either of those events. For more details, see “Using Sampling Analysis for Optimization” in Chapter 7. If a particular stall occurs more than about 3% of the execution time, then the code associated with this stall should be modified to eliminate the stall. Intel C/C++ Compiler at the default optimization level (switch -O2) ensures that partial stalls do not occur in the generated code. Alignment Rules and Guidelines This section discusses guidelines for alignment of both code and data. On Pentium II and Pentium III processors, a misaligned access that crosses a cache line boundary does incur a penalty. A Data Cache Unit (DCU) split is a memory access that crosses a 32-byte line boundary. Unaligned accesses may cause a DCU split and stall Pentium II and Pentium III processors. For best performance, make sure that in data structures and arrays greater than 32 bytes, the structure or array elements are 32-byte-aligned and that access patterns to data structure and array elements do not break the alignment rules. Code Pentium II and Pentium III processors have a cache line size of 32 bytes. Since the instruction prefetch buffers fetch on 16-byte boundaries, code alignment has a direct impact on prefetch buffer efficiency. For optimal performance across the Intel architecture family, the following is recommended: • Loop entry labels should be 16-byte-aligned when less than eight bytes away from a 16-byte boundary. • Labels that follow a conditional branch need not be aligned. • Labels that follow an unconditional branch or function call should be 16-byte-aligned when less than eight bytes away from a 16-byte boundary. • Use a compiler that will assure these rules are met for the generated code. 2-11 2 Intel Architecture Optimization Reference Manual On Pentium II and Pentium III processors, avoid loops that execute in less than two cycles. The target of the tight loops should be aligned on a 16-byte boundary to maximize the use of instructions that will be fetched. On Pentium II and Pentium III processors, it can limit the number of instructions available for execution, limiting the number of instructions retired every cycle. It is recommended that critical loop entries be located on a cache line boundary. Additionally, loops that execute in less than two cycles should be unrolled. See section “MMX™ Technology” in Chapter 1 for more information about decoding on the Pentium II and Pentium III processors. Data A misaligned data access that causes an access request for data already in the L1 cache can cost six to nine cycles. A misaligned access that causes an access request from L2 cache or from memory, however, incurs a penalty that is processor-dependent. Align the data as follows: • Align 8-bit data at any address. • Align 16-bit data to be contained within an aligned four byte word. • Align 32-bit data so that its base address is a multiple of four. • Align 64-bit data so that its base address is a multiple of eight. • Align 80-bit data so that its base address is a multiple of sixteen. A 32-byte or greater data structure or array should be aligned so that the beginning of each structure or array element is aligned in a way that its base address is a multiple of thirty-two. Data Cache Unit (DCU) Split Figure 2-1 shows the type of code that can cause a cache split. The code loads the addresses of two dword arrays. In this example, every four iterations of the first two dword loads cause a cache split. The data declared at address 029e70feh is not 32-byte-aligned, therefore each load to this address and every load that occurs 32 bytes (every four iterations) from this address will cross the cache line boundary. When the misaligned data crosses a cache line boundary it causes a six- to twelve-cycle stall. . 2-12 General Optimization Guidelines 2 Figure 2-2 DCU Split in the Data Cache Performance Tuning Tip for Misaligned Accesses Misaligned data can be detected by using the Misaligned Accesses event counter on Pentium II and Pentium III processors. Use the VTune analyzer’s dynamic execution functionality to determine the exact location of a misaligned access. Code and data rearrangements for optimal memory usage are discussed in Chapter 6, “Optimizing Cache Utilization for Pentium® III Processors.” 2-13 2 Intel Architecture Optimization Reference Manual Instruction Scheduling Scheduling or pipelining should be done in a way that optimizes performance across all processor generations. The following section presents scheduling rules that can improve the performance of your code on Pentium II and Pentium III processors. Scheduling Rules for Pentium II and Pentium III Processors Pentium II and Pentium III processors have three decoders that translate Intel architecture (IA) macroinstructions into µops as discussed in Chapter 1, “Processor Architecture Overview.” The decoder limitations are as follows: • In each clock cycle, the first decoder is capable of decoding one macroinstruction made up of four or fewer µops. It can handle any number of bytes up to the maximum of 15, but nine-or-more-byte instructions require additional cycles. • In each clock cycle, the other two decoders can each decode an instruction of one µop, and up to eight bytes. Instructions composed of more than four µops take multiple cycles to decode. Appendix C, “Instruction to Decoder Specification,” contains a table of all Intel macroinstructions with the number of µops into which they are decoded. Use this information to determine the decoder on which they can be decoded. The macroinstructions entering the decoder travel through the pipe in order, therefore if a macroinstruction will not fit in the next available decoder, the instruction must wait until the next cycle to be decoded. It is possible to schedule instructions for the decoder so that the instructions in the in-order pipeline are less likely to be stalled. Consider the following code series in Example 2-9. 2-14 General Optimization Guidelines 2 Example 2-9 Scheduling Instructions for the Decoder A. add eax, ecx ; 1 µop instruction (decoder 0) add edx, [ebx] ; 2 µop instruction (stall 1 cycle ; wait till decoder 0 is available) B. add eax, [ebx] ; 2 µop instruction (decoder 0) mov [eax], ecx ; 2 µop instruction (stall 1 cycle ; to wait until decoder 0 is available) C. add eax, [ebx] ; 2 µop instruction (decoder 0) mov ecx, [eax] ; 2 µop instruction (stall 1 cycle ; to wait until decoder 0 is available) add ebx, 8 ; 1 µop instruction (decoder 1) D. pmaddwd mm6, [ebx]; 2 µops instruction (decoder 0) paddd mm7, mm6 ; 1 µop instruction (decoder 1) add ebx, 8 ; 1 µop instruction (decoder 2) The sections of Example 2-9 are explained as follows: A. If the next available decoder for a multi-µop instruction is not decoder 0, the multi-op instruction will wait for decoder 0 to be available; this usu- ally happens in the next clock, leaving the other decoders empty during the current clock. Hence, the following two instructions will take two cycles to decode. B. During the beginning of the decoding cycle, if two consecutive instruc- tions are more than one µop, decoder 0 will decode one instruction and the next instruction will not be decoded until the next cycle. C. Instructions of the op reg, mem type require two µops: the load from memory and the operation µop. Scheduling for the decoder template (4-1-1) can improve the decoding throughput of your application. In general, op reg, mem forms of instructions are used to reduce register pressure in code that is not memory bound, and when the data is in the cache. Use simple instructions for improved speed on Pentium II and Pentium III processors. 2-15 2 Intel Architecture Optimization Reference Manual D. The following rules should be observed while using the op reg, mem instruction with MMX technology: when scheduling, keep in mind the decoder template (4-1-1) on Pentium II and Pentium III processors, as shown in Example 2-10, D. Prefixed Opcodes On the Pentium II and Pentium III processors, avoid the following prefixes: • lock • segment override • address size • operand size On Pentium II and Pentium III processors, instructions longer than seven bytes limit the number of instructions decoded in each cycle. Prefixes add one to two bytes to the length of instruction, possibly limiting the decoder. Whenever possible, avoid prefixing instructions. Schedule them behind instructions that themselves stall the pipe for some other reason. Pentium II and Pentium III processors can only decode one instruction at a time when an instruction is longer than seven bytes. So for best performance, use simple instructions that are less than eight bytes in length. Performance Tuning Tip for Instruction Scheduling Intel C/C++ Compiler generates highly optimized code specifically for the Intel architecture-based processors. For assembly code applications, you can use the assembly coach of the VTune analyzer to get a scheduling advice, see Chapter 7, “Application Performance Tools.” Instruction Selection The following sections explain which instruction sequences to avoid or use when generating optimal assembly code. 2-16 General Optimization Guidelines 2 The Use of lea Instruction In many cases a lea instruction or a sequence of lea, add, sub, and shift instructions can be used to replace constant multiply instructions. Use the integer multiply instruction to optimize code designed for Pentium II and Pentium III processors. The lea instruction can be used sometimes as a three/four operand addition instruction, for example, lea ecx, [eax+ebx+4+a] Using the lea instruction in this way can avoid some unnecessary register usage by not tying up registers for the operands of some arithmetic instructions. On the Pentium II and Pentium III processors, both lea and shift instructions are single µop instructions that execute in one cycle. However, that short latency may not persist in future implementations. The Intel C/C++ Compiler checks to ensure that these instructions are used correctly whenever possible. For the best blended code, replace the shift instruction with two or more add instructions, since the short latency of this instruction may not be maintained across all implementations. Complex Instructions Avoid using complex instructions (for example, enter, leave, or loop) that generally have more than four µops and require multiple cycles to decode. Use sequences of simple instructions instead. Short Opcodes Use one-byte instructions as much as possible. This reduces code size and increases instruction density in the instruction cache. For example, use the push and pop instructions instead of mov instructions to save registers to the stack. 2-17 2 Intel Architecture Optimization Reference Manual 8/16-bit Operands With eight-bit operands, try to use the byte opcodes, rather than using 32-bit operations on sign and zero-extended bytes. Prefixes for operand size override apply to 16-bit operands, not to eight-bit operands. Sign extension is usually quite expensive. Often, the semantics can be maintained by zero-extending 16-bit operands. For example, the C code in the following statements does not need sign extension, nor does it need prefixes for operand size overrides: static short int a, b; if (a==b) { . . . } Code for comparing these 16-bit operands might be: xor eax, eax xor ebx, ebx movw ax, [a] movw bx, [b] cmp eax, ebx Of course, this can only be done under certain circumstances, but the circumstances tend to be quite common. This would not work if the compare was for greater than, less than, greater than or equal, and so on, or if the values in eax or ebx were to be used in another operation where sign extension was required. movsw eax, a ; 1 prefix + 3 movsw ebx, b ; 5 cmp ebx, eax ; 9 Pentium II and Pentium III processors provide special support to XOR a register with itself, recognizing that clearing a register does not depend on the old value of the register. Additionally, special support is provided for the above specific code sequence to avoid the partial stall. See “Partial Register Stalls” section for more information. The performance of the movzx instructions has been improved in order to reduce the prevalence of partial stalls on Pentium II and Pentium III processors. Use the movzx instructions when coding for these processors. 2-18 General Optimization Guidelines 2 Comparing Register Values Use test when comparing a value in a register with zero. Test essentially ands the operands together without writing to a destination register. Test is preferred over and because and writes the result register, which may subsequently cause an artificial output dependence on the processor. Test is better than cmp .., 0 because the instruction size is smaller. Use test when comparing the result of a logical and with an immediate constant for equality or inequality if the register is eax for cases such as: if (avar & 8) { } Address Calculations Pull address calculations into load and store instructions. Internally, memory reference instructions can have four operands: • relocatable load-time constant • immediate constant • base register • scaled index register. In the segmented model, a segment register may constitute an additional operand in the linear address calculation. In many cases, several integer instructions can be eliminated by fully using the operands of memory references. Clearing a Register The preferred sequence to move zero to a register is: xor reg, reg This saves code space but sets the condition codes. In contexts where the condition codes must be preserved, move 0 into the register: mov reg, 0 2-19 2 Intel Architecture Optimization Reference Manual Integer Divide Typically, an integer divide is preceded by a cdq instruction. Divide instructions use EDX:EAX as the dividend and cdq sets up EDX. It is better to copy EAX into EDX, then right-shift EDX 31 places to sign-extend. If you know that the value is positive, use sequence xor edx, edx On Pentium II and Pentium III processors, the cdq instruction is faster since cdq is a single µop instruction as opposed to two instructions for the copy/shift sequence. Comparing with Immediate Zero Often when a value is compared with zero, the operation produces the value sets condition codes, which can be tested directly by a jcc instruction. The most notable exceptions are mov and lea. In these cases, use test. Prolog Sequences In routines that do not call other routines (leaf routines), use ESP as the base register to free up EBP. If you are not using the 32-bit flat model, remember that EBP cannot be used as a general purpose base register because it references the stack segment. Epilog Sequences If only four bytes were allocated in the stack frame for the current function, use pop instructions instead of incrementing the stack pointer by four. Improving the Performance of Floating-point Applications When programming floating-point applications, it is best to start at the C, C++, or FORTRAN language level. Many compilers perform floating-point scheduling and optimization when it is possible. However in order to produce optimal code, the compiler may need some assistance. 2-20 General Optimization Guidelines 2 Guidelines for Optimizing Floating-point Code Follow these rules to improve the speed of your floating-point applications: • Understand how the compiler handles floating-point code. • Look at the assembly dump and see what transforms are already performed on the program. • Study the loop nests in the application that dominate the execution time. • Determine why the compiler is not creating the fastest code. • See if there is a dependence that can be resolved. • Consider large memory bandwidth requirements. • Think of poor cache locality improvement. • See if there is a lot of long-latency floating-point arithmetic operations. • Do not use high precision unless necessary. Single precision (32-bits) is faster on some operations and consumes only half the memory space as double precision (64-bits) or double extended (80-bits). • Make sure you have fast float-to-int routines. Many libraries do more work than is necessary; make sure your float-to-int is a fast routine. • Make sure your application stays in range. Out of range numbers cause very high overhead. • FXCH can be helpful by increasing the effective name space. This in turn allows instructions to be reordered to make instructions available to be executed in parallel. Out of order execution precludes the need for using FXCH to move instructions for very short distances. • Unroll loops and pipeline your code. • Perform transformations to improve memory access patterns. Use loop fusion or compression to keep as much of the computation in the cache as possible. • Break dependency chains. Improving Parallelism The Pentium II and Pentium III processors have a pipelined floating-point unit. To achieve maximum throughput from the Pentium II and Pentium III processors floating-point unit, schedule properly the floating-point instructions to improve pipelining. Consider the example in Figure 2-2. 2-21 2 Intel Architecture Optimization Reference Manual To exploit the parallel capability of the Pentium II and Pentium III processors, determine which instructions can be executed in parallel. The two high-level code statements in the example are independent, therefore their assembly instructions can be scheduled to execute in parallel, thereby improving the execution speed, see source code in Example 2-10. Example 2-10 Scheduling Floating-Point Instructions A = B + C + D; E = F + G + H; fld B fld F fadd C fadd G fadd D fadd H fstp A fstp E Most floating-point operations require that one operand and the result use the top of stack. This makes each instruction dependent on the previous instruction and inhibits overlapping the instructions. One obvious way to get around this is to imagine that we have a flat floating-point register file available, rather than a stack. The code is shown in Example 2-11. Example 2-11 Coding for a Floating-Point Register File fld B ?F1 fadd F1, C ?F1 fld F ?F2 fadd F2,G ?F2 fadd F1,D ?F1 fadd F2,H ?F2 fstp F1 ?A fstp F2 ?E 2-22 General Optimization Guidelines 2 In order to implement these imaginary registers we need to use the FXCH instruction to change the value on the top of stack. This provides a way to avoid the top of stack dependency. The FXCH instruction uses no extra execution cycles on Pentium II and Pentium III processors. Example 2-12 shows its use. Example 2-12 Using the FXCH Instruction STO ST1 fld B ⇒F1 fld B B fadd C ⇒F1 fadd C B+C fld F ⇒F2 fld F B+C fadd G ⇒F2 fadd G F+G B+C fxch ST(1) B+C F+G fadd D ⇒F1 fadd D B+C+D F+G fxch ST(1) F+G B+C+D fadd H ⇒F2 fadd H F+G+H B+C+D fxch ST(1) B+C+D F+G+H fstp D ⇒A fstp A F+G+H fstp E ⇒E fstp E The FXCH instructions move an operand into position for the next floating-point instruction. Rules and Regulations of the fxch Instruction The fxch instruction costs no extra cycles on Pentium II and Pentium III processors. The instruction is almost “free” and can be used to access elements in the deeper levels of the floating-point stack instead of storing them and then loading them again. 2-23 2 Intel Architecture Optimization Reference Manual Memory Operands Floating-point operands that are 64-bit operands need to be eight-byte-aligned. Performing a floating-point operation on a memory operand instead of on a stack register on Pentium II or Pentium III processor, produces two µops, which can limit decoding. Additionally, memory operands may cause a data cache miss, causing a penalty. Memory Access Stall Information Floating-point registers allow loading of 64-bit values as doubles. Instead of loading single array values that are 8-, 16-, or 32-bits long, consider loading the values in a single quadword, then incrementing the structure or array pointer accordingly. First, the loading and storing of quadword data is more efficient using the larger quadword data block sizes. Second, this helps to avoid the mixing of 8-, 16-, or 32-bit load and store operations with a 64-bit load and store operation to the memory address. This avoids the possibility of a memory access stall on Pentium II and Pentium III processors. Memory access stalls occur when • small loads follow large stores to the same area of memory • large loads follow small stores to the same area of memory. Consider the following cases in Example 2-13. In the first case (A), there is a large load after a series of small stores to the same area of memory (beginning at memory address mem), and the large load will stall. The fld must wait for the stores to write to memory before it can access all the data it requires. This stall can also occur with other data types (for example, when bytes or words are stored and then words or doublewords are read from the same area of memory). 2-24 General Optimization Guidelines 2 Example 2-13 Large and Small Load Stalls ;A. Large load stall mov mem, eax ; store dword to address “mem" mov mem + 4, ebx; store dword to address “mem + 4" : : fld mem ; load qword at address “mem", stalls ;B. Small Load stall fstp mem ;store qword to address “mem" : : mov bx,mem+2 ;load word at address “mem + 2", stalls mov cx,mem+4 ;load word at address “mem + 4", stalls In the second case (B), there is a series of small loads after a large store to the same area of memory (beginning at memory address mem), and the small loads will stall. The word loads must wait for the quadword store to write to memory before they can access the data they require. This stall can also occur with other data types (for example, when doublewords or words are stored and then words or bytes are read from the same area of memory). This can be avoided by moving the store as far from the loads as possible. In general, the loads and stores should be separated by at least 10 instructions to avoid the stall condition. Floating-point to Integer Conversion Many libraries provide the float to integer library routines that convert floating-point values to integer. Many of these libraries conform to ANSI C coding standards which state that the rounding mode should be truncation. The default of the fist instruction is round to nearest, therefore many compiler writers implement a change in the rounding mode in the processor in order to conform to the C and FORTRAN standards. This implementation requires changing the control word on the processor using 2-25 2 Intel Architecture Optimization Reference Manual the fldcw instruction. This instruction is a synchronizing instruction and will cause a significant slowdown in the performance of your application on all IA-based processors. When implementing an application, consider if the rounding mode is important to the results. If not, use the algorithm in Example to avoid the synchronization and overhead of the fldcw instruction and changing the rounding mode. Example 2-14 Algorithm to Avoid Changing the Rounding Mode _fto132proc lea ecx,[esp-8] sub esp,16 ; allocate frame and ecx,-8 ; align pointer on boundary of 8 fld st(0) ; duplicate FPU stack top fistp qword ptr[ecx] fild qword ptr[ecx] mov edx,[ecx+4]; high dword of integer mov eax,[ecx] ; low dword of integer test eax,eax je integer_QnaN_or_zero continued 2-26 General Optimization Guidelines 2 Example 2-14 Algorithm to Avoid Changing the Rounding Mode (continued) arg is not integer QnaN: fsubp st(1),st ; TOS=d-round(d), ; { st(1)=st(1)-st & pop ST) test edx,edx ; what’s sign of integer jns positive ; number is negative ; dead cycle ; dead cycle fstp dword ptr[ecx]; result of subtraction mov ecx,[ecx] ; dword of diff(single- ; precision) add esp,16 xor ecx,80000000h add ecx,7fffffffh ; if diff<0 then decrement ; integer adc eax,0 ; inc eax (add CARRY flag) ret positive: fstp dword ptr[ecx]; 17-18 result of subtraction mov ecx,[ecx] ; dword of diff(single- ; precision) add esp,16 add ecx,7fffffffh ; if diff<0 then decrement ; integer sbb eax,0 ; dec eax (subtract CARRY flag) ret integer_QnaN_or_zero: test edx,7fffffffh jnz arg_is_not_integer_QnaN add esp,16 ret 2-27 2 Intel Architecture Optimization Reference Manual Loop Unrolling The benefits of unrolling loops are: • Unrolling amortizes the branch overhead. The BTB is good at predicting loops on Pentium II and Pentium III processors and the instructions to increment the loop index and jump are inexpensive. • Unrolling allows you to aggressively schedule (or pipeline) the loop to hide latencies. This is useful if you have enough free registers to keep variables live as you stretch out the dependency chain to expose the critical path. • You can aggressively schedule the loop to better set up I-fetch and decode constraints. • The backwards branch (predicted as taken) has only a 1 clock penalty on Pentium II and Pentium III processors, so you can unroll very tiny loop bodies for free. You can use a -Qunroll option of the Intel C/C++ Compiler, see Intel C/C++ Compiler User’s Guide for Win32* Systems, order number 718195. Unrolling can expose other optimizations, as shown in Example 2-15. This example illustrates a loop executes 100 times assigning x to every even-numbered element and y to every odd-numbered element. Example 2-15 Loop Unrolling Before unrolling: do i=1,100 if (i mod 2 == 0) then a(i) = x else a(i) = y enddo After unrolling do i=1,100,2 a(i) = y a(i+1) = x enddo By unrolling the loop you can make both assignments each iteration, removing one branch in the loop body. 2-28 General Optimization Guidelines 2 Floating-Point Stalls Many of the floating-point instructions have a latency greater than one cycle but, because of the out-of-order nature of Pentium II and Pentium III processors, stalls will not necessarily occur on an instruction or µop basis. However, if an instruction has a very long latency such as an fdiv, then scheduling can improve the throughput of the overall application. The following sections discuss scheduling issues and offer good tips for any IA-based processor. Hiding the One-Clock Latency of a Floating-Point Store A floating-point store must wait an extra cycle for its floating-point operand. After an fld, an fst must wait one clock. After the common arithmetic operations, fmul and fadd, which normally have a latency of three, fst waits an extra cycle for a total of four. This set also includes other instructions, for example, faddp and fsubrp, see Example 2-16. Example 2-16 Hiding One-Clock Latency ; Store is dependent on the previous load. fld meml ; 1 fld takes 1 clock ; 2 fst waits, schedule something here fst mem2 ; 3,4 fst takes 2 clocks fadd meml ; 1 add takes 3 clocks ; 2 add, schedule something here ; 3 add, schedule something here ; 4 fst waits, schedule something here fst mem2 ; 5,2 fst takes 2 clocks ; Store is not dependent on the previous load: fld meml ; 1 fld mem2 ; 2 fxch st(l) ; 2 fst mem3 ; 3 stores values loaded from meml ; A register may be used immediately after it has ; been loaded (with FLD): fld mem1 ; l fadd mem2 ; 2,3,4 2-29 2 Intel Architecture Optimization Reference Manual Use of a register by a floating-point operation immediately after it has been written by another fadd, fsub, or fmul causes a two-cycle delay. If instructions are inserted between these two, then latency and a potential stall can be hidden. Additionally, while the multi-cycle floating-point instructions, fdiv and fsqrt, execute in the floating-point unit pipe, integer instructions can be executed in parallel. Emitting a number of integer instructions after such an instruction as fdiv or fsqrt will keep the integer execution units busy. The exact number of instructions depends on the floating-point instruction’s cycle count. Integer and Floating-point Multiply The integer multiply operations, mul and imul, are executed in the floating-point unit so these instructions cannot be executed in parallel with a floating-point instruction. A floating-point multiply instruction (fmul) delays for one cycle if the immediately preceding cycle executed an fmul or an fmul / fxch pair. The multiplier can only accept a new pair of operands every other cycle. For the best blended code, replace the integer multiply instruction with two or more add instructions, since the short latency of this instruction may not be maintained across all implementations Floating-point Operations with Integer Operands Floating-point operations that take integer operands (fiadd or fisub ..) should be avoided. These instructions should be split into two instructions: fild and a floating-point operation. The number of cycles before another instruction can be issued (throughput) for fiadd is four, while for fild and simple floating-point operations it is one, as shown in the comparison below. Complex Instructions Use These for Potential Overlap fiadd [ebp] ; 4 fild [ebp] ; 1 faddp st(l) ; 2 Using the fild - faddp instructions yields two free cycles for executing other instructions. 2-30 General Optimization Guidelines 2 FSTSW Instructions The fstsw instruction that usually appears after a floating-point comparison instruction (fcom, fcomp, fcompp) delays for three cycles. Other instructions may be inserted after the comparison instruction in order to hide the latency. On Pentium II and Pentium III processors the fcmov instruction can be used instead. Transcendental Functions If an application needs to emulate these math functions in software, it may be worthwhile to inline some of these math library calls because the call and the prologue/epilogue involved with the calls can significantly affect the latency of the operations. Emulating these operations in software will not be faster than the hardware unless accuracy is sacrificed. 2-31 Coding for SIMD Architectures 3 The capabilities of the Pentium® II and Pentium III processors enable the development of advanced multimedia applications. The Streaming SIMD Extensions and MMX™ technology provide coding extensions to make use of the processors’ multimedia features, specifically, the single-instruction, multiple-data (SIMD) characteristics of the instruction set architecture (ISA). To take advantage of the performance opportunities presented by these new capabilities, take into consideration the following: • Ensure that your processor supports MMX technology and Streaming SIMD Extensions. • Employ all of the optimization and scheduling strategies described in this book. • Use stack and data alignment techniques to keep data properly aligned for efficient memory use. • Utilize the cacheability instructions offered by Streaming SIMD Extensions. This chapter gives an overview of the capabilities that allow you to better understand SIMD features and develop applications utilizing SIMD features of MMX technology and Streaming SIMD Extensions. 3-1 3 Intel Architecture Optimization Reference Manual Checking for Processor Support of Streaming SIMD Extensions and MMX™ Technology This section shows how to check whether a system supports MMX™ technology and Streaming SIMD Extensions. Checking for MMX Technology Support Before you start coding with MMX technology check if MMX technology is available on your system. Use the cpuid instruction to check the feature flags in the edx register. If cpuid returns bit 23 set to 1 in the feature flags, the processor supports MMX technology. Use the code segment in Example 3-1 to load the feature flags in edx and test the result for the existence of MMX technology. Example 3-1 Identification of MMX Technology with cpuid …identify existence of cpuid instruction … ; … ; identify Intel processor … ; mov eax, 1 ; request for feature flags cpuid ; 0Fh, 0A2h cpuid instruction test edx, 00800000h; is MMX technology bit (bit ; 23)in feature flags equal to 1 jnz Found For more information on cpuid see, Intel Processor Identification with CPUID Instruction, order number 241618. Once this check has been made, MMX technology can be included in your application in two ways: 1. Check for MMX technology during installation. If MMX technology is available, the appropriate DLLs can be installed. 2. Check for MMX technology during program execution and install the proper DLLs at runtime. This is effective for programs that may be executed on different machines. 3-2 Coding for SIMD Architectures 3 Checking for Streaming SIMD Extensions Support Checking for support of Streaming SIMD Extensions on your processor is similar to doing the same for MMX technology, but you must also check whether your operating system (OS) supports Streaming SIMD Extensions. This is because the OS needs to manage saving and restoring the new state introduced by Streaming SIMD Extensions for your application to properly function. To check whether your system supports Streaming SIMD Extensions, follow these steps: 1. Check that your processor has the cpuid instruction and is in the Intel Pentium II and Pentium III processors. 2. Check the feature bits of cpuid for Streaming SIMD Extensions existence. 3. Check for OS support for Streaming SIMD Extensions. Example 3-2 shows how to find the Streaming SIMD Extensions feature bit (bit 25) in the cpuid feature flags. Example 3-2 Identification of Streaming SIMD Extensions with cpuid …identify existence of cpuid instruction … ; identify Intel Processor mov eax, 1 ; request for feature flags cpuid ; 0Fh, 0A2h cpuid instruction test EDX, 002000000h; bit 25 in feature flags equal to 1 jnz Found To find out whether the operating system supports Streaming SIMD Extensions, simply execute a Streaming SIMD Extension and trap for the exception if one occurs. An invalid opcode will be raised by the operating system and processor if either is not enabled for Streaming SIMD Extensions. Catching the exception in a simple try/except clause (using structured exception handling in C++) and checking whether the exception code is an invalid opcode will give you the answer. See Example 3-3. 3-3 3 Intel Architecture Optimization Reference Manual Example 3-3 Identification of Streaming SIMD Extensions by the OS bool OSSupportCheck() { _try { __asm xorps xmm0, xmm0 ;Streaming SIMD Extension } _except(EXCEPTION_EXECUTE_HANDLER) { if (_exception_code()==STATUS_ILLEGAL_INSTRUCTION) return (false); Streaming SIMD Extensions not supported } ; Streaming SIMD Extensions are supported by OS return (true); } Considerations for Code Conversion to SIMD Programming The VTune™ Performance Enhancement Environment CD provides tools to aid in the evaluation and tuning. But before you start implementing them, you need to know the answers to the following questions: 1. Will the current code benefit by using MMX technology or Streaming SIMD Extensions? 2. Is this code integer or floating-point? 3. What coding techniques should I use? 4. What guidelines do I need to follow? 5. How should I arrange and align the datatypes? Figure 3-1 provides a flowchart for the process of converting code to MMX technology or the Streaming SIMD Extensions. 3-4 Coding for SIMD Architectures 3 Figure 3-1 Converting to Streaming SIMD Extensions Chart 1a. Identify hotspots in the code. 1b. Determine if code benefits by using SIMD technology. 2b. FP data 2a. Integer data Why FP data? FP or Integer? Performance 3. Convert the code to use SIMD- 2c. FP or SIMD-Integer Conversion to Yes Integer without data Range or Precision loss? 4. Follow the SIMD-Integer or SIMD-FP coding techniques. No 2d. 5. Use data alignment rules. Convert to Yes SIMD-FP? 6. Use memory optimizations No Stop 7. Use aggressive instruction scheduling To use MMX technology or Streaming SIMD Extensions optimally, you must evaluate the following segments of your code: • segments that are computationally intensive • segments that require integer implementations that support efficient use of the cache architecture • segments that require floating-point computations. 3-5 3 Intel Architecture Optimization Reference Manual Identifying Hotspots To optimize performance, you can use the VTune Performance Analyzer to isolate the computation-intensive sections of code. For details on the VTune analyzer, see “VTune™ Performance Analyzer” in Chapter 7. VTune analyzer provides a hotspots view of a specific module to help you identify sections in your code that take the most CPU time and that have potential performance problems. For more explanation, see section “Using Sampling Analysis for Optimization” in Chapter 7, which includes an example of a hotspots report. The hotspots view helps you identify sections in your code that take the most CPU time and that have potential performance problems. The VTune analyzer enables you to change the view to show hotspots by memory location, functions, classes, or source files. You can double-click on a hotspot and open the source or assembly view for the hotspot and see more detailed information about the performance of each instruction in the hotspot. The VTune analyzer offers focused analysis and performance data at all levels of your source code and can also provide advice at the assembly language level. The code coach analyzes and identifies opportunities for better performance of C/C++, FORTRAN and Java* programs, and suggests specific optimizations. Where appropriate, the coach displays pseudo-code to suggest the use of Intel’s highly optimized intrinsics and functions of the MMX technology and Streaming SIMD Extensions from Intel® Performance Library Suite. Because VTune analyzer is designed specifically for all of the Intel architecture (IA)-based processors, Pentium II and Pentium III processors in particular, it can offer these detailed approaches to working with IA. See “Code Coach Optimizations” in Chapter 7, for more details and example of a code coach advice. 3-6 Coding for SIMD Architectures 3 Determine If Code Benefits by Conversion to Streaming SIMD Extensions Identifying code that benefits by using MMX technology and/or Streaming SIMD Extensions can be time-consuming and difficult. Likely candidates for conversion are applications that are highly computation- intensive such as the following: • speech compression algorithms and filters • video display routines • rendering routines • 3D graphics (geometry) • image and video processing algorithms • spatial (3D) audio Generally, these characteristics can be identified by the use of small-sized repetitive loops that operate on integers of 8 or 16 bits for MMX technology, or single-precision, 32-bit floating-point data for Streaming SIMD Extensions technology (integer and floating-point data items should be sequential in memory). The repetitiveness of these loops incurs costly application processing time. However, these routines have potential for increased performance when you convert them to use MMX technology or Streaming SIMD Extensions. Once you identify your opportunities for using MMX technology or Streaming SIMD Extensions, you must evaluate what should be done to determine whether the current algorithm or a modified one will ensure the best performance. Coding Techniques The SIMD features of Streaming SIMD Extensions and MMX technology require new methods of coding algorithms. One of them is vectorization. Vectorization is the process of transforming sequentially executing, or scalar, code into code that can execute in parallel, taking advantage of the SIMD architecture parallelism. Using this feature is critical for Streaming SIMD Extensions and MMX technology. This section discusses the coding techniques available for an application to make use of the SIMD architecture. 3-7 3 Intel Architecture Optimization Reference Manual To vectorize your code and thus take advantage of the SIMD architecture, do the following: • Determine if the memory accesses have dependencies that would prevent parallel execution • “Strip-mine” the loop to reduce the iteration count by the length of the SIMD operations (four for Streaming SIMD Extensions and MMX technology) • Recode the loop with the SIMD instructions Each of these actions is discussed in detail in the subsequent sections of this chapter. Coding Methodologies Software developers need to compare the performance improvement that can be obtained from assembly code versus the cost of those improvements. Programming directly in assembly language for a target platform may produce the required performance gain, however, assembly code is not portable between processor architectures and is expensive to write and maintain. Performance objectives can be met by taking advantage of the Streaming SIMD Extensions or MMX technology ISA using high-level languages as well as assembly. The new C/C++ language extensions designed specifically for the Streaming SIMD Extensions and MMX technology help make this possible. Figure 3-2 illustrates the tradeoffs involved in the performance of hand- coded assembly versus the ease of programming and portability. 3-8 Coding for SIMD Architectures 3 Figure 3-2 Hand-Coded Assembly and High-Level Compiler Performance Tradeoffs Assembly Intrinsics Performance C++ classes Automatic vectorization Portability, ease of use The examples that follow illustrate the use of assembly coding adjustments for this new ISA to benefit from the Streaming SIMD Extensions and C/C++ language extensions. Floating-point data may be used with the Streaming SIMD Extensions as well as the intrinsics and vector classes with MMX technology. As a basis for the usage model discussed in this section, consider a simple loop shown in Example 3-4. Example 3-4 Simple Four-Iteration Loop void add(float *a, float *b, float *c) { int i; for (i = 0; i < 4; i++) { c[i] = a[i] + b[i]; } } _____________________________________________________________ 3-9 3 Intel Architecture Optimization Reference Manual Note that the loop runs for only four iterations. This allows a simple replacement of the code with Streaming SIMD Extensions. For the optimal use of the Streaming SIMD Extensions that need data alignment on the 16-byte boundary, this example assumes that the arrays passed to the routine, a, b, c, are aligned to 16-byte boundaries by a calling routine. See Intel application note AP-833, Data Alignment and Programming Considerations for Streaming SIMD Extensions with the Intel C/C++ Compiler, order number 243872, for the methods to ensure this alignment. The sections that follow detail on the following coding methodologies: inlined assembly, intrinsics, C++ vector classes, and automatic vectorization. Assembly Key loops can be coded directly in assembly language using an assembler or by using inlined assembly (C-asm) in C/C++ code. The Intel compiler or assembler recognizes the new instructions and registers, then directly generates the corresponding code. This model offers the greatest performance, but this performance is not portable across the different processor architectures. Example 3-5 shows the Streaming SIMD Extensions inlined-asm encoding. Example 3-5 Streaming SIMD Extensions Using Inlined Assembly Encoding void add(float *a, float *b, float *c) { __asm { mov eax, a mov edx, b mov ecx, c movaps xmm0, XMMWORD PTR [eax] addps xmm0, XMMWORD PTR [edx] movaps XMMWORD PTR [ecx], xmm0 } } _____________________________________________________________ 3-10 Coding for SIMD Architectures 3 Intrinsics Intrinsics provide the access to the ISA functionality using C/C++ style coding instead of assembly language. Intel has defined two sets of intrinsic functions that are implemented in the Intel C/C++ Compiler to support the MMX technology and the Streaming SIMD Extensions. Two new C data types, representing 64-bit and 128-bit objects (__m64 and __m128, respectively) are used as the operands of these intrinsic functions. This enables to choose the implementation of an algorithm directly, while also performing optimal register allocation and instruction scheduling where possible. These intrinsics are portable among all Intel architecture-based processors supported by a compiler. The use of intrinsics allows you to obtain performance close to the levels achievable with assembly. The cost of writing and maintaining programs with intrinsics is considerably less. For a detailed description of the intrinsics and their use, refer to the Intel C/C++ Compiler User’s Guide. Example 3-6 shows the loop from Example 3-4 using intrinsics. Example 3-6 Simple Four-Iteration Loop Coded with Intrinsics #include void add(float *a, float *b, float *c) { __m128 t0, t1; t0 = _mm_load_ps(a); t1 = _mm_load_ps(b); t0 = _mm_add_ps(t0, t1); _mm_store_ps(c, t0); } _____________________________________________________________ The intrinsics map one-to-one with actual Streaming SIMD Extensions assembly code. The xmmintrin.h header file in which the prototypes for the intrinsics are defined is part of the Intel C/C++ Compiler for Win32* Systems included with the VTune Performance Enhancement Environment CD. 3-11 3 Intel Architecture Optimization Reference Manual Intrinsics are also defined for the MMX technology ISA. These are based on the __m64 data type to represent the contents of an mm register. You can specify values in bytes, short integers, 32-bit values, or as a 64-bit object. The __m64 and __m128 data types, however, are not a basic ANSI C data type, and therefore you must observe the following usage restrictions: • Use __m64 and __m128 data only on the left-hand side of an assignment as a return value or as a parameter. You cannot use it with other arithmetic expressions (“+”, “>>”, and so on). • Use __m64 and __m128 objects in aggregates, such as unions to access the byte elements and structures; the address of an __m64 object may be also used. • Use __m64 and __m128 data only with the MMX intrinsics described in this guide. For complete details of the hardware instructions, see the Intel Architecture MMX™ Technology Programmer’s Reference Manual. For descriptions of data types, see the Intel Architecture Software Developer's Manual, Volume 2: Instruction Set Reference Manual. Classes Intel has also defined a set of C++ classes to provide both a higher-level abstraction and more flexibility for programming with MMX technology and the Streaming SIMD Extensions. These classes provide an easy-to-use and flexible interface to the intrinsic functions, allowing developers to write more natural C++ code without worrying about which intrinsic or assembly language instruction to use for a given operation. Since the intrinsic functions underlie the implementation of these C++ classes, the performance of applications using this methodology can approach that of one using the intrinsics. Further details on the use of these classes can be found in the Intel C++ Class Libraries for SIMD Operations User’s Guide, order number 693500. Example 3-7 shows the C++ code using a vector class library. The example assumes the arrays passed to the routine are already aligned to 16-byte boundaries. 3-12 Coding for SIMD Architectures 3 Example 3-7 C++ Code Using the Vector Classes #include void add(float *a, float *b, float *c) { F32vec4 *av=(F32vec4 *) a; F32vec4 *bv=(F32vec4 *) b; F32vec4 *cv=(F32vec4 *) c; *cv=*av + *bv } _____________________________________________________________ Here, fvec.h is the class definition file and F32vec4 is the class representing an array of four floats. The “+” and “=” operators are overloaded so that the actual Streaming SIMD Extensions implementation in the previous example is abstracted out, or hidden, from the developer. Note how much more this resembles the original code, allowing for simpler and faster programming. Again, the example is assuming the arrays passed to the routine are already aligned to 16-byte boundary. Automatic Vectorization The Intel C/C++ Compiler provides an optimization mechanism by which simple loops, such as in Example 3-4 can be automatically vectorized, or converted into Streaming SIMD Extensions code. The compiler uses similar techniques to those used by a programmer to identify whether a loop is suitable for conversion to SIMD. This involves determining whether the following might prevent vectorization: • the layout of the loop and the data structures used • dependencies amongst the data accesses in each iteration and across iterations Once the compiler has made such a determination, it can generate vectorized code for the loop, allowing the application to use the SIMD instructions. 3-13 3 Intel Architecture Optimization Reference Manual The caveat to this is that only certain types of loops can be automatically vectorized, and in most cases user interaction with the compiler is needed to fully enable this. Example 3-8 shows the code for automatic vectorization for the simple four-iteration loop (from Example 3-4). Example 3-8 Automatic Vectorization for a Simple Loop void add (float *restrict a, float *restrict b, float *restrict c) { int i; for (i = 0; i < 100; i++) { c[i] = a[i] + b[i]; } } _____________________________________________________________ Compile this code using the -Qvec and -Qrestrict switches of the Intel C/C++ Compiler, version 4.0 or later. The restrict qualifier in the argument list is necessary to let the compiler know that there are no other aliases to the memory to which the pointers point. In other words, the pointer for which it is used, provides the only means of accessing the memory in question in the scope in which the pointers live. Without this qualifier, the compiler will not vectorize the loop because it cannot ascertain whether the array references in the loop overlap, and without this information, generating vectorized code is unsafe. Refer to the Intel C/C++ Compiler User’s Guide for Win32 Systems, order number 718195, for more details on the use of automatic vectorization. 3-14 Coding for SIMD Architectures 3 Stack and Data Alignment To get the most performance out of code written for MMX technology and Streaming SIMD Extensions, data should be formatted in memory according to the guidelines described in this section. A misaligned access in assembly code is a lot more costly than an aligned access. Alignment of Data Access Patterns The new 64-bit packed data types defined by MMX technology, and the 128-bit packed data types for Streaming SIMD Extensions create more potential for misaligned data accesses. The data access patterns of many algorithms are inherently misaligned when using MMX technology and Streaming SIMD Extensions. However, when accessing SIMD data using SIMD operations, access to data can be improved simply by a change in the declaration. For example, consider a declaration of a structure, which represents a point in space. The structure consists of three 16-bit values plus one 16-bit value for padding. The sample declaration follows: typedef struct { short x,y,z; short junk; } Point; Point pt[N]; In the following code, for (i=0; i #include #include void main(void) { double a[5] ; double *p, *newp; double i, res; continued _____________________________________________________________ 3-17 3 Intel Architecture Optimization Reference Manual Example 3-9 C Algorithm for 64-bit Data Alignment (continued) p = (double*)malloc (((sizeof a[0])*5)+4); newp = ((unsigned int)(&p)+4) & (~0x7); /* res =0; for(i =0; i<4; i++) { res += a[i]; } printf("res = %ld\n",res); */ } _____________________________________________________________ The algorithm in Example 3-9 aligns a 64-bit variable on a 64-bit boundary. Once aligned, every access to this variable saves six to nine cycles on the Pentium II and Pentium III processors when the misaligned data previously crossed a cache line boundary. Another way to improve data alignment is to copy the data into locations that are aligned on 64-bit boundaries. When the data is accessed frequently, this can provide a significant performance improvement. Data Alignment for Streaming SIMD Extensions Data must be 16-byte-aligned when using the Streaming SIMD Extensions to avoid severe performance penalties at best, and at worst, execution faults. Although there are move instructions (and intrinsics) to allow unaligned data to be copied into and out of Streaming SIMD Extension registers when not using aligned data, such operations are much slower than aligned accesses. If, however, the data is not 16-byte-aligned and the programmer or the compiler does not detect this and uses the aligned instructions, a fault will occur. So, the rule is: keep the data 16-byte-aligned. Such alignment will also work for MMX technology code, even though MMX technology 3-18 Coding for SIMD Architectures 3 only requires 8-byte alignment. The following discussion and examples describe alignment techniques for Pentium III processor as implemented with the Intel C/C++ Compiler. Compiler-Supported Alignment The Intel C/C++ Compiler provides the following methods to ensure that the data is aligned. Alignment by F32vec4 or __m128 Data Types. When compiler detects F32vec4 or __m128 data declarations or parameters, it will force alignment of the object to a 16-byte boundary for both global and local data, as well as parameters. If the declaration is within a function, the compiler will also align the function’s stack frame to ensure that local data and parameters are 16-byte-aligned. Please refer to the Intel application note AP-589, Software Conventions for Streaming SIMD Extensions, order number 243873, for details on the stack frame layout that the compiler generates for both debug and optimized (“release”-mode) compilations. The __declspec(align(16)) specifications can be placed before data declarations to force 16-byte alignment. This is particularly useful for local or global data declarations that are assigned to Streaming SIMD Extensions data types. The syntax for it is __declspec(align(integer-constant)) where the integer-constant is an integral power of two but no greater than 32. For example, the following increases the alignment to 16-bytes: __declspec(align(16)) float buffer[400]; The variable buffer could then be used as if it contained 100 objects of type __m128 or F32vec4. In the code below, the construction of the F32vec4 object, x, will occur with aligned data. void foo() { F32vec4 x = *(__m128 *) buffer; ... } Without the declaration of __declspec(align(16)), a fault may occur. 3-19 3 Intel Architecture Optimization Reference Manual Alignment by Using a union Structure. Preferably, when feasible, a union can be used with Streaming SIMD Extensions data types to allow the compiler to align the data structure by default. Doing so is preferred to forcing alignment with __declspec(align(16)) because it exposes the true program intent to the compiler in that __m128 data is being used. For example: union { float f[400]; __m128 m[100]; } buffer; The 16-byte alignment is used by default due to the __m128 type in the union; it is not necessary to use __declspec(align(16)) to force it. In C++ (but not in C) it is also possible to force the alignment of a class/struct/union type, as in the code that follows: struct __declspec(align(16)) my_m128 { float f[4]; }; But, if the data in such a class is going to be used with the Streaming SIMD Extensions, it is preferable to use a union to make this explicit. In C++, an anonymous union can be used to make this more convenient: class my_m128 { union { __m128 m; float f[4]; }; }; In this example, because the union is anonymous, the names, m and f, can be used as immediate member names of my__m128. Note that __declspec(align) has no effect when applied to a class, struct, or union member in either C or C++. 3-20 Coding for SIMD Architectures 3 Alignment by Using __m64 or double Data. In some cases, for better performance, the compiler will align routines with __m64 or double data to 16-bytes by default. The command-line switch, -Qsfalign16, can be used to limit the compiler to only align in routines that contain Streaming SIMD Extensions data. The default behavior is to use -Qsfalign8, which instructs to align routines with 8- or 16-byte data types to 16-bytes. For more details, see the Intel application note AP-833, Data Alignment and Programming Issues with the Intel C/C++ Compiler, order number 243872, and Intel C/C++ Compiler for Windows32 Systems User’s Guide, order number 718195. Improving Memory Utilization Memory performance can be improved by rearranging data and algorithms for Streaming SIMD Extensions and MMX technology intrinsics. The methods for improving memory performance involve working with the following: • Data structure layout • Strip-mining for vectorization and memory utilization • Loop-blocking Using the cacheability instructions, prefetch and streaming store, also greatly enhance memory utilization. For these instructions, see Chapter 6, “Optimizing Cache Utilization for Pentium® III Processors.” Data Structure Layout For certain algorithms, like 3D transformations and lighting, there are two basic ways of arranging the vertices data. The traditional method is the array of structures (AoS) arrangement, with a structure for each vertex. However this method does not take full advantage of the Streaming SIMD Extensions SIMD capabilities. The best processing method for code using Streaming SIMD Extensions is to arrange the data in an array for each coordinate. This data arrangement is called structure of arrays (SoA). This arrangement allows more efficient use of the parallelism of Streaming SIMD Extensions because the data is ready for transformation. Another advantage of this arrangement is reduced memory traffic, because only the 3-21 3 Intel Architecture Optimization Reference Manual relevant data is loaded into the cache. Data that is not relevant for the transformation (such as: texture coordinates, color, and specular) is not loaded into the cache. There are two options for transforming data in AoS format. One is to perform SIMD operations on the original AoS format. However, this option requires more calculations. In addition, some of the operations do not take advantage of the four SIMD elements in the Streaming SIMD Extensions. Therefore, this option is less efficient. The recommended way for transforming data in AoS format is to temporarily transpose each set of four vertices to SoA format before processing it with Streaming SIMD Extensions. The following is a simplified transposition example: Original format: x1,y1,z1 x2,y2,z2 x3,y3,z3 x4,y4,z4 Transposed format: x1,x2,x3,x4 y1,y2,y3,y4 z1,z2,z3,z4 The data structures for the methods are presented, respectively, in Example 3-10 and Example 3-11. Example 3-10 AoS data structure typedef struct{ float x,y,z; int color; . . . } Vertex; Vertex Vertices[NumOfVertices]; _____________________________________________________________ Example 3-11 SoA data structure typedef struct{ float x[NumOfVertices]; float y[NumOfVertices]; float z[NumOfVertices]; int color[NumOfVertices]; . . . } VerticesList; VerticesList Vertices; _____________________________________________________________ 3-22 Coding for SIMD Architectures 3 The transposition methods also apply to MMX technology. Consider a simple example of adding a 16-bit bias to all the 16-bit elements of a vector. In regular scalar code, you would load the bias into a register at the beginning of the loop, access the vector elements in another register, and do the addition of one element at a time. Converting this routine to MMX technology code, you would expect a four times speedup since MMX instructions can process four elements of the vector at a time using the movq instruction, and can perform four additions at a time using the paddw instruction. However, to achieve the expected speedup, you would need four contiguous copies of the bias in an MMX technology register when adding. In the original scalar code, only one copy of the bias is in memory. To use MMX instructions, you could use various manipulations to get four copies of the bias in an MMX technology register. Or you could format your memory in advance to hold four contiguous copies of the bias. Then, you need only load these copies using one MOVQ instruction before the loop, and the four times speedup is achieved. Additionally, when accessing SIMD data with SIMD operations, access to data can be improved simply by a change in the declaration. For example, consider a declaration of a structure that represents a point in space. The structure consists of three 16-bit values plus one 16-bit value for padding: typedef struct { short x,y,z; short junk; } Point; Point pt[N]; In the following code the second dimension y needs to be multiplied by a scaling value. Here the for loop accesses each y dimension in the pt array: for (i=0; isource2 (A>B) movq MM4, MM2 ; make another copy of A pxor MM2, MM1 ; create the intermediate value of ; the swap operation - xor(A,B) pand MM2, MM0 ; create a mask of 0s and xor(A,B) ; elements. Where A>B there will ; be a value xor(A,B) and where ; A<=B there will be 0. pxor MM4, MM2 ; minima-xor(A,swap mask) pxor MM1, MM3 ; maxima-xor(B, swap mask) psubw MM1, MM4 ; absolute difference = ; maxima-minima _____________________________________________________________ 4-16 Using SIMD Integer Instructions 4 Absolute Value Use Example 4-10 to compute |x|, where x is signed. This example assumes signed words to be the operands. Example 4-10 Computing Absolute Value ; Input: MM0 signed source operand ; Output: MM1 ABS(MMO) movq MM1, MM0 ; make a copy of x psraw MM0,15 ; replicate sign bit (use 31 if doing ; DWORDS) pxor MM0, MM1 ; take 1’s complement of just the ; negative fields psubs MM1, MM0 ; add 1 to just the negative fields _____________________________________________________________ CAUTION. The absolute value of the most negative number (that is, 8000 hex for 16-bit) does not fit, but this code suggests what is possible to do for this case: it gives 0x7fff which is off by one. Clipping to an Arbitrary Signed Range [high, low] This section explains how to clip a signed value to the signed range [high, low]. Specifically, if the value is less than low or greater than high then clip to low or high, respectively. This technique uses the packed-add and packed-subtract instructions with unsigned saturation, which means that this technique can only be used on packed-byte and packed-word data types. 4-17 4 Intel Architecture Optimization Reference Manual Example 4-11 and Example 4-12 in this section use the constants packed_max and packed_min and show operations on word values. For simplicity we use the following constants (corresponding constants are used in case the operation is done on byte values): • packed_max equals 0x7fff7fff7fff7fff • packed_min equals 0x8000800080008000 • packedD_low contains the value low in all four words of the packed-words data type • packed_high contains the value high in all four words of the packed-words data type • packed_usmax all values equal 1 • high_us adds the high value to all data elements (4 words) of packed_min • low_us adds the low value to all data elements (4 words) of packed_min Example 4-11 Clipping to an Arbitrary Signed Range [high, low] ; Input: MM0 signed source operands ; Output: MM1 signed operands clipped to the unsigned ; range [high, low] padd MM0, packed_min ; add with no saturation ; 0x8000 to convert to unsigned paddusw MM0, (packed_usmax - high_us) ; in effect this clips to high psubusw MM0, (packed_usmax - high_us + low_us) ; in effect this clips to low paddw MM0, packed_low ; undo the previous two offsets _____________________________________________________________ The code above converts values to unsigned numbers first and then clips them to an unsigned range. The last instruction converts the data back to signed data and places the data within the signed range. Conversion to unsigned data is required for correct results when (high - low) < 0x8000. 4-18 Using SIMD Integer Instructions 4 If (high - low) >= 0x8000, the algorithm can be simplified as shown in Example 4-12: Example 4-12 Simplified Clipping to an Arbitrary Signed Range ; Input: MM0 signed source operands ; Output: MM1 signed operands clipped to the unsigned ; range [high, low] paddssw MM0, (packed_max - packed_high) ; in effect this clips to high psubssw MM0, (packed_usmax - packed_high + packed_ow); ; clips to low paddw MM0, low ; undo the previous two offsets _____________________________________________________________ This algorithm saves a cycle when it is known that (high - low) >= 0x8000. The three-instruction algorithm does not work when (high - low) < 0x8000, because 0xffff minus any number < 0x8000 will yield a number greater in magnitude than 0x8000, which is a negative number. When the second instruction, psubssw MM0, (0xffff - high + low), in the three-step algorithm (Example 4-12) is executed, a negative number is subtracted. The result of this subtraction causes the values in MM0 to be increased instead of decreased, as should be the case, and an incorrect answer is generated. Clipping to an Arbitrary Unsigned Range [high, low] The code in Example 4-13 clips an unsigned value to the unsigned range [high, low]. If the value is less than low or greater than high, then clip to low or high, respectively. This technique uses the packed-add and packed-subtract instructions with unsigned saturation, thus this technique can only be used on packed-bytes and packed-words data types. The example illustrates the operation on word values. 4-19 4 Intel Architecture Optimization Reference Manual Example 4-13 Clipping to an Arbitrary Unsigned Range [high, low] ; Input: MM0 unsigned source operands ;Output: MM1 unsigned operands clipped to the unsigned ; range [HIGH, LOW] // paddusw MM0, 0xffff - high ; in effect this clips to high psubusw MM0, (0xffff - high + low) ; in effect this clips to low paddw MM0, low ; undo the previous two offsets _____________________________________________________________ Generating Constants The MMX instruction set does not have an instruction that will load immediate constants to MMX technology registers. The following code segments generate frequently used constants in an MMX technology register. Of course, you can also put constants as local variables in memory, but when doing so be sure to duplicate the values in memory and load the values with a movq instruction, see Example 4-14. Example 4-14 Generating Constants pxor MM0, MM0 ; generate a zero register in MM0 pcmpeq MM1, MM1 ; Generate all 1’s in register MM1, ; which is -1 in each of the packed ; data type fields pxor MM0, MM0 pcmpeq MM1, MM1 psubb MM0, MM1 [psubb MM0, MM1] (psubd MM0, MM1) continued 4-20 Using SIMD Integer Instructions 4 Example 4-14 Generating Constants (continued) ; three instructions above generate ; the constant 1 in every ; packed-byte [or packed-word] ; (or packed-dword) field pcmpeq MM1, MM1 psrlw MM1, 16-n (psrld MM1, 32-n) ; two instructions above generate ; the signed constant 2n–1 in every ; packed-word (or packed-dword) field pcmpeq MM1, MM1 psllw MM1, n (pslldMM1, n) ; two instructions above generate ; the signed constant -2n in every ; packed-word (or packed-dword) field NOTE. Because the MMX instruction set does not support shift instructions for bytes, 2n–1 and -2n are relevant only for packed words and packed dwords. Coding Techniques for Integer Streaming SIMD Extensions This section contains examples of the new SIMD integer instructions. Each example includes a short description, sample code, and notes where necessary. These short examples, which usually are incorporated in longer code sequences, do not address scheduling. 4-21 4 Intel Architecture Optimization Reference Manual Extract Word The pextrw instruction takes the word in the designated MMX technology register selected by the two least significant bits of the immediate value and moves it to the lower half of a 32-bit integer register, see Figure 4-6 and Example 4-15. Figure 4-6 pextrw Instruction 63 MM 0 X4 X3 X2 X1 31 R32 0 0..0 X1 Example 4-15 pextrw Instruction Code ; Input: eax source value immediate value:“0” ; Output: edx 32-bit integer register containing the extracted word in the low-order bits & the high-order bits zero-extended movq mm0, [eax] pextrw edx, mm0, 0 _____________________________________________________________ Insert Word The pinsrw instruction loads a word from the lower half of a 32-bit integer register or from memory and inserts it in the MMX technology destination register at a position defined by the two least significant bits of the immediate constant. Insertion is done in such a way that the three other words from the destination register are left untouched, see Figure 4-7 and Example 4-16. 4-22 Using SIMD Integer Instructions 4 Figure 4-7 pinsrw Instruction 63 MM 0 X4 X3 Y1 X1 31 R32 0 Y2 Y1 Example 4-16 pinsrw Instruction Code ; Input: 32-bit integer register: source value immediate value: “1”. ; Output: MMX technology register with new 16-bit value inserted movq mm0, [edx] pinsrw mm0, eax, 1 _____________________________________________________________ Packed Signed Integer Word Maximum The pmaxsw instruction returns the maximum between the four signed words in either two MMX technology registers, or one MMX technology register and a 64-bit memory location. Packed Unsigned Integer Byte Maximum The pmaxub instruction returns the maximum between the eight unsigned bytes in either two MMX technology registers, or one MMX technology register and a 64-bit memory location. Packed Signed Integer Word Minimum The pminsw instruction returns the minimum between the four signed words in either two MMX technology registers, or one MMX technology register and a 64-bit memory location. 4-23 4 Intel Architecture Optimization Reference Manual Packed Unsigned Integer Byte Minimum The pminub instruction returns the minimum between the eight unsigned bytes in either two MMX technology registers, or one MMX technology register and a 64-bit memory location. Move Byte Mask to Integer The pmovmskb instruction returns an 8-bit mask formed from the most significant bits of each byte of its source operand, see Figure 4-8 and Example 4-17. Figure 4-8 pmovmskb Instruction Example MM 63 55 47 39 31 23 15 7 0 31 0..0 0..0 7 0 R32 Example 4-17 pmovmskb Instruction Code ; Input: source value ; Output: 32-bit register containing the byte mask in the lower eight bits movq mm0, [edi] pmovmskb eax, mm0 _____________________________________________________________ 4-24 Using SIMD Integer Instructions 4 Packed Multiply High Unsigned The pmulhuw instruction multiplies the four unsigned words in the destination operand with the four unsigned words in the source operand. The high-order 16 bits of the 32-bit immediate results are written to the destination operand. Packed Shuffle Word The pshuf instruction (see Figure 4-9, Example 4-18) uses the immediate (imm8) operand to select between the four words in either two MMX technology registers or one MMX technology register and a 64-bit memory location. Bits 1 and 0 of the immediate value encode the source for destination word 0 (MMX[15-0]), and so on as shown in the table: Bits Word 1-0 0 3-2 1 5-4 2 7-6 3 Bits 7 and 6 encode for word 3 (MMX[63-48]). Similarly, the 2-bit encoding represents which source word is used, for example, binary encoding of 10 indicates that source word 2 (MM2/mem[47-32]) is used, see Example 4-18 and Example 4-18. Figure 4-9 pshuf Instruction Example MM/m64 63 0 X4 X3 X2 X1 MM 0 63 X1 X2 X3 X4 4-25 4 Intel Architecture Optimization Reference Manual Example 4-18 pshuf Instruction Code ; Input: edi source value ; Output: MM1 MM register containing the byte mask in the lower eight bits movq mm0, [edi] pshufw mm1, mm0, 0x1b _____________________________________________________________ Packed Sum of Absolute Differences The PSADBW instruction (see Figure 4-10) computes the absolute value of the difference of unsigned bytes for either two MMX technology registers, or one MMX technology register and a 64-bit memory location. These differences are then summed to produce a word result in the lower 16-bit field, and the upper three words are set to zero. Figure 4-10 PSADBW Instruction Example MM/m64 63 0 X8 X7 X6 X5 X4 X3 X2 X1 - - - - - - - - 63 MM 0 Y8 Y7 Y6 Y5 Y4 Y3 Y2 Y1 = = = = = = = = 63 Te m p 0 T8 T7 T6 T5 T4 T3 T2 T1 47 31 15 MM 0 63 0..0 0..0 0..0 T1+T2+T3+T4+T5+T6+t7+T8 4-26 Using SIMD Integer Instructions 4 The subtraction operation presented above is an absolute difference, that is, t = abs(x-y). The byte values are stored in temporary space, all values are summed together, and the result is written into the lower word of the destination register. Packed Average (Byte/Word) The pavgb and pavgw instructions add the unsigned data elements of the source operand to the unsigned data elements of the destination register, along with a carry-in. The results of the addition are then each independently shifted to the right by one bit position. The high order bits of each element are filled with the carry bits of the corresponding sum. The destination operand is an MMX technology register. The source operand can either be an MMX technology register or a 64-bit memory operand. The PAVGB instruction operates on packed unsigned bytes and the PAVGW instruction operates on packed unsigned words. Memory Optimizations You can improve memory accesses using the following techniques: • Partial Memory Accesses • Instruction Selection • Increasing Bandwidth of Memory Fills and Video Fills • Pre-fetching data with Streaming SIMD Extensions (see Chapter 6, “Optimizing Cache Utilization for Pentium® III Processors”). The MMX technology registers allow you to move large quantities of data without stalling the processor. Instead of loading single array values that are 8, 16, or 32 bits long, consider loading the values in a single quadword, then incrementing the structure or array pointer accordingly. Any data that will be manipulated by MMX instructions should be loaded using either: • the MMX instruction that loads a 64-bit operand (for example, movq MM0, m64) 4-27 4 Intel Architecture Optimization Reference Manual • the register-memory form of any MMX instruction that operates on a quadword memory operand (for example, pmaddw MM0, m64) • all SIMD data should be stored using the MMX instruction that stores a 64-bit operand (for example, movq m64, MM0) The goal of these recommendations is twofold. First, the loading and storing of SIMD data is more efficient using the larger quadword block sizes. Second, this helps to avoid the mixing of 8-, 16-, or 32-bit load and store operations with 64-bit MMX technology load and store operations to the same SIMD data. This, in turn, prevents situations in which small loads follow large stores to the same area of memory, or large loads follow small stores to the same area of memory. Pentium II and Pentium III processors stall in these situations. Partial Memory Accesses Let’s consider a case with large load after a series of small stores to the same area of memory (beginning at memory address mem). The large load will stall in this case as shown in Example 4-19. Example 4-19 A Large Load after a Series of Small Stalls mov mem, eax ; store dword to address “mem" mov mem + 4, ebx ; store dword to address “mem + 4" : : movq mm0, mem ; load qword at address “mem", stalls _____________________________________________________________ The movq must wait for the stores to write memory before it can access all the data it requires. This stall can also occur with other data types (for example, when bytes or words are stored and then words or doublewords are read from the same area of memory). When you change the code sequence as shown in Example 4-20, the processor can access the data without delay. 4-28 Using SIMD Integer Instructions 4 Example 4-20 Accessing Data without Delay movd mm1, ebx ; build data into a qword first ; before storing it to memory movd mm2, eax psllq mm1, 32 por mm1, mm2 movq mem, mm1 ; store SIMD variable to “mem" as ; a qword : : movq mm0, mem ; load qword SIMD “mem", no stall _____________________________________________________________ Let us now consider a case with a series of small loads after a large store to the same area of memory (beginning at memory address mem). The small loads will stall in this case as shown in Example 4-21. Example 4-21 A Series of Small Loads after a Large Store movq mem, mm0 ; store qword to address “mem" : : mov bx, mem + 2 ; load word at “mem + 2" stalls mov cx, mem + 4; load word at “mem + 4" stalls _____________________________________________________________ The word loads must wait for the quadword store to write to memory before they can access the data they require. This stall can also occur with other data types (for example, when doublewords or words are stored and then words or bytes are read from the same area of memory). When you change the code sequence as shown in Example 4-22, the processor can access the data without delay. 4-29 4 Intel Architecture Optimization Reference Manual Example 4-22 Eliminating Delay for a Series of Small Loads after a Large Store movq mem, mm0 ; store qword to address “mem" : : movq mm1, mem ; load qword at address “mem" movd eax, mm1 ; transfer “mem + 2" to eax from ; MMX technology register, not ; memory psrlq mm1, 32 shr eax, 16 movd ebx, mm1 ; transfer “mem + 4" to bx from ; MMX technology register, not ; memory and ebx, 0ffffh ____________________________________________________________ These transformations, in general, increase the number of instructions required to perform the desired operation. For Pentium II and Pentium III processors, the performance penalty due to the increased number of instructions is more than offset by the benefit. Instruction Selection to Reduce Memory Access Hits An MMX instruction may have two register operands (OP reg, reg) or one register and one memory operand (OP reg, mem), where OP represents the instruction opcode, reg represents the register, and mem represents memory. OP reg, mem instructions are useful in some cases to reduce register pressure, increase the number of operations per cycle, and reduce code size. The following discussion assumes that the memory operand is present in the data cache. If it is not, then the resulting penalty is usually large enough to obviate the scheduling effects discussed in this section. In Pentium processors, OP reg, mem MMX instructions do not have longer latency than OP reg, reg instructions (assuming a cache hit). They do have more limited pairing opportunities, however. In Pentium II and Pentium III processors, OP reg, mem MMX instructions translate into 4-30 Using SIMD Integer Instructions 4 two µops, as opposed to one µop for the OP reg, reg instructions. Thus, they tend to limit decoding bandwidth and occupy more resources than OP reg, reg instructions. Recommended usage of OP reg, mem instructions depends on whether the MMX technology code is memory bound (that is, execution speed is limited by memory accesses). Generally, an MMX technology code section is considered to be memory-bound if the following inequality is true: Instructions/2 < Memory Accesses + non-MMX Instructions/2 For memory-bound MMX technology code, the recommendation is to merge loads whenever the same memory address is used more than once. This reduces the number of memory accesses. For example, OP MM0, [address A] OP MM1, [address A] becomes movq MM2, [address A] OP MM0, MM2 OP MM1, MM2 For MMX technology code that is not memory-bound, load merging is recommended only if the same memory address is used more than twice. Where load merging is not possible, usage of OP reg, mem instructions is recommended to minimize instruction count and code size. For example, movq MM0, [address A] OP MM1, MM0 becomes OP MM1, [address A] In many cases, a movq reg, reg and OP reg, mem can be replaced by a movq reg, mem and OP reg, reg. This should be done where possible, since it saves one µop on Pentium II and Pentium III processors. 4-31 4 Intel Architecture Optimization Reference Manual The code below, where OP is a commutative operation, movq MM1, MM0 (1 µop) OP MM1, [address A] (2 µops) becomes: movq MM1, [address A] (1 µop) OP MM1, MM0 (1 µop) Increasing Bandwidth of Memory Fills and Video Fills It is beneficial to understand how memory is accessed and filled. A memory-to-memory fill (for example a memory-to-video fill) is defined as a 32-byte (cache line) load from memory which is immediately stored back to memory (such as a video frame buffer). The following are guidelines for obtaining higher bandwidth and shorter latencies for sequential memory fills (video fills). These recommendations are relevant for all Intel® architecture processors with MMX technology and refer to cases in which the loads and stores do not hit in the second level cache. Increasing Memory Bandwidth Using the MOVQ Instruction Loading any value will cause an entire cache line to be loaded into the on-chip cache. But using movq to store the data back to memory instead of using 32-bit stores (for example, movd) will reduce by half the number of stores per memory fill cycle. As a result, the bandwidth of the memory fill cycle increases significantly. On some Pentium processor-based systems, 30% higher bandwidth was measured when 64-bit stores were used instead of 32-bit stores. Additionally, on Pentium II and Pentium III processors, this avoids a partial memory access when both the loads and stores are done with the MOVQ instruction. Also, intermixing reads and writes is slower than doing a series of reads then writing out the data. For example when moving memory, it is faster to read several lines into the cache from memory then write them out again to the new memory location, instead of issuing one read and one write. Increasing Memory Bandwidth by Loading and Storing to and from the Same DRAM Page DRAM is divided into pages, which are not the same as operating system (OS) pages. The size of a DRAM page is a function of the total size of the DRAM and the organization of the DRAM. Page sizes of several Kbytes are 4-32 Using SIMD Integer Instructions 4 common. Like OS pages, DRAM pages are constructed of sequential addresses. Sequential memory accesses to the same DRAM page have shorter latencies than sequential accesses to different DRAM pages. In many systems the latency for a page miss (that is, an access to a different page instead of the page previously accessed) can be twice as large as the latency of a memory page hit (access to the same page as the previous access). Therefore, if the loads and stores of the memory fill cycle are to the same DRAM page, a significant increase in the bandwidth of the memory fill cycles can be achieved. Increasing the Memory Fill Bandwidth by Using Aligned STORES Unaligned stores will double the number of stores to memory. Intel strongly recommends that quadword stores be 8-byte aligned. Four aligned quadword stores are required to write a cache line to memory. If the quadword store is not 8-byte aligned, then two 32-bit writes result from each MOVQ store instruction. On some systems, a 20% lower bandwidth was measured when 64-bit misaligned stores were used instead of aligned stores. Use 64-Bit Stores to Increase the Bandwidth to Video Although the PCI bus between the processor and the frame buffer is 32 bits wide, using movq to store to video is faster on most Pentium processor-based systems than using twice as many 32-bit stores to video. This occurs because the bandwidth to PCI write buffers (which are located between the processor and the PCI bus) is higher when quadword stores are used. Increase the Bandwidth to Video Using Aligned Stores When a nonaligned store is encountered, there is a dramatic decrease in the bandwidth to video. Misalignment causes twice as many stores and the latency of stores on the PCI bus (to the frame buffer) is much longer. On the PCI bus, it is not possible to burst sequential misaligned stores. On Pentium processor-based systems, a decrease of 80% in the video fill bandwidth is typical when misaligned stores are used instead of aligned stores. 4-33 4 Intel Architecture Optimization Reference Manual Scheduling for the SIMD Integer Instructions Scheduling instructions affects performance because the latency of instructions affects other instructions acting on them. Scheduling Rules All MMX instructions can be pipelined, including the multiply instructions on Pentium II and Pentium III processors. All instructions take a single clock to execute except MMX technology multiply instructions which take three clocks. Since multiply instructions take three clocks to execute, the result of a multiply instruction can be used only by other instructions issued three clocks later. For this reason, avoid scheduling a dependent instruction in the two-instruction sequences following the multiply. The store of a register after writing the register must wait for two clock cycles after the update of the register. Scheduling the store of at least two clock cycles after the update avoids a pipeline stall. 4-34 Optimizing Floating-point Applications 5 This chapter discusses general rules for optimizing single-instruction, multiple-data (SIMD) floating-point code and provides examples that illustrate the optimization techniques for SIMD floating-point applications. Rules and Suggestions The rules and suggestions listed in this section help optimize floating-point code containing SIMD floating-point instructions. Generally, it is important to understand and balance port utilization to create efficient SIMD floating-point code. The basic rules and suggestions include the following: • Balance the limitations of the architecture. • Schedule instructions to resolve dependencies. • Schedule usage of the triple/quadruple rule (port 0, port 1, port 2, 3, and 4). • Group instructions that use the same registers as closely as possible. Take into consideration the resolution of true dependencies. • Intermix SIMD floating-point operations that use port 0 and port 1. • Do not issue consecutive instructions that use the same port. • Exceptions: mask exceptions to achieve higher performance. Unmasked exceptions may cause a reduction in the retirement rate. • Utilize the flush-to-zero mode for higher performance to avoid the penalty of dealing with denormals and underflows. • Incorporate the prefetch instruction whenever possible (for details, refer to Chapter 6, “Optimizing Cache Utilization for Pentium® III Processors” ). 5-1 5 Intel Architecture Optimization Reference Manual • Try to emulate conditional moves by using masked compares and logicals instead of using conditional jumps. • Use MMX™ technology instructions if the computations can be done in SIMD integer, for shuffling data, or for copying data that is not used later in SIMD floating-point computations. • If the algorithm requires extended precision, then conversion to SIMD floating-point code is not advised because the Streaming SIMD Extensions for floating-point instructions are single-precision. • Use the reciprocal instructions followed by iteration for increased accuracy. These instructions yield reduced accuracy but execute much faster. Note the following: — If reduced accuracy is acceptable, use them with no iteration. — If near full accuracy is needed, use a Newton-Raphson iteration. — If full accuracy is needed, then use divide and square root which provide more accuracy, but slow down performance. Planning Considerations Whether adapting an existing application or creating a new one, using SIMD floating-point instructions to optimal advantage requires consideration of several issues. In general, when choosing candidates for optimization, look for code segments that are computationally intensive and floating-point intensive. Also consider efficient use of the cache architecture. Intel provides tools for evaluation and tuning. The sections that follow answer the questions that should be raised before implementation: • Which part of the code benefits from SIMD floating-point instructions? • Is the current algorithm the most appropriate for SIMD floating-point instructions? • Is the code floating-point intensive? • Is the data arranged for efficient utilization of the SIMD floating-point registers? • Is this application targeted for processors without SIMD floating-point instructions? 5-2 Optimizing Floating-point Applications 5 Which Part of the Code Benefits from SIMD Floating-point Instructions? Determine which code will benefit from SIMD floating-point instructions. Floating-point intensive applications that repeatedly execute similar operations where operations are repeated for multiple data sets, such as loops, might benefit from using SIMD floating-point instructions. Other factors that need to be considered include data organization if the kernel operation can use parallelism. If the algorithm employed requires performance, range, and precision, then floating-point computation is the best choice. If performance is the primary reason for floating-point implementation, then the algorithm could increase its performance if converted to SIMD floating-point code. MMX Technology and Streaming SIMD Extensions Floating-point Code When generating SIMD floating-point code, the rules for mixing MMX technology code and floating-point code do not apply. Since the SIMD floating-point registers are separate registers and are not mapped onto existing registers, SIMD floating-point code can be mixed with floating-point and MMX technology code. The SIMD floating-point instructions map to the same ports as the MMX technology and floating-point code. To avoid instruction stalls, consult Appendix C, “Instruction to Decoder Specification,” when writing an application that mixes these various codes. Scalar Code Optimization In terms of performance, the Streaming SIMD Extensions scalar code can do as well as x87 but has the following advantages: • Using a flat register model rather than a stack model. • Mixing with MMX technology code without penalty. • Using scalar instructions on packed SIMD floating-point data when needed, since they bypass the upper fields of the packed data. This bypassing mechanism allows scalar code to have extra register storage by using the upper fields for temporary storage. 5-3 5 Intel Architecture Optimization Reference Manual The following are some additional points to take into consideration when writing scalar code: • The scalar code can run on two execution ports in addition to the load and store ports, an advantage over x87 code where it had only one floating-point execution port. • The scalar code is decoded as 1 per cycle. • To increase performance while avoiding this decoder limitation, use implicit loads with arithmetic instructions that increase the number of µops decoded. EMMS Instruction Usage Guidelines The EMMS instruction sets the values of all the tags in the floating-point unit (FPU) tag word to empty (all ones). There are no requirements for using the emms instruction when mixing SIMD floating-point code with either MMX technology code or floating-point code. The emms instruction need only be used in the context of the existing rules for MMX technology intrinsics and floating-point code. It is only required when transitioning from MMX technology code to floating-point code. See Table 5-1 for details. Table 5-1 EMMS Instruction Usage Guidelines EMMS Flow 1 Flow 2 Required x87 MMX technology No; ensure that stack is empty x87 Streaming SIMD Extensions No; ensure that stack is empty x87 Streaming SIMD Extensions- No SIMD floating-point MMX technology x87 Yes MMX technology Streaming SIMD Extensions- No SIMD integer MMX technology Streaming SIMD Extensions- No SIMD floating-point continued 5-4 Optimizing Floating-point Applications 5 Table 5-1 EMMS Instruction Usage Guidelines (continued) EMMS Flow 1 Flow 2 Required Streaming SIMD x87 Yes Extensions- SIMD integer Streaming SIMD MMX technology No Extensions- SIMD integer Streaming SIMD Streaming SIMD Extensions- No Extensions- SIMD floating-point SIMD integer Streaming SIMD x87 No Extensions- SIMD floating-point Streaming SIMD MMX technology No Extensions- SIMD floating-point Streaming SIMD Streaming SIMD Extensions- No Extensions- SIMD integer SIMD floating-point CPUID Usage for Detection of SIMD Floating-point Support Applications must be able to determine if Streaming SIMD Extensions are available. Please refer the section “Checking for Processor Support of Streaming SIMD Extensions and MMX™ Technology” in Chapter 3 for the techniques to determine whether the processor and operating system support Streaming SIMD Extensions. Data Alignment The data must be 16-byte-aligned for packed floating-point operations (that is, no alignment constraint for scalar floating-point). If the data is not 16-byte-aligned, a general protection exception will be generated. If you know that the data is not aligned, use the movups (mov unaligned) instruction to avoid the protection error exception. The movups instruction is the only one that can access unaligned data. 5-5 5 Intel Architecture Optimization Reference Manual Accessing data that is properly aligned can save six to nine cycles on the Pentium® III processor. If the data is properly aligned on a 16-byte boundary, frequent access can provide a significant performance improvement. Data Arrangement Since the Streaming SIMD Extensions incorporate a SIMD architecture, arranging the data to fully use the SIMD registers produces optimum performance. This implies contiguous data for processing, which leads to fewer cache misses and potentially quadruples the speed. These performance gains occur because the four-element SIMD registers can be loaded with 128-bit load instructions (movaps – move aligned packed single precision). Refer to the “Stack and Data Alignment” in Chapter 3 for data arrangement recommendations. Duplicating and padding techniques overcome the misalignment problem that can occur in some data structures and arrangements. This increases the data space but avoids the expensive penalty for misaligned data access. The traditional data arrangement does not lend itself to SIMD parallel techniques in some applications. Traditional 3D data structures, for example, do not lead to full utilization of the SIMD registers. This data layout has traditionally been an array of structures (AoS). To fully utilize the SIMD registers, a new data layout has been proposed—a structure of arrays (SoA). The SoA structure allows the application to fully utilize the SIMD registers. With full utilization comes more optimized performance. Vertical versus Horizontal Computation Traditional 3D data structures do not lend themselves to vertical computation. The data can still be operated on and computation can proceed, but without optimally utilizing the SIMD registers. To optimally utilize the SIMD registers the data can be organized in the SoA format as mentioned above. 5-6 Optimizing Floating-point Applications 5 Consider 3D geometry data organization. One way to apply SIMD technology to a typical 3D geometry is to use horizontal execution. This means to parallelize the computation on the x, y, z, and w components of a single vertex (that is, of a single vector simultaneously referred to as an xyz data representation, see the diagram below). X Y Z W Vertical computation, SoA, is recommended over horizontal, for several reasons: • When computing on a single vector (xyz), it is common to use only a subset of the vector components; for example, in 3D graphics the W component is sometimes ignored. This means that for single-vector operations, 1 of 4 computation slots is not being utilized. This results in a 25% reduction of peak efficiency, and only 75% peak performance can be attained. • It may become difficult to hide long latency operations. For instance, another common function in 3D graphics is normalization, which requires the computation of a reciprocal square root (that is, 1/sqrt); both the division and square root are long latency operations. With vertical computation (SoA), each of the 4 computation slots in a SIMD operation is producing a unique result, so the net latency per slot is L/4 where L is the overall latency of the operation. However, for horizontal computation, the 4 computation slots each produce the same result, hence to produce 4 separate results requires a net latency per slot of L. How can the data be organized to utilize all 4 computation slots? The vertex data can be reorganized to allow computation on each component of 4 separate vertices, that is, processing multiple vectors simultaneously. This will also be referred to as an SoA form of representing vertices data shown in Table 5-2. Table 5-2 SoA Form of Representing Vertices Data Vx array X1 X2 X3 X4 ..... Xn Vy array Y1 Y2 Y3 Y4 ..... Yn Vz array Z1 Z2 Z3 Y4 ..... Zn Vw array W1 W2 W3 W4 ..... Wn 5-7 5 Intel Architecture Optimization Reference Manual Organizing data in this manner yields a unique result for each computational slot for each arithmetic operation. Vertical computation takes advantage of the inherent parallelism in 3D geometry processing of vertices. It assigns the computation of four vertices to the four compute slots of the Pentium III processor, thereby eliminating the disadvantages of the horizontal approach described earlier. The dot product operation implements the SoA representation of vertices data. A schematic representation of dot product operation is shown in Figure 5-1. Figure 5-1 Dot Product Operation X1 X2 X3 X4 x Fx Fx Fx Fx + Y1 Y2 Y3 Y4 x Fy Fy Fy Fy + Z1 Z2 Z3 Z4 x Fz Fz Fz Fz + W1 W2 W3 W4 x Fw Fw Fw Fw = R1 R2 R3 R4 Example 5-1 shows how 1 result would be computed for 7 instructions if the data were organized as AoS. Hence 4 results would require 28 instructions. 5-8 Optimizing Floating-point Applications 5 Example 5-1 Pseudocode for Horizontal (xyz, AoS) Computation mulps ; x*x’, y*y’, z*z’ movaps ; reg->reg move, since next steps overwrite shufps ; get b,a,d,c from a,b,c,d addps ; get a+b,a+b,c+d,c+d movaps ; reg->reg move shufps ; get c+d,c+d,a+b,a+b from prior addps addps ; get a+b+c+d,a+b+c+d,a+b+c+d,a+b+c+d _____________________________________________________________ Now consider the case when the data is organized as SoA. Example 5-2 demonstrates how 4 results are computed for 5 instructions. Example 5-2 Pseudocode for Vertical (xxxx, yyyy, zzzz, SoA) Computation mulps ; x*x’ for all 4 x-components of 4 vertices mulps ; y*y’ for all 4 y-components of 4 vertices mulps ; z*z’ for all 4 z-components of 4 vertices addps ; x*x’ + y*y’ addps ; x*x’+y*y’+z*z’ _____________________________________________________________ For the most efficient use of the four component-wide registers, reorganizing the data into the SoA format yields increased throughput and hence much better performance for the instructions used. As can be seen from this simple example, vertical computation yielded 100% use of the available SIMD registers and produced 4 results. If the data structures are restricted to a format that is not “friendly to vertical computation,” it can be rearranged “on the fly” to achieve full utilization of the SIMD registers. This operation referred to as “swizzling” and the “deswizzling” operation are discussed in the following sections. 5-9 5 Intel Architecture Optimization Reference Manual Data Swizzling In many algorithms, swizzling data from one format to another is required. An example of this is AoS format, where the vertices come as xyz adjacent coordinates. Rearranging them into SoA format, xxxx, yyyy, zzzz, allows more efficient SIMD computations. The following instructions can be used for efficient data shuffling and swizzling: • movlps, movhps load/store and move data on half sections of the registers • shuffps, unpackhps, and unpacklps unpack data To gather data from 4 different memory locations on the fly, follow steps: 1. identify the first half of the 128-bit memory location. 2. group the different halves together using the movlps and movhps to form an xyxy layout in two registers 3. from the 4 attached halves, get the xxxx by using one shuffle, the yyyy by using another shuffle. The zzzz is derived the same way but only requires one shuffle. Example 5-3 illustrates the swizzle function. Example 5-3 Swizzling Data typedef struct _VERTEX_AOS { float x, y, z, color; } Vertex_aos; // AoS structure declaration typedef struct _VERTEX_SOA { float x[4], float y[4], float z[4]; float color[4]; } Vertex_soa; // SoA structure declaration void swizzle_asm (Vertex_aos *in, Vertex_soa *out) { // in mem: x1y1z1w1-x2y2z2w2-x3y3z3w3-x4y4z4w4- // SWIZZLE XYZW --> XXXX asm { mov ecx, in // get structure addresses mov edx, out _____________________________________________________________ continued 5-10 Optimizing Floating-point Applications 5 Example 5-3 Swizzling Data (continued) movlps xmm7, [ecx] // xmm7 = -- -- y1 x1 movhps xmm7, [ecx+16] // xmm7 = y2 x2 y1 x1 movlps xmm0, [ecx+32] // xmm0 = -- -- y3 x3 movhps xmm0, [ecx+48] // xmm0 = y4 x4 y3 x3 movaps xmm6, xmm7 // xmm6 = y1 x1 y1 x1 shufps xmm7, xmm0, 0x88// xmm7 = x1 x2 x3 x4 => X shufps xmm6, xmm0, 0xDD// xmm6 = y1 y2 y3 y4 => Y movlps xmm2, [ecx+8] // xmm2 = -- -- w1 z1 movhps xmm2, [ecx+24] // xmm2 = w2 z2 u1 z1 movlps xmm1, [ecx+40] // xmm1 = -- -- s3 z3 movhps xmm1, [ecx+56] // xmm1 = w4 z4 w3 z3 movaps xmm0, xmm2 // xmm0 = w1 z1 w1 z1 shufps xmm2, xmm1, 0x88// xmm2 = z1 z2 z3 z4 => Z shufps xmm0, xmm1, 0xDD// xmm6 = w1 w2 w3 w4 => W movaps [edx], xmm7 // store X movaps [edx+16], xmm6 // store Y movaps [edx+32], xmm2 // store Z movaps [edx+48], xmm0 // store W // SWIZZLE XYZ -> XXX } } _____________________________________________________________ Example 5-4 shows the same data swizzling algorithm encoded using the Intel® C/C++ Compiler’s intrinsics for Streaming SIMD Extensions. 5-11 5 Intel Architecture Optimization Reference Manual Example 5-4 Swizzling Data Using Intrinsics //Intrinsics version of data swizzle void swizzle_intrin (Vertex_aos *in, Vertex_soa *out, int stride) { __m128 x, y, z, w; __m128 tmp; x = _mm_loadl_pi(x,(__m64 *)(in)); x = _mm_loadh_pi(x,(__m64 *)(stride + (char *)(in))); y = _mm_loadl_pi(y,(__m64 *)(2*stride+char *)(in))); y = _mm_loadh_pi(y,(__m64 *)(3*stride+(char *)(in))); tmp = _mm_shuffle_ps( x, y, _MM_SHUFFLE( 2, 0, 2, 0)); y = _mm_shuffle_ps( x, y, _MM_SHUFFLE( 3, 1, 3, 1)); x = tmp; z = _mm_loadl_pi(z,(__m64 *)(8 + (char *)(in))); z = _mm_loadh_pi(z,(__m64 *)(stride+8+(char *)(in))); w = _mm_loadl_pi(w,(__m64 *)(2*stride+8+(char*)(in))); w = _mm_loadh_pi( w,(__m64 *)(3*stride+8+(char*)(in))); w = _mm_shuffle_ps( z, w, _MM_SHUFFLE( 3, 1, 3, 1)); z = tmp; tmp = _mm_shuffle_ps( z, w, _MM_SHUFFLE( 2, 0, 2, 0)); _mm_store_ps(&out->x[0], x); _mm_store_ps(&out->y[0], y); _mm_store_ps(&out->z[0], z); _mm_store_ps(&out->w[0], w); } _____________________________________________________________ 5-12 Optimizing Floating-point Applications 5 CAUTION. Avoid creating a dependency chain from previous computations because the movhps/movlps instructions bypass one part of the register. The same issue can occur with the use of an exclusive-OR function within an inner loop in order to clear a register: XORPS %xmm0, %xmm0; All 0’s written to xmm0 Although the generated result of all zeros does not depend on the specific data contained in the source operand (that is, XOR of a register with itself always produces all zeros), the instruction cannot execute until the instruction that generates xmm0 has completed. In the worst case, this creates a dependency chain that links successive iterations of the loop, even if those iterations are otherwise independent; the resulting performance impact can be significant depending on how much other independent intra-loop computation is being performed. The same situation can occur for the above movhps/movlps/shufps sequence. Since each movhps/movlps instruction bypasses part of the destination register, the instruction cannot execute until the prior instruction to generate this register has completed. As with the xorps example, in the worst case this dependency can prevent successive loop iterations from executing in parallel. A solution is to include a 128-bit load (that is, from a dummy local variable, such as tmp in Example 5-4) to each register to be used with a movhps/movlps instruction; this action effectively breaks the dependency by performing an independent load from a memory or cached location. Data Deswizzling In the deswizzle operation, we want to arrange the SoA format back into AoS format so the xxxx, yyyy, zzzz are rearranged and stored in memory as xyz. To do this we can use the unpcklps/unpckhps instructions to regenerate the xyxy layout and then store each half (xy) into its corresponding memory location using movlps/movhps followed by another movlps/movhps to store the z component. 5-13 5 Intel Architecture Optimization Reference Manual Example 5-5 illustrates the deswizzle function: Example 5-5 Deswizzling Data void deswizzle_asm(Vertex_soa *in, Vertex_aos *out) { __asm { mov ecx, in // load structure addresses mov edx, out movaps xmm7, [ecx] // load x1 x2 x3 x4 => xmm7 movaps xmm6, [ecx+16] // load y1 y2 y3 y4 => xmm6 movaps xmm5, [ecx+32] // load z1 z2 z3 z4 => xmm5 movaps xmm4, [ecx+48] // load w1 w2 w3 w4 => xmm4 // START THE DESWIZZLING HERE movaps xmm0, xmm7 // xmm0= x1 x2 x3 x4 unpcklps xmm7, xmm6 // xmm7= x1 y1 x2 y2 movlps [edx], xmm7 // v1 = x1 y1 -- -- movhps [edx+16], xmm7 // v2 = x2 y2 -- -- unpckhps xmm0, xmm6 // xmm0= x3 y3 x4 y4movlps [edx+32], xmm0 // v3 = x3 y3 -- -- movhps [edx+48], xmm0 // v4 = x4 y4 -- -- movaps xmm0, xmm5 // xmm0= z1 z2 z3 z4 unpcklps xmm5, xmm4 // xmm5= z1 w1 z2 w2 unpckhps xmm0, xmm4 // xmm0= z3 w3 z4 w4 movlps [edx+8], xmm5 // v1 = x1 y1 z1 w1 movhps [edx+24], xmm5 // v2 = x2 y2 z2 w2 movlps [edx+40], xmm0 // v3 = x3 y3 z3 w3 movhps [edx+56], xmm0 // v4 = x4 y4 z4 w4 // DESWIZZLING ENDS HERE } } _____________________________________________________________ 5-14 Optimizing Floating-point Applications 5 You may have to swizzle data in the registers, but not in memory. This occurs when two different functions want to process the data in different layout. In lighting, for example, data comes as rrrr gggg bbbb aaaa, and you must deswizzle them into rgba before converting into integers. In this case you use the movlhps/movhlps instructions to do the first part of the deswizzle, followed by shuffle instructions, Example 5-6 and Example 5-7. Example 5-6 Deswizzling Data Using the movlhps and shuffle Instructions void deswizzle_rgb(Vertex_soa *in, Vertex_aos *out) { //-----------deswizzle rgb--------------- // xmm1 = rrrr, xmm2 = gggg, xmm3 = bbbb, xmm4 = aaaa (assumed) __asm { mov ecx, in // load structure addresses mov edx, out movaps xmm1, [ecx] // load r1 r2 r3 r4 => xmm1 movaps xmm2, [ecx+16] // load g1 g2 g3 g4 => xmm2 movaps xmm3, [ecx+32] // load b1 b2 b3 b4 => xmm3 movaps xmm4, [ecx+48] // load a1 a2 a3 a4 => xmm4 // Start deswizzling here movaps xmm7, xmm4 // xmm7= a1 a2 a3 a4 movhlps xmm7, xmm3 // xmm7= b3 b4 a3 a4 movaps xmm6, xmm2 // xmm6= g1 g2 g3 g4 movlhps xmm3, xmm4 // xmm3= b1 b2 a1 a2 movhlps xmm2, xmm1 // xmm2= r3 r4 g3 g4 movlhps xmm1, xmm6 // xmm1= r1 r2 g1 g2 movaps xmm6, xmm2 // xmm6= r3 r4 g3 g4 movaps xmm5, xmm1 // xmm5= r1 r2 g1 g2 shufps xmm2, xmm7, 0xDD // xmm2= r4 g4 b4 a4 shufps xmm1, xmm3, 0x88 // xmm4= r1 g1 b1 a1 shufps xmm5, xmm3, 0x88 // xmm5= r2 g2 b2 a2 shufps xmm6, xmm7, 0xDD // xmm6= r3 g3 b3 a3 _____________________________________________________________ continued 5-15 5 Intel Architecture Optimization Reference Manual Example 5-6 Deswizzling Data Using the movlhps and shuffle Instructions (continued) movaps [edx], xmm4 // v1 = r1 g1 b1 a1 movaps [edx+16], xmm5 // v2 = r2 g2 b2 a2 movaps [edx+32], xmm6 // v3 = r3 g3 b3 a3 movaps [edx+48], xmm2 // v4 = r4 g4 b4 a4 // DESWIZZLING ENDS HERE } } _____________________________________________________________ Example 5-7 Deswizzling Data Using Intrinsics with the movlhps and shuffle Instructions void mmx_deswizzle(IVertex_soa *in, IVertex_aos *out) { __asm { mov ebx, in mov edx, out movq mm0, [ebx] // mm0= u1 u2 movq mm1, [ebx+16] // mm1= v1 v2 movq mm2, mm0 // mm2= u1 u2 punpckhdq mm0, mm1 // mm0= u1 v1 punpckldq mm2, mm1 // mm0= u2 v2 movq [edx], mm2 // store u1 v1 movq [edx+8], mm0 // store u2 v2 movq mm4, [ebx+8] // mm0= u3 u4 movq mm5, [ebx+24] // mm1= v3 v4 movq mm6, mm4 // mm2= u3 u4 punpckhdq mm4, mm5 // mm0= u3 v3 punpckldq mm6, mm5 // mm0= u4 v4 movq [edx+16], mm6 // store u3v3 movq [edx+24], mm4 // store u4v4 } } _____________________________________________________________ 5-16 Optimizing Floating-point Applications 5 Using MMX Technology Code for Copy or Shuffling Functions If there are some parts in the code that are mainly copying, shuffling, or doing logical manipulations that do not require use of Streaming SIMD Extensions code, consider performing these actions with MMX technology code. For example, if texture data is stored in memory as SoA (uuuu, vvvv) and they need only to be deswizzled into AoS layout (uv) for the graphic cards to process, you can use either the Streaming SIMD Extensions or MMX technology code, but MMX technology code has these two advantages: • The MMX instructions can decode on 3 decoders while Streaming SIMD Extensions code uses only one decoder. • The MMX instructions allow you to avoid consuming Streaming SIMD Extension registers for just rearranging data from memory back to memory. Example 5-8 illustrates how to use MMX technology code for copying or shuffling. 5-17 5 Intel Architecture Optimization Reference Manual Example 5-8 Using MMX Technology Code for Copying or Shuffling asm("movq TRICOUNT*12(%ebx, %esi, 4),%mm0"); // mm0= u1 u2 asm("movq TRICOUNT*16(%ebx, %esi, 4),%mm1"); // mm1= v1 v2 asm("movq %mm0,%mm2"); // mm2= u1 u2 asm("punpckhdq %mm1,%mm0");// mm0= u1 v1 asm("punpckldq %mm1,%mm2");// mm0= u2 v2 asm("movq %mm0, 24+0*32(%edx)");// store u1v1 asm("movq %mm2, 24+1*32(%edx)");// store u2v2 asm("movq TRICOUNT*12(%ebx, %esi, 4), %mm4"); // mm0= u3 u4 should be address+8 asm("movq TRICOUNT*16(%ebx, %esi, 4), %mm5"); // mm1= v3 v4 should be address+8 asm("movq %mm4,%mm6");// mm2= u3 u4 asm("punpckhdq %mm5,%mm4");// mm0= u3 v3 asm("punpckldq %mm5,%mm6");// mm0= u4 v4 asm("movq %mm4, 24+0*32(%edx)");// store u3v3 asm("movq %mm6, 24+1*32(%edx)");// store u4v4 _____________________________________________________________ Horizontal ADD Although vertical computations use the SIMD performance better than horizontal computations do, in some cases, the code must use a horizontal operation. The movlhps/movhlps and shuffle can be used to sum data horizontally. For example, starting with four 128-bit registers, to sum up each register horizontally while having the final results in one register, use the movlhps/movhlps instructions to align the upper and lower parts of each register. This allows you to use a vertical add. With the resulting partial horizontal summation, full summation follows easily. Figure 5-2 schematically presents horizontal add using movhlps/movlhps, while Example 5-9 and Example 5-10 provide the code for this operation. 5-18 Optimizing Floating-point Applications 5 Figure 5-2 Horizontal Add Using movhlps/movlhps xmm0 xmm1 xmm2 xmm3 a4 a3 a2 a1 b4 b3 b2 b1 c4 c3 c2 c1 d4 d3 d2 d1 movlhps movhlps movlhps movhlps b2 b1 a2 a1 b4 b3 a4 a3 d2 d1 c2 c1 d4 d3 c4 c3 addps addps b2+b4 b1+b3 a2+a4 a1+a3 d2+d4 d1+d3 c2+c4 c1+c3 b2+b4 b1+b3 a2+a4 a1+a3 d2+d4 d1+d3 c2+c4 c1+c3 shufps shufps d1+d3 c1+c3 b1+b3 a1+a3 d2+d4 c2+c4 b2+b4 a2+a4 addps d1+d2+d3+d4 c1+c2+c3+c4 b1+b2+b3+b4 a1+a2+a3+a4 5-19 5 Intel Architecture Optimization Reference Manual Example 5-9 Horizontal Add Using movhlps/movlhps void horiz_add(Vertex_soa *in, float *out) { __asm { mov ecx, in // load structure addresses mov edx, out movaps xmm0, [ecx] // load A1 A2 A3 A4 => xmm0 movaps xmm1, [ecx+16] // load B1 B2 B3 B4 => xmm1 movaps xmm2, [ecx+32] // load C1 C2 C3 C4 => xmm2 movaps xmm3, [ecx+48] // load D1 D2 D3 D4 => xmm3 // START HORIZONTAL ADD movaps xmm5, xmm0 // xmm5= A1,A2,A3,A4 movlhps xmm5, xmm1 // xmm5= A1,A2,B1,B2 movhlps xmm1, xmm0 // xmm1= A3,A4,B3,B4 addps xmm5, xmm1 // xmm5= A1+A3,A2+A4,B1+B3,B2+B4 movaps xmm4, xmm2 movlhps xmm2, xmm3 // xmm2= C1,C2,D1,D2 movhlps xmm3, xmm4 // xmm3= C3,C4,D3,D4 addps xmm3, xmm2 // xmm3= C1+C3,C2+C4,D1+D3,D2+D4 movaps xmm6, xmm5 // xmm6= A1+A3,A2+A4,B1+B3,B2+B4 shufps xmm6, xmm3, 0x31 //xmm6=A1+A3,B1+B3,C1+C3,D1+D3 shufps xmm5, xmm3, 0xAA // xmm5= A2+A4,B2+B4,C2+C4,D2+D4 addps xmm6, xmm5 // xmm6= D,C,B,A // END HORIZONTAL ADD movaps [edx], xmm6 } } _____________________________________________________________ 5-20 Optimizing Floating-point Applications 5 Example 5-10 Horizontal Add Using Intrinsics with movhlps/movlhps void horiz_add_intrin(Vertex_soa *in, float *out) { __m128 v1, v2, v3, v4; __m128 tmm0,tmm1,tmm2,tmm3,tmm4,tmm5,tmm6; // Temporary variables tmm0 = _mm_load_ps(in->x);//tmm0 = A1 A2 A3 A4 tmm1 = _mm_load_ps(in->y);//tmm1 = B1 B2 B3 B4 tmm2 = _mm_load_ps(in->z);//tmm2 = C1 C2 C3 C4 tmm3 = _mm_load_ps(in->w);//tmm3 = D1 D2 D3 D4 tmm5 = tmm0; //tmm0 = A1 A2 A3 A4 tmm5 = _mm_movelh_ps(tmm5, tmm1);//tmm5 = A1 A2 B1 B2 tmm1 = _mm_movehl_ps(tmm1, tmm0);//tmm1 = A3 A4 B3 B4 tmm5 = _mm_add_ps(tmm5, tmm1); //tmm5 = A1+A3 A2+A4 B1+B3 B2+B4 tmm4 = tmm2; tmm2 = _mm_movelh_ps(tmm2, tmm3);//tmm2 = C1 C2 D1 D2 tmm3 = _mm_movehl_ps(tmm3, tmm4);//tmm3 = C3 C4 D3 D4 tmm3 = _mm_add_ps(tmm3, tmm2); //tmm3 = C1+C3 C2+C4 D1+D3 D2+D4 tmm6 = tmm5; //tmm6 = A1+A3 A2+A4 B1+B3 B2+B4 tmm6 = _mm_shuffle_ps(tmm6, tmm3, 0x88); //tmm6 = A1+A3 B1+B3 C1+C3 D1+D3 tmm5 = _mm_shuffle_ps(tmm5, tmm3, 0xDD); //tmm5 = A2+A4 B2+B4 C2+C4 D2+D4 tmm6 = _mm_add_ps(tmm6, tmm5); //tmm6 = A1+A2+A3+A4 B1+B2+B3+B4 //C1+C2+C3+C4 D1+D2+D3+D4 _mm_store_ps(out, tmm6); } _____________________________________________________________ 5-21 5 Intel Architecture Optimization Reference Manual Scheduling Instructions using the same registers should be scheduled close to each other. There are two read ports for registers. You can obtain the most efficient code if you schedule those instructions that read from the same registers together without severely affecting the resolution of true dependencies. As an exercise, first examine the non-optimal code in the first block of Example 5-11, then examine the second block of optimized code. The reads from the registers can only read two physical registers per clock. Example 5-11 Scheduling Instructions that Use the Same Register int toy(unsigned char *sptr1, unsigned char *sptr2) { __asm { push ecx mov ebx, [ebp+8] // sptr1 mov eax, [ebp+12] // sptr2 movq mm1, [eax] movq mm3, [ebx] pxor mm0, mm0 // initialize mm0 to 0 pxor mm5, mm5 // initialize mm5 to 0 pxor mm6, mm6 // initialize mm6 to 0 pxor mm7, mm7 // initialize mm7 to 0 mov ecx, 256 // initialize loop counter top_of_loop: movq mm2, [ebx+ecx+8] movq mm4, [eax+ecx+8] paddw mm6, mm5 pmullw mm1, mm3 movq mm3, [ebx+ecx+16] movq mm5, [eax+ecx+16] paddw mm7, mm6 _____________________________________________________________ continued 5-22 Optimizing Floating-point Applications 5 Example 5-11 Scheduling Instructions that Use the Same Register (continued) pmullw mm2, mm4 movq mm4, [ebx+ecx+24] movq mm6, [eax+ecx+24] paddw mm0, mm7 pmullw mm3, mm5 movq mm5, [ebx+ecx+32] movq mm7, [eax+ecx+32] paddw mm1, mm0 pmullw mm4, mm6 movq mm6, [ebx+ecx+40] movq mm0, [eax+ecx+40] paddw mm2, mm1 pmullw mm5, mm7 movq mm7, [ebx+ecx+48] movq mm1, [eax+ecx+48] paddw mm3, mm2 pmullw mm6, mm0 movq mm0, [ebx+ecx+56] movq mm2, [eax+ecx+56] paddw mm4, mm3 pmullw mm7, mm1 movq mm1, [ebx+ecx+64] movq mm3, [eax+ecx+64] paddw mm5, mm4 pmullw mm0, mm2 movq mm2, [ebx+ecx+72] movq mm4, [eax+ecx+72] paddw mm6, mm5 _____________________________________________________________ continued 5-23 5 Intel Architecture Optimization Reference Manual Example 5-11 Scheduling Instructions that Use the Same Register (continued) pmullw mm1, mm3 sub ecx, 64 jg top_of_loop // no horizontal reduction needed at the end movd [eax], mm6 pop ecx } } _____________________________________________________________ Try to group instructions using the same registers as closely as possible. Also try to schedule instructions so that data is still in the reservation station when new instructions that use the same registers are issued to them. The source remains in the reservation station until the instruction is dispatched. Now you can bypass directly to the functional unit because dependent instructions have spaced far enough away to resolve dependencies. Scheduling with the Triple-Quadruple Rule Schedule instructions using the triple/quadruple rule, add/mult/load, and combine triplets from independent chains of instructions. Split register-memory instructions into a load followed by the actual computation. As an example, split addps xmm0, [edi] into movaps xmm1, [edi] and addps xmm0, xmm1. Increase the distance between the load and the actual computation and try to insert independent instructions between them. This technique works well unless you have register pressure or you are limited by decoder throughput, see Example 5-12. 5-24 Optimizing Floating-point Applications 5 Example 5-12 Scheduling with the Triple/Quadruple Rule int toy(sptr1, sptr2) __m64 *sptr1, *sptr2; { __m64 src1; /* source 1 */ __m64 src2; /* source 2 */ __m64 m; /* mul */ __m64 result; /* result */ int i; result=0; for(i=0; i