Nytro Posted August 31, 2010 Report Posted August 31, 2010 Optimizing subroutines in assembly languageAn optimization guide for x86 platformsContents1 Introduction ....................................................................................................................... 41.1 Reasons for using assembly code .............................................................................. 51.2 Reasons for not using assembly code ........................................................................ 51.3 Microprocessors covered by this manual .................................................................... 61.4 Operating systems covered by this manual................................................................. 72 Before you start................................................................................................................. 72.1 Things to decide before you start programming .......................................................... 72.2 Make a test strategy.................................................................................................... 92.3 Common coding pitfalls............................................................................................... 93 The basics of assembly coding........................................................................................ 123.1 Assemblers available................................................................................................ 123.2 Register set and basic instructions............................................................................ 143.3 Addressing modes .................................................................................................... 183.4 Instruction code format ............................................................................................. 243.5 Instruction prefixes.................................................................................................... 254 ABI standards.................................................................................................................. 274.1 Register usage.......................................................................................................... 274.2 Data storage ............................................................................................................. 284.3 Function calling conventions ..................................................................................... 284.4 Name mangling and name decoration ...................................................................... 304.5 Function examples.................................................................................................... 305 Using intrinsic functions in C++ ....................................................................................... 335.1 Using intrinsic functions for system code .................................................................. 345.2 Using intrinsic functions for instructions not available in standard C++ ..................... 355.3 Using intrinsic functions for vector operations ........................................................... 355.4 Availability of intrinsic functions................................................................................. 356 Using inline assembly in C++ .......................................................................................... 356.1 MASM style inline assembly ..................................................................................... 366.2 Gnu style inline assembly ......................................................................................... 417 Using an assembler......................................................................................................... 447.1 Static link libraries..................................................................................................... 457.2 Dynamic link libraries................................................................................................ 467.3 Libraries in source code form.................................................................................... 477.4 Making classes in assembly...................................................................................... 487.5 Thread-safe functions ............................................................................................... 497.6 Makefiles ..................................................................................................................508 Making function libraries compatible with multiple compilers and platforms..................... 518.1 Supporting multiple name mangling schemes........................................................... 518.2 Supporting multiple calling conventions in 32 bit mode ............................................. 538.3 Supporting multiple calling conventions in 64 bit mode ............................................. 558.4 Supporting different object file formats...................................................................... 578.5 Supporting other high level languages ...................................................................... 589 Optimizing for speed ....................................................................................................... 599.1 Identify the most critical parts of your code ............................................................... 599.2 Out of order execution .............................................................................................. 5929.3 Instruction fetch, decoding and retirement ................................................................ 629.4 Instruction latency and throughput ............................................................................ 639.5 Break dependency chains......................................................................................... 649.6 Jumps and calls ........................................................................................................ 6510 Optimizing for size......................................................................................................... 7210.1 Choosing shorter instructions.................................................................................. 7210.2 Using shorter constants and addresses .................................................................. 7310.3 Reusing constants .................................................................................................. 7510.4 Constants in 64-bit mode ........................................................................................ 7510.5 Addresses and pointers in 64-bit mode................................................................... 7510.6 Making instructions longer for the sake of alignment............................................... 7711 Optimizing memory access............................................................................................ 8011.1 How caching works................................................................................................. 8011.2 Trace cache............................................................................................................ 8111.3 Alignment of data.................................................................................................... 8211.4 Alignment of code ................................................................................................... 8411.5 Organizing data for improved caching..................................................................... 8611.6 Organizing code for improved caching.................................................................... 8611.7 Cache control instructions....................................................................................... 8712 Loops ............................................................................................................................ 8712.1 Minimize loop overhead .......................................................................................... 8712.2 Induction variables.................................................................................................. 9012.3 Move loop-invariant code........................................................................................ 9112.4 Find the bottlenecks................................................................................................ 9112.5 Instruction fetch, decoding and retirement in a loop ................................................ 9212.6 Distribute µops evenly between execution units...................................................... 9212.7 An example of analysis for bottlenecks on PM........................................................ 9312.8 Same example on Core2 ........................................................................................ 9612.9 Loop unrolling ......................................................................................................... 9812.10 Optimize caching ................................................................................................ 10012.11 Parallelization ..................................................................................................... 10112.12 Analyzing dependences...................................................................................... 10212.13 Loops on processors without out-of-order execution........................................... 10512.14 Macro loops ........................................................................................................ 10713 Vector programming.................................................................................................... 10913.1 Conditional moves in SIMD registers .................................................................... 11013.2 Using vector instructions with other types of data than they are intended for ........ 11313.3 Shuffling data........................................................................................................ 11513.4 Generating constants............................................................................................ 11813.5 Accessing unaligned data ..................................................................................... 12113.6 Using AVX instruction set and YMM registers ....................................................... 12513.7 Vector operations in general purpose registers ..................................................... 13014 Multithreading.............................................................................................................. 13114.1 Hyperthreading ..................................................................................................... 13215 CPU dispatching.......................................................................................................... 13215.1 Checking for operating system support for XMM and YMM registers .................... 13416 Problematic Instructions .............................................................................................. 13516.1 LEA instruction (all processors)............................................................................. 13516.2 INC and DEC (all Intel processors) ....................................................................... 13616.3 XCHG (all processors) .......................................................................................... 13616.4 Shifts and rotates (P4) .......................................................................................... 13616.5 Rotates through carry (all processors) .................................................................. 13716.6 Bit test (all processors) ......................................................................................... 13716.7 LAHF and SAHF (all processors) .......................................................................... 13716.8 Integer multiplication (all processors).................................................................... 13716.9 Division (all processors)........................................................................................ 13716.10 String instructions (all processors) ...................................................................... 14216.11 WAIT instruction (all processors) ........................................................................ 143316.12 FCOM + FSTSW AX (all processors).................................................................. 14416.13 FPREM (all processors) ...................................................................................... 14516.14 FRNDINT (all processors)................................................................................... 14516.15 FSCALE and exponential function (all processors) ............................................. 14516.16 FPTAN (all processors)....................................................................................... 14716.17 FSQRT (SSE processors)................................................................................... 14716.18 FLDCW (Most Intel processors) .......................................................................... 14717 Special topics .............................................................................................................. 14817.1 XMM versus floating point registers (Processors with SSE).................................. 14817.2 MMX versus XMM registers (Processors with SSE2)............................................ 14917.3 XMM versus YMM registers (Processors with AVX).............................................. 14917.4 Freeing floating point registers (all processors)..................................................... 15017.5 Transitions between floating point and MMX instructions (Processors with MMX). 15017.6 Converting from floating point to integer (All processors) ...................................... 15017.7 Using integer instructions for floating point operations .......................................... 15217.8 Using floating point instructions for integer operations .......................................... 15417.9 Moving blocks of data (All processors).................................................................. 15517.10 Self-modifying code (All processors) ................................................................... 15618 Measuring performance............................................................................................... 15618.1 Testing speed ....................................................................................................... 15618.2 The pitfalls of unit-testing ...................................................................................... 15819 Literature..................................................................................................................... 15820 Copyright notice .......................................................................................................... 159Download:http://www.speedyshare.com/files/24051425/optimizing_assembly.pdf Quote