1.
Optimizing software in C++
An optimization guide for Windows, Linux, and Mac
platforms
By Agner Fog. Technical University of Denmark.
Copyright © 2004 - 2024. Last updated 2024-03-15.

Contents
1 Introduction ....................................................................................................................... 3
1.1 Why software is often slow.......................................................................................... 4
1.2 The costs of optimizing ............................................................................................... 4
2 Choosing the optimal platform ........................................................................................... 5
2.1 Choice of hardware platform ....................................................................................... 5
2.2 Choice of microprocessor ........................................................................................... 6
2.3 Choice of operating system......................................................................................... 7
2.4 Choice of programming language ............................................................................... 8
2.5 Choice of compiler .................................................................................................... 10
2.6 Choice of function libraries ........................................................................................ 11
2.7 Choice of user interface framework ........................................................................... 13
2.8 Overcoming the drawbacks of the C++ language ...................................................... 14
3 Finding the biggest time consumers ................................................................................ 15
3.1 How much is a clock cycle? ...................................................................................... 15
3.2 Use a profiler to find hot spots .................................................................................. 16
3.3 Program installation .................................................................................................. 18
3.4 Automatic updates .................................................................................................... 18
3.5 Program loading ....................................................................................................... 18
3.6 Dynamic linking and position-independent code ....................................................... 19
3.7 File access ................................................................................................................ 19
3.8 System database ...................................................................................................... 20
3.9 Other databases ....................................................................................................... 20
3.10 Graphics ................................................................................................................. 20
3.11 Other system resources .......................................................................................... 20
3.12 Network access ...................................................................................................... 20
3.13 Memory access....................................................................................................... 21
3.14 Context switches ..................................................................................................... 21
3.15 Dependency chains ................................................................................................ 21
3.16 Execution unit throughput ....................................................................................... 21
4 Performance and usability ............................................................................................... 22
5 Choosing the optimal algorithm ....................................................................................... 24
6 Development process ...................................................................................................... 24
7 The efficiency of different C++ constructs ........................................................................ 25
7.1 Different kinds of variable storage ............................................................................. 25
7.2 Integers variables and operators ............................................................................... 29
7.3 Floating point variables and operators ...................................................................... 31
7.4 Enums ...................................................................................................................... 33
7.5 Booleans................................................................................................................... 33
7.6 Pointers and references ............................................................................................ 35
7.7 Function pointers ...................................................................................................... 37
7.8 Member pointers ....................................................................................................... 37
7.9 Smart pointers .......................................................................................................... 37
7.10 Arrays ..................................................................................................................... 38
7.11 Type conversions .................................................................................................... 40
7.12 Branches and switch statements............................................................................. 43

7.13 Loops ...................................................................................................................... 45
7.14 Functions ................................................................................................................ 47
7.15 Function parameters ............................................................................................... 50
7.16 Function return types .............................................................................................. 50
7.17 Function tail calls .................................................................................................... 51
7.18 Recursive functions................................................................................................. 51
7.19 Structures and classes ............................................................................................ 52
7.20 Class data members (instance variables) ............................................................... 53
7.21 Class member functions (methods) ......................................................................... 54
7.22 Virtual member functions ........................................................................................ 55
7.23 Runtime type identification (RTTI) ........................................................................... 55
7.24 Inheritance .............................................................................................................. 55
7.25 Constructors and destructors .................................................................................. 56
7.26 Unions .................................................................................................................... 57
7.27 Bitfields ................................................................................................................... 57
7.28 Overloaded functions .............................................................................................. 58
7.29 Overloaded operators ............................................................................................. 58
7.30 Templates ............................................................................................................... 58
7.31 Threads .................................................................................................................. 61
7.32 Exceptions and error handling ................................................................................ 62
7.33 Other cases of stack unwinding .............................................................................. 66
7.34 Propagation of NAN and INF .................................................................................. 66
7.35 Preprocessing directives ......................................................................................... 67
7.36 Namespaces ........................................................................................................... 67
8 Optimizations in the compiler .......................................................................................... 67
8.1 How compilers optimize ............................................................................................ 67
8.2 Comparison of different compilers............................................................................. 76
8.3 Obstacles to optimization by compiler ....................................................................... 80
8.4 Obstacles to optimization by CPU ............................................................................. 85
8.5 Compiler optimization options ................................................................................... 85
8.6 Optimization directives .............................................................................................. 87
8.7 Checking what the compiler does ............................................................................. 88
9 Optimizing memory access ............................................................................................. 91
9.1 Caching of code and data ......................................................................................... 91
9.2 Cache organization ................................................................................................... 91
9.3 Functions that are used together should be stored together...................................... 92
9.4 Variables that are used together should be stored together ...................................... 93
9.5 Alignment of data ...................................................................................................... 94
9.6 Dynamic memory allocation ...................................................................................... 95
9.7 Data structures and container classes ...................................................................... 97
9.8 Strings .................................................................................................................... 105
9.9 Access data sequentially ........................................................................................ 105
9.10 Cache contentions in large data structures ........................................................... 106
9.11 Explicit cache control ............................................................................................ 108
10 Multithreading .............................................................................................................. 110
10.1 Simultaneous multithreading ................................................................................. 112
11 Out of order execution ................................................................................................. 113
12 Using vector operations ............................................................................................... 115
12.1 AVX instruction set and YMM registers ................................................................. 117
12.2 AVX512 instruction set and ZMM registers ........................................................... 117
12.3 Automatic vectorization ......................................................................................... 118
12.4 Using intrinsic functions ........................................................................................ 121
12.5 Using vector classes ............................................................................................. 125
12.6 Transforming serial code for vectorization ............................................................. 129
12.7 Mathematical functions for vectors ........................................................................ 131
12.8 Aligning dynamically allocated memory................................................................. 133
12.9 Aligning RGB video or 3-dimensional vectors ....................................................... 133
12.10 Conclusion .......................................................................................................... 133
2

13 Making critical code in multiple versions for different instruction sets........................... 135
13.1 CPU dispatch strategies........................................................................................ 135
13.2 Model-specific dispatching .................................................................................... 137
13.3 Difficult cases........................................................................................................ 138
13.4 Test and maintenance .......................................................................................... 139
13.5 Implementation ..................................................................................................... 139
13.6 CPU dispatching at load time in Linux ................................................................... 141
13.7 CPU dispatching in Intel compiler ......................................................................... 143
14 Specific optimization topics ......................................................................................... 144
14.1 Use lookup tables ................................................................................................. 144
14.2 Bounds checking .................................................................................................. 147
14.3 Use bitwise operators for checking multiple values at once................................... 148
14.4 Integer multiplication ............................................................................................. 149
14.5 Integer division...................................................................................................... 150
14.6 Floating point division ........................................................................................... 152
14.7 Do not mix float and double .................................................................................. 153
14.8 Conversions between floating point numbers and integers ................................... 153
14.9 Using integer operations for manipulating floating point variables ......................... 154
14.10 Mathematical functions ....................................................................................... 158
14.11 Static versus dynamic libraries ............................................................................ 158
14.12 Position-independent code.................................................................................. 160
14.13 System programming .......................................................................................... 162
15 Metaprogramming ....................................................................................................... 163
15.1 Template metaprogramming ................................................................................. 163
15.2 Metaprogramming with constexpr branches .......................................................... 166
15.3 Metaprogramming with constexpr functions .......................................................... 167
16 Testing speed.............................................................................................................. 167
16.1 Using performance monitor counters .................................................................... 169
16.2 The pitfalls of unit-testing ...................................................................................... 170
16.3 Worst-case testing ................................................................................................ 171
17 Optimization in embedded systems ............................................................................. 172
18 Overview of compiler options....................................................................................... 174
19 Literature ..................................................................................................................... 178
20 Copyright notice .......................................................................................................... 179

1 Introduction
This manual is for advanced programmers and software developers who want to make theirsoftware faster. It is assumed that the reader has a good knowledge of the C++ programming language and a basic understanding of how compilers work. The C++ language ischosen as the basis for this manual for reasons explained on page 8 below.
This manual is based mainly on my study of how compilers and microprocessors work. The
recommendations are based on the x86 family of microprocessors from Intel, AMD, and VIA
including the 64-bit versions. The x86 processors are used in the most common platforms
with Windows, Linux, BSD, and Mac OS X operating systems, though these operating
systems can also be used with other microprocessors and instruction sets. Many of theadvices may apply to other platforms and other compiled programming languages as well.
This is the first in a series of five manuals:
1. Optimizing software in C++: An optimization guide for Windows, Linux, and Mac
platforms.
2. Optimizing subroutines in assembly language: An optimization guide for x86
platforms.
33. The microarchitecture of Intel, AMD, and VIA CPUs: An optimization guide forassembly programmers and compiler makers.
4. Instruction tables: Lists of instruction latencies, throughputs and micro-operation
breakdowns for Intel, AMD, and VIA CPUs.
5. Calling conventions for different C++ compilers and operating systems.
The latest versions of these manuals are always available from www.agner.org/optimize.
Copyright conditions are listed on page 179 below.
Programmers who are satisfied with making software in a high-level language need only
read this first manual. The subsequent manuals are for those who want to go deeper into
the technical details of instruction timing, assembly language programming, compiler
technology, and microprocessor microarchitecture.
Please note that my optimization manuals are used by thousands of people around theworld. I simply do not have the time to answer questions from everybody. So please do notsend your programming questions to me. You will not get any answer. Beginners areadvised to seek information elsewhere and get a good deal of programming experience
before trying the techniques in the present manual. There are various discussion forums on
the Internet where you can get answers to your programming questions if you cannot findthe answers in the relevant books and manuals.
I want to thank the many people who have sent me corrections and suggestions for my
optimization manuals. I am always happy to receive new relevant information.

1.1 Why software is often slow
It is a paradox that we still have many software products with frustratingly long response
times while microprocessor performance has grown exponentially for decades. In mostcases, the reason for unsatisfactory performance is not poor microprocessor design, butpoor software design. Quite often, the culprit is extremely wasteful software development
tools, frameworks, virtual machines, script languages, and abstract many-layer software
designs. The growth in hardware performance according to Moore's law is slowing down as
we are approaching the limits of what is physically possible. Instead, Wirth's law claims
jokingly that software speed is decreasing more quickly than hardware speed is increasing.
In this situation, software developers are advised to improve the software rather than relying
on still faster microprocessors: Avoid the most wasteful software tools and frameworks, andavoid feature bloat. Reducing the level of abstraction in software development will actually
make it easier to understand the performance consequences of different code constructs,
as explained in this manual.

1.2 The costs of optimizing
University courses in programming nowadays stress the importance of structured andobject-oriented programming, modularity, reusability, multiple layers of abstraction, andsystematization of the software development process. These requirements are often
conflicting with the requirements of optimizing the software for speed or size.
Today, it is not uncommon for software teachers to recommend that no function or methodshould be longer than a few lines. A few decades ago, the recommendation was the
opposite: Do not put something in a separate subroutine if it is only called once. The
reasons for this shift in software writing style are that software projects have become bigger
and more complex, that there is more focus on the costs of software development, and thatcomputers have become more powerful.
4The high priority of structured software development and the low priority of program
efficiency is reflected, first and foremost, in the choice of programming language andinterface frameworks. This is often a disadvantage for the end user who has to invest in
ever more powerful computers to keep up with the ever bigger software packages and who
is still frustrated by unacceptably long response times, even for simple tasks.
Sometimes, it is necessary to compromise on the advanced principles of software development in order to make software packages faster and smaller. This manual discusses how tomake a sensible balance between these considerations. It is discussed how to identify andisolate the most critical part of a program and concentrate the optimization effort on thatparticular part. It is discussed how to overcome the dangers of a relatively primitive
programming style that does not automatically check for array bounds violations, invalid
pointers, etc. And it is discussed which of the advanced programming constructs are costly
and which ones are cheap, in relation to execution time.

2 Choosing the optimal platform
2.1 Choice of hardware platform
The choice of hardware platform has become less important than it used to be. The
distinctions between RISC and CISC processors, between PC's and mainframes, andbetween simple processors and vector processors are becoming increasingly blurred as thestandard PC processors with CISC instruction sets have got RISC cores, vector processing
instructions, multiple cores, and a processing speed exceeding that of yesterday's big
mainframe computers.
Today, the choice of a hardware platform for a given task is often determined byconsiderations such as price, compatibility, second source, and the availability of gooddevelopment tools, rather than by the processing power. Connecting several standard PC'sin a network may be both cheaper and more efficient than investing in a big mainframe
computer. Big supercomputers with massively parallel vector processing capabilities still
have a niche in scientific computing, but for most purposes the standard PC processors are
preferred because of their superior performance/price ratio.
The CISC instruction set (x86) of the standard PC processors is not optimal from a technological point of view. This instruction set is maintained for the sake of backwards
compatibility with a lineage of software that dates back to around 1980 where RAM memoryand disk space were scarce resources. However, the CISC instruction set is better than its
reputation. The compactness of the code makes caching more efficient today where cache
size is a limited resource. The CISC instruction set may actually be better than RISC in
situations where code caching is critical. A further advantage of the CISC instruction set isthat it needs fewer instructions to do the same job. A serious problem with the original x86
instruction set is the scarcity of registers. This problem has been alleviated in the 64-bit
extension to the x86 instruction set where the number of general purpose registers has
been doubled, and the number of vector register has been quadrupled with the AVX512
instruction set extension.
Thin clients that depend on network resources are not recommended for critical applications
because the response times for network resources cannot be controlled.
Small hand-held devices are becoming more popular and used for an increasing number of
purposes such as email and web browsing that previously required a PC. Similarly, we areseeing an increasing number of devices and machines with embedded microcontrollers. I
am not making any specific recommendation about which platforms and operating systems
are most efficient for such applications, but it is important to realize that such devices
5typically have much less memory and computing power than PCs. Therefore, it is evenmore important to economize the resource use on such systems than it is on a PC platform.
However, with a well optimized software design, it is possible to get a good performance formany applications even on such small devices, as discussed on page 172.
This manual is based on the standard PC platform with an Intel, AMD, or VIA processor anda Windows, Linux, BSD, or Mac operating system running in 32-bit or 64-bit mode. Much of
the advice given here may apply to other platforms as well, but the examples have beentested only on PC platforms.

Graphics accelerators
The choice of platform is obviously influenced by the requirements of the task in question.
For example, a heavy graphics application is preferably implemented on a platform with agraphics coprocessor or graphics accelerator card. Some systems also have a dedicatedphysics processor for calculating the physical movements of objects in a computer game oranimation.
It is possible in some cases to use the high processing power of the processors on agraphics accelerator card for other purposes than rendering graphics on the screen.
However, such applications are highly system dependent and therefore not recommended if
portability is important. This manual does not cover graphics processors.

Programmable logic devices
A programmable logic device is a chip that can be programmed in a hardware definition
language, such as Verilog or VHDL. Common devices are CPLDs and FPGAs. The
difference between a software programming language, e.g. C++, and a hardware definition
language is that the software programming language defines an algorithm of sequential
instructions, where a hardware definition language defines hardware circuits consisting ofdigital building blocks such as gates, flip-flops, multiplexers, arithmetic units, etc. and thewires that connect them. The hardware definition language is inherently parallel because it
defines electrical connections rather than sequences of operations.
A complex digital operation can often be executed faster in a programmable logic device
than in a microprocessor because the hardware can be wired for a specific purpose.
It is possible to implement a microprocessor in an FPGA as a so-called soft processor. Sucha soft processor is much slower than a dedicated microprocessor and therefore notadvantageous by itself. But a solution where a soft processor activates critical applicationspecific instructions that are coded in a hardware definition language in the same chip can
be a very efficient solution in some cases. An even more powerful solution is the
combination of a dedicated microprocessor core and an FPGA in the same chip. Suchhybrid solutions are now used in some embedded systems.
A look in my crystal ball reveals that similar solutions may someday be implemented in PC
processors. The application program will be able to define application-specific instructions
that can be coded in a hardware definition language. Such a processor will have an extra
cache for the hardware definition code in addition to the code cache and the data cache.

2.2 Choice of microprocessor
The benchmark performance of competing brands of microprocessors are very similar
thanks to heavy competition. Processors with multiple cores are advantageous forapplications that can be divided into multiple threads that run in parallel. Small lightweightprocessors with low power consumption are actually quite powerful and may be sufficient forless intensive applications.

62.3 Choice of operating system
All newer microprocessors in the x86 family can run in both 16-bit, 32-bit, and 64-bit mode.
16-bit mode was used in the old operating systems DOS and Windows 3.x. These systems
use segmentation of the memory if the size of program or data exceeds 64 kbytes. This isquite inefficient for bigger programs. The modern microprocessors are not optimized for 16bit mode and some operating systems are not backwards compatible with 16-bit programs.
It is not recommended to make 16-bit programs, except for small embedded systems.
64-bit operating systems are common today. These system are capable of running both 32bit and 64-bit programs. The 64-bit systems can improve the performance by 5-10% forsome CPU-intensive applications with many function calls and for applications that use
large amounts of RAM memory. If the bottleneck is elsewhere, then there is no difference inperformance between 32-bit and 64-bit systems. Applications that use large amounts ofmemory will benefit from the larger address space of the 64-bit systems.
The Windows and Linux operating systems give almost identical performance for 32-bit
software because the two operating systems are using the same function calling
conventions. Everything that is said here about Linux also applies to BSD systems.
The Intel-based Mac OS X operating system is based on BSD, but the compiler uses
position-independent code and lazy binding by default, which makes it less efficient. The
performance can be improved by using static linking and by not using position-independentcode (option -fno-pic).
64 bit systems have several advantages over 32 bit systems:•

The number of registers is doubled. This makes it possible to store intermediate data
and local variables in registers rather than in memory. The number of vector registers in
64-bit mode is even larger if the AVX512 instruction set is enabled.

•

Function parameters are transferred in registers rather than on the stack. This makes
function calls more efficient.

•

The size of the integer registers is extended to 64 bits. This is an advantage inapplications that use 64-bit integers.

•

The allocation and deallocation of big memory blocks is more efficient.

•

The SSE2 instruction set is supported on all 64-bit CPUs and operating systems.

•

The 64 bit instruction set supports self-relative addressing of data. This makes positionindependent code more efficient.

64 bit systems have the following disadvantages compared to 32 bit systems:•

Pointers, references, and stack entries use 64 bits rather than 32 bits. This makes datacaching less efficient.

•

Access to static or global arrays require a few extra instructions for address calculationin 64 bit mode if the image base is not guaranteed to be less than 231.

•

Address calculation is more complicated in a large memory model where the combined
size of code and data can exceed 2 Gbytes. This large memory model is hardly everused, though.

7•Some instructions are one byte longer in 64 bit mode than in 32 bit mode.

In general, you can expect 64-bit programs to run a little faster than 32-bit programs if there
are many function calls, if there are many allocations of large memory blocks, or if the
program can take advantage of 64-bit integer calculations. It is necessary to use 64-bit
systems if the program uses more than 2 gigabytes of data.
The similarity between the operating systems disappears when running in 64-bit mode
because the function calling conventions are different. 64-bit Windows allows only fourfunction parameters to be transferred in registers, whereas 64-bit Linux, BSD and Mac allow
up to fourteen parameters to be transferred in registers (6 integer and 8 floating point).
There are also other details that make function calling more efficient in 64-bit Linux than in
64-bit Windows (See page 49 and manual 5: "Calling conventions for different C++
compilers and operating systems"). An application with many function calls may run slightly
faster in 64-bit Linux than in 64-bit Windows. The disadvantage of 64-bit Windows may be
mitigated by making critical functions inline or static or by using a compiler that can dowhole program optimization.

2.4 Choice of programming language
Before starting a new software project, it is important to decide which programming
language is best suited for the project at hand. Low-level languages are good for optimizing
execution speed or program size, while high-level languages are good for making clear andwell-structured code and for fast and easy development of user interfaces and interfaces to
network resources, databases, etc.
The efficiency of the final application depends on the way the programming language is
implemented. The highest efficiency is obtained when the code is compiled and distributed
as binary executable code. Most implementations of C++, Pascal, and Fortran are based oncompilers.
Several other programming languages are implemented with interpretation. The programcode is distributed as it is and interpreted line by line when it is run. Examples include
JavaScript, PHP, ASP, and UNIX shell script. Interpreted code is very inefficient because
the body of a loop is interpreted again and again for every iteration of the loop.
Some implementations use just-in-time compilation. The program code is distributed andstored as it is, and is compiled when it is executed. An example is Perl.
Several modern programming languages use an intermediate code (byte code). The sourcecode is compiled into an intermediate code, which is the code that is distributed. Theintermediate code cannot be executed as it is, but must go through a second step ofinterpretation or compilation before it can run. Some implementations of Java are based on
an interpreter which interprets the intermediate code by emulating the so-called Java virtual
machine. The best Java machines use just-in-time compilation of the most used parts of thecode. C#, managed C++, and other languages in Microsoft's .NET framework are based onjust-in-time compilation of an intermediate code.
The reason for using an intermediate code is that it is intended to be platform-independentand compact. The biggest disadvantage of using an intermediate code is that the user must
install a large runtime framework for interpreting or compiling the intermediate code. This
framework typically uses much more resources than the code itself.
Another disadvantage of intermediate code is that it adds an extra level of abstraction whichmakes detailed optimization more difficult. On the other hand, a just-in-time compiler can
optimize specifically for the CPU it is running on, while it is more complicated to make CPUspecific optimizations in precompiled code.
8The history of programming languages and their implementations reveal a zigzag coursethat reflects the conflicting considerations of efficiency, platform independence, and easy
development. For example, the first PC's had an interpreter for Basic. A compiler for Basicsoon became available because the interpreted version of Basic was too slow. Today, themost popular version of Basic is Visual Basic .NET, which is implemented with anintermediate code and just-in-time compilation. Some early implementations of Pascal used
an intermediate code like the one that is used for Java today. But this language gained
remarkably in popularity when a genuine compiler became available.
It should be clear from this discussion that the choice of programming language is acompromise between efficiency, portability, and development time. Interpreted languages
are out of the question when efficiency is important. A language based on intermediate codeand just-in-time compilation may be a viable compromise when portability and ease ofdevelopment are more important than speed. This includes languages such as C#, Visual
Basic .NET and the best Java implementations. However, these languages have thedisadvantage of a very large runtime framework that must be loaded every time the programis run. The time it takes to load the framework and compile the program are often much
more than the time it takes to execute the program. The runtime framework may use moreresources than the program itself when running. Programs using such a frameworksometimes have unacceptably long response times for simple tasks like pressing a button
or moving the mouse. The .NET framework should definitely be avoided when speed is
critical.
The fastest execution is no doubt obtained with a fully compiled code. Compiled languages
include C, C++, D, Pascal, Fortran, and several other less well-known languages. My
preference is for C++ for several reasons. C++ is supported by some very good compilers
and optimized function libraries. C++ is an advanced high-level language with a wealth ofadvanced features rarely found in other languages. But the C++ language also includes the
low-level C language as a subset, giving access to low-level optimizations. Most C++
compilers are able to generate an assembly language output, which is useful for checking
how well the compiler optimizes a piece of code. Furthermore, most C++ compilers allow
assembly-like intrinsic functions, inline assembly, or easy linking to assembly language
modules when the highest level of optimization is needed. The C++ language is portable inthe sense that C++ compilers exist for all major platforms. Pascal has many of theadvantages of C++ but is not quite as versatile. Fortran is also quite efficient, but the syntax
is very old-fashioned.
Development in C++ is quite efficient thanks to the availability of powerful development
tools. One popular development tool is Microsoft Visual Studio. This tool can make twodifferent implementations of C++, directly compiled code and intermediate code for thecommon language runtime of the .NET framework. Obviously, the directly compiled version
is preferred when speed is important.
An important disadvantage of C++ relates to security. There are no checks for array bounds
violation, integer overflow, and invalid pointers. The absence of such checks makes thecode execute faster than other languages that do have such checks. But it is the responsibility of the programmer to make explicit checks for such errors in cases where they cannot
be ruled out by the program logic. Some guidelines are provided below, on page 14.
C++ is definitely the preferred programming language when the optimization of performance
has high priority. The gain in performance over other programming languages can be quite
substantial. This gain in performance can easily justify a possible minor increase in development time when performance is important to the end user.
There may be situations where a high level framework based on intermediate code isneeded for other reasons, but part of the code still needs careful optimization. A mixed
implementation can be a viable solution in such cases. The most critical part of the code9can be implemented in compiled C++ and the rest of the code, including user interface etc.,
can be implemented in the high level framework. The optimized part of the code canpossibly be compiled as a dynamic link library (DLL) or shared object which is called by therest of the code. This is not an optimal solution because the high level framework stillconsumes a lot of resources, and the transitions between the two kinds of code gives anextra overhead. But this solution can still give a considerable improvement in performance if
the time-critical part of the code can be completely contained in a compiled DLL or sharedobject.

2.5 Choice of compiler
C++ compilers have become more and more complicated in recent decades. With the
AVX512 instruction set extensions for advanced vector operations, we now have thousandsof different machine instructions. Many new advanced features in the C++ language
standard have also added to the complexity. In the same period, the number of C++
compilers on the market has decreased, and some old compilers that were popular in thepast are now either obsolete or discontinued. I suspect that the number of compilers on themarket may decrease further in the future.
Some of the best up-to-date compilers for x86 and x86-64 platforms are mentioned below.
These compilers all support 32-bit and 64-bit code and automatic vectorization.

Microsoft Visual StudioThis is a very user friendly compiler with many features and a good debugger. The fullversion is very expensive, but a limited non-commercial version is available for free. Visual
Studio can build code with various programming languages (C++, C#, Visual Basic) for
various platforms. Visual Studio is not the best compiler in terms of code optimization, but it
can be very useful during the development phase of a project. You may use a different
compiler for the release version of your code if performance is important. It is possible touse the Visual Studio integrated development environment (IDE) with a different compiler.
The plugin for using the Clang compiler can be recommended

Gnu
The gcc or Gnu compiler is among the best optimizing compilers available. It is less user
friendly than Visual Studio, and sometimes you have to be a real nerd to understand themany command line options. This compiler is free and open source, and it is available foralmost any platform. The stand-alone compiler is run from the command line, but several
IDE's (integrated development environments) are available, including Eclipse, NetBeans,
CodeBlocks, and many more.

Clang
The Clang compiler is a very good optimizing compiler. In many cases, it is actually the best
one. Clang is very similar to the Gnu compiler with the same features and options. Clang is
the most common compiler on the x86-based Mac platform, but it also supports Linux andWindows platforms.
The Clang compiler for Windows comes in different versions. The best version is currently
the one that is available as a plugin to Microsoft Visual Studio version 17 or later.
The Cygwin64 version of Clang is using a medium memory model by default. This is quite
wasteful because it will use 64-bit absolute addresses rather than 32-bit relative addresses
for static variables and constants. You can improve performance byspecifying -mcmodel=small. The medium memory model is needed only if you are making
a direct link to a variable inside an external DLL (which is bad programming practice
anyway). Another disadvantage of the Cygwin version is that you have to include the
Cygwin DLL when distributing the executable.
10Intel C++ compiler
This compiler does not have its own IDE. It is intended as a plug-in to Microsoft Visual
Studio when compiling for Windows or to Eclipse when compiling for Linux. It can also be
used as a stand-alone compiler when called from a command line or a make utility. It
supports Windows and Linux as well as Intel-based Mac OS.
The Intel compiler comes in two versions: a legacy version named “Classic” and a newversion named Intel oneAPI LLVM-based compiler. The latter version is a forking of theClang compiler and behaves very much like Clang. It can optimize much better than theClassic version.
The “Classic” Intel compiler supports automatic CPU dispatching to make multiple code
versions for different Intel CPUs. The most important disadvantage of the Classic Intel
compiler is that the code is optimized for a specific Intel CPU. The compiled code checks
whether it is running on an Intel CPU. If another brand of CPU (AMD or VIA) is detected,
then it will run a different branch of the code with reduced speed, even if the optimal code
branch is compatible with the processor it is running on.

Comments
The open source compilers - Clang and Gnu - are the best ones in terms of code
optimization according to my tests. See page 76 for a detailed test of the compilers
mentioned above.
All of the compilers can be used as command-line versions without an IDE. Free trial
versions or reduced versions are available for the commercial compilers.
Mixing object files from different compilers is often possible if they have been compiled forthe same platform and with similar options. The Intel compiler produces object files that arecompatible with either Microsoft Visual Studio or the Gnu compiler, depending on theplatform. Object files from Gnu and Clang compilers are also mutually compatible. Other
combinations of compilers require that the linking functions are declared extern "C".
Some older compilers use a different object file format that is not compatible with thecompilers mentioned above.
The choice of compiler may in some cases be determined by the requirements of compatibility with legacy code, specific preferences for the IDE, for debugging facilities, easy GUI
development, database integration, web application integration, mixed language programming, etc. It may be useful to make the most critical modules with a different compiler incases where the chosen compiler does not provide the best optimization. If the main projectis written in a different programming language, then it may be necessary to put the
optimized C++ code in a dynamic link library (.DLL or .so). Otherwise, static linking ispreferred.

2.6 Choice of function libraries
Some applications spend most of their execution time on executing library functions. Timeconsuming library functions often belong to one of these categories:•

File input/output•

Graphics and sound processing•

Memory and string manipulation•

Mathematical functions•

Encryption, decryption, data compression
11

Most compilers include standard libraries for many of these purposes. Unfortunately, thestandard libraries are not always fully optimized.
Library functions are typically small pieces of code that are used by many users in manydifferent applications. Therefore, it is worthwhile to invest more efforts in optimizing libraryfunctions than in optimizing application-specific code. The best function libraries are highly
optimized, using automatic CPU-dispatching (see page 135) for the latest instruction set
extensions.
If a profiling (see page 16) shows that a particular application uses a lot of CPU-time inlibrary functions, or if this is obvious, then it may be possible to improve the performance
significantly simply by using a different function library. If the application uses most of its
time in library functions then it may not be necessary to optimize anything else than finding
the most efficient library and economize the library function calls. It is recommended to try
different libraries and see which one works best.
Some common function libraries are discussed below. Many libraries for special purposes
are also available.

Microsoft
Comes with Microsoft compiler. Some functions are optimized well, others are not. Supports
32-bit and 64-bit Windows.

Gnu
Comes with the Gnu compiler. The 64-bit version is better than the 32-bit version. The Gnu
compiler often inserts built-in code instead of the most common memory and string
instructions. The built-in code is not always optimal. Use option -fno-builtin to getlibrary versions instead.

Mac
The libraries included with the Gnu compiler for Mac OS X (Darwin) are part of the Xnu
project. Some of the most important functions are included in the operating system kernel inthe so-called commpage. These functions are highly optimized for the Intel Core and later
Intel processors. AMD processors and earlier Intel processors are not supported at all. Can
only run on Mac platform.

Intel
The Intel compiler includes standard function libraries. Several special purpose libraries arealso available, such as the "Intel Math Kernel Library" and "Integrated Performance
Primitives". These function libraries are highly optimized for large data sets. The Intellibraries are optimized for best performance on Intel processors, but they often givesatisfactory performance on AMD processors as well, unless they are used with the“Classic” version of the Intel compiler. See page 143 for a discussion of the use of Intel
compilers and libraries on non-Intel processors.

AMD
AMD core Math library contains optimized mathematical functions. It also works on Intelprocessors. The performance is inferior to the Intel libraries.

Asmlib
My own function library made for demonstration purposes. Available fromwww.agner.org/optimize/asmlib.zip. Currently includes optimized versions of memory andstring functions and some other functions that are difficult to find elsewhere. Faster thanmany other libraries when running on the newest processors. Supports all x86 and x86-64
platforms.
12Comparison of function libraries
AsmlibGnu 64 bit-fno-builtin

Gnu 32-bit-fno-builtin

Gnu 32-bit

Mac

Intel

CodeGearMicrosoft

Test
Processor
memcpy 16kB
Intel
0.12 0.18 0.12 0.11 0.18 0.18 0.18 0.11Core2alignedoperands
memcpy 16kB
Intel
0.63 0.75 0.18 0.11 1.21 0.57 0.44 0.12
Core 2
unaligned op.
memcpy 16kB
AMD
0.24 0.25 0.24 n.a.
1.00 0.25 0.28 0.22
Opteron K8alignedoperands
memcpy 16kB
AMD
0.38 0.44 0.40 n.a.
1.00 0.35 0.29 0.28
Opteron K8
unaligned op.
strlen 128
Intel0.77 0.89 0.40 0.30 4.5
0.82 0.59 0.27
Core
2
bytes
strlen 128
AMD
1.09 1.25 1.61 n.a.
2.23 0.95 0.6
1.19
Opteron K8
bytes
Table 2.1. Comparing performance of different function libraries.
Numbers in the table are core clock cycles per byte of data (low numbers mean good
performance). Aligned operands means that source and destination both have addresses
divisible by 16.
Library versions tested (not up to date!):Microsoft Visual studio 2008, v. 9.0
CodeGear Borland bcc, v. 5.5
Mac: Darwin8 g++ v 4.0.1.
Gnu: Glibc v. 2.7, 2.8.
Asmlib: v. 2.00.
Intel C++ compiler, v. 10.1.020. Functions _intel_fast_memcpy and__intel_new_strlen in library libircmt.lib. Function names are undocumented.

2.7 Choice of user interface framework
Most of the code in a typical software project goes to the user interface. Applications thatare not computationally intensive may very well spend more CPU time on the user interface
than on the essential task of the program.
Application programmers rarely program their own graphical user interfaces from scratch.
This would not only be a waste of the programmers' time, but also inconvenient to the end
user. Menus, buttons, dialog boxes, etc. should be as standardized as possible for usability
reasons. The programmer can use standard user interface elements that come with theoperating system or libraries that come with compilers and development tools.
A popular user interface library for Windows and C++ is Microsoft Foundation Classes(MFC). Several graphical interface frameworks are available for Linux systems. The userinterface library can be linked either as a runtime DLL or a static library. A runtime DLL
takes more memory resources than a static library, except when several applications use
the same DLL at the same time.
A user interface library may be bigger than the application itself and take more time to load.
A light-weight alternative is the Windows Template Library (WTL). A WTL application is13generally faster and more compact than an MFC application. The development time for
WTL applications can be expected to be higher due to poor documentation and lack ofadvanced development tools.
There are several cross-platform user interface libraries, such as Qt and wxWidgets. These
are useful for applications that may run on multiple platforms and operating systems.
The simplest possible user interface is obtained by dropping the graphical user interface
and making a console mode program. The inputs for a console mode program are typicallyspecified on a command line or an input file. The output goes to the console or to an output
file. A console mode program is fast, compact, and simple to develop. It is easy to port todifferent platforms because it does not depend on system-specific graphical interface calls.
The usability may be poor because it lacks the self-explaining menus of a graphical userinterface. A console mode program is useful for calling from other applications such as amake utility.
The conclusion is that the choice of user interface framework must be a compromise
between development time, usability, program compactness, and execution time. No
universal solution is best for all applications.

2.8 Overcoming the drawbacks of the C++ language
While C++ has many advantages when it comes to optimization, it does have somedisadvantages that make developers choose other programming languages. This section
discusses how to overcome these disadvantages when C++ is chosen for the sake of
optimization.

Portability
C++ is fully portable in the sense that the syntax is fully standardized and supported on all
major platforms. However, C++ is also a language that allows direct access to hardwareinterfaces and system calls. These are of course system-specific. In order to facilitate
porting between platforms, it is recommended to place the user interface and other systemspecific parts of the code in a separate module, and to put the task-specific part of the code,
which supposedly is system-independent, in another module.
The size of integers and other hardware-related details depend on the hardware platform
and operating system. See page 29 for details.

Development time
Some developers feel that a particular programming language and development tool isfaster to use than others. While some of the difference is simply a matter of habit, it is truethat some development tools have powerful facilities that do much of the trivial programming
work automatically. The development time and maintainability of C++ projects can be
improved by consistent modularity and reusable classes.

Security
The most serious problem with the C++ language relates to security. Standard C++ implementations have no checking for array bounds violations and invalid pointers. This is afrequent source of errors in C++ programs and also a possible point of attack for hackers. It
is necessary to adhere to certain programming principles in order to prevent such errors inprograms where security matters.
Problems with invalid pointers can be avoided by using references instead of pointers, byinitializing pointers to zero, by setting pointers to zero whenever the objects they point tobecome invalid, and by avoiding pointer arithmetics and pointer type casting. Linked lists
and other data structures that typically use pointers may be replaced by more efficient
container class templates, as explained on page 97. Avoid the function scanf.
14Violation of array bounds is probably the most common cause of errors in C++ programs.
Writing past the end of an array can cause other variables to be overwritten, and evenworse, it can overwrite the return address of the function in which the array is defined. Thiscan cause all kinds of strange and unexpected behaviors. Arrays are often used as buffers
for storing text or input data. A missing check for buffer overflow on input data is a common
error that hackers often have exploited.
A good way to prevent such errors is to replace arrays by container classes. The efficiency
of different container classes is discussed on page 97.
Text strings are particularly problematic because there may be no certain limit to the lengthof a string. The old C-style method of storing strings in character arrays is fast and efficient,but not safe unless the length of each string is checked before storing. The standard
solution to this problem is to use string classes, such as string or CString. This is safeand flexible, but quite inefficient in large applications. The string classes allocate a newmemory block every time a string is created or modified. This can cause the memory to be
fragmented and involve a high overhead cost of heap management and garbage collection.
A more efficient solution that does not compromise safety is to store all strings in one
memory pool. See the examples in the appendix at
www.agner.org/optimize/cppexamples.zip for how to store strings in a memory pool.
Integer overflow is another security problem. The official C standard says that the behaviorof signed integers in case of overflow is “undefined”. This allows the compiler to ignore
overflow or assume that it does not occur. In the case of the Gnu compiler, the assumptionthat signed integer overflow does not occur has the unfortunate consequence that it allows
the compiler to optimize away an overflow check. There are a number of possible remedies
against this problem: (1) check for overflow before it occurs, (2) use unsigned integers they are guaranteed to wrap around, (3) trap integer overflow with the option -ftrapv, but
this is extremely inefficient, (4) get a compiler warning for such optimizations with
option -Wstrict-overflow=2, or (5) make the overflow behavior well-defined with
option -fwrapv or -fno-strict-overflow.
You may deviate from the above security advices in critical parts of the code where speed isimportant. This can be permissible if the unsafe code is limited to well-tested functions,classes, templates or modules with a well-defined interface to the rest of the program.

3 Finding the biggest time consumers
3.1 How much is a clock cycle?
In my manuals, I am using CPU clock cycles rather than seconds or microseconds as a timemeasure. This is because computers have very different speeds. If I write that somethingtakes 10 μs today, then it may take only 5 μs on the next generation of computers and mymanual will soon be obsolete. But if I write that something takes 10 clock cycles then it will
still take 10 clock cycles even if the CPU clock frequency is doubled.
The length of a clock cycle is the reciprocal of the clock frequency. For example, if the clock
frequency is 4 GHz then the length of a clock cycle is1= 0.25ns.
4 GHz
Assume that a loop in a program repeats 1000 times and that there are 100 floating point
operations (addition, multiplication, etc.) inside the loop. If each floating point operation
takes 5 clock cycles, then we can roughly estimate that the loop will take 1000 * 100 * 5 *0.25 ns = 125 μs on a 4 GHz CPU. Should we try to optimize this loop? Certainly not! 12515

μs is less than 1% of the time it takes to refresh the screen. There is no way the user can
see the delay. But if the loop is inside another loop that also repeats 1000 times then we
have an estimated execution time of 125 ms. This delay is just long enough to be noticeable
but not long enough to be annoying. We may decide to do some measurements to see if ourestimate is correct or if the calculation time is actually more than 125 ms. If the responsetime is so long that the user actually has to wait for a result then we will consider if there issomething that can be improved.

3.2 Use a profiler to find hot spots
Before you start to optimize anything, you have to identify the critical parts of the program.
In some programs, more than 99% of the time is spent in the innermost loop doing
mathematical calculations. In other programs, 99% of the time is spent on reading andwriting data files while less than 1% goes to actually doing something on these data. It isvery important to optimize the parts of the code that matters rather than the parts of thecode that use only a small fraction of the total time. Optimizing less critical parts of the code
will not only be a waste of time, it also makes the code less clear and more difficult to debug
and maintain.
Most compiler packages include a profiler that can tell how many times each function iscalled and how much time it uses. There are also third-party profilers such as AQtime, Intel
VTune and AMD CodeAnalyst.
There are several different profiling methods:•

Instrumentation: The compiler inserts extra code at each function call to count how
many times the function is called and how much time it takes.

•

Debugging. The profiler inserts temporary debug breakpoints at every function or every
code line.

•

Time-based sampling: The profiler tells the operating system to generate an interrupt,
e.g. every millisecond. The profiler counts how many times an interrupt occurs in eachpart of the program. This requires no modification of the program under test, but is lessreliable.

•

Event-based sampling: The profiler tells the CPU to generate interrupts at certain
events, for example every time a thousand cache misses have occurred. This makes it
possible to see which part of the program has most cache misses, branchmispredictions, floating point exceptions, etc. Event-based sampling requires a CPUspecific profiler. For Intel CPUs use Intel VTune, for AMD CPUs use AMD CodeAnalyst.

Unfortunately, profilers are often unreliable. They sometimes give misleading results or failcompletely because of technical problems.
Some common problems with profilers are:•Coarse time measurement. If time is measured with millisecond resolution and thecritical functions take microseconds to execute then measurements can become
imprecise or simply zero.

•

Execution time too small or too long. If the program under test finishes in a short timethen the sampling generates too little data for analysis. If the program takes too longtime to execute then the profiler may sample more data than it can handle.

•

Waiting for user input. Many programs spend most of their time waiting for user input or
network resources. This time is included in the profile. It may be necessary to modify the16program to use a set of test data instead of user input in order to make profiling feasible.
•

Interference from other processes. The profiler measures not only the time spent in the
program under test but also the time used by all other processes running on the same
computer, including the profiler itself.

•

Function addresses are obscured in optimized programs. The profiler identifies any hot
spots in the program by their address and attempts to translate these addresses tofunction names. But a highly optimized program is often reorganized in such a way that
there is no clear correspondence between function names and code addresses. The
names of inlined functions may not be visible at all to the profiler. The result will be
misleading reports of which functions take most time.

•

Uses debug version of the code. Some profilers require that the code you are testing
contains debug information in order to identify individual functions or code lines. The
debug version of the code is not optimized.

•

Jumps between CPU cores. A process or thread does not necessarily stay in the sameprocessor core on multi-core CPUs, but event-counters do. This results in meaninglessevent counts for threads that jump between multiple CPU cores. You may need to lock athread to a specific CPU core by setting a thread affinity mask.

•

Poor reproducibility. Delays in program execution may be caused by random events thatare not reproducible. Such events as task switches and garbage collection can occur at
random times and make parts of the program appear to take longer time than normally.

There are various alternatives to using a profiler. A simple alternative is to run the programin a debugger and press break while the program is running. If there is a hot spot that uses
90% of the CPU time then there is a 90% chance that the break will occur in this hot spot.
Repeating the break a few times may be enough to identify a hot spot. Use the call stack inthe debugger to identify the circumstances around the hot spot.
Sometimes, the best way to identify performance bottlenecks is to put measurement
instruments directly into the code rather than using a ready-made profiler. This does not
solve all the problems associated with profiling, but it often gives more reliable results. If you
are not satisfied with the way a profiler works then you may put the desired measurement
instruments into the program itself. You may add counter variables that count how manytimes each part of the program is executed. Furthermore, you may read the time before andafter each of the most important or critical parts of the program to measure how much timeeach part takes. See page 167 for further discussion of this method.
Your measurement code should have #if directives around it so that it can be disabled in
the final version of the code. Inserting your own profiling instruments in the code itself is avery useful way to keep track of the performance during the development of a program.
The time measurements may require a very high resolution if time intervals are short. In
Windows, you can use the GetTickCount or QueryPerformanceCounter functions formillisecond resolution. A much higher resolution can be obtained with the time stamp
counter in the CPU, which counts at the CPU clock frequency (_rdtsc() or __rdtsc()).
The time stamp counter becomes invalid if a thread jumps between different CPU cores.
You may have to fix the thread to a specific CPU core during time measurements to avoid
this. (In Windows, SetThreadAffinityMask, in Linux, sched_setaffinity).
The program should be tested with a realistic set of test data. The test data should contain atypical degree of randomness in order to get a realistic number of cache misses and branchmispredictions.
17When the most time-consuming parts of the program have been found, then it is importantto focus the optimization efforts on the time consuming parts only. Critical pieces of code
can be further tested and investigated by the methods described on page 167.
A profiler is most useful for finding problems that relate to CPU-intensive code. But many
programs use more time loading files or accessing databases, network and other resources
than doing arithmetic operations. The most common time-consumers are discussed in thefollowing sections.

3.3 Program installation
The time it takes to install a program package is not traditionally considered a software
optimization issue. But it is certainly something that can steal the user's time. The time ittakes to install a software package and make it work cannot be ignored if the goal ofsoftware optimization is to save time for the user. With the high complexity of modern
software, it is not unusual for the installation process to take more than an hour. Neither is it
unusual that a user has to reinstall a software package several times in order to find andresolve compatibility problems.
Software developers should take installation time and compatibility problems into accountwhen deciding whether to base a software package on a complex framework requiring
many files to be installed.
The installation process should always use standardized installation tools. It should bepossible to select all installation options at the start so that the rest of the installation
process can proceed unattended. Uninstallation should also proceed in a standardized
manner.

3.4 Automatic updates
Many software programs automatically download updates through the Internet at regulartime intervals. Some programs search for updates every time the computer starts up, even if
the program is never used. A computer with many such programs installed can take several
minutes to start up, which is a total waste of the user's time. Other programs spend time
searching for updates each time the program starts. The user may not need the updates if
the current version satisfies the user's needs. The search for updates should be optionaland off by default unless there is a compelling security reason for updating. The updateprocess should run in a low priority thread, and only if the program is actually used. No
program should leave a background process running when it is not in use. The installationof downloaded program updates should be postponed until the program is shut down andrestarted anyway.
Updates to the operating system can be particularly time consuming. Sometimes it takes
hours to install automatic updates to the operating system. This is very problematic because
these time-consuming updates may come unpredictably at inconvenient times. This can bea very big problem if the user has to turn off or log off the computer for security reasons
before leaving their workplace and the system forbids the user to turn off the computer
during the update process.

3.5 Program loading
Often, it takes more time to load a program than to execute it. The load time can be
annoyingly high for programs that are based on big runtime frameworks, intermediate code,
interpreters, just-in-time compilers, etc., as is commonly the case with programs written in
Java, C#, Visual Basic, etc.
18

But program loading can be a time-consumer even for programs implemented in compiled
C++. This typically happens if the program uses a lot of runtime DLL's (dynamically linked
libraries or shared objects), resource files, configuration files, help files and databases. The
operating system may not load all the modules of a big program when the program starts
up. Some modules may be loaded only when they are needed, or they may be swapped tothe hard disk if the RAM size is insufficient.
The user expects immediate responses to simple actions like a key press or mouse move. It
is unacceptable to the user if such a response is delayed for several seconds because it
requires the loading of modules or resource files from disk. Memory-hungry applications
force the operating system to swap memory to disk. Memory swapping is a frequent causeof unacceptably long response times to simple things like a mouse move or key press.
Avoid an excessive number of DLLs, configuration files, resource files, help files etc.
scattered around on the hard disk. A few files, preferably in the same directory as theexecutable file, is acceptable.

3.6 Dynamic linking and position-independent code
Function libraries can be implemented either as static link libraries (*.lib, *.a) or dynamic
link libraries, also called shared objects (*.dll, *.so). There are several factors that canmake dynamic link libraries slower than static link libraries. These factors are explained indetail on page 158 below.
Position-independent code is used in shared objects in Unix-like systems. Mac systems
often use position-independent code everywhere by default. Position-independent code isinefficient, especially in 32-bit mode, for reasons explained on page 158 below.

3.7 File access
Reading or writing a file on a hard disk often takes much more time than processing thedata in the file, especially if the user has a virus scanner that scans all files on access.
Sequential forward access to a file is faster than random access. Reading or writing big
blocks is faster than reading or writing a small bit at a time. Do not read or write less than a
few kilobytes at a time.
You may mirror the entire file in a memory buffer and read or write it in one operation rather
than reading or writing small bits in a non-sequential manner.
It is usually much faster to access a file that has been accessed recently than to access it
the first time. This is because the file has been copied to the disk cache.
Files on remote or removable media such as USB sticks may not be cached. This can havequite dramatic consequences. I once made a Windows program that created a file by calling
WritePrivateProfileString, which opens and closes the file for each line written. This
worked sufficiently fast on a hard disk because of disk caching, but it took several minutes
to write the file to a floppy disk.
A big file containing numerical data is more compact and efficient if the data are stored inbinary form than if the data are stored in ASCII form. A disadvantage of binary data storageis that it is not human readable and not easily ported to systems with big-endian storage.
Optimizing file access is more important than optimizing CPU use in programs that have
many file input/output operations. It can be advantageous to put file access in a separate
thread if there is other work that the processor can do while waiting for disk operations tofinish.
19

3.8 System database
It can take several seconds to access the system database in Windows. It is more efficient
to store application-specific information in a separate file than in the big registration
database in the Windows system. Note that the system may store the information in the
database anyway if you are using functions such as GetPrivateProfileString andWritePrivateProfileString to read and write configuration files (*.ini files).

3.9 Other databases
Many software applications use a database for storing user data. A database can consume
a lot of CPU time, RAM and disk space. It may be possible to replace a database by a plain
old data file in simple cases. Database queries can often be optimized by using indexes,
working with sets rather than loops, etc. Optimizing database queries is beyond the scopeof this manual, but you should be aware that there is often a lot to gain by optimizing
database access.

3.10 Graphics
A graphical user interface can use a lot of computing resources. Typically, a specificgraphics framework is used. The operating system may supply such a framework in its API.
In some cases, there is an extra layer of a third-party graphics framework between theoperating system API and the application software. Such an extra framework can consume
a lot of extra resources.
Each graphics operation in the application software is implemented as a function call to agraphics library or API function which then calls a device driver. A call to a graphics function
is time consuming because it may go through multiple layers and it needs to switch toprotected mode and back again. Obviously, it is more efficient to make a single call to agraphics function that draws a whole polygon or bitmap than to draw each pixel or line
separately through multiple function calls.
The calculation of graphics objects in computer games and animations is of course also
time consuming, especially if there is no graphics processing unit.
Various graphics function libraries and drivers differ a lot in performance. I have no specificrecommendation of which one is best.

3.11 Other system resources
Writes to a printer or other device should preferably be done in big blocks rather than asmall piece at a time because each call to a driver involves the overhead of switching toprotected mode and back again.
Accessing system devices and using advanced facilities of the operating system can betime consuming because it may involve the loading of several drivers, configuration files andsystem modules.

3.12 Network accessSome application programs use internet or intranet for automatic updates, remote help files,
database access, etc. The problem here is that access times cannot be controlled. The
network access may be fast in a simple test setup but slow or completely absent in a use
situation where the network is overloaded or the user is far from the server.
20

These problems should be taken into account when deciding whether to store help files andother resources locally or remotely. If frequent updates are necessary then it may be
optimal to mirror the remote data locally.
Access to remote databases usually requires log on with a password. The log on process isknown to be an annoying time consumer to many hard working software users. In somecases, the log on process may take more than a minute if the network or database is heavily
loaded.

3.13 Memory access
Accessing data from RAM memory can take quite a long time compared to the time it takes
to do calculations on the data. This is the reason why all modern computers have memory
caches. Typically, there is a level-1 data cache of 8 - 64 Kbytes and a level-2 cache of 256
Kbytes to 2 Mbytes. Often, there is also a level-3 cache of several Mbytes.
If the combined size of all data in a program is bigger than the level-2 cache and the data
are scattered around in memory or accessed in a non-sequential manner then it is likely thatmemory access is the biggest time-consumer in the program. Reading or writing to avariable in memory takes only 2-4 clock cycles if it is cached, but several hundred clock
cycles if it is not cached. See page 25 about data storage and page 91 about memorycaching.

3.14 Context switches
A context switch is a switch between different tasks in a multitasking environment, between
different threads in a multithreaded program, or between different parts of a big program.
Frequent context switches can reduce the performance because the contents of data cache,code cache, branch target buffer, branch pattern history, etc. may have to be renewed.
Context switches are more frequent if the time slices allocated to each task or thread aresmaller. The lengths of the time slices is determined by the operating system, not by theapplication program.
The number of context switches is smaller in a computer with multiple CPUs or a CPU withmultiple cores.

3.15 Dependency chains
Modern microprocessors can do out-of-order execution. This means that if a piece ofsoftware specifies the calculation of A and then B, and the calculation of A is slow, then the
microprocessor can begin the calculation of B before the calculation of A is finished.
Obviously, this is only possible if the value of A is not needed for the calculation of B.
In order to take advantage of out-of-order execution, you have to avoid long dependency
chains. A dependency chain is a series of calculations, where each calculation depends on
the result of the preceding one. This prevents the CPU from doing multiple calculations
simultaneously or out of order. See page 113 for examples of how to break a dependency
chain.

3.16 Execution unit throughput
There is an important distinction between the latency and the throughput of an execution
unit. For example, it may take three clock cycles to do a floating point addition on a modern
CPU. But it is possible to start one or two new floating point additions every clock cycle.
This means that if each addition depends on the result of the preceding addition then you
21will have only one addition every three clock cycles. But if all the additions are independent
then you can have one or two additions every clock cycle.
The highest performance that can possibly be obtained in a computationally intensive
program is achieved when none of the time-consumers mentioned in the above sections are
dominating and there are no long dependency chains. In this case, the performance islimited by the throughput of the execution units rather than by the latency or by memory
access.
The execution core of modern microprocessors is split between several execution units.
Typically, there are two or more integer units, one or two floating point addition units, andone or two floating point multiplication units. This means that it is possible to do an integer
addition, a floating point addition, and a floating point multiplication at the same time.
A code that does floating point calculations should therefore preferably have a balanced mixof additions and multiplications. Subtractions use the same unit as additions. Divisions takelonger time. It is possible to do integer operations in-between the floating point operations
without reducing the performance because the integer operations use different execution
units. For example, a loop that does floating point calculations will typically use integer
operations for incrementing a loop counter, comparing the loop counter with its limit, etc. In
most cases, you can assume that these integer operations do not add to the total
computation time.

4 Performance and usability
A better performing software product is one that saves time for the user. Time is a precious
resource for many computer users and much time is wasted on software that is slow,
difficult to use, incompatible, or error prone. All these problems are usability issues, and I
believe that software performance should be seen in the broader perspective of usability.
This is not a manual on usability, but I think it is necessary here to draw the attention ofsoftware programmers to some of the most common obstacles to efficient use of software.
For more on this topic, see my free E-book Usability for Nerds at Wikibooks.
The following list points out some typical sources of frustration and waste of time forsoftware users as well as important usability problems that software developers should beaware of.
•Big runtime frameworks. The .NET framework and the Java virtual machine areframeworks that typically take much more resources than the programs they arerunning. Such frameworks are frequent sources of resource problems and compatibility
problems and they waste a lot of time both during installation of the framework itself,
during installation of the program that runs under the framework, during the frequent
updates, during start of the program, and while the program is running. The main reason
why such runtime frameworks are used at all is for the sake of cross-platform portability.
Unfortunately, the cross-platform compatibility is not always as good as expected. I
believe that the portability could be achieved more efficiently by better standardization ofprogramming languages, operating systems, and API's.

•

Memory swapping. Software developers typically have more powerful computers withmore RAM than end users have. The developers may therefore fail to see the excessive
memory swapping and other resource problems that cause the resource-hungry
applications to perform poorly for the end user.

•

Installation problems. The procedures for installation and uninstallation of programs
should be standardized and done by the operating system rather than by individual
22installation tools.
•

Automatic updates. Automatic updating of software can cause problems if the network is
unstable or if the new version causes problem that were not present in the old version.
Updating mechanisms often disturb the users with nagging pop-up messages saying
please install this important new update or even telling the user to restart the computer
while he or she is busy concentrating on important work. The updating mechanism
should never interrupt the user, but only show a discrete icon signaling the availability ofan update, or update automatically when the program is restarted anyway. Software
distributors are often abusing the update mechanism to advertise new versions of theirsoftware. This is annoying to the user.

•

Compatibility problems. All software should be tested on different platforms, different
screen resolutions, different system color settings, and different user access rights.
Software should use standard API calls rather than self-styled hacks and direct
hardware access. Available protocols and standardized file formats should be used.
Web systems should be tested in different browsers, different platforms, different screen
resolutions, etc. Accessibility guidelines should be obeyed.

•

Copy protection. Some copy protection schemes are based on hacks that violate or
circumvent operating system standards. Such schemes are frequent sources ofcompatibility problems and system breakdown. Many copy protection schemes are
based on hardware identification. Such schemes cause problems when the hardware is
updated. Most copy protection schemes are annoying to the user and prevent legitimate
backup copying without effectively preventing illegitimate copying. The benefits of acopy protection scheme should be weighed against the costs in terms of usability
problems and necessary support.

•

Hardware updating. The change of a hard disk or other hardware often requires that all
software be reinstalled and user settings are lost. It is not unusual for the reinstallation
work to take a whole workday or more. Many software applications need better backup
features, and current operating systems need better support for hard disk copying.

•

Security. The vulnerability of software with network access to virus attacks and otherabuse is extremely costly to many users. Firewalls, virus scanners, and other protectionmeans are among the most frequent causes of compatibility problems and systemcrash. Furthermore, it is not uncommon for virus scanners to consume more time thananything else on a computer. Security software that is part of the operating system isoften more reliable than third party security software.

•

Background services. Many services that run in the background are unnecessary for theuser and a waste of resources. Consider running the services only when activated by
the user.

•Feature bloat. It is common for software to add new features to each new version formarketing reasons. This may cause the software to be slower or require moreresources, even if the user never uses the new features.

•

Take user feedback seriously. User complaints should be regarded as a valuable sourceof information about bugs, compatibility problems, usability problems, and desired newfeatures. User feedback should be handled in a systematic manner to make sure thisinformation is utilized appropriately. Users should get a reply about investigation of the
problems and planned solutions. Patches should be easily available from a website.

23

5 Choosing the optimal algorithm
The first thing to do when you want to optimize a piece of CPU-intensive software is to findthe best algorithm. The choice of algorithm is very important for tasks such as sorting,searching, and mathematical calculations. In such cases, you can obtain much more bychoosing the best algorithm than by optimizing the first algorithm that comes to mind. In
some cases you may have to test several different algorithms in order to find the one thatworks best on a typical set of test data.
That being said, I must warn against overkill. Do not use an advanced and complicatedalgorithm if a simple algorithm can do the job fast enough. For example, some programmers
use a hash table for even the smallest list of data. A hash table can improve search times
dramatically for very large databases, but there is no reason to use it for lists that are sosmall that a binary search, or even a linear search, is fast enough. A hash table increases
the size of the program as well as the size of data files. This can actually reduce speed if
the bottleneck is file access or cache access rather than CPU time. Another disadvantage ofcomplicated algorithms is that it makes program development more expensive and moreerror prone.
A discussion of different algorithms for different purposes is beyond the scope of thismanual. You have to consult the general literature on algorithms and data structures forstandard tasks such as sorting and searching, or the specific literature for more complicatedmathematical tasks.
Before you start to code, you may consider whether others have done the job before you.
Optimized function libraries for many standard tasks are available from a number ofsources. For example, the Boost collection contains well-tested libraries for many common
purposes (www.boost.org). The "Intel Math Kernel Library" contains many functions forcommon mathematical calculations including linear algebra and statistics, and the "Intel
Performance Primitives" library contains many functions for audio and video processing,
signal processing, data compression and cryptography (www.intel.com). If you are using anIntel function library then make sure it works well on non-Intel processors, as explained on
page 143.
It is often easier said than done to choose the optimal algorithm before you start to program.
Many programmers have discovered that there are smarter ways of doing things only after
they have put the whole software project together and tested it. The insight you gain bytesting and analyzing program performance and studying the bottlenecks can lead to abetter understanding of the whole structure of the problem. This new insight can lead to acomplete redesign of the program, for example when you discover that there are smarter
ways of organizing the data.
A complete redesign of a program that already works is of course a considerable job, but it
may be quite a good investment. A redesign can not only improve the performance, it is also
likely to lead to a more well-structured program that is easier to maintain. The time you
spend on redesigning a program may in fact be less than the time you would have spentfighting with the problems of the original, poorly designed program.

6 Development process
There is a long standing debate about which software development process and software
engineering principles to use. I am not going to recommend any specific model. Instead, I
will make a few comments about how the development process can influence theperformance of the final product.

24

It is good to do a thorough analysis of the data structure, data flow and algorithms in theplanning phase in order to predict which resources are most critical. However, there may be
so many unknown factors in the early planning stage that a detailed overview of the problem
cannot easily be obtained. In complicated cases, a full understanding only comes at a latestage in the development process. In this case, you may view the software development
work as a learning process where the main feedback comes from testing and fromprogramming problems. Here, you should be prepared for several iterations of redesign incase you discover that a different program structure is more efficient or more manageable.
Some software development models have a strict formalism that requires several layers of
abstraction in the logical architecture of the software. You should be aware that there are
inherent performance costs to such a formalism. The splitting of software into an excessive
number of separate layers of abstraction is a common cause of reduced performance.
Since most development methods are incremental or iterative in nature, it is important tohave a strategy for saving a backup copy of every intermediate version. For one-man
projects, it is sufficient to make a zip file of every version. For team projects, it isrecommended to use a version control tool.

7 The efficiency of different C++ constructs
Most programmers have little or no idea how a piece of program code is translated intomachine code and how the microprocessor handles this code. For example, many
programmers do not know that double precision calculations are just as fast as single
precision. And who would know that a template class is more efficient than a polymorphousclass?
This chapter is aiming at explaining the relative efficiency of different C++ language
elements in order to help the programmer choosing the most efficient alternative. The
theoretical background is further explained in the other volumes in this series of manuals.

7.1 Different kinds of variable storage
Variables and objects are stored in different parts of the memory, depending on how they
are declared in a C++ program. This has influence on the efficiency of the data cache (seepage 91). Data caching is poor if data are scattered randomly around in the memory. It istherefore important to understand how variables are stored. The storage principles are thesame for simple variables, arrays, and objects.

Storage on the stack
Variables and objects declared inside a function are stored on the stack, except for thecases described in the sections below.
The stack is a part of memory that is organized in a first-in-last-out fashion. It is used forstoring function return addresses (i.e. where the function was called from), function
parameters, local variables, and for saving registers that have to be restored before thefunction returns. Every time a function is called, it allocates the required amount of space on
the stack for all these purposes. This memory space is freed when the function returns. The
next time a function is called, it can use the same space for the parameters of the newfunction.
The stack is the most efficient memory space to store data in because the same range ofmemory addresses is reused again and again. It is almost certain that this part of thememory is mirrored in the level-1 data cache if there are no big arrays.

25

The lesson we can learn from this is that all variables and objects should preferably bedeclared inside the function in which they are used.
It is possible to make the scope of a variable even smaller by declaring it inside {} brackets.
However, most compilers do not free the memory used by a variable until the function
returns even though it could free the memory when exiting the {} brackets in which thevariable is declared. If the variable is stored in a register (see below) then it may be freed
before the function returns.

Global or static storage
Variables that are declared outside of any function are called global variables. They can beaccessed from any function. Global variables are stored in a static part of the memory. The
static memory is also used for variables declared with the static keyword, for floating
point constants, string constants, array initializer lists, switch statement jump tables, andvirtual function tables.
The static data area is usually divided into three parts: one for constants that are nevermodified by the program, one for initialized variables that may be modified by the program,and one for uninitialized variables that may be modified by the program.
The advantage of static data is that it can be initialized to desired values before the program
starts. The disadvantage is that the memory space is occupied throughout the whole
program execution, even if the variable is only used in a small part of the program. This
makes data caching less efficient because the memory space cannot be reused for anotherpurpose.
Do not make variables global if you can avoid it. Global variables may be needed forcommunication between different threads, but that is about the only situation where they are
unavoidable. It may be useful to make a variable global if it is accessed by several different
functions and you want to avoid the overhead of transferring the variable as function
parameter. But it may be a better solution to make the functions that access the saved
variable members of the same class and store the shared variable inside the class. Whichsolution you prefer is a matter of programming style.
It is preferable to declare a lookup table static and constant. Example:// Example 7.1
float SomeFunction (int x) {static const float list[] = {1.1, 0.3, -2.0, 4.4, 2.5};
return list[x];}The advantage of this is that the list does not need to be initialized every time the function iscalled. The static declaration helps the compiler decide that the table can be reused fromone call to the next. The const declaration helps the compiler see that the table neverchanges. A static declaration on an initialized variable inside a function means that thevariable has to be initialized the first time the function is called, but not on subsequent calls.
This is inefficient because the function needs extra overhead to check whether it called forthe first time or it has been called before. The const declaration helps the compiler decidethat the check for first call is not needed. Some compilers are able to optimize the lookup
table anyway, but it is best to put both static and const on lookup tables in order to
optimize performance on all compilers.
String constants and floating point constants are often stored in static memory. Example:// Example 7.2
a = b * 3.5;c = d + 3.5;26

Here, the constant 3.5 will be stored in static memory. Most compilers will recognize thatthe two constants are identical so that only one constant needs to be stored. All identicalconstants in the entire program will be joined together in order to minimize the amount ofcache space used for constants.
Integer constants are usually included as part of the instruction code. You can assume that
there are no caching problems for integer constants.

Register storage
A limited number of variables can be stored in registers instead of main memory. A register
is a small piece of memory inside the CPU used for temporary storage. Variables that are
stored in registers are accessed very fast. All optimizing compilers will automatically choose
the most often used variables in a function for register storage. The same register can beused for multiple variables as long as their uses (live ranges) do not overlap.
Local variables are particularly suited for register storage. This is another reason for
preferring local variables.
The number of registers is limited. There are approximately six integer registers available forgeneral purposes in 32-bit x86 operating systems and fourteen integer registers in 64-bit
systems.
Floating point variables use a different kind of registers. There are eight floating point
registers available in 32-bit operating systems, sixteen in 64-bit operating systems, andthirty two when the AVX512 instruction set is enabled in 64-bit mode. Some compilers havedifficulties making floating point register variables in 32-bit mode unless the SSE instruction
set (or higher) is enabled.

Volatile
The volatile keyword specifies that a variable can be changed by another thread. This
prevents the compiler from making optimizations that rely on the assumption that thevariable always has the value it was assigned previously in the code. Example:// Example 7.3. Explain volatile
volatile int seconds; // incremented every second by another thread
void DelayFiveSeconds() {seconds = 0;
while (seconds < 5) {// do nothing while seconds count to 5}}

In this example, the DelayFiveSeconds function will wait until seconds has been
incremented to 5 by another thread. If seconds was not declared volatile then an
optimizing compiler would assume that seconds remains zero in the while loop because
nothing inside the loop can change the value. The loop would be while (0 < 5) {}which would be an infinite loop.
The effect of the keyword volatile is that it makes sure the variable is stored in memory
rather than in a register and prevents all optimizations on the variable. This can be useful intest situations to avoid that some expression is optimized away.
Note that volatile does not mean atomic. It will not prevent two threads from attempting
to write the variable at the same time. The code in the above example may fail in the eventthat it attempts to set seconds to zero at the same time as the other thread increments
27seconds. A safer implementation would only read the value of seconds and wait until thevalue has changed five times.

Thread-local storage
Most compilers can make thread-local storage of static and global variables by using the
keyword thread_local, __thread or __declspec(thread). Such variables have
one instance for each thread. Thread-local storage is inefficient because it is accessed
through a pointer stored in a thread environment block. Static thread-local storage should
be avoided, if possible, and replaced by storage on the thread's own stack (see above, p.
25). Non-static variables and objects that are declared in the thread function, or in any
function called by the thread function, will be stored on the thread's stack. These variables
and objects will have a separate instance for each thread. The need for static thread-local
storage can be avoided in most cases by declaring the variables or objects inside the thread
function.

Far
Systems with segmented memory, such as the old DOS and 16-bit Windows, allow
variables to be stored in a far data segment by using the keyword far (DOS arrays canalso be huge). Far storage, far pointers, and far procedures are inefficient. If a program has
too much data for one segment then it is recommended to use a different operating systems
that allows bigger segments (32-bit or 64-bit systems).

Dynamic memory allocation
Dynamic memory allocation is done with the operators new and delete or with thefunctions malloc and free. These operators and functions consume a significant amountof time. A part of memory called the heap is reserved for dynamic allocation. The heap caneasily become fragmented when objects of different sizes are allocated and deallocated in
random order. The heap manager can spend a lot of time cleaning up spaces that are nolonger used and searching for vacant spaces. This is called garbage collection. Objects thatare allocated in sequence are not necessarily stored sequentially in memory. They may bescattered around at different places when the heap has become fragmented. This makes
data caching inefficient.
Dynamic memory allocation also tends to make the code more complicated and error-prone.
The program has to keep pointers to all allocated objects and keep track of when they are
no longer used. It is important that all allocated objects are also deallocated in all possible
cases of program flow. Failure to do so is a common source of error known as memory leak.
An even worse kind of error is to access an object after it has been deallocated. The
program logic may need extra overhead to prevent such errors.
See page 95 for a further discussion of the advantages and drawbacks of using dynamic
memory allocation.
Some programming languages, such as Java, use dynamic memory allocation for all
objects. This is of course inefficient.

Variables declared inside a class
Variables declared inside a class are stored in the order in which they appear in the classdeclaration. The type of storage is determined when the object of the class is declared. An
object of a class, structure, or union can use any of the storage methods mentioned above.
An object cannot be stored in a register except in the simplest cases, but its data members
can be copied into registers.
A class member variable with the static modifier will be stored in static memory and will
have one and only one instance. Non-static members of the same class will be stored witheach instance of the class.
28

Storing variables in a class or structure is a good way of making sure that variables that areused in the same part of the program are also stored near each other. See page 52 for thepros and cons of using classes.

7.2 Integers variables and operators
Integer sizes
Integers can be different sizes, and they can be signed or unsigned. The following tablesummarizes the different integer types available.
declaration

size, bitschar
8
short int16
in 16-bit systems: int
int
32
in 16-bit systems: long int
long long or int64_t
64
MS compiler: __int64
64-bit Linux: long int
unsigned char
8
unsigned short int
16
in 16-bit systems: unsigned int
unsigned int
32
in 16-bit systems: unsigned longunsigned long long or
64
uint64_t
MS compiler: unsigned __int64
64-bit Linux: unsigned long int
Table 7.1. Sizes of different integer types

minimumvaluemaximumvalue

in stdint.h-128-3276812732767

int8_tint16_t

-231

231-1

int32_t

-263

263-1

int64_t

0
0

255
65535

uint8_tuint16_t

0

232-1

uint32_t

0

264-1

uint64_t

Unfortunately, the way of declaring an integer of a specific size is different for different
platforms as shown in the above table. It is recommended to use the standard header filestdint.h or inttypes.h for a portable way of defining integer types of a specific size.
Integer operations are fast in most cases, regardless of the size. However, it is inefficient touse an integer size that is larger than the largest available register size. In other words, it isinefficient to use 32-bit integers in 16-bit systems or 64-bit integers in 32-bit systems,
especially if the code involves multiplication or division.
The compiler will always select the most efficient integer size if you declare an int, without
specifying the size. Integers of smaller sizes (char, short int) are only slightly lessefficient. In many cases, the compiler will convert these types to integers of the default size
when doing calculations, and then use only the lower 8 or 16 bits of the result. You canassume that the type conversion takes zero or one clock cycle. In 64-bit systems, there is
only a minimal difference between the efficiency of 32-bit integers and 64-bit integers, aslong as you are not doing divisions.
It is recommended to use the default integer size in cases where the size does not matterand there is no risk of overflow, such as simple variables, loop counters, etc. In large arrays,
it may be preferred to use the smallest integer size that is big enough for the specificpurpose in order to make better use of the data cache. Bit-fields of sizes other than 8, 16,29

32 and 64 bits are less efficient. In 64-bit systems, you may use 64-bit integers if theapplication can make use of the extra bits.
The unsigned integer type size_t is 32 bits in 32-bit systems and 64 bits in 64-bit systems.
It is intended for array sizes and array indices when you want to make sure that overflow
never occurs, even for arrays bigger than 2 GB.
When considering whether a particular integer size is big enough for a specific purpose, youmust consider if intermediate calculations can cause overflow. For example, in theexpression a = (b*c)/d, it can happen that (b*c) overflows, even if a, b, c and d would
all be below the maximum value. There is no automatic check for integer overflow.

Signed versus unsigned integers
In most cases, there is no difference in speed between using signed and unsigned integers.
But there are a few cases where it matters:•

Division by a constant: Unsigned is faster than signed when you divide an integer with aconstant (see page 150). This also applies to the modulo operator %.

•Conversion to floating point is faster with signed than with unsigned integers for most
instruction sets.

•

Overflow behaves differently on signed and unsigned variables. An overflow of an
unsigned variable produces a low positive result. An overflow of a signed variable isofficially undefined. The normal behavior is wrap-around of positive overflow to anegative value, but the compiler may optimize away branches that depend on overflow,
based on the assumption that overflow does not occur.

The conversion between signed and unsigned integers is costless. It is simply a matter ofinterpreting the same bits differently. A negative integer will be interpreted as a very large
positive number when converted to unsigned.
// Example 7.4. Signed and unsigned integers
int a, b;
double c;b = (unsigned int)a / 10;// Convert to unsigned for fast divisionc = a * 2.5;// Use signed when converting to double

In example 7.4 we are converting a to unsigned in order to make the division faster. Of
course, this works only if it is certain that a will never be negative. The last line is implicitly
converting a to double before multiplying with the constant 2.5, which is double. Here we
prefer a to be signed.
Be sure not to mix signed and unsigned integers in comparisons, such as <. The result ofcomparing signed with unsigned integers is ambiguous and may produce undesired results.

Integer operators
Integer operations are generally very fast. Simple integer operations such as addition,
subtraction, comparison, bit operations and shift operations take only one clock cycle on
most microprocessors.
Multiplication and division take longer time. Integer multiplication takes 11 clock cycles on
Pentium 4 processors, and 3 - 4 clock cycles on most other microprocessors. Integer
division takes 40 - 80 clock cycles, depending on the microprocessor. Integer division isfaster the smaller the integer size on AMD processors, but not on Intel processors. Details
30about instruction latencies are listed in manual 4: "Instruction tables". Tips about how to
speed up multiplications and divisions are given on page 149 and 150, respectively.

Increment and decrement operators
The pre-increment operator ++i and the post-increment operator i++ are as fast asadditions. When used simply to increment an integer variable, it makes no difference
whether you use pre-increment or post-increment. The effect is simply identical. For
example,for (i=0; i<n; i++) is the same as for (i=0; i<n; ++i). But when the result of
the expression is used, then there may be a difference in efficiency. For example,
x = array[i++] is more efficient than x = array[++i] because in the latter case,the calculation of the address of the array element has to wait for the new value of i which
will delay the availability of x for approximately two clock cycles. Obviously, the initial valueof i must be adjusted if you change pre-increment to post-increment.
There are also situations where pre-increment is more efficient than post-increment. For
example, in the case a = ++b; the compiler will recognize that the values of a and b are
the same after this statement so that it can use the same register for both, while theexpression a = b++; will make the values of a and b different so that they cannot use thesame register.
Everything that is said here about increment operators also applies to decrement operators
on integer variables.

7.3 Floating point variables and operators
Modern microprocessors in the x86 family have two different types of floating point registers
and correspondingly two different types of floating point instructions. Each type hasadvantages and disadvantages.
The original method of doing floating point operations involves eight floating point registersorganized as a register stack, called x87 registers. These registers have long double
precision (80 bits). The advantages of using the register stack are:•All calculations are done with long double precision.

•

Conversions between different precisions take no extra time.

•

There are intrinsic instructions for mathematical functions such as logarithms andtrigonometric functions.

•

The code is compact and takes little space in the code cache.

The register stack also has disadvantages:•

It is difficult for the compiler to make register variables because of the way the register
stack is organized.

•

Floating point comparisons are slow.

•

Conversions between integers and floating point numbers is inefficient.

•

Division, square root and mathematical functions take more time to calculate when long
double precision is used.

31

A newer method of doing floating point operations involves vector registers (XMM, YMM, or
ZMM) which can be used for multiple purposes. Floating point operations are done withsingle or double precision, and intermediate results are always calculated with the same
precision as the operands. The advantages of using the vector registers are:•

It is easy to make floating point register variables.

•

Vector operations are available for doing parallel calculations on multiple variables invector registers (see page 115).

Disadvantages are:•Long double precision is not supported.

•

The calculation of expressions where operands have mixed precision require precision
conversion instructions which can be quite time-consuming (see page 153).

•

Mathematical functions must use a function library, but this is often faster than the
intrinsic hardware functions.

The x87 floating point registers are available in all systems that have floating point
capabilities (except in device drivers for 64-bit Windows). The XMM, YMM, and ZMM
registers require the SSE, AVX, and AVX512 instruction set, respectively. See page 135 for
how to test for the availability of these instruction sets.
Modern compilers will use vector registers for floating point calculations whenever they areavailable, i.e. in 64-bit mode or when the SSE or higher instruction set is enabled. Few
compilers are able to mix the two types of floating point operations and choose the type thatis optimal for each calculation.
In most cases, double precision calculations take no more time than single precision as longas they are not joined into vectors. Single precision division, square root, and mathematical
functions are calculated faster than double precision when the XMM registers are used,while the speed of addition, subtraction, multiplication, etc. is still the same regardless of
precision on most processors when vector operations are not used.
You may use double precision without worrying too much about the costs if it is good for theapplication. You may use single precision if you have big arrays and want to get as muchdata as possible into the data cache. Single precision is good if you can take advantage ofvector operations, as explained on page 115. Half precision can be used on processors with
the AVX512-FP16 instruction set extension.
Floating point addition takes 2 - 6 clock cycles, depending on the microprocessor.
Multiplication takes 3 - 8 clock cycles. Division takes 14 - 45 clock cycles. Floating pointcomparisons and floating point to integer conversions are inefficient when the old x87
floating point registers are used.
Do not mix single and double precision in the same expression. See page 153.
Avoid conversions between integers and floating point variables, if possible. See page 153.
Applications that generate floating point underflow in vector registers can benefit fromsetting the flush-to-zero mode rather than generating subnormal numbers in case ofunderflow:// Example 7.5. Set flush-to-zero mode (SSE):#include <xmmintrin.h>_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);32

It is strongly recommended to set the flush-to-zero mode unless you have special reasons
to use subnormal numbers. You may, in addition, set the denormals-are-zero mode if vector
regsiters are available:// Example 7.6. Set flush-to-zero and denormals-are-zero mode (SSE2):#include <xmmintrin.h>_mm_setcsr(_mm_getcsr() | 0x8040);

See page 131 and 158 for more information about mathematical functions.

7.4 Enums
An enum is simply an integer in disguise. Enums are exactly as efficient as integers.
Note that the enumerators (value names) will clash with any variable or function having thesame name. Enums in header files should therefore have long and unique enumerator
names or be put into a namespace.

7.5 Booleans
The order of Boolean operands
The operands of the Boolean operators && and || are evaluated in the following way. If thefirst operand of && is false, then the second operand is not evaluated at all because theresult is known to be false regardless of the value of the second operand. Likewise, if thefirst operand of || is true, then the second operand is not evaluated, because the result isknown to be true anyway.
It may be advantageous to put the operand that is most often true last in an && expression,or first in an || expression. Assume, for example, that a is true 50% of the time and b istrue 10% of the time. The expression a && b needs to evaluate b when a is true, which is
50% of the cases. The equivalent expression b && a needs to evaluate a only when b istrue, which is only 10% of the time. This is faster if a and b take the same time to evaluate
and are equally likely to be predicted by the branch prediction mechanism. See page 43 foran explanation of branch prediction.
If one operand is more predictable than the other, then put the most predictable operand
first.
If one operand is faster to calculate than the other then put the operand that is calculated
the fastest first.
However, you must be careful when swapping the order of Boolean operands. You cannot
swap the operands if the evaluation of the operands has side effects or if the first operand
determines whether the second operand is valid. For example:// Example 7.7
unsigned int i; const int ARRAYSIZE = 100;
if (i < ARRAYSIZE && list[i] > 1.0) { ...

float list[ARRAYSIZE];

Here, you cannot swap the order of the operands because the expression list[i] isinvalid when i is not less than ARRAYSIZE. Another example:// Example 7.8
if (handle != INVALID_HANDLE_VALUE && WriteFile(handle, ...)) { ...

33Here you cannot swap the order of the Boolean operands because you should not callWriteFile if the handle is invalid.

Boolean variables are overdetermined
Boolean variables are stored as 8-bit integers with the value 0 for false and 1 for true.
Boolean variables are overdetermined in the sense that all operators that have Boolean
variables as input check if the inputs have any other value than 0 or 1, but operators thathave Booleans as output can produce no other value than 0 or 1. This makes operations
with Boolean variables as input less efficient than necessary. Take the example:// Example 7.9abool a, b, c, d;c = a && b;
d = a || b;This is typically implemented by the compiler in the following way:bool a, b, c, d;if (a != 0) {if (b != 0) {c = 1;}
else {
goto CFALSE;}}
else {CFALSE:c = 0;}
if (a == 0) {if (b == 0) {d = 0;}
else {
goto DTRUE;}}
else {DTRUE:
d = 1;}

This is of course far from optimal. The branches may take a long time in case ofmispredictions (see page 43). The Boolean operations can be made much more efficient if it
is known with certainty that the operands have no other values than 0 and 1. The reason
why the compiler does not make such an assumption is that the variables might have othervalues if they are uninitialized or come from unknown sources. The above code can be
optimized if a and b have been initialized to valid values or if they come from operators that
produce Boolean output. The optimized code looks like this:// Example 7.9bchar a = 0, b = 0, c, d;c = a & b;
d = a | b;

Here, I have used char (or int) instead of bool in order to make it possible to use thebitwise operators (& and |) instead of the Boolean operators (&& and ||). The bitwise
operators are single instructions that take only one clock cycle. The OR operator (|) works
34even if a and b have other values than 0 or 1. The AND operator (&) and the EXCLUSIVE
OR operator (^) may give inconsistent results if the operands have other values than 0 and
1.
Note that there are a few pitfalls here. You cannot use ~ for NOT. Instead, you can make a
Boolean NOT on a variable which is known to be 0 or 1 by XOR'ing it with 1:// Example 7.10abool a, b;
b = !a;can be optimized to:// Example 7.10bchar a = 0, b;
b = a ^ 1;

You cannot replace a && b with a & b if b is an expression that should not be
evaluated if a is false. Likewise, you cannot replace a || b with a | b if b is anexpression that should not be evaluated if a is true.
The trick of using bitwise operators is more advantageous if the operands are variables thanif the operands are comparisons, etc. For example:// Example 7.11
bool a; float x, y, z;
a = x > y && z != 0;

This is optimal in most cases. Do not change && to & unless you expect the && expression
to generate many branch mispredictions.

Boolean vector operations
An integer may be used as a Boolean vector. For example, if a and b are 32-bit integers,then the expression y = a & b; will make 32 AND-operations in just one clock cycle.
The operators &, |, ^, ~ are useful for Boolean vector operations.

7.6 Pointers and references
Pointers versus references
Pointers and references are equally efficient because they are in fact doing the same thing.
Example:// Example 7.12
void FuncA (int * p) {*p = *p + 2;}
void FuncB (int & r) {r = r + 2;}

These two functions are doing the same thing and if you look at the code generated by thecompiler you will notice that the code is exactly identical for the two functions. The
difference is simply a matter of programming style. The advantages of using pointers rather
than references are:35•When you look at the function bodies above, it is clear that p is a pointer, but it is notclear whether r is a reference or a simple variable. Using pointers makes it more clear
to the reader what is happening.

•

It is possible to do things with pointers that are impossible with references. You canchange what a pointer points to and you can do arithmetic operations with pointers.

The advantages of using references rather than pointers are:•

The syntax is simpler when using references.

•

References are safer to use than pointers because in most cases they are sure to point
to a valid address. Pointers can be invalid and cause fatal errors if they are uninitialized,
if pointer arithmetic calculations go outside the bounds of valid addresses, or if pointers
are type-casted to a wrong type.

•

References are useful for copy constructors and overloaded operators.

•

Function parameters that are declared as constant references accept expressions asarguments while pointers and non-constant references require a variable.

Efficiency
Accessing a variable or object through a pointer or reference may be just as fast as
accessing it directly. The reason for this efficiency lies in the way microprocessors areconstructed. All non-static variables and objects declared inside a function are stored on thestack and are in fact addressed relative to the stack pointer. Likewise, all non-static
variables and objects declared in a class are accessed through the implicit pointer known in
C++ as 'this'. We can therefore conclude that most variables in a well-structured C++
program are in fact accessed through pointers in one way or another. Therefore, microprocessors have to be designed so as to make pointers efficient, and that's what they are.
However, there are disadvantages of using pointers and references. Most importantly, it
requires an extra register to hold the value of the pointer or reference. Registers are ascarce resource, especially in 32-bit mode. If there are not enough registers then the pointerhas to be loaded from memory each time it is used and this will make the program slower.
Another disadvantage is that the value of the pointer is needed a few clock cycles before
the time the variable pointed to can be accessed.

Pointer arithmetic
A pointer is in fact an integer that holds a memory address. Pointer arithmetic operations
are therefore as fast as integer arithmetic operations. When an integer is added to a pointerthen its value is multiplied by the size of the object pointed to. For example:// Example 7.13
struct abc {int a; int b; int c;};abc * p; int i;
p = p + i;

Here, the value that is added to p is not i but i*12, because the size of abc is 12 bytes.
The time it takes to add i to p is therefore equal to the time it takes to make a multiplication
and an addition. If the size of abc is a power of 2 then the multiplication can be replaced bya shift operation which is much faster. In the above example, the size of abc can beincreased to 16 bytes by adding one more integer to the structure.
Incrementing or decrementing a pointer does not require a multiplication but only an
addition. Comparing two pointers requires only an integer comparison, which is fast.
36

Calculating the difference between two pointers requires a division, which is slow unless thesize of the type of object pointed to is a power of 2 (See page 150 about division).
The object pointed to can be accessed approximately two clock cycles after the value of thepointer has been calculated. Therefore, it is recommended to calculate the value of apointer well before the pointer is used. For example, x = *(p++) is more efficient thanx = *(++p) because in the latter case the reading of x must wait until a few clock cycles
after the pointer p has been incremented, while in the former case x can be read before p isincremented. See page 31 for more discussion of the increment and decrement operators.

7.7 Function pointers
Calling a function through a function pointer may take a few clock cycles more than callingthe function directly if the target address can be predicted. The target address is predicted if
the value of the function pointer is the same as last time the statement was executed. The
target address will quite likely be mispredicted if the value of the function pointer changes. A
misprediction causes a long delay. See page 43 about branch prediction.

7.8 Member pointers
In simple cases, a data member pointer simply stores the offset of a data member relative tothe beginning of the object, and a member function pointer is simply the address of themember function. But there are special cases such as multiple inheritance where a much
more complicated implementation is needed. These complicated cases should definitely be
avoided.
A compiler has to use the most complicated implementation of member pointers if it has
incomplete information about the class that the member pointer refers to. For example:// Example 7.14class c1;
int c1::*MemberPointer;

Here, the compiler has no information about the class c1 other than its name at the timeMemberPointer is declared. Therefore, it has to assume the worst possible case andmake a complicated implementation of the member pointer. This can be avoided by making
the full declaration of c1 before MemberPointer is declared. Avoid multiple inheritance,
virtual functions, and other complications that make member pointers less efficient.
Most C++ compilers have various options to control the way member pointers are
implemented. Use the option that gives the simplest possible implementation if possible,
and make sure you are using the same compiler option for all modules that use the same
member pointer.

7.9 Smart pointers
A smart pointer is an object that behaves like a pointer. It has the special feature that theobject it points to is deleted when the pointer is deleted. Smart pointers are convenient for
objects stored in dynamically allocated memory, using new. The purpose of using smart
pointers is to make sure the object is deleted properly and the memory released when theobject is no longer used. A smart pointer may be regarded as a simple way to implement adata container or a dynamically sized array without defining a container class.
The C++11 standard defines smart pointers as std::unique_ptr andstd::shared_ptr. std::unique_ptr has the feature that there is always one, andonly one pointer that owns the allocated object. Ownership is transferred from one
37

unique_ptr to another by assignment. shared_ptr allows multiple pointers to the same
object.
There is no extra cost to accessing an object through a smart pointer. Accessing an object
by *p or p->member is equally fast whether p is a simple pointer or a smart pointer. But
there is an extra cost whenever a smart pointer is created, deleted, copied, or transferredfrom one function to another. These costs are higher for shared_ptr than for
unique_ptr.
The best optimizing compilers (Clang, Gnu) are able to peel off most or all of the overhead
for a unique_ptr in simple cases so that the compiled code contains just the calls to new
and delete.
Smart pointers can be useful in the situation where the logic structure of a program dictatesthat an object must be dynamically created by one function and later deleted by anotherfunction and these two functions are unrelated to each other (not member of the sameclass). If the same function or class is responsible for creating and deleting the object thenyou do not need a smart pointer.
If a program uses many small dynamically allocated objects with each their smart pointerthen you may consider if the cost of this solution is too high. It may be more efficient to pool
all the objects together into a single container, preferably with contiguous memory. See thediscussion of container classes on page 97.

7.10 Arrays
An array is implemented simply by storing the elements consecutively in memory. No
information about the dimensions of the array is stored. This makes the use of arrays in C
and C++ faster than in other programming languages, but also less safe. This safety
problem can be overcome by defining a container class that behaves like an array withbounds checking, as illustrated in this example:// Example 7.15a. Array with bounds checking
template <typename T, unsigned int N> class SafeArray {protected:
T a[N];// Array with N elements of type T
public:SafeArray() {// Constructor
memset(a, 0, sizeof(a)); // Initialize to zero}int Size() {// Return the size of the array
return N;}
T & operator[] (unsigned int i) { // Safe [] array index operator
if (i >= N) {// Index out of range. The next line provokes an error.
// You may insert any other error reporting here:
return *(T*)0;// Return a null reference to provoke error}// No error
return a[i];// Return reference to a[i]}};More examples of container classes are given below on page 97.
An array using the above template class is declared by specifying the type and size as
template parameters, as example 7.15b below shows. It is accessed with a square brackets
index, just as a normal array. The constructor sets all elements to zero. You may remove
38

the memset line if you do not want this initialization, or if the type T is a class with a default
constructor that does the necessary initialization. The compiler may report that memset isdeprecated. This is because it can cause errors if the size parameter is wrong, but it is still
the fastest way to set an array to zero. The [] operator will detect an error if the index is outof range (see page 147 on bounds checking). An error message is provoked here in arather unconventional manner by returning a null reference. This will provoke an error
message in a protected operating system if the array element is accessed, and this error iseasy to trace with a debugger. You may replace this line by any other form of error
reporting. For example, in Windows, you may write FatalAppExitA(0,"Array index
out of range"); or better, make your own error message function.
The following example illustrates how to use SafeArray:// Example 7.15b
SafeArray <float, 100> list;// Make array of 100 floats
for (int i = 0; i < list.Size(); i++) { // Loop through arraycout << list[i] << endl;// Output array element}

An array initialized by a list should preferably be static, as explained on page 26. An arraycan be initialized to zero by using memset:// Example 7.16
float list[100];
memset(list, 0, sizeof(list));A multidimensional array should be organized so that the last index changes fastest:// Example 7.17
const int rows = 20, columns = 50;
float matrix[rows][columns];
int i, j; float x;
for (i = 0; i < rows; i++)for (j = 0; j < columns; j++)matrix[i][j] += x;This makes sure that the elements are accessed sequentially. The opposite order of the twoloops would make the access non-sequential which makes the data caching less efficient.
The size of all but the first dimension may preferably be a power of 2 if the rows are indexed
in a non-sequential order in order to make the address calculation more efficient:// Example 7.18
int FuncRow(int); int FuncCol(int);
const int rows = 20, columns = 32;
float matrix[rows][columns];
int i; float x;
for (i = 0; i < 100; i++)matrix[FuncRow(i)][FuncCol(i)] += x;

Here, the code must compute (FuncRow(i)*columns + FuncCol(i)) *sizeof(float) in order to find the address of the matrix element. The multiplication bycolumns in this case is faster when columns is a power of two. In the preceding example,
this is not an issue because an optimizing compiler can see that the rows are accessed
consecutively and can calculate the address of each row by adding the length of a row tothe address of the preceding row.

39

The same advice applies to arrays of structure or class objects. The size (in bytes) of theobjects should preferably be a power of 2 if the elements are accessed in a non-sequential
order.
The advice of making the number of columns a power of 2 does not always apply to arraysthat are bigger than the level-1 data cache and accessed non-sequentially because it may
cause cache contentions. See page 91 for a discussion of this problem.

7.11 Type conversions
The C++ syntax has several different ways of doing type conversions:// Example 7.19
int i; float f;
f = i;
f = (float)i;f = float(i);f = static_cast<float>(i);// Implicit type conversion// C-style type casting// Constructor-style type casting// C++ casting operator

These different methods have exactly the same effect. Which method you use is a matter ofprogramming style. The time consumption of different type conversions is discussed below.

Signed / unsigned conversion// Example 7.20
int i;
if ((unsigned int)i < 10) { ...

Conversions between signed and unsigned integers simply makes the compiler interpret thebits of the integer in a different way. There is no checking for overflow, and the code takes
no extra time. These conversions can be used freely without any cost in performance.

Integer size conversion// Example 7.21
int i; short int s;
i = s;

An integer is converted to a longer size by extending the sign-bit if the integer is signed, orby extending with zero-bits if unsigned. This typically takes one clock cycle if the source isan arithmetic expression. The size conversion often takes no extra time if it is done inconnection with reading the value from a variable in memory, as in example 7.22.
// Example 7.22
short int a[100]; int i, sum = 0;
for (i=0; i<100; i++) sum += a[i];Converting an integer to a smaller size is done simply by ignoring the higher bits. There is
no check for overflow. Example:// Example 7.23
int i; short int s;s = (short int)i;

This conversion takes no extra time. It simply stores the lower 16 bits of the 32-bit integer.

40

Floating point precision conversion
Conversions between float, double and long double take no extra time when the
floating point register stack is used. It takes between 2 and 15 clock cycles (depending on
the processor) when the XMM registers are used. See page 31 for an explanation ofregister stack versus XMM registers. Example:// Example 7.24
float a; double b;a += b;In this example, the conversion is costly if XMM registers are used. a and b should be of thesame type to avoid this. See page 153 for further discussion.

Integer to float conversion
Conversion of a signed integer to a float or double takes 4 - 16 clock cycles, dependingon the processor and the type of registers used. Conversion of unsigned integers takes
longer time than signed integers unless the AVX512 instruction set is enabled (AVX512DQ
for 64 bit unsigned integers). It is faster to first convert the unsigned integer to a signed
integer if there is no risk of overflow:// Example 7.25
unsigned int u; double d;d = (double)(signed int)u;// Faster, but risk of overflow

Integer to float conversions can sometimes be avoided by replacing an integer variable by afloat variable. Example:// Example 7.26afloat a[100]; int i;for (i = 0; i < 100;i++) a[i] = 2 * i;

The conversion of i to float in this example can be avoided by making an additional floating
point variable:// Example 7.26b
float a[100]; int i; float i2;for (i = 0, i2 = 0; i < 100; i++, i2 += 2.0f) a[i] = i2;

Float to integer conversion
Conversion of a floating point number to an integer takes a very long time unless the SSE2
or later instruction set is enabled. Typically, the conversion takes 50 - 100 clock cycles. The
reason is that the C/C++ standard specifies truncation so the floating point rounding mode
has to be changed to truncation and back again.
If there are floating point-to-integer conversions in the critical part of a code then it isimportant to do something about it. Possible solutions are:
Avoid the conversions by using different types of variables.
Move the conversions out of the innermost loop by storing intermediate results as
floating point.
Use 64-bit mode or enable the SSE2 instruction set (requires a microprocessor that
supports this).
Use rounding instead of truncation and make a round function using assembly
language. See page 154 for details about rounding.
41

Pointer type conversion
A pointer can be converted to a pointer of a different type. Likewise, a pointer can beconverted to an integer, or an integer can be converted to a pointer. It is important that the
integer has enough bits for holding the pointer.
These conversions do not produce any extra code. It is simply a matter of interpreting thesame bits in a different way or bypassing syntax checks.
These conversions are not safe, of course. It is the responsibility of the programmer tomake sure the result is valid.

Re-interpreting the type of an object
It is possible to make the compiler treat a variable or object as if it had a different type by
type-casting its address:// Example 7.27
float x;*(int*)&x |= 0x80000000;// Set sign bit of x

The syntax may seem a little odd here. The address of x is type-casted to a pointer to an
integer, and this pointer is then de-referenced in order to access x as an integer. The
compiler does not produce any extra code for actually making a pointer. The pointer issimply optimized away and the result is that x is treated as an integer. But the & operator
forces the compiler to store x in memory rather than in a register. The above example sets
the sign bit of x by using the | operator which otherwise can only be applied to integers. It isfaster than x = -abs(x);.
There are a number of dangers to be aware of when type-casting pointers:•

The trick violates the strict aliasing rule of standard C, specifying that two pointers ofdifferent types cannot point to the same object (except for char pointers). An
optimizing compiler might store the floating point and integer representations in two
different registers. You need to check if the compiler does what you want it to. It issafer to use a union, as in example 14.22 page 155.

•

The trick will fail if the object is treated as bigger than it actually is. This above code
will fail if an int uses more bits than a float. (Both use 32 bits in x86 systems).

•

If you access part of a variable, for example 32 bits of a 64-bit double, then the code
will not be portable to platforms that use big endian storage.

•

If you access a variable in parts, for example if you write a 64-bit double 32 bits at atime, then the code is likely to execute slower than intended because of a store
forwarding delay in the CPU (See manual 3: "The microarchitecture of Intel, AMD
and VIA CPUs").

Const cast
The const_cast operator is used for relieving the const restriction from a pointer. It has
some syntax checking and is therefore more safe than the C-style type-casting without
adding any extra code. Example:// Example 7.28
class c1 {const int x;
public:c1() : x(0) {};void xplus2() {// constant data// constructor initializes x to 0// this function can modify x42

*const_cast<int*>(&x) += 2;}// add 2 to x};

The effect of the const_cast operator here is to remove the const restriction on x. It is a
way of relieving a syntax restriction, but it does not generate any extra code and does nottake any extra time. This is a useful way of making sure that one function can modify x,
while other functions cannot.

Static cast
The static_cast operator does the same as the C-style type-casting. It is used, forexample, to convert float to int.

Reinterpret cast
The reinterpret_cast operator is used for pointer conversions. It does the same as Cstyle type-casting with a little more syntax check. It does not produce any extra code.

Dynamic cast
The dynamic_cast operator is used for converting a pointer to one class to a pointer toanother class. It makes a runtime check that the conversion is valid. For example, when apointer to a base class is converted to a pointer to a derived class, it checks whether theoriginal pointer actually points to an object of the derived class. This check makes
dynamic_cast more time-consuming than a simple type casting, but also safer. It may
catch programming errors that would otherwise go undetected.

Converting class objects
Conversions involving class objects (rather than pointers to objects) are possible only if the
programmer has defined a constructor, an overloaded assignment operator, or an overloaded type casting operator that specifies how to do the conversion. The constructor or
overloaded operator is as efficient as a member function.

7.12 Branches and switch statements
The high speed of modern microprocessors is obtained by using a pipeline where
instructions are fetched and decoded in several stages before they are executed. However,the pipeline structure has one big problem. Whenever the code has a branch (e.g. an if-else
structure), the microprocessor does not know in advance which of the two branches to feed
into the pipeline. If the wrong branch is fed into the pipeline then the error is not detecteduntil 10 - 20 clock cycles later and the work it has done by fetching, decoding, and perhaps
speculatively executing instructions during this time has been wasted. The consequence isthat the microprocessor wastes several clock cycles whenever it feeds a branch into thepipeline and later discovers that it has chosen the wrong branch.
Microprocessor designers have gone to great lengths to reduce this problem. The mostimportant method that is used is branch prediction. Modern microprocessors are using
advanced algorithms to predict which way a branch will go based on the past history of thatbranch and other nearby branches. The algorithms used for branch prediction are different
for each type of microprocessor. These algorithms are described in detail in manual 3: "Themicroarchitecture of Intel, AMD and VIA CPUs".
A branch instruction takes typically 0 - 2 clock cycles in the case that the microprocessor
has made the right prediction. The time it takes to recover from a branch misprediction isapproximately 12 - 25 clock cycles, depending on the processor. This is called the branchmisprediction penalty.
Branches are relatively cheap if they are predicted most of the time, but expensive if they
are often mispredicted. A branch that always goes the same way is predicted well, of43

course. A branch that goes one way most of the time and rarely the other way ismispredicted only when it goes the other way. A branch that goes many times one way,then many times the other way is mispredicted only when it changes. A branch that follows
a simple periodic pattern can also be predicted quite well if it is inside a loop with few or noother branches. A simple periodic pattern can be, for example, to go one way two times andthe other way three times. Then again two times the first way and three times the other way,etc. The worst case is a branch that goes randomly one way or the other with a 50-50
chance of going either way. Such a branch will be mispredicted 50% of the time.
A for-loop or while-loop is also a kind of branch. After each iteration it decides whether torepeat or to exit the loop. The loop-branch is usually predicted well if the repeat count issmall and always the same. The maximum loop count that can be predicted perfectly varies
between 9 and 64, depending on the processor. Nested loops are predicted well only onsome processors. On many processors, a loop that contains several branches is notpredicted well.
A switch statements is a kind of branch that can go more than two ways. Switch statements
are most efficient if the case labels follow a sequence where each label is equal to thepreceding label plus one, because it can be implemented as a table of jump targets. A
switch statement with many labels that have values far from each other is inefficientbecause the compiler must convert it to a branch tree.
On older processors, a switch statement with sequential labels is simply predicted to go thesame way as last time it was executed. It is therefore certain to be mispredicted whenever itgoes another way than last time. Newer processors are sometimes able to predict a switch
statement if it follows a simple periodic pattern or if it is correlated with preceding branches
and the number of different targets is small.
The number of branches and switch statements should preferably be kept small in thecritical part of a program, especially if the branches are poorly predictable. It may be usefulto roll out a loop if this can eliminate branches, as explained in the next paragraph.
The target of branches and function calls are saved in a special cache called the branchtarget buffer. Contentions in the branch target buffer can occur if a program has manybranches or function calls. The consequence of such contentions is that branches can bemispredicted even if they otherwise would be predicted well. Even direct function calls canbe mispredicted for this reason. A program with many branches and function calls in thecritical part of the code can therefore suffer from mispredictions.
In some cases it is possible to replace a poorly predictable branch by a table lookup. For
example:// Example 7.29afloat a; bool b;
a = b ? 1.5f : 2.6f;

The ?: operator here is a branch. If it is poorly predictable then replace it by a table lookup:// Example 7.29b
float a; bool b = 0;
const float lookup[2] = {2.6f, 1.5f};
a = lookup[b];If a bool is used as an array index then it is important to make sure it is initialized or comesfrom a reliable source so that it can have no other values than 0 or 1. See page 34.
In some cases the compiler can automatically replace a branch by a conditional move.
44

The examples on page 147 and 148 show various ways of reducing the number ofbranches.
Manual 3: "The microarchitecture of Intel, AMD and VIA CPUs" gives more details on
branch predictions in the different microprocessors.

7.13 Loops
The efficiency of a loop depends on how well the microprocessor can predict the loop
control branch. See the preceding paragraph and manual 3: "The microarchitecture of Intel,
AMD and VIA CPUs" for an explanation of branch prediction. A loop with a small and fixed
repeat count and no branches inside can be predicted perfectly. As explained above, themaximum loop count that can be predicted depends on the processor. Nested loops are
predicted well only on some processors that have a special loop predictor. On otherprocessors, only the innermost loop is predicted well. A loop with a high repeat count ismispredicted only when it exits. For example, if a loop repeats a thousand times then theloop control branch is mispredicted only one time in thousand so the misprediction penalty
is only a negligible contribution to the total execution time.

Loop unrolling
In some cases it can be an advantage to unroll a loop. Example:// Example 7.30a
int i;for (i = 0; i < 20; i++) {if (i % 2 == 0) {FuncA(i);}else {
FuncB(i);}FuncC(i);}This loop repeats 20 times and calls alternately FuncA and FuncB, then FuncC. Unrolling
the loop by two gives:// Example 7.30b
int i;for (i = 0; i < 20; i += 2) {FuncA(i);FuncC(i);FuncB(i+1);
FuncC(i+1);}This has three advantages:•

The i<20 loop control branch is executed 10 times rather than 20.

•

The fact that the repeat count has been reduced from 20 to 10 means that it can bepredicted perfectly on most CPUs.

•

The if branch is eliminated.

Loop unrolling also has disadvantages:•

The unrolled loop takes up more space in the code cache or micro-op cache.
45•Many CPUs have a loopback buffer that improves performance for very small loops, as
explained in my microarchitecture manual. The unrolled loop is unlikely to fit into theloopback buffer.

•

If the repeat count is odd and you unroll by two then there is an extra iteration that has
to be done outside the loop. In general, you have this problem when the repeat count is
not certain to be divisible by the unroll factor.

Loop unrolling should only be used if there are specific advantages that can be obtained. If
a loop contains floating point calculations or vector instructions and the loop counter is an
integer, then you can generally assume that the overall computation time is determined by
the floating point code rather than by the loop control branch. There is nothing to gain byunrolling the loop in this case.
Loop unrolling is useful when there is a loop-carried dependency chain, as explained on
page 113.
Loop unrolling should preferably be avoided on processors with a micro-op cache because
it is important to economize the use of the micro-op cache. This is also the case forprocessors with a loopback buffer.
Compilers will usually unroll a loop automatically if this appears to be profitable (see page72). The programmer does not have to unroll a loop manually unless there is a specificadvantage to obtain, such as eliminating the if-branch in example 7.30b.

The loop control condition
The most efficient loop control condition is a simple integer counter. A microprocessor with
out-of-order capabilities (see page 113) will be able to evaluate the loop control statement
several iterations ahead.
It is less efficient if the loop control branch depends on the calculations inside the loop. The
following example converts a zero-terminated ASCII string to lower case:// Example 7.31achar string[100], *p = string;
while (*p != 0) *(p++) |= 0x20;

If the length of the string is already known then it is more efficient to use a loop counter:// Example 7.31bchar string[100], *p = string; int i, StringLength;
for (i = StringLength; i > 0; i--) *(p++) |= 0x20;

A common situation where the loop control branch depends on calculations inside the loop
is in mathematical iterations such as Taylor expansions and Newton-Raphson iterations.
Here the iteration is repeated until the residual error is lower than a certain tolerance. The
time it takes to calculate the absolute value of the residual error and compare it to thetolerance may be so high that it is more efficient to determine the worst-case maximum
repeat count and always use this number of iterations. The advantage of this method is thatthe microprocessor can execute the loop control branch ahead of time and resolve any
branch misprediction long before the floating point calculations inside the loop are finished.
This method is advantageous if the typical repeat count is near the maximum repeat countand the calculation of the residual error for each iteration is a significant contribution to thetotal calculation time.
A loop counter should preferably be an integer. If a loop needs a floating point counter thenmake an additional integer counter. Example:
46

// Example 7.32a
double x, n, factorial = 1.0;
for (x = 2.0; x <= n; x++) factorial *= x;This can be improved by adding an integer counter and using the integer in the loop control
condition:// Example 7.32b
double x, n, factorial = 1.0; int i;for (i = (int)n - 2, x = 2.0; i >= 0; i--, x++) factorial *= x;

Note the difference between commas and semicolons in a loop with multiple counters, as inexample 7.32b. A for-loop has three clauses: initialization, condition, and increment. The
three clauses are separated by semicolons, while multiple statements within each clause
are separated by commas. There should be only one statement in the condition clause.
Comparing an integer to zero is sometimes more efficient than comparing it to any othernumber. Therefore, it is slightly more efficient to make a loop count down to zero thanmaking it count up to some positive value, n. But not if the loop counter is used as an arrayindex. The data cache is optimized for accessing arrays forwards, not backwards.

Copying or clearing arrays
It may not be optimal to use a loop for trivial tasks such as copying an array or setting anarray to all zeroes. Example:// Example 7.33a
const int size = 1000; int i;
float a[size], b[size];// set a to zerofor (i = 0; i < size; i++) a[i] = 0.0;// copy a to b
for (i = 0; i < size; i++) b[i] = a[i];It is often faster to use the functions memset and memcpy:// Example 7.33b
const int size = 1000;
float a[size], b[size];// set a to zeromemset(a, 0, sizeof(a));// copy a to b
memcpy(b, a, sizeof(b));

Most compilers will automatically replace such loops by calls to memset and memcpy, atleast in simple cases. The explicit use of memset and memcpy is unsafe because serious
errors can happen if the size parameter is bigger than the destination array. But the sameerrors can happen with the loops if the loop count is too big.

7.14 Functions
Function calls may slow down a program for the following reasons:•

The function call makes the microprocessor jump to a different code address and backagain. This may take up to 4 clock cycles. In most cases the microprocessor is able tooverlap the call and return operations with other calculations to save time.

•

The code cache works less efficiently if the code is fragmented and scattered around inmemory.
47•Function parameters are stored on the stack in 32-bit mode. Storing the parameters on
the stack and reading them again takes extra time. The delay is significant if aparameter is part of a critical dependency chain.

•

Extra time is needed for setting up a stack frame, saving and restoring registers, andpossibly save exception handling information.

•

Each function call statement occupies a space in the branch target buffer (BTB).
Contentions in the BTB can cause branch mispredictions if the critical part of a program
has many calls and branches.

The following methods may be used for reducing the time spent on function calls in thecritical part of a program.

Avoid unnecessary functions
Some programming textbooks recommend that every function that is longer than a few lines
should be split up into multiple functions. I disagree with this rule. Splitting up a function into
multiple smaller functions only makes the program less efficient. Splitting up a function justbecause it is long does not make the program more clear unless the function is doing
multiple logically distinct tasks. A critical innermost loop should preferably be kept entirely
inside one function, if possible.

Use inline functions
An inline function is expanded like a macro so that each statement that calls the function is
replaced by the function body. A function is usually inlined if the inline keyword is used or
if its body is defined inside a class definition. Inlining a function is advantageous if thefunction is small or if it is called only from one place in the program. Small functions areoften inlined automatically by the compiler. On the other hand, the compiler may in somecases ignore a request for inlining a function if the inlining causes technical problems or
performance problems.

Avoid nested function calls in the innermost loop
A function that calls other functions is called a frame function, while a function that does notcall any other function is called a leaf function. Leaf functions are more efficient than frame
functions for reasons explained on page 63. If the critical innermost loop of a program
contains calls to frame functions then the code can probably be improved by inlining theframe function or by turning the frame function into a leaf function by inlining all thefunctions that it calls.

Use macros instead of functions
A macro declared with #define is certain to be inlined. But beware that macro parameters
are evaluated every time they are used. Example:// Example 7.34a. Use macro as inline function#define MAX(a,b) (a > b ? a : b)
y = MAX(f(x), g(x));
In this example, f(x) or g(x) is calculated twice because the macro is referencing it twice.
You can avoid this by using an inline function instead of a macro. If you want the function towork with any type of parameters then make it a template:// Example 7.34b. Replace macro by template
template <typename T>
static inline T max(T const & a, T const & b) {return a > b ? a : b;}48

Another problem with macros is that the name cannot be overloaded or limited in scope. A
macro will interfere with any function or variable having the same name, regardless of scopeor namespaces. Therefore, it is important to use long and unique names for macros,
especially in header files.

Use fastcall and vectorcall functions
The keyword __fastcall changes the function calling method in 32-bit mode so that thefirst two integer parameters are transferred in registers rather than on the stack. This canimprove the speed of functions with integer parameters.
Floating point parameters are not affected by __fastcall. The implicit 'this' pointer inmember functions is also treated like a parameter, so there may be only one free register
left for transferring additional parameters. Therefore, make sure that the most critical integer
parameter comes first when you are using __fastcall. Function parameters aretransferred in registers by default in 64-bit mode. Therefore, the __fastcall keyword is
not recognized in 64-bit mode.
The keyword __vectorcall changes the function calling method in Windows systems forfloating point and vector operands so that parameters are passed and returned in vector
registers. This is particularly advantageous in 64 bit Windows. The __vectorcall method
is currently supported by Microsoft, Intel, and Clang compilers.

Make functions local
A function that is used only within the same module (i.e. the current .cpp file) should bemade local. This makes it easier for the compiler to inline the function and to optimize
across function calls. There are three ways to make a function local:
1. Add the keyword static to the function declaration. This is the simplest method,
but it does not work with class member functions, where static has a different
meaning.
2. Put the function or class into an anonymous namespace.
3. The Gnu compiler allows "__attribute__((visibility("hidden")))".

Use whole program optimization
Some compilers have an option for whole program optimization or for combining multiple.cpp files into a single object file. This enables the compiler to optimize register allocationand parameter transfer across all .cpp modules that make up a program. Whole program
optimization cannot be used for function libraries distributed as object or library files.

Use 64-bit mode
Parameter transfer is more efficient in 64-bit mode than in 32-bit mode, and more efficient in
64-bit Linux than in 64-bit Windows. In 64-bit Linux, the first six integer parameters and thefirst eight floating point parameters are transferred in registers, totaling up to fourteen
register parameters. In 64-bit Windows, the first four parameters are transferred in registers,
regardless of whether they are integers or floating point numbers. Therefore, 64-bit Linux ismore efficient than 64-bit Windows if functions have more than four parameters. There is nodifference between 32-bit Linux and 32-bit Windows in this respect.
The number of registers available for floating point and vector variables is 8 registers in 32bit mode, and 16 registers in 64-bit mode. It is further increased to 32 registers in 64 bitmode when the AVX512 instruction set is enabled. A high number of registers improves theperformance because the compiler can store variables in registers rather than in memory.
49

7.15 Function parameters
Function parameters are transferred by value in most cases. This means that the value ofthe parameter is copied to a local variable. This is efficient for simple types such as int,
float, double, bool, enum as well as pointers and references.
Arrays are always transferred as pointers unless they are wrapped into a class or structure.
The situation is more complex if the parameter has a composite type such as a structure orclass. The transfer of a parameter of composite type is most efficient if all of the following
conditions are met:••••

the object is so small that it fits into a single registerthe object has no copy constructor and no destructor
the object has no virtual member
the object does not use runtime type identification (RTTI)

If any of these conditions is not met then it is usually faster to transfer a pointer or reference
to the object. If the object is large then it obviously takes time to copy the entire object. Any
copy constructor must be called when the object is copied to the parameter, and thedestructor, if any, must be called before the function returns.
The preferred method for transferring composite objects to a function is by a const
reference. A const reference makes sure that the original object is not modified. Unlike apointer or a non-const reference, a const reference allows the function argument to be an
expression or an anonymous object. The compiler can easily optimize away a const
reference if the function is inlined.
An alternative solution is to make the function a member of the object's class or structure.
This is equally efficient.
Simple function parameters are transferred on the stack in 32-bit systems, but in registers in
64-bit systems. The latter is more efficient. 64-bit Windows allows a maximum of fourparameters to be transferred in registers. 64-bit Unix systems allow up to fourteenparameters to be transferred in registers (8 float or double plus 6 integer, pointer orreference parameters). The this pointer in member functions counts a one parameter.
Further details are given in manual 5: "Calling conventions for different C++ compilers andoperating systems".

7.16 Function return types
The return type of a function should preferably be a simple type, a pointer, a reference, or
void. Returning objects of a composite type is more complex and often inefficient.
Objects of a composite type can be returned in registers only in the simplest cases. See
manual 5: "Calling conventions for different C++ compilers and operating systems" fordetails on when objects can be returned in registers.
Except for the simplest cases, composite objects are returned by copying them into a place
indicated by the caller through a hidden pointer. The copy constructor, if any, is usuallycalled in the copying process, and the destructor is called when the original is destroyed. In
simple cases, the compiler may be able to avoid the calls to the copy constructor and thedestructor by constructing the object on its final destination, but do not count on it.
Instead of returning a composite object, you may consider the following alternatives:•

Make the function a constructor for the object.
50•

Make the function modify an existing object rather than making a new one. The
existing object can be made available to the function through a pointer or reference,or the function could be a member of the object's class.

•Make the function return a pointer or reference to a static object defined inside thefunction. This is efficient, but risky. The returned pointer or reference is valid only
until the next time the function is called and the local object is overwritten, possibly ina different thread. If you forget to make the local object static then it becomes invalid
as soon as the function returns.

•

Make the function construct an object with new and return a pointer to it. This isinefficient because of the costs of dynamic memory allocation. This method also
involves the risk of memory leaks if you forget to delete the object.

7.17 Function tail calls
A tail call is a way of optimizing function calls. If the last statement of a function is a call toanother function, then the compiler can replace the call by a jump to the second function. An
optimizing compiler will do this automatically. The second function will not return to the firstfunction, but directly to the place where the first function was called from. This is more
efficient because it eliminates a return. Example:// Example 7.35. Tail callvoid function2(int x);
void function1(int y) {...
function2(y+1);}

Here, the return from function1 is eliminated by jumping directly to function2. This
works even if there is a return value:// Example 7.36. Tail call with return value
int function2(int x);
int function1(int y) {...
return function2(y+1);}

The tail call optimization works only if the two functions have the same return type. If thefunctions have parameters on the stack (which is mostly the case in 32-bit mode) then thetwo functions must use the same amount of stack space for parameters.

7.18 Recursive functions
A recursive function is a function that calls itself. Recursive function calls can be useful for
handling recursive data structures. The cost of recursive functions is that all parameters andlocal variables get a new instance for every recursion, and this takes up stack space. Deep
recursions also makes the prediction of return addresses less efficient. This problem
typically appears with recursion levels deeper than 16 (see the explanation of return stackbuffer in manual 3: "The microarchitecture of Intel, AMD and VIA CPUs").
Recursive function calls can still be the most efficient solution for handling a branching data
tree structure. Recursion is more efficient if the tree structure is broad than if it is deep. A
non-branching recursion can always be replaced by a loop, which is more efficient. A
common textbook example of a recursive function is the factorial function:
51

// Example 7.37. Factorial as recursive function
unsigned long int factorial(unsigned int n) {if (n < 2) return 1;
return n * factorial(n-1);}This implementation is very inefficient because all the instances of n and all the return
addresses take up storage space on the stack. It is more efficient to use a loop:// Example 7.38. Factorial function as loop
unsigned long int factorial(unsigned int n) {unsigned long int product = 1;
while (n > 1) {product *= n;n--;}return product;}

Recursive tail calls are more efficient than other recursive calls, but still less efficient than aloop.
Novice programmers sometimes make a call to main in order to restart their program. Thisis a bad idea because the stack becomes filled up with new instances of all local variables
for every recursive call to main. The proper way to restart a program is to make a loop in
main.

7.19 Structures and classes
Nowadays, programming textbooks recommend object oriented programming as a meansof making software more clear and modular. The so-called objects are instances ofstructures and classes. The object oriented programming style has both positive andnegative impacts on program performance. The positive effects are:•Variables that are used together are also stored together if they are members of thesame structure or class. This makes data caching more efficient.

•

Variables that are members of a class need not be passed as parameters to a classmember function. The overhead of parameter transfer is avoided for these variables.

The negative effects of object oriented programming are:•Some programmers are dividing the code into too many small classes. This isinefficient.

•Non-static member functions have a 'this' pointer which is transferred as an implicit
parameter to the function. The overhead of parameter transfer for 'this' is incurredon all non-static member functions.

•

The 'this' pointer takes up one register. Registers are a scarce resource in 32-bit
systems.

•

Virtual member functions are less efficient (see page 55).

No general statement can be made about whether the positive or the negative effects ofobject oriented programming are dominating. At least, it can be said that the use of classes
and member functions is not expensive. You may use an object oriented programming style52

if it is good for the logical structure and clarity of the program as long as you avoid anexcessive number of function calls in the most critical part of the program. The use ofstructures (without member functions) has no negative effect on performance.

7.20 Class data members (instance variables)
The data members of a class or structure are stored consecutively in the order in which they
are declared whenever an instance of the class or structure is created. There is no
performance penalty for organizing data into classes or structures. Accessing a data
member of a class or structure object takes no more time than accessing a simple variable.
Most compilers will align data members to round addresses in order to optimize access, as
given in the following table.
Type
bool
char, signed or unsigned
short int, signed or unsigned
int, signed or unsigned
64-bit integer, signed or unsigned
pointer or reference, 32-bit mode
pointer or reference, 64-bit mode
float
doublelong double

size, bytes
1
1
2
4
8
4
8
4
8
8, 10, 12 or 16alignment, bytes
1
1
2
4
8
4
8
4
8
8 or 16

Table 7.2. Alignment of data members.

This alignment can cause holes of unused bytes in a structure or class with members of
mixed sizes. For example:// Example 7.39astruct S1 {short int a; // 2 bytes. first byte at 0, last byte at 1// 6 unused bytes
double b;// 8 bytes. first byte at 8, last byte at 15
int d;// 4 bytes. first byte at 16, last byte at 19// 4 unused bytes};
S1 ArrayOfStructures[100];

Here, there are 6 unused bytes between a and b because b has to start at an address
divisible by 8. There are also 4 unused bytes in the end. The reason for this is that the next
instance of S1 in the array must begin at an address divisible by 8 in order to align its bmember by 8. The number of unused bytes can be reduced to 2 by putting the smallest
members last:// Example 7.39b
struct S1 {double b;// 8 bytes. first byte at 0, last byte at 7
int d;// 4 bytes. first byte at 8, last byte at 11
short int a; // 2 bytes. first byte at 12, last byte at 13// 2 unused bytes};
S1 ArrayOfStructures[100];

This reordering has made the structure 8 bytes smaller and the array 800 bytes smaller.

53

Structure and class objects can often be made smaller by reordering the data members. If
the class has at least one virtual member functions then there is a pointer to a virtual table
before the first data member or after the last member. This pointer is 4 bytes in 32-bit
systems and 8 bytes in 64-bit systems. If you are in doubt how big a structure or each of its
members are then you may make some tests with the sizeof operator. The value returnedby the sizeof operator includes any unused bytes in the end of the object.
The code for accessing a data member is more compact if the offset of the member relative
to the beginning of the structure or class is less than 128 because the offset can be
expressed as an 8-bit signed number. If the offset relative to the beginning of the structure
or class is 128 bytes or more then the offset has to be expressed as a 32-bit number (theinstruction set has nothing between 8 bit and 32 bit offsets). Example:// Example 7.40
class S2 {public:int a[100]; // 400 bytes. first byte at
0, last byte at 399
int b;// 4 bytes.
first byte at 400, last byte at 403
int ReadB() {return b;}};

The offset of b is 400 here. Any code that accesses b through a pointer or a member
function such as ReadB needs to code the offset as a 32-bit number. If a and b areswapped then both can be accessed with an offset that is coded as an 8-bit signed number,or no offset at all. This makes the code more compact so that the code cache is used more
efficiently. It is therefore recommended that big arrays and other big objects come last in a
structure or class declaration and the most often used data members come first. If it is notpossible to contain all data members within the first 128 bytes then put the most often usedmembers in the first 128 bytes.

7.21 Class member functions (methods)
Each time a new object of a class is declared or created it will generate a new instance ofthe data members. But each member function has only one instance. The function code is
not copied because the same code can be applied to all instances of the class.
Calling a member function is as fast as calling a simple function with a pointer or reference
to a structure. For example:// Example 7.41
class S3 {public:int a;
int b;
int Sum1() {return a + b;}};
int Sum2(S3 * p) {return p->a + p->b;}
int Sum3(S3 & r) {return r.a + r.b;}

The three functions Sum1, Sum2 and Sum3 are doing exactly the same thing and they are
equally efficient. If you look at the code generated by the compiler, you will notice that somecompilers will make exactly identical code for the three functions. Sum1 has an implicit'this' pointer which does the same thing as p and r in Sum2 and Sum3. Whether you wantto make the function a member of the class or give it a pointer or reference to the class or
structure is simply a matter of programming style. Some compilers make Sum1 slightly more
efficient than Sum2 and Sum3 in 32-bit Windows by transferring 'this' in a register rather
than on the stack.
54

A static member function cannot access any non-static data members or non-static
member functions. A static member function is faster than a non-static member function
because it does not need the 'this' pointer. You may make member functions faster bymaking them static if they do not need any non-static access.

7.22 Virtual member functions
Virtual functions are used for implementing polymorphic classes. Each instance of apolymorphic class has a pointer to a table of pointers to the different versions of the virtualfunctions. This so-called virtual table is used for finding the right version of the virtualfunction at runtime. Polymorphism is one of the main reasons why object oriented programs
can be less efficient than non-object oriented programs. If you can avoid virtual functions
then you can obtain most of the advantages of object oriented programming without paying
the performance costs.
The time it takes to call a virtual member function is a few clock cycles more than it takes tocall a non-virtual member function, provided that the function call statement always calls thesame version of the virtual function. If the version changes then you may get a misprediction
penalty of 10 - 20 clock cycles. The rules for prediction and misprediction of virtual function
calls is the same as for switch statements, as explained on page 44.
The dispatching mechanism can be bypassed when the virtual function is called on an
object of known type, but you cannot always rely on the compiler bypassing the dispatch
mechanism even when it would be obvious to do so. See page 76.
Runtime polymorphism is needed only if it cannot be known at compile time which version
of a polymorphic member function is called. If virtual functions are used in a critical part of aprogram then you may consider whether it is possible to obtain the desired functionality
without polymorphism or with compile-time polymorphism.
It is sometimes possible to obtain the desired polymorphism effect with templates instead of
virtual functions. The template parameter should be a class containing the functions thathave multiple versions. This method is faster because the template parameter is always
resolved at compile time rather than at runtime. Example 7.47 on page 59 shows anexample of how to do this. Unfortunately, the syntax is so kludgy that it may not be worth
the effort.

7.23 Runtime type identification (RTTI)
Runtime type identification adds extra information to all class objects and is not efficient. If
the compiler has an option for RTTI then turn it off and use alternative implementations.

7.24 InheritanceAn object of a derived class is implemented in the same way as an object of a simple classcontaining the members of both parent and child class. Members of parent and child classare accessed equally fast. In general, you can assume that there is hardly any performance
penalty to using inheritance.
There may be a degradation in code caching and data caching for the following reasons:•

The child class includes all data members of the parent class, even if it does notneed them.

•

The size of the parent class data members is added to the offset of the child classmembers. The code that accesses data members with a total offset bigger than 127
55

bytes is slightly less compact. See page 54.
•

The member functions of parent and child are possibly stored in different modules.
This may cause a lot of jumping around and less efficient code caching. This
problem can be solved by making sure that functions which are called near eachother are also stored near each other. See page 92 for details.

Inheritance from multiple parent classes in the same generation can cause complications
with member pointers and virtual functions or when accessing an object of a derived classthrough a pointer to one of the base classes. You may avoid multiple inheritance by making
objects inside the derived class:// Example 7.42a. Multiple inheritanceclass B1; class B2;
class D : public B1, public B2 {public:int c;};

Replace with:// Example 7.42b. Alternative to multiple inheritanceclass B1; class B2;
class D : public B1 {public:B2 b2;
int c;};

In general, you should use inheritance only if it is good for logic structure of the program.

7.25 Constructors and destructors
A constructor is implemented internally as a member function which returns a reference tothe object. The allocation of memory for a new object is not necessarily done by theconstructor itself. Constructors are therefore as efficient as any other member functions.
This applies to default constructors, copy constructors, and any other constructors.
A class does not need a constructor. A default constructor is not needed if the object does
not need initialization. A copy constructor is not needed if the object can be copied simply
by copying all data members. A simple constructor may be inlined for improved
performance.
A copy constructor may be called whenever an object is copied by assignment, as afunction parameter, or as a function return value. The copy constructor can be a time
consumer if it involves allocation of memory or other resources. There are various ways to
avoid this wasteful copying of memory blocks, for example:• Use a reference or pointer to the object instead of copying it• Use a "move constructor" to transfer ownership of the memory block. This requires
C++0x or later.
• Make a member function or friend function or operator that transfers ownership ofthe memory block from one object to another. The object that loses ownership of thememory block should have its pointer set to NULL. There should of course be adestructor that destroys any memory block that the object owns.
A destructor is as efficient as a member function. Do not make a destructor if it is notnecessary. A virtual destructor is as efficient as a virtual member function. See page 55.

56

7.26 Unions
A union is a structure where data members share the same memory space. A union can beused for saving memory space by allowing two data members that are never used at thesame time to share the same piece of memory. See page 94 for an example.
A union can also be used for accessing the same data in different ways. Example:// Example 7.43
union {
float f;int i;} x;
x.f = 2.0f;
x.i |= 0x80000000;
cout << x.f;// set sign bit of f// will give -2.0

In this example, the sign bit of f is set by using the bitwise OR operator, which can only beapplied to integers.

7.27 Bitfields
Bitfields may be useful for making data more compact. Accessing a member of a bitfield isless efficient than accessing a member of a structure. The extra time may be justified incase of large arrays if it can save cache space or make files smaller.
It is faster to compose a bitfield by the use of << and | operations than to write themembers individually. Example:// Example 7.44astruct Bitfield {int a:4;
int b:2;
int c:2;};
Bitfield x;
int A, B, C;
x.a = A;x.b = B;x.c = C;

Assuming that the values of A, B and C are too small to cause overflow, this code can be
improved in the following way:// Example 7.44b
union Bitfield {struct {int a:4;
int b:2;
int c:2;};
char abc;};
Bitfield x;
int A, B, C;
x.abc = A | (B << 4) | (C << 6);

Or, if protection against overflow is needed:// Example 7.44c
x.abc = (A & 0x0F) | ((B & 3) << 4) | ((C & 3) <<6 );57

7.28 Overloaded functions
The different versions of an overloaded function are simply treated as different functions.
There is no performance penalty for using overloaded functions.

7.29 Overloaded operators
An overloaded operator is equivalent to a function. Using an overloaded operator is exactlyas efficient as using a function that does the same thing.
An expression with multiple overloaded operators will cause the creation of temporary
objects for intermediate results, which may be undesired. Example:// Example 7.45aclass vector {public:float x, y;
vector() {}vector(float a, float b) {x = a; y = b;}
vector operator + (vector const & a) {return vector(x + a.x, y + a.y);}};vector a, b, c, d;
a = b + c + d;// 2-dimensional vector// x,y coordinates// default constructor// constructor// sum operator// add elements// makes intermediate object for (b + c)

The creation of a temporary object for the intermediate result (b+c) can be avoided byjoining the operations:// Example 7.45b
a.x = b.x + c.x + d.x;a.y = b.y + c.y + d.y;Fortunately, most compilers will do this optimization automatically in simple cases.

7.30 Templates
A template is similar to a macro in the sense that the template parameters are replaced bytheir values before compilation. The following example illustrates the difference between afunction parameter and a template parameter:// Example 7.46
int Multiply (int x, int m) {return x * m;}
template <int m>
int MultiplyBy (int x) {return x * m;}
int a, b;
a = Multiply(10,8);b = MultiplyBy<8>(10);a and b will both get the value 10 * 8 = 80. The difference lies in the way m is transferred to
the function. In the simple function, m is transferred at runtime from the caller to the called
function. But in the template function, m is replaced by its value at compile time so that thecompiler sees the constant 8 rather than the variable m. The advantage of using a templateparameter rather than a function parameter is that the overhead of parameter transfer is58

avoided. The disadvantage is that the compiler needs to make a new instance of thetemplate function for each different value of the template parameter. If MultiplyBy in thisexample is called with many different factors as template parameters then the code canbecome very big.
In the above example, the template function is faster than the simple function because thecompiler knows that it can multiply by a power of 2 by using a shift operation. x*8 is
replaced by x<<3, which is faster. In the case of the simple function, the compiler does notknow the value of m and therefore cannot do the optimization unless the function can beinlined. (In the above example, the compiler is able to inline and optimize both functions andsimply put 80 into a and b. But in more complex cases it might not be able to do so).
A template parameter can also be a type. The example on page 38 shows how you canmake arrays of different types with the same template.
Templates are efficient because the template parameters are always resolved at compile
time. Templates make the source code more complex, but not the compiled code. In
general, there is no cost in terms of execution speed to using templates.
Two or more template instances will be joined into one if the template parameters areexactly the same. If the template parameters differ then you will get one instance for each
set of template parameters. A template with many instances makes the compiled code big
and uses more cache space.
Excessive use of templates makes the code difficult to read. If a template has only one
instance then you may as well use a #define, const or typedef instead of a templateparameter.
Templates may be used for metaprogramming, as explained at page 163.

Using templates for polymorphism
A template class can be used for implementing a compile-time polymorphism, which is more
efficient than the runtime polymorphism that is obtained with virtual member functions. The
following example shows first the runtime polymorphism:// Example 7.47a. Runtime polymorphism with virtual functionsclass CHello {public:void NotPolymorphic();// Non-polymorphic functions go here
virtual void Disp();// Virtual function
void Hello() {cout << "Hello ";Disp();// Call to virtual function}};
class C1 : public CHello {public:virtual void Disp() {cout << 1;}};class C2 : public CHello {public:virtual void Disp() {cout << 2;
}
};

59

void test () {C1 Object1; C2 Object2;
CHello * p;
p = &Object1;
p->NotPolymorphic();
p->Hello();p = &Object2;
p->Hello();}// Called directly// Writes "Hello 1"// Writes "Hello 2"

The dispatching to C1::Disp() or C2::Disp() is done at runtime here if the compiler
does not know what class of object p points to (see page 76). The best compilers may beable to optimize away p and inline the call to Object1.Hello() in cases like this.
If it is known at compile-time whether the object belongs to class C1 or C2, then we can
avoid the inefficient virtual function dispatch process. This can be done with a special trick
which is used in the Active Template Library (ATL) and Windows Template Library (WTL):// Example 7.47b. Compile-time polymorphism with templates// Place non-polymorphic functions in the grandparent class:class CGrandParent {public:void NotPolymorphic();};// Any function that needs to call a polymorphic function goes in the// parent class. The child class is given as a template parameter:
template <typename MyChild>class CParent : public CGrandParent {public:void Hello() {cout << "Hello ";// call polymorphic child function:(static_cast<MyChild*>(this))->Disp();}};// The child classes implement the functions that have multiple// versions:class CChild1 : public CParent<CChild1> {public:void Disp() {cout << 1;}};class CChild2 : public CParent<CChild2> {public:void Disp() {cout << 2;}};void test () {CChild1 Object1;
CChild1 * p1;
p1 = &Object1;
p1->Hello();
CChild2 * p2;
p2 = &Object2;
p2->Hello();}

CChild2 Object2;// Writes "Hello 1"// Writes "Hello 2"60Here CParent is a template class which gets information about its child class through atemplate parameter. It can call the polymorphic member of its child class by type-casting its'this' pointer to a pointer to its child class. This is only safe if it has the correct child classname as template parameter. In other words, you must make sure that the declaration
class CChild1 : public CParent<CChild1> {has the same name for the child class name and the template parameter.
The order of inheritance is now as follows. The first generation class (CGrandParent)
contains any non-polymorphic member functions. The second generation class(CParent<>) contains any member functions that need to call a polymorphic function. The
third generations classes contain the different versions of the polymorphic functions. The
second generation class gets information about the third generation class through atemplate parameter.
No time is wasted on runtime dispatch to virtual member functions if the class of the object
is known. This information is contained in p1 and p2 having different types. A disadvantage
is that CParent::Hello() has multiple instances that take up cache space.
The syntax in example 7.47b is admittedly very kludgy. The few clock cycles that we may
save by avoiding the virtual function dispatch mechanism is rarely enough to justify such acomplicated code that is difficult to understand and therefore difficult to maintain. If thecompiler is able to do the devirtualization (see page 76) automatically then it is certainly
more convenient to rely on compiler optimization than to use this complicated template
method.

7.31 Threads
Threads are used for doing two or more jobs simultaneously or seemingly simultaneously.
Modern CPUs have multiple cores that makes it possible to run multiple threads
simultaneously. Each thread will get time slices of typically 30 ms for foreground jobs and
10 ms for background jobs when there are more threads than CPU cores. The context
switches after each time slice are quite costly because all caches have to adapt to the newcontext. It is possible to reduce the number of context switches by making longer timeslices. This will make applications run faster at the cost of longer response times for user
input.
Threads are useful for assigning different priorities to different tasks. For example, in a word
processor the user expects an immediate response to pressing a key or moving the mouse.
This task must have a high priority. Other tasks such as spell-checking and repagination are
running in other threads with lower priority. If the different tasks were not divided into
threads with different priorities, then the user might experience unacceptably long response
times to keyboard and mouse inputs when the program is busy doing the spell checking.
Any task that takes a long time, such as heavy mathematical calculations, should bescheduled in a separate thread if the application has a graphical user interface. Otherwise
the program will be unable to respond quickly to keyboard or mouse input.
It is possible to make a thread-like scheduling in an application program without invoking theoverhead of the operating system thread scheduler. This can be accomplished by doing theheavy background calculations piece by piece in a function that is called from the message
loop of a graphical user interface (OnIdle in Windows MFC). This method may be fasterthan making a separate thread in systems with few CPU cores, but it requires that thebackground job can be divided into small pieces of a suitable duration.
61

The best way to fully utilize systems with multiple CPU cores is to divide the job into multiple
threads. Each thread can then run on its own CPU core.
There are four kinds of costs to multithreading that we have to take into account when
optimizing multithreaded applications:•

The cost of starting and stopping threads. Do not put a task into a separate thread if
it is short in duration compared with the time it takes to start and stop the thread.

•

The cost of task switching. This cost is minimized if the number of threads with thesame priority is no more than the number of CPU cores.

•

The cost of synchronizing and communicating between threads. The overhead ofsemaphores, mutexes, etc. is considerable. If two threads are often waiting for eachother in order to get access to the same resource, then it may be better to join theminto one thread. A variable that is shared between multiple threads must be declared
volatile. This prevents the compiler from storing that variable in a register, which
is not shared between threads.

•

The different threads need separate storage. No function or class that is used bymultiple threads should rely on static or global variables. (See thread-local storage p.
28) The threads have each their stack. This can cause cache contentions if thethreads share the same cache.

Multithreaded programs must use thread-safe functions. A thread-safe function should
never use static variables.
See chapter 10 page 110 for further discussion of the techniques of multithreading.

7.32 Exceptions and error handling
Runtime errors cause exceptions which can be detected in the form of traps or softwareinterrupts. These exceptions can be caught with the use of try-catch blocks. The program
will crash with an error message if exception handling is enabled and there is no try-catch
block.
Exception handling is intended for detecting errors that seldom occur and recovering fromerror conditions in a graceful way. You may think that exception handling takes no extra
time as long as the error does not occur, but unfortunately this is not always true. The
program may have to do a lot of bookkeeping in order to know how to recover in the eventof an exception. The costs of this bookkeeping depends very much on the compiler. Somecompilers have efficient table-based methods with little or no overhead while othercompilers have inefficient code-based methods or require runtime type identification (RTTI),
which affects other parts of the code. See ISO/IEC TR18015 Technical Report on C++
Performance for further explanation.
The following example explains why bookkeeping is needed:// Example 7.48
class C1 {public:...
~C1();};
void F1() {
C1 x;
...

62

}
void F0() {
try {
F1();
}
catch (...) {...
}}The function F1 is supposed to call the destructor for the object x when it returns. But what
if an exception occurs somewhere in F1? Then we are breaking out of F1 without returning.
F1 is prevented from cleaning up because it has been interrupted. Now it is the
responsibility of the exception handler to call the destructor of x. This is only possible if F1
has saved all information about the destructor to call or any other cleanup that may benecessary. If F1 calls another function which in turn calls another function, etc., and if an
exception occurs in the innermost function, then the exception handler needs all information
about the chain of function calls and it needs to follow the track backwards though thefunction calls to check for all the necessary cleanup jobs to do. This is called stackunwinding.
All functions have to save some information for the exception handler, even if no exception
ever happens. This is the reason why exception handling can be expensive in somecompilers. If exception handling is not necessary for your application then you should
disable it in order to make the code smaller and more efficient. You can disable exception
handling for the whole program by turning off the exception handling option in the compiler.
You can disable exception handling for a single function by adding throw() to the function
prototype:void F1() throw();

This allows the compiler to assume that F1 will never throw any exception so that it does nothave to save recovery information for function F1. However, if F1 calls another function F2
that can possibly throw an exception then F1 has to check for exceptions thrown by F2 andcall the std::unexpected() function in case F2 actually throws an exception. Therefore,
you should apply the empty throw() specification to F1 only if all functions called by F1
also have an empty throw() specification. The empty throw()specification is useful for
library functions.
The compiler makes a distinction between leaf functions and frame functions. A frame
function is a function that calls at least one other function. A leaf function is a function that
does not call any other function. A leaf function is simpler than a frame function because thestack unwinding information can be left out if exceptions can be ruled out or if there isnothing to clean up in case of an exception. A frame function can be turned into a leaf
function by inlining all the functions that it calls. The best performance is obtained if thecritical innermost loop of a program contains no calls to frame functions.
While an empty throw() statement can improve optimizations in some cases, there is no
reason to add statements like throw(A,B,C) to tell explicitly what kind of exceptions afunction can throw. In fact, the compiler may actually add extra code to check that thrown
exceptions are indeed of the specified types (See Sutter: A Pragmatic Look at Exception
Specifications, Dr Dobbs Journal, 2002).
In some cases, it is optimal to use exception handling even in the most critical part of aprogram. This is the case if alternative implementations are less efficient and you want to beable to recover from errors. The following example illustrates such a case:63

// Example 7.49// Portability note: This example is specific to Microsoft compilers.
// It will look different in other compilers.
#include <excpt.h>#include <float.h>#include <math.h>#define EXCEPTION_FLT_OVERFLOW 0xC0000091L
void MathLoop() {const int arraysize = 1000; unsigned int dummy;
double a[arraysize], b[arraysize], c[arraysize];// Enable exception for floating point overflow:_controlfp_s(&dummy, 0, _EM_OVERFLOW);// _controlfp(0, _EM_OVERFLOW); // if above line doesn't work
int i = 0;// Initialize loop counter outside both loops// The purpose of the while loop is to resume after exceptions:while (i < arraysize) {// Catch exceptions in this block:__try {// Main loop for calculations:for ( ; i < arraysize; i++) {// Overflow may occur in multiplication here:
a[i] = log (b[i] * c[i]);}}// Catch floating point overflow but no other exceptions:__except (GetExceptionCode() == EXCEPTION_FLT_OVERFLOW? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {// Floating point overflow has occurred.
// Reset floating point status:_fpreset();_controlfp_s(&dummy, 0, _EM_OVERFLOW);// _controlfp(0, _EM_OVERFLOW); // if above doesn't work// Re-do the calculation in a way that avoids overflow:a[i] = log(b[i]) + log(c[i]);// Increment loop counter and go back into the for-loop:
i++;
}
}
}

Assume that the numbers in b[i] and c[i] are so big that overflow can occur in themultiplication b[i]*c[i], though this only happens rarely. The above code will catch an
exception in case of overflow and redo the calculation in a way that takes more time but
avoids the overflow. Taking the logarithm of each factor rather than the product makes surethat no overflow can occur, but the calculation time is doubled.
The time it takes to make support for the exception handling is negligible because there isno try block or function call (other than log) inside the critical innermost loop. log is alibrary function which we assume is optimized. We cannot change its possible exception
handling support anyway. The exception is costly when it occurs, but this is not a problem
since we are assuming that the occurrence is rare.
Testing for the overflow condition inside the loop does not cost anything here because weare relying on the microprocessor hardware for raising an exception in case of overflow. The
exception is caught by the operating system which redirects it to the exception handler inthe program if there is a try block.
64

There is a portability issue to catching hardware exceptions. The mechanism relies on nonstandardized details in both compiler, operating system and CPU hardware. Porting such an
application to a different platform is likely to require modifications in the code.
Let's look at the possible alternatives to exception handling in this example. We might checkfor overflow by checking if b[i] and c[i] are too big before multiplying them. This would
require two floating point comparisons, which are relatively costly because they must be
inside the innermost loop. Another possibility is to always use the safe formula a[i] =log(b[i]) + log(c[i]);. This would double the number of calls to log, andlogarithms take a long time to calculate. If there is a way to check for overflow outside theloop without checking all the array elements then this might be a better solution. It might bepossible to do such a check before the loop if all the factors are generated from the samefew parameters. Or it might be possible to do the check after the loop if the results are
combined by some formula into a single result.

Exceptions and vector code
Vector instructions are useful for doing multiple calculations in parallel. This is described inchapter 12 below. Exception handling does not work well with vector code because a single
element in a vector may cause an exception while perhaps the other vector elements donot. You may even generate an exception in a not-taken branch because of the way
branches are implemented in vector code. If the code can benefit from vector instructions
then it is better to disable the trapping of exceptions and rely on the propagation of NAN and
INF instead. See chapter 7.34 below. This is further discussed in the document
www.agner.org/optimize/nan_propagation.pdf.

Avoiding the cost of exception handling
Exception handling is not necessary when no attempt is made to recover from errors. If you
just want the program to issue an error message and stop the program in case of an error
then there is no reason to use try, catch, and throw. It is more efficient to define yourown error-handling function that simply prints an appropriate error message and then calls
exit.
Calling exit may not be safe if there are allocated resources that need to be cleaned up,
as explained below. There are other possible ways of handling errors without using
exceptions. The function that detects an error can return with an error code which the calling
function can use for recovering or for issuing an error message.
It is recommended to use a systematic and well thought-through approach to error handling.
You have to distinguish between recoverable and non-recoverable errors; make sure
allocated resources are cleaned up in case of an error; and make appropriate error
messages to the user.

Making exception-safe code
Assume that a function opens a file in exclusive mode, and an error condition terminates the
program before the file is closed. The file will remain locked after the program is terminatedand the user will be unable to access the file until the computer is rebooted. To prevent thiskind of problems you must make your program exception safe. In other words, the program
must clean up everything in case of an exception or other error condition. Things that may
need to be cleaned up include:•

Memory allocated with new or malloc.

•

Handles to windows, graphic brushes, etc.

65•Locked mutexes.

•

Open database connections.

•

Open files and network connections.

•Temporary files that need to be deleted.

•

User work that needs to be saved.

•

Any other allocated resource.

The C++ way of handling cleanup jobs is to make a destructor. A function that reads orwrites a file can be wrapped into a class with a destructor that makes sure the file is closed.
The same method can be used for any other resource, such as dynamically allocatedmemory, windows, mutexes, database connections, etc.
The C++ exception handling system makes sure that all destructors for local objects arecalled. The program is exception safe if there are wrapper classes with destructors to takecare of all cleanup of allocated resources. The system is likely to fail if the destructor causes
another exception.
If you make your own error handling system instead of using exception handling then you
cannot be sure that all destructors are called and resources cleaned up. If an error handler
calls exit(), abort(), _endthread(), etc. then there is no guarantee that all
destructors are called. The safe way to handle an unrecoverable error without using
exceptions is to return from the function. The function may return an error code if possible,or the error code may be stored in a global object. The calling function must then check forthe error code. If the latter function also has something to clean up then it must return to its
own caller, and so on.

7.33 Other cases of stack unwinding
The preceding paragraph described a mechanism called stack unwinding that is used by
exception handlers for cleaning up and calling any necessary destructors after jumping out
of a function in case of an exception without using the normal return route. This mechanism
is also used in two other situations:
The stack unwinding mechanism may be used when a thread is terminated. The purpose isto detect if any objects declared in the thread have a destructor that needs to be called. It is
recommended to return from functions that require cleanup before terminating a thread. You
cannot be certain that a call to _endthread() cleans up the stack. This behaviour isimplementation dependent.
The stack unwinding mechanism is also used when the function longjmp is used forjumping out of a function. Avoid the use of longjmp if possible. Do not rely on longjmp intime-critical code.

7.34 Propagation of NAN and INF
Floating point errors will propagate to the end result of a series of calculations in mostcases. This is a very efficient alternative to exceptions and fault trapping.
Floating point overflow and division by zero gives infinity. If you add or multiply infinity withsomething you get infinity as a result. The INF code may propagate to the end result in thisway. However, not all operations with INF input will give INF as a result. If you divide anormal number by INF you get zero. The special cases INF-INF and INF/INF give NAN (not66

a-number). The special code NAN also occurs when you divide zero by zero and when the
input of a function is out of range, such as sqrt(-1) and log(-1).
Most operations with a NAN input will give a NAN output, so that the NAN will propagate to
the end result. This is a simple and efficient way of detecting floating point errors. Almost all
floating point errors will propagate to the end result where they appear as INF or NAN. If
you print out the results, you will see INF or NAN instead of a number. No extra code isneeded to keep track of the errors, and there is no extra cost to the propagation of INF andNAN.
A NAN can contain a payload with extra information. A function library can put an error code
into this payload in case of an error, and this payload will propagate to the end result.
The function finite() will return false when the parameter is INF or NAN, and true if it is anormal floating point number. This can be used for detecting errors before a floating point
number is converted to an integer and in other cases where we want to check for errors.
The details of INF and NAN propagation are further explained in the document "NAN
propagation versus fault trapping in floating point code" atwww.agner.org/optimize/nan_propagation.pdf. This document also discusses situations
where the propagation of INF and NAN fails, as well as compiler optimization options that
influence the propagation of these codes.

7.35 Preprocessing directives
Preprocessing directives (everything that begins with #) are costless in terms of program
performance because they are resolved before the program is compiled.
#if directives are useful for supporting multiple platforms or multiple configurations with thesame source code. #if is more efficient than if because #if is resolved at compile time
while if is resolved at runtime.
#define directives are equivalent to const definitions when used for defining constants.
For example, #define ABC 123 and const int ABC = 123; are equally efficientbecause, in most cases, an optimizing compiler can replace an integer constant with its
value. However, the const int declaration may in some cases take memory space
where a #define directive never takes memory space. A floating point constant always
takes memory space, even when it has not been given a name.
#define directives when used as macros are sometimes more efficient than functions.
See page 48 for a discussion.

7.36 Namespaces
There is no cost in terms of execution speed to using namespaces.

8 Optimizations in the compiler
8.1 How compilers optimize
Modern compilers can do a lot of modifications to the code in order to improve performance.
It is useful for the programmer to know what the compiler can do and what it cannot do. The
following sections describe some of the compiler optimizations that it is relevant for the
programmer to know about.
67

Function inlining
The compiler can replace a function call by the body of the called function. Example:// Example 8.1afloat square (float a) {return a * a;}float parabola (float x) {return square(x) + 1.0f;}

The compiler may replace the call to square by the code inside square:// Example 8.1b
float parabola (float x) {return x * x + 1.0f;}

The advantages of function inlining are:••••

The overhead of call and return and parameter transfer are eliminated.
Code caching will be better because the code becomes contiguous.
The code becomes smaller if there is only one call to the inlined function.
Function inlining can open the possibility for other optimizations, as explained below.

The disadvantage of function inlining is that the code becomes bigger if there is more than
one call to the inlined function and the function is big. The compiler is more likely to inline afunction if it is small or if it is called from only one or a few places.

Constant folding and constant propagation
An expression or subexpression containing only constants will be replaced by the calculated
result. Example:// Example 8.2adouble a, b;
a = b + 2.0 / 3.0;

The compiler will replace this by// Example 8.2b
a = b + 0.666666666666666666667;

This is actually quite convenient. It is easier to write 2.0/3.0 than to calculate the value
and write it with many decimals. It is recommended to put a parenthesis around such a
subexpression to make sure the compiler recognizes it as a subexpression. For example,
b*2.0/3.0 will be calculated as (b*2.0)/3.0 rather than as b*(2.0/3.0) unless you
put a parenthesis around the constant subexpression.
A constant can be propagated through a series of calculations:// Example 8.3a
float parabola (float x) {return x * x + 1.0f;}
float a, b;
a = parabola (2.0f);
b = a + 1.0f;

The compiler may replace this by// Example 8.3b68

a = 5.0f;
b = 6.0f;

Constant folding and constant propagation is not possible if the expression contains afunction which cannot be inlined or cannot be calculated at compile time. For example:// Example 8.4
double a = sin(0.8);

The sin function is defined in a separate function library and you cannot expect thecompiler to be able to inline this function and calculate it at compile time. Some compilers
are able to calculate the most common math functions such as sqrt and pow at compiletime, but not the more complicated functions like sin.

Pointer elimination
A pointer or reference can be eliminated if the target pointed to is known. Example:// Example 8.5a
void Plus2 (int * p) {*p = *p + 2;}
int a;
Plus2 (&a);

The compiler may replace this by// Example 8.5ba += 2;

Common subexpression elimination
If the same subexpression occurs more than once then the compiler may calculate it only
once. Example:// Example 8.6a
int a, b, c;
b = (a+1) * (a+1);
c = (a+1) / 4;

The compiler may replace this by// Example 8.6b
int a, b, c, temp;
temp = a+1;
b = temp * temp;c = temp / 4;

Register variables
The most commonly used variables are stored in registers (see page 27).
The maximum number of integer register variables is approximately six in 32-bit systems
and fourteen in 64-bit systems.
The maximum number of floating point register variables is eight in 32-bit systems, sixteen
in 64-bit systems, and thirty two when the AVX512 instruction set is enabled in 64-bit mode.
Some compilers have difficulties making floating point register variables in 32-bit systems
unless the SSE (or later) instruction set is enabled.
The compiler will choose the variables that are used most for register variables. Typicalcandidates for register variables are local variables, temporary intermediates, loop counters,
69

function parameters, pointers, references, 'this' pointer, common subexpressions, andinduction variables (see below).
A variable cannot be stored in a register if its address is taken, i.e. if there is a pointer orreference to it. Therefore, you should avoid making any pointer or reference to a variable
that could benefit from register storage.

Live range analysis
The live range of a variable is the range of code in which the variable is used. An optimizing
compiler can use the same register for more than one variable if their live-ranges do not
overlap or if they are sure to have the same value. This is useful when the number ofavailable registers is limited. Example:// Example 8.7
int SomeFunction (int a, int x[]) {int b, c;x[0] = a;b = a + 1;
x[1] = b;c = b + 1;
return c;}

In this example, a, b and c can share the same register because their live ranges do not
overlap. If c = b + 1 is changed to c = a + 2 then a and b cannot use the sameregister because their live ranges now overlap.
Compilers do not normally use this principle for objects stored in memory. It will not use thesame memory area for different objects even when their live ranges do not overlap. See
page 93 for an example of how to make different objects share the same memory area.

Join identical branches
The code can be made more compact by joining identical pieces of code. Example:// Example 8.8adouble x, y, z;
if (b) {y = sin(x);z = y + 1.;}
else {
y = cos(x);z = y + 1.;}

bool b;

The compiler may replace this by// Example 8.8b
double x, y; bool b;
if (b) {y = sin(x);}else {
y = cos(x);}z = y + 1.;

Eliminate jumps
Jumps can be avoided by copying the code that it jumps to. Example:70

// Example 8.9a
int SomeFunction (int a, bool b) {if (b) {a = a * 2;}else {
a = a * 3;}
return a + 1;}

This code has a jump from a=a*2; to return a+1;. The compiler can eliminate this jumpby copying the return statement:// Example 8.9b
int SomeFunction (int a, bool b) {if (b) {a = a * 2;return a + 1;}
else {
a = a * 3;
return a + 1;}}

A branch can be eliminated if the condition can be reduced to always true or always false:// Example 8.10a
if (true) {a = b;}
else {
a = c;}Can be reduced to:// Example 8.10b
a = b;

A branch can also be eliminated if the condition is known from a previous branch. Example:// Example 8.11a
int SomeFunction (int a, bool b) {if (b) {a = a * 2;}else {
a = a * 3;}
if (b) {return a + 1;}
else {
return a - 1;}}

The compiler may reduce this to:// Example 8.11b

71

int SomeFunction (int a, bool b) {if (b) {a = a * 2;return a + 1;}
else {
a = a * 3;
return a - 1;}}

Loop unrolling
Compilers will unroll loops if a high degree of optimization is requested. See page 45. Thismay be advantageous if the loop body is very small or if it opens the possibility for further
optimizations. Loops with a very low repeat count may be completely unrolled to avoid theloop overhead. Example:// Example 8.12a
int i, a[2];for (i = 0; i < 2; i++) a[i] = i+1;

The compiler may reduce this to:// Example 8.12b
int a[2];a[0] = 1; a[1] = 2;

Unfortunately, some compilers unroll too much. Excessive loop unrolling is not optimal
because it increases the load on code cache, micro-op cache, and loop buffer, which are
critical resources.

Loop invariant code motion
A calculation may be moved out of a loop if it is independent of the loop counter. Example:// Example 8.13a
int i, a[100], b;for (i = 0; i < 100; i++) {a[i] = b * b + 1;}

The compiler may change this to:// Example 8.13b
int i, a[100], b, temp;
temp = b * b + 1;
for (i = 0; i < 100; i++) {a[i] = temp;}

Vectorization
The compiler can use vector registers for handling multiple data simultaneously, forexample:// Example 8.14. Automatic vectorization
const int size = 1024;
float a[size], b[size], c[size];// ...
for (int i = 0; i < size; i++) {a[i] = b[i] + c[i];}72The SSE2 instruction set provides 128-bit vector registers. A 128-bit register can hold four
32-bit float values. The compiler can roll out the loop in example 8.14 by four and do four
additions in one operation, using 128-bit vector registers.
If the AVX2 instruction set is available, then it is possible to do eight additions at a time,using 256-bit vector registers. If the AVX512 instruction set is available, then it is possible todo sixteen additions at a time, using 512-bit vector registers.
It is necessary to specify to the compiler which instruction set to use. The best performanceis obtained with the highest instruction set that the microprocessor supports.
Automatic vectorization is further explained on page 118.

Induction variables
An expression that is a linear function of a loop counter can be calculated by adding a
constant to the previous value. Example:// Example 8.15a
int i, a[100];for (i = 0; i < 100; i++) {a[i] = i * 9 + 3;}

The compiler may avoid the multiplication by changing this to:// Example 8.15b
int i, a[100], temp;
temp = 3;
for (i = 0; i < 100; i++) {a[i] = temp;
temp += 9;}Induction variables are often used for calculating the addresses of array elements. Example:// Example 8.16a
struct S1 {double a; double b;};S1 list[100]; int i;for (i = 0; i < 100; i++) {list[i].a = 1.0;
list[i].b = 2.0;}

In order to access an element in list, the compiler must calculate its address. The
address of list[i] is equal to the address of the beginning of list plus i*sizeof(S1).
This is a linear function of i which can be calculated by an induction variable. The compiler
can use the same induction variable for accessing list[i].a and list[i].b. It can also
eliminate i and use the induction variable as loop counter when the final value of theinduction variable can be calculated in advance. This reduces the code to:// Example 8.16b
struct S1 {double a; double b;};S1 list[100];
S1 * temp;
for (temp = &list[0]; temp < &list[100]; temp++) {temp->a = 1.0;
temp->b = 2.0;}73

The factor sizeof(S1) = 16 is actually hidden behind the C++ syntax in example 8.16b.
The integer representation of &list[100] is (int)(&list[100]) =(int)(&list[0]) + 100*16, and temp++ actually adds 16 to the integer value oftemp.
The compiler does not need induction variables to calculate the addresses of arrayelements of simple types because the CPU has hardware support for calculating theaddress of an array element if the address can be expressed as a base address plus a
constant plus an index multiplied by a factor of 1, 2, 4 or 8, but not any other factor. If a andb in example 8.16a were float instead of double, then sizeof(S1) would be 8 and no
induction variable would be needed because the CPU has hardware support for multiplying
the index by 8.
Compilers will rarely make induction variables for floating point expressions or more
complex integer expressions because this generates a loop-carried dependency chain that
may limit the overall performance. See page 83 for an example of how to use floating point
induction variables for calculating a polynomial.

Scheduling
A compiler may reorder instructions for the sake of parallel execution. Example:// Example 8.17
float a, b, c, d, e, f, x, y;x = a + b + c;
y = d + e + f;

The compiler may interleave the two formulas in this example so that a+b is calculated first,
then d+e, then c is added to the first sum, then f is added to the second sum, then the first
result is stored in x, and last the second result is stored in y. The purpose of this is to help
the CPU doing multiple calculations in parallel. Modern CPUs are actually able to reorderinstructions without help of the compiler (see page 113), but the compiler can make thisreordering easier for the CPU.

Algebraic reductions
Most compilers can reduce simple algebraic expressions using the fundamental laws ofalgebra. For example, a compiler may change the expression -(-a) to a.
I do not think that programmers write expressions like -(-a) very often, but such
expressions may occur as a result of other optimizations such as function inlining.
Reducible expressions also occur quite often as a result of macro expansions.
Programmers do, however, often write expressions that can be reduced. This may be
because the non-reduced expression better explains the logic behind the program or
because the programmer has not thought about the possibility of algebraic reduction. For
example, a programmer may prefer to write if(!a && !b) rather than the equivalentif(!(a || b)) even though the latter has one operator less. Fortunately, all compilers
are able to do the reduction in this case.
You cannot expect a compiler to reduce complicated algebraic expressions. For example,
not all compilers are able to reduce (a*b*c)+(c*b*a) to a*b*c*2. It is quite difficult toimplement the many rules of algebra in a compiler. Some compilers can reduce some typesof expressions and other compilers can reduce other types of expressions, but no compiler I
have ever seen can reduce them all. In the case of Boolean algebra, it is possible toimplement a universal algorithm (e.g. Quine–McCluskey or Espresso) that can reduce any
expression, but none of the compilers I have tested seem to do so.

74

The compilers are better at reducing integer expressions than floating point expressions,even though the rules of algebra are the same in both cases. This is because algebraic
manipulations of floating point expressions may have undesired effects. This effect can beillustrated by the following example:// Example 8.18
int a, b, c, y;y = a + b + c;

According to the rules of algebra, we may write:y = c + b + a;This may be useful if the subexpression c+b can be reused elsewhere. Now consider thecase where a is a large negative number and b and c are large positive numbers so that thecalculation of c+b overflows. An integer overflow will make the value wrap around and givea negative result. Fortunately, the overflow of c+b is canceled out by a subsequent
underflow when a is added. The result of a+b+c will be the same as c+b+a, even though
the latter expression involves overflow and underflow, while the former does not. This is thereason why it is safe to use algebraic manipulations on integer expressions (except for the<, <=, > and >= operators).
The same argument does not apply to floating point expressions. Floating point variables do
not wrap around on overflow and underflow. The range of floating point variables is so largethat we do not have to worry much about overflow and underflow except in special mathematical applications. But we do have to worry about loss of precision. Let's repeat the above
example with floating point numbers:// Example 8.19
float a = -1.0E8, b = 1.0E8, c = 1.23456, y;y = a + b + c;

The calculation here gives a+b=0, and then 0+1.23456 = 1.23456. But we will not get
the same result if we change the order of the operands and add b and c first. b+c =100000001.23456. The float type holds a precision of approximately seven significant
digits, so the value of b+c will be rounded to 100000000. When we add a to this number
we get 0 rather than 1.23456.
The conclusion to this argument is that the order of floating point operands cannot bechanged without the risk of losing precision. The compilers will not do so unless you specify
an option that allows less precise floating point calculations. Even with relaxed precision,
the compiler will not do such obvious reductions as 0*a = 0 because this would be invalid
if a was infinity or NAN (not a number). Different compilers behave differently because there
are different opinions on which imprecisions should be allowed and which not.
You cannot rely on the compiler to do any algebraic reductions on floating point code unless
you carefully specify the relevant floating point options that enable the reductions you willallow. See page 174 for a list of compiler options.
It is more safe to do algebraic reductions manually. I have tested the capability to reduce
various algebraic expressions on different compilers. The results are listed in table 8.1
below.

Devirtualization
An optimizing compiler can bypass the virtual table lookup for a virtual function call if it is
known which version of the virtual function is needed. Example:
75

// Example 8.20. Devirtualization
class C0 {public:virtual void f();};class C1 : public C0 {public:virtual void f();};void g() {C1 obj1;
C0 * p = & obj1;
p->f();}// Virtual call to C1::f

Without optimization, the compiler needs to look up in a virtual table to see whether the callp->f() goes to C0::f or C1::f. But an optimizing compiler will see that p always points
to an object of class C1, so it can call C1::f directly without using the virtual table.

8.2 Comparison of different compilers
I have made a series of experiments on different C++ compilers to see whether they areable to do different kinds of optimizations. All the compilers have been developed throughthe years and their capabilities have been improved several times. You may get better
performance by using the newest version of a compiler. Several less efficient compilers
have disappeared from the market or are no longer maintained.
My test results are summarized in table 8.1. The table shows whether the different
compilers succeeded in applying the various optimization methods and algebraic reductions
in my test examples. The table can give some indication of which optimizations you can
expect a particular compiler to do and which optimizations you have to do manually.
It must be emphasized that the compilers may behave differently on different test examples.
You cannot expect a compiler to always behave according to the table.

Gnu

Clang

Microsoft

Intel ClassicIntel
LLVM based

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x

Optimization method

General optimizations:
Function inlining
Constant folding
Constant propagation
Constant propagation through loop
Pointer elimination
Common subexpression elimination
Register variables
Live range analysis
Join identical branches
Eliminate jumps
Tail calls
Remove branch that is always false76Loop unrolling, array loops

x

exces
sive

x

x

exces
sive

Loop unrolling, structures
Loop invariant code motion
Induction variables for array elements
Induction variables for other integer expressions
Induction variables for float expressions
Multiple accumulators, integer
Multiple accumulators, float
Devirtualization
Profile-guided optimization
Whole program optimization

x
x
x
x
x
x

x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x

x
x
x
x
x
x
x
x

x
x
x
x
x
x
x

Integer algebra reductions:a+b = b+a, a*b = b*a (commutative)(a+b)+c = a+(b+c), (a*b)*c = a*(b*c) (associative)

x
-

x
x

x
x

x

x
x

a*b+a*c = a*(b+c) (distributive)
a+b+c+d = (a+b)+(c+d) (improve parallelism)
a*b*c*d = (a*b)*(c*d) (improve parallelism)x*x*x*x*x*x*x*x = ((x2) 2) 2
a+a+a+a = a*4
a*x*x*x + b*x*x + c*x + d = ((a*x+b)*x+c)*x+d-(-a) = aa-(-b) = a+ba-a = 0a+0 = aa*0 = 0a*1 = a(-a)*(-b) = a*ba/a = 1a/1 = a0/a = 0Multiply by constant = shift and add
Divide by constant = multiply and shift
Divide by power of 2 = shift(-a == -b) = (a == b)(a+c == b+c) = (a == b)!(a < b) = (a >= b)(a<b && b<c && a<c) = (a<b && b<c)

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

Floating point algebra reductions:a+b = b+a, a*b = b*a (commutative)
a+b+c = a+(b+c) (associative)
a*b*c = a*(b*c) (associative)
a*b+a*c = a*(b+c) (distributive)
a+b+c+d = (a+b)+(c+d), a*b*c*d = (a*b)*(c*d)
a*x*x*x + b*x*x + c*x + d = ((a*x+b)*x+c)*x+dx*x*x*x*x*x*x*x = ((x2) 2) 2
a+a+a+a = a*4-(-a) = aa-(-b) = a+ba-a = 0a+0 = aa*0 = 0a*1 = a(-a)*(-b) = a*b
77

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

mul
only

a/a = 1
a/1 = a0/a = 0(-a == -b) = (a == b)(-a > -b) = (a < b)
Divide by constant = multiply by reciprocal

x
x
x
x
x
x

x
x
x
x
x
x

x
x
x
x
x

x
x
x

x
x
x
x
x
x

Boolean algebra reductions:
Boolean operations without branchinga && b = b && a, a || b = b || a (commutative)
a && b && c = a && (b && c) (associative)(a&&b) || (a&&c) = a&&(b||c) (distributive)(a||b) && (a||c) = a||(b&&c) (distributive)!a && !b = !(a || b) (de Morgan)!(!a) = aa && !a = false, a || !a = true
a && true = a, a || false = aa && false = false, a || true = true
a && a = a(a&&b) || (a&&!b) = a(a&&b) || (!a&&c) = a ? b : c(a&&b) || (!a&&c) || (b&&c) = a ? b : c(a&&b) || (a&&b&&c) = a&&b
(a&&!b) || (!a&&b) = a XOR b

x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
-

rarely

x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x

Bitwise operator integer reductions:a & b = b & a, a | b = b | a (commutative)a & b & c = a & (b & c) (associative)(a&b) | (a&c) = a&(b|c) (distributive)(a|b) & (a|c) = a|(b&c) (distributive)~a & ~b = ~(a | b) (de Morgan)~(~a) = aa & ~a = 0, a | ~a = -1
a & -1 = a, a | 0 = aa&0=0a | -1 = -1
a & a = a, a | a = a(a&b) | (a&~b) = a(a&b) | (~a&c) | (b&c) = (a&b) | (~a&c) = a ? b : c(a&b) | (a&b&c) = a&b(a&!b) | (~a&b) = a ^ b~a ^ ~b = a ^ b
a&b&c&d = (a&b)&(c&d)
a<<b<<c = a<<(b+c)

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x

-

x
x

x
x
x
x
x
x
x
x
x
x

Bitwise operator integer vector reductions:a & b = b & a, a | b = b | a (commutative)a & b & c = a & (b & c) (associative)(a&b) | (a&c) = a&(b|c) (distributive)(a|b) & (a|c) = a|(b&c) (distributive)~a & ~b = ~(a | b) (de Morgan)~(~a) = aa & ~a = 0, a | ~a = -1
a & -1 = a
a|0=a
a&0=0
78

a | -1 = -1
a & a = a, a | a = a(a&b) | (a&~b) = a(a&b) | (~a&c) | (b&c) = (a&b) | (~a&c) = a ? b : c(a&b) | (a&b&c) = a&b(a&!b) | (~a&b) = a ^ b~a ^ ~b = a ^ b
a&b&c&d = (a&b)&(c&d)
Ternary operator instruction
a<<b<<c = a<<(b+c)

x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
-

x
-

x
-

x
x
x
x
x
x
x
x

Integer vector algebra reductions:a+b = b+a, a*b = b*a (commutative)(a+b)+c = a+(b+c), (a*b)*c = a*(b*c) (associative)
a*b+a*c = a*(b+c) (distributive)
a+b+c+d = (a+b)+(c+d)
x*x*x*x*x*x*x*x = ((x2) 2) 2
a+a+a+a = a*4
a*x*x*x + b*x*x + c*x + d = ((a*x+b)*x+c)*x+d-(-a) = aa-(-b) = a+ba-a = 0a+0 = aa*0 = 0a*1 = a(-a)*(-b) = a*b
Multiply by power of 2 = shift(-a == -b) = (a == b)(a+c == b+c) = (a == b)!(a < b) = (a >= b)(a<b && b<c && a<c) = (a<b && b<c)

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
-

x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

Floating point vector algebra reductions:a+b = b+a, a*b = b*a (commutative)(a+b)+c = a+(b+c), (a*b)*c = a*(b*c) (associative)
a*b+a*c = a*(b+c) (distributive)
a+b+c+d = (a+b)+(c+d)
x*x*x*x*x*x*x*x = ((x2) 2) 2
a+a+a+a = 4*aa*x*x*x + b*x*x + c*x + d = ((a*x+b)*x+c)*x+d-(-a) = aa-(-b) = a+ba-a = 0a+0 = aa*0 = 0a*1 = a(-a)*(-b) = a*ba/a = 1a/1 = a0/a = 0
Divide by constant = multiply by reciprocal(-a == -b) = (a == b)!(a < b) = (a >= b)

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
-

x
x
x
x
-

x
-

x
-

-

-

x
x
x
x
x
x
x
-

x
x
x
x
x
-

General vector optimizations:
automatic vectorization of loop

x

x

x

x

x

79

automatic vectorization with branch
conditional instructions using maskmerge conditional zero into masked instruction
merge broadcast into instruction
merge blend into masked instruction
merge boolean AND into masked compare
eliminate mask all true
eliminate mask all false
vectorization of math library function
optimize across vector permutation

x
x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x

x
-

x
x

-

x
x
x
x
x
x
x
x
-

x
x
x
x
x
x
x
x
x
x

Table 8.1. Comparison of optimizations in different C++ compilers

These tests were carried out by compiling test code for 64-bit Linux or Windows with all
relevant optimization options turned on, including relaxed floating point precision. The
following compiler versions were tested:
Gnu C++ version 12.1.0
Clang C++ version 12.0.0
Microsoft C++ Compiler Visual Studio version 17.2.5, 2022
Intel C++ Compiler Classic version 2021.6.0
Intel C++ Compiler oneAPI LLVM-based version 2022.1.0
The Clang and Gnu compilers are the ones that optimize best, while the Microsoft compiler
has inferior performance. The Microsoft compiler is generally using more instructions thanthe other compilers for doing the same thing. The Microsoft compiler is still good enough forless demanding purposes.
The Intel compiler comes in two versions, the legacy version named “Classic”, and a newversion which is a forking of the Clang compiler using the open source LLVM project. The
LLVM-based Intel compiler is very similar to the Clang compiler, but is using Intel’s own
highly optimized function libraries. The Classic version is not recommended because the
LLVM-based Intel compiler is better, and because code produced with the Classic version
has poor performance on AMD processors.
The Microsoft Visual Studio has options for using a Clang or Intel compiler as plug-in.
The Clang compiler tends to unroll loops too much. Excessive loop unrolling is not optimal
because it increases the load on the code cache, micro-op cache, and loop buffer, which
are critical resources. Apart from that, Clang is an excellent compiler that sometimes does
ingenious things to optimize the code. You may optimize for size (-Os) to limit loop
unrolling.

8.3 Obstacles to optimization by compiler
There are several factors that can prevent the compiler from doing the optimizations that wewant it to do. It is important for the programmer to be aware of these obstacles and to know
how to avoid them. Some important obstacles to optimization are discussed below.

Cannot optimize across modules
The compiler does not have information about functions in other modules than the one it iscompiling. This prevents it from making optimizations across function calls. Example:// Example 8.21
module1.cpp
int Func1(int x) {return x*x + 1;}
module2.cpp

80

int Func2() {int a = Func1(2);...
}

If Func1 and Func2 were in the same module then the compiler would be able do function
inlining and constant propagation and reduce a to the constant 5. But the compiler does nothave the necessary information about Func1 when compiling module2.cpp.
The simplest way to solve this problem is to combine the multiple .cpp modules into one bymeans of #include directives. This is sure to work on all compilers. Most compilers have afeature called whole program optimization, which will enable optimizations across modules(See page 85).

Pointer aliasing
When accessing a variable through a pointer or reference, the compiler may not be able tocompletely rule out the possibility that the variable pointed to is identical to some othervariable in the code. Example:// Example 8.22a
void Func1 (int a[], int p[]) {int i;for (i = 0; i < 100; i++) {a[i] = p[0] + 2;}}
void Func2() {int list[100];
Func1(list, &list[8]);}Here, it is necessary to reload p[0] and calculate p[0]+2 a hundred times because thevalue pointed to by p[0] is identical to one of the elements in a[] which will change during
the loop. It is not permissible to assume that p[0]+2 is a loop-invariant code that can be
moved out of the loop. Example 8.22a is indeed a very contrived example, but the point isthat the compiler cannot rule out the theoretical possibility that such contrived examples
exist. Therefore the compiler is prevented from assuming that p[0]+2 is a loop-invariant
expression that it can move outside the loop.
It is possible to tell the compiler that a specific pointer does not alias anything by using the
keyword __restrict or __restrict__, if supported by the compiler:// Example 8.22b
void Func1 (int * __restrict__ a, int * __restrict__ p) {int i;for (i = 0; i < 100; i++) {a[i] = p[0] + 2;}}

Some compilers have an option for assuming no pointer aliasing (/Oa). The easiest way to
overcome the obstacle of possible pointer aliasing is to turn on this option. This requires thatyou analyze all pointers and references in the code carefully to make sure that no variable
or object is accessed in more than one way in the same part of the code.
We can never be sure that the compiler takes the hint about no pointer aliasing. The only
way to make sure that the code is optimized is to do it explicitly. In example 8.22b, you81could calculate p[0]+2 and store it in a temporary variable outside the loop if you are surethat the pointer does not alias any elements in the array.
// Example 8.22c
void Func1 (int a[], int p[]) {int i;
int p02 = p[0] + 2;
for (i = 0; i < 100; i++) {a[i] = p02;}}

This method requires that you can predict where the obstacles to optimization are.
Pointer aliasing is also an obstacle to automatic vectorization. The compiler cannot
vectorize a loop such as the one in example 8.22a if pointer aliasing cannot be ruled out.

Dynamic memory allocation
Any array or object that is allocated dynamically (with new or malloc) is necessarily
accessed through a pointer. It may be obvious to the programmer that pointers to different
dynamically allocated objects are not overlapping or aliasing, but the compiler is usually notable to see this. It may also prevent the compiler from aligning the data optimally, or fromknowing that the objects are aligned. It is more efficient to declare objects and fixed sizearrays inside the function that needs them. Local declarations also improve data caching
because the memory space that is allocated for local arrays and objects is recycled when
the function returns and a new function is called.

Pure functions
A pure function is a function that has no side-effects and whose return value depends only
on the values of its arguments. This closely follows the mathematical notion of a function.
Multiple calls to a pure function with the same arguments are sure to produce the same
result. A compiler can eliminate common subexpressions that contain pure function calls
and it can move out loop-invariant code containing pure function calls. Unfortunately, thecompiler cannot know that a function is pure if the function is defined in a different module
or a function library.
Therefore, it is necessary to do optimizations such as common subexpression elimination,
constant propagation, and loop-invariant code motion manually when it involves purefunction calls.
Some compilers have an attribute which can be applied to a function prototype to tell thecompiler that this is a pure function. Example:// Example 8.23
#ifdef __GNUC__#define pure_function#else#define pure_function#endif__attribute__((const))

double Func1(double) pure_function ;double Func2(double x) {return Func1(x) * Func1(x) + 1.;}

Here, the Gnu compiler will make only one call to Func1, while other compilers will maketwo.
82Compilers generally know that standard library functions like sqrt, pow and log are purefunctions, but unfortunately there is no standardized way to tell a compiler that a userdefined function is pure.

Virtual functions and function pointers
It is not always possible for the compiler to determine with certainty which version of avirtual function will be called, or what a function pointer points to. Therefore, it cannot inline
a virtual function or otherwise optimize across the function call if the exact class is notknown.

Algebraic reduction
Most compilers can do simple algebraic reductions such as -(-a) = a, but they are notalways able to do more complicated reductions. Algebraic reduction is a complicatedprocess which is difficult to implement in a compiler.
Many algebraic reductions are not permissible for reasons of mathematical purity andprecision. In many cases it is possible to construct obscure examples where the reduction
would cause overflow or loss of precision, especially in floating point expressions (see page75). The compiler cannot rule out the possibility that a particular reduction would be invalid
in a particular situation. It is therefore necessary to do the algebraic reductions explicitly inmany cases.
Integer expressions are less susceptible to problems of overflow and loss of precision forreasons explained on page 75. It is therefore possible for the compiler to do more
reductions on integer expressions than on floating point expressions. Most reductions
involving integer addition, subtraction and multiplication are permissible in all cases, while
many reductions involving division and relational operators (e.g. '>') are not permissible forreasons of mathematical purity. For example, compilers may not be able to reduce theinteger expression -a > -b to a < b because of the obscure possibility of overflow.
Various compiler options are available for telling the compiler which kinds of algebraic
reductions it may use. These options are explained below on page 86.
Table 8.1 (page 80) shows which reductions the compilers are able to do, at least in somesituations, with the relevant options enabled. All reductions that the compilers cannot do
must be done manually by the programmer.

Floating point induction variablesCompilers rarely make floating point induction variables for floating point expressions
because the loop-carried dependency chain may decrease performance, and because
accumulating rounding errors may reduce the precision. Floating point induction variables
may be useful, however, if a function of a loop counter can be calculated more efficiently
from the previous value than from the loop counter. Any expression that is an n'th degree
polynomial of the loop counter can be calculated by n additions and no multiplications. The
following example shows the principle for a 2'nd order polynomial:// Example 8.24a. Loop to make table of polynomialconst double A = 1.5, B = 2.25, C = 3.5;// Polynomial coefficients
double Table[100];// Tableint x;// Loop counterfor (x = 0; x < 100; x++) {Table[x] = A*x*x + B*x + C;// Calculate polynomial}

The calculation of this polynomial can be done with just two additions by the use of two
induction variables:83

// Example 8.24b. Calculate polynomial with induction variables
const double A = 1.5, B = 2.25, C = 3.5;// Polynomial coefficients
double Table[100];// Tableint x;// Loop counter
const double A2 = A + A;// = 2*A
double Y = C;// = A*x*x + B*x + C
double Z = A + B;// = Delta Y
for (x = 0; x < 100; x++) {Table[x] = Y;// Store result
Y += Z;// Update induction variable Y
Z += A2;// Update induction variable Z}

Note that the accuracy is decreased by accumulating rounding errors if the induction
variables have values that cannot be expressed exactly.
The loop in example 8.24b has two loop-carried dependency chains, namely the two
induction variables Y and Z. Each dependency chain has a latency which is the same as thelatency of a floating point addition. This may be small enough to justify the method if theloop also contains other time-consuming operations. A longer loop-carried dependency
chain would make the induction variable method unfavorable, unless the value is calculated
from a value that is two or more iterations back.
The method of induction variables can also be vectorized if you take into account that eachvalue is calculated from the value that lies r places back in the sequence, where r is thenumber of elements in a vector or the loop unroll factor. A little math is required for finding
the right formula in each case.

Inlined functions have a non-inlined copy
Function inlining has the complication that the same function may be called from anothermodule. The compiler has to make a non-inlined copy of the inlined function for the sake ofthe possibility that the function is also called from another module. This non-inlined copy isdead code if no other modules call the function. This fragmentation of the code makes
caching less efficient.
There are various ways around this problem. If a function is not referenced from any othermodule then add the keyword static to the function definition. This tells the compiler thatthe function cannot be called from any other module. The static declaration makes it
easier for the compiler to evaluate whether it is optimal to inline the function, and it prevents
the compiler from making an unused copy of an inlined function. The static keyword also
makes various other optimizations possible because the compiler does not have to obey
any specific calling conventions for functions that are not accessible from other modules.
You may add the static keyword to all local non-member functions.
Unfortunately, this method does not work for class member functions because the static
keyword has a different meaning for member functions. You can force a member function to
be inlined by declaring the function body inside the class definition. This will prevent thecompiler from making a non-inlined copy of the function, but it has the disadvantage that thefunction is always inlined even when it is not optimal to do so (i.e. if the member function is
big and is called from many different places).
Most compilers have an option for function-level linking (Windows: /Gy, Linux:-ffunction-sections) which allows the linker to remove unreferenced functions. It is
recommended to turn on this option.

84

8.4 Obstacles to optimization by CPU
Modern CPUs can do a lot of optimization by executing instructions out of order. Long
dependency chains in the code prevent the CPU from doing out-of-order execution, as
explained on page 21. Avoid long dependency chains, especially loop-carried dependency
chains with long latencies.

8.5 Compiler optimization options
All C++ compilers have various optimization options that can be turned on and off. It is
important to study the available options for the compiler you are using and turn on all
relevant options. The most important options are explained below.
Many optimization options are incompatible with debugging. A debugger can execute a
code one line at a time and show the values of all variables. Obviously, this is not possible
when parts of the code have been reordered, inlined, or optimized away. It is common to
make two versions of a program executable: a debug version with full debugging support
which is used during program development, and a release version with all relevant
optimization options turned on. Most IDE's (Integrated Development Environments) havefacilities for making a debug version and a release version of object files and executables.
Make sure to distinguish these two versions and turn off debugging and profiling support in
the optimized version of the executable.
Most compilers offer a choice between optimizing for size and optimizing for speed.
Optimizing for size is relevant when the code is fast anyway and you want the executable tobe as small as possible. In some cases, you may get better performance when optimizing
for size than for speed if the code cache, micro-op cache, or loop buffer in the CPU is alimiting resource.
Optimizing for speed is relevant when CPU access and memory access are critical time
consumers. The highest optimization option is often the best, but in some cases the highest
optimization option is actually doing things that are not optimal in the specific case.
Some compilers offer profile-guided optimization. This works in the following way. First you
compile the program with profiling support. Then you make a test run with a profiler which
determines the program flow and the number of times each function and branch is
executed. The compiler can then use this information to target optimizations to the hot spots
and to put the different branches and functions in optimal order.
Most compilers have support for whole program optimization. This works by compiling in two
steps. All source files are first compiled to an intermediate file format instead of the usual
object file format. The intermediate files are then linked together in the second step where
the compilation is finished. Register allocation and function inlining is done at the second
step. The intermediate file format is not standardized. It is not even compatible with different
versions of the same compiler. It is therefore not possible to distribute function libraries inthis format.
Other compilers offer the possibility of compiling multiple .cpp files into a single object file.
This enables the compiler to do cross-module optimizations when interprocedural
optimization is enabled. A more primitive, but very efficient, way of doing whole program
optimization is to join all source files into one by means of #include directives and declare
all non-member functions static or inline. This will enable the compiler to do interprocedural
optimizations of the whole program.
You may choose the newest instruction set available when compatibility with old CPUs is
not needed. Alternatively, you may make multiple versions of the most critical part of thecode to support different CPUs. This method is explained on page 135.
85

The code becomes more efficient when there is no exception handling. It is recommended
to turn off support for exception handling unless the code relies on structured exception
handling and you want the code to be able to recover from exceptions. See page 62.
It is recommended to turn off support for runtime type identification (RTTI). See page 55.
It is recommended to enable fast floating point calculations or turn off requirements for strict
floating point calculations unless the strictness is required. See page 75 for discussions.
Turn on the option for "function level linking" if available. See page 84 for an explanation ofthis option.
Use an option for "assume no pointer aliasing" if you are sure the code has no pointeraliasing. See page 81 for an explanation.
Many compilers have an option for "standard stack frame" or "frame pointer". The standard
stack frame is used for debugging and exception handling. Omitting the standard stack
frame makes function calls faster and makes an extra register available for other purposes.
This is advantageous because registers is a scarce resource. Do not use a stack frame
unless your program relies on exception handling.
See page 174 for a detailed list of compiler options.

Options for optimizing floating point code
Some of the obstacles to optimizing floating point code can be overcome by the use of
specific compiler options.
Most optimizations of integer expressions are enabled by setting the compiler optimizationlevel to 2 (-O2) or higher. Optimization of floating point expressions is more complicatedbecause many optimizations may compromise precision or standards compliance. The
programmer needs to consider several compiler options to tune the optimization of floating
point code. The following discussion applies to the Gnu, Clang, and LLVM-based Intel
compilers.
The options -ffp-model=fast or -funsafe-math-optimizations will enableoptimizations that require reordering of operands. Calculating a + b + c and c + b + a
will not necessarily give the same result, as explained at example 8.19. A permissive
floating point model is necessary for optimizations that involve reordering of operands orotherwise give a slightly different precision. These options also enable the combination of amultiplication and an addition into a fused multiply-and-add instruction.
Optimizations such as a*0 = 0 and a-a = 0 are invalid if the variable a can be infinity or
NAN. The option -ffinite-math-only tells the compiler to allow such optimizations andassume that numbers are finite.
-fno-trapping-math. This option tells the compiler that you do not rely on catching
floating point exceptions with try/catch blocks or with a global floating point status
register. This option is necessary for vectorization of loops that contain branches. The
compiler cannot make branches in vector code without this option because an exception ina not-taken branch might raise a trap.
-fno-math-errno. Mathematical functions such as sqrt and log are setting a variablenamed errno in case of errors. This way of detecting errors is inefficient and should be
avoided. The option -fno-math-errno is necessary for vectorization of loops containing
mathematical functions. Further considerations for mathematical functions are discussed in
the next section.
86

The Microsoft compiler has fewer options. Maximum optimization of floating point code canbe obtained with the option /fp:fast. Do not enable floating point exceptions.
See page 174 for a detailed list of compiler options.
See page 131 for features for vectorizing mathematical functions.

8.6 Optimization directives
Some compilers have many keywords and directives which are used for giving specificoptimization instructions at specific places in the code. Many of these directives arecompiler-specific. You cannot expect a directive for a Windows compiler to work on a Linuxcompiler, or vice versa. But most of the Microsoft directives work on other compilers for
Windows, while most of the Gnu directives work on other compilers for Linux.

Keywords that work on all C++ compilers
The keyword volatile makes sure that a variable is never stored in a register, not eventemporarily. This is intended for variables that are shared between multiple threads, but it
can also be used for turning off all optimizations of a variable for test purposes.
The const keyword tells that a variable is never changed. This will allow the compiler to
optimize away the variable in many cases. For example:// Example 8.25. Integer constantconst int ArraySize = 1000;
int List[ArraySize];...
for (int i = 0; i < ArraySize; i++) List[i]++;Here, the compiler can replace all occurrences of ArraySize by the value 1000. The loop
in example 8.25 can be implemented in a more efficient way if the value of the loop count(ArraySize) is constant and known to the compiler at compile time. No memory will beallocated for an integer constant, unless the address of it (&ArraySize) is taken.
A const pointer or const reference cannot change what it points to. A const member
function cannot modify data members. It is recommended to use the const keyword
wherever appropriate to give the compiler additional information about a variable, pointer ormember function because this may improve the possibilities for optimization. For example,
the compiler can safely assume that the value of a class data member is unchanged acrossa call to a const function that is member of the same class.
The static keyword has several meanings depending on the context. The keyword
static, when applied to a non-member function, means that the function is not accessedby any other modules. This makes inlining more efficient and enables interprocedural
optimizations. See page 84.
The keyword static, when applied to a global variable means that it is not accessed byany other modules. This enables interprocedural optimizations.
The keyword static, when applied to a local variable inside a function means that thevariable will be preserved when the function returns and remain unchanged the next time
the function is called. This may be inefficient because some compilers will insert extra codeto guard the variable against access from multiple threads simultaneously. This may apply
even if the variable is const.
There may, nevertheless, be a reason to make a local variable static and const to makesure it is initialized only the first time the function is called. Example:87

// Example 8.26
void Func () {static const double log2 = log(2.0);...
}

Here, log(2.0) is only calculated the first time Func is executed. Without static, thelogarithm would be re-calculated every time Func is executed. This has the disadvantagethat the function must check if it has been called before. This is faster than calculating thelogarithm again, but it would be even faster to make log2 a global const variable orreplace it with the calculated value.
The keyword static, when applied to a class member function means that it cannot
access any non-static data members or member functions. A static member function iscalled faster than a non-static member function because it does not need a 'this' pointer. It
is recommended to make member functions static where appropriate.

Compiler-specific keywords
Fast function calling. __fastcall or __attribute__((fastcall)). The fastcall
modifier can make function calls faster in 32-bit mode. The first two integer parameters aretransferred in registers rather than on the stack. Fastcall functions are not compatible
across compilers. Fastcall is not needed in 64-bit mode where the parameters aretransferred in registers anyway. A similar keyword is __vectorcall. This will improvefunction calls with floating point or vector parameters in 64-bit Windows. See page 49.
Pure function. __attribute__((const)) (Linux only). Specifies a function to be pure.
This allows common subexpression elimination and loop-invariant code motion. See page82.
Assume no pointer aliasing. __restrict or __restrict__ or __declspec(noalias)
or #pragma optimize("a",on). Specifies that pointer aliasing does not occur. See
page 81 for an explanation. Note that these directives do not always work.
Data alignment. __declspec(align(16)) or __attribute__((aligned(16))).
Specifies alignment of arrays and structures. Superseded by alignas(16) in C++11.
Useful for vector operations, see page 115.

8.7 Checking what the compiler does
It can be very useful to study the code that a compiler generates to see how well it
optimizes the code. Sometimes the compiler does quite ingenious things to make the code
more efficient, and sometimes it codes things in incredibly stupid ways. Looking at thecompiler output can often reveal things that can be improved by modifications of the source
code, as the example below shows.
The best way to check the code that the compiler generates is to use a compiler option forassembly language output. On most compilers you can do this by invoking the compiler
from the command line with all the relevant optimization options and the options -S or /Fa
for assembly output. If the compiler does not have an assembly output option, or if theassembly output is using a confusing format, then use a disassembler on the object file. The
objconv disassembler can be useful here.
The Intel compiler has an option for source annotation in the assembly output (/FAs or fsource-asm). This option makes the assembly output more readable but unfortunately it
prevents certain optimizations. Do not use the source annotation option if you want to see
the result of full optimization.
88

It is also possible to see the compiler-generated code in the disassembly window of adebugger. However, the code that you see in the debugger is not the optimized version if
you compile in debug mode. The debugger may not be able to set a breakpoint in the fully
optimized release-mode code because it does not have the line number information. It is still
possible to single-step through the optimized code in a debugger. Another solution is toinsert a fixed breakpoint in the code with an inline assembly instruction for interrupt 3. The
code is __asm int 3; or __asm ("int 3"); or __debugbreak();. If you run the
optimized code (release version) in the debugger then it will break at the interrupt 3
breakpoint and show a disassembly, probably without information about function names andvariable names. Remember to remove the interrupt 3 breakpoint again.
The following example shows what the assembly output of a compiler can look like and how
you can use it for improving the code.
// Example 8.27a
void Func(int a[], int & r) {int i;for (i = 0; i < 100; i++) {a[i] = r + i/2;}}

The Intel compiler generates the following assembly code from example 8.27a (32-bitmode):; Example 8.27a compiled to assembly:
ALIGN
4; align by 4
PUBLIC ?Func@@YAXQAHAAH@Z; mangled function name
?Func@@YAXQAHAAH@Z PROC NEAR
; start of Func
; parameter 1: 8 + esp; a; parameter 2: 12 + esp; r$B1$1:; unused label
pushebx; save ebx on stackmovecx, DWORD PTR [esp+8]; ecx = axor
eax, eax; eax = i = 0
movedx, DWORD PTR [esp+12] ; edx = r$B1$2:; top of loopmovebx, eax; compute i/2 in ebx
shrebx, 31; shift down sign bit of iaddebx, eax; i + sign(i)sarebx, 1; shift right = divide by 2
addebx, DWORD PTR [edx]; add what r points to
mov
DWORD PTR[ecx+eax*4],ebx ; store result in arrayaddeax, 1; i++
cmp
eax, 100; check if i < 100jl$B1$2; repeat loop if true
$B1$3:; unused label
popebx; restore ebx from stackret; return
ALIGN
4; align?Func@@YAXQAHAAH@Z ENDP
; mark end of procedure

Most of the comments generated by the compiler have been replaced by my comments, ingreen. It takes some experience to get used to read and understand compiler-generated
assembly code. Let me explain the above code in details. The funny looking name
?Func@@YAXQAHAAH@Z is the name of Func with a lot of added information about thefunction type and its parameters. This is called name mangling. The characters '?', '@' and'$' are allowed in assembly names. The details about name mangling are explained inmanual 5: "Calling conventions for different C++ compilers and operating systems". The
parameters a and r are transferred on the stack at address esp+8 and esp+12 and loaded
89

into ecx and edx, respectively. (In 64-bit mode, the parameters would be transferred inregisters rather than on the stack). ecx now contains the address of the first element of thearray a and edx contains the address of the variable that r points to. A reference is thesame as a pointer in assembly code. Register ebx is pushed on the stack before it is usedand popped from the stack before the function returns. This is because the register usage
convention says that a function is not allowed to change the value of ebx. Only the registers
eax, ecx and edx can be changed freely. The loop counter i is stored as a registervariable in eax. The loop initialisation i=0; has been translated to the instruction
xor eax,eax. This is a common way of setting a register to zero that is more efficient thanmov eax,0. The loop body begins at the label $B1$2:. This is just an arbitrary name thatthe compiler has chosen for the label. It uses ebx as a temporary register for computing
i/2+r. The instructions mov ebx,eax / shr ebx,31 copies the sign bit of i into theleast significant bit of ebx. The next two instructions add ebx, eax / sar ebx,1 adds
this to i and shifts one place to the right in order to divide i by 2. The instruction add
ebx, DWORD PTR [edx] adds, not edx but the variable whose address is in edx, to ebx.
The square brackets mean use the value in edx as a memory pointer. This is the variable
that r points to. Now ebx contains i/2+r. The next instruction mov DWORD PTR[ecx+eax*4],ebx stores this result in a[i]. Note how efficient the calculation of thearray address is. ecx contains the address of the beginning of the array. eax holds theindex, i. This index must be multiplied by the size (in bytes) of each array element in orderto calculate the address of element number i. The size of an int is 4. So the address ofarray element a[i] is ecx+eax*4. The result ebx is then stored at address[ecx+eax*4]. This is all done in a single instruction. The CPU supports this kind of
instructions for fast access to array elements. The instruction add eax,1 is the loop
increment i++. cmp eax, 100 / jl $B1$2 is the loop condition i < 100. It compareseax with 100 and jumps back to the $B1$2 label if i < 100. pop ebx restores the valueof ebx that was saved in the beginning. ret returns from the function.
The assembly listing reveals three things that can be optimized further. The first thing wenotice is that it does some funny things with the sign bit of i in order to divide i by 2. The
compiler has not noticed that i can never be negative so that we do not have to care about
the sign bit. We can tell it this by making i an unsigned int or by type-casting i tounsigned int before dividing by 2 (See page 150).
The second thing we notice is that the value pointed to by r is re-loaded from memory ahundred times. This is because we forgot to tell the compiler to assume no pointer aliasing(see page 81). Adding the compiler option "assume no pointer aliasing" (if available) can
possibly improve the code.
The third thing that can be improved is that r+i/2 could be calculated by an induction
variable because it is a staircase function of the loop index. The integer division prevents
the compiler from making an induction variable unless the loop is rolled out by 2. (See page72).
The conclusion is that we can help the compiler optimize example 8.27a by rolling out theloop by two and making an explicit induction variable. (This eliminates the need for the firsttwo suggested improvements).
// Example 8.27b
void Func(int a[], int & r) {int i;
int induction = r;for (i = 0; i < 100; i += 2) {a[i] = induction;a[i+1] = induction;induction++;

90

}
}

The compiler generates the following assembly code from example 8.27b:; Example 8.27b compiled to assembly:
ALIGN
4; align by 4
PUBLIC ?Func@@YAXQAHAAH@Z; mangled function name
?Func@@YAXQAHAAH@Z PROC NEAR
; start of Func
; parameter 1: 4 + esp; a; parameter 2: 8 + esp; r$B1$1:; unused label
moveax, DWORD PTR [esp+4]; eax = address of amovedx, DWORD PTR [esp+8]; edx = address in rmovecx, DWORD PTR [edx]; ecx = Inductionleaedx, DWORD PTR [eax+400] ; edx = point to end of a
$B2$2:; top of loop
mov
DWORD PTR [eax], ecx; a[i] = Induction;
mov
DWORD PTR [eax+4], ecx; a[i+1] = Induction;addecx, 1; Induction++;add
eax, 8; point to a[i+2]cmpedx, eax; compare with end of arrayja$B2$2; jump to top of loop
$B2$3:; unused labelret; return from Func
ALIGN
4
; mark_end;?Func2@@YAXQAHAAH@Z ENDP

This solution is clearly better. The loop body now contains only six instructions rather thannine, even though it is doing two iterations in one. The compiler has replaced i by a second
induction variable (eax) which contains the address of the current array element. Rather
than comparing i with 100 in the loop control it compares the array pointer eax to theaddress of the end of the array, which it has calculated in advance and stored in edx.
Furthermore, this solution is using one register less so that it does not have to push and pop
ebx.

9 Optimizing memory access
9.1 Caching of code and data
A cache is a proxy for the main memory in a computer. The proxy is smaller and closer tothe CPU than the main memory and therefore it is accessed much faster. There may be twoor three levels of cache for the sake of fastest possible access to the most used data.
The speed of the CPU is much faster than the speed of RAM memory. Efficient caching istherefore quite important.

9.2 Cache organization
It is useful to know how a cache is organized if you are making programs that have big data
structures with non-sequential access and you want to prevent cache contention. You may
skip this section if you are satisfied with more heuristic guidelines.
Most caches are organized into lines and sets. Let me explain this with an example. My
example is a cache of 8 kb size with a line size of 64 bytes. Each line covers 64 consecutive
bytes of memory. One kilobyte is 1024 bytes, so we can calculate that the number of lines is
8*1024/64 = 128. These lines are organized as 32 sets  4 ways. This means that a91

particular memory address cannot be loaded into an arbitrary cache line. Only one of the 32
sets can be used, but any of the 4 lines in the set can be used. We can calculate which set
of cache lines to use for a particular memory address by the formula: (set) = (memory
address) / (line size) % (number of sets). Here, / means integer division with truncation, and %means modulo. For example, if we want to read from memory address a = 10000, then we
have (set) = (10000 / 64) % 32 = 28. This means that a must be read into one of the fourcache lines in set number 28. The calculation becomes easier if we use hexadecimal
numbers because all the numbers are powers of 2. Using hexadecimal numbers, we have a= 0x2710 and (set) = (0x2710 / 0x40) % 0x20 = 0x1C. Reading or writing a variable fromaddress 0x2710 will cause the cache to load the entire 64 or 0x40 bytes from address
0x2700 to 0x273F into one of the four cache lines from set 0x1C. If the program afterwards
reads or writes to any other address in this range then the value is already in the cache so
we do not have to wait for another memory access.
Assume that a program reads from address 0x2710 and later reads from addresses
0x2F00, 0x3700, 0x3F00 and 0x4700. These addresses all belong to set number 0x1C.
There are only four cache lines in each set. If the cache always chooses the least recently
used cache line then the line that covered the address range from 0x2700 to 0x273F will beevicted when we read from 0x4700. Reading again from address 0x2710 will cause a cachemiss. But if the program had read from different addresses with different set values then theline containing the address range from 0x2700 to 0x273F would still be in the cache. The
problem only occurs because the addresses are spaced a multiple of 0x800 apart. I will callthis distance the critical stride. Variables whose distance in memory is a multiple of thecritical stride will contend for the same cache lines. The critical stride can be calculated as(critical stride) = (number of sets)  (line size) = (total cache size) / (number of ways).
If a program contains many variables and objects that are scattered around in memory, thenthere is a risk that several variables happen to be spaced by a multiple of the critical stride
and cause contentions in the data cache. The same can happen in the code cache if there
are many functions scattered around in program memory. If several functions that are usedin the same part of the program happen to be spaced by a multiple of the critical stride thenthis can cause contentions in the code cache. The subsequent sections describe various
ways to avoid these problems.
More details about how caches work can be found in Wikipedia under CPU cache(en.wikipedia.org/wiki/L2_cache).
The details of cache organization for different processors are covered in manual 3: "Themicroarchitecture of Intel, AMD and VIA CPUs".

9.3 Functions that are used together should be stored together
The code cache works most efficiently if functions that are used near each other are also
stored near each other in the code memory. The functions are usually stored in the order inwhich they appear in the source code. It is therefore a good idea to collect the functions thatare used in the most critical part of the code together near each other in the same source
file. Keep often used functions separate from seldom used functions, and put seldom usedbranches such as error handling in the end of a function or in a separate function.
Sometimes, functions are kept in different source files for the sake of modularity. For
example, it may be convenient to have the member functions of a parent class in one
source file and the derived class in another source file. If the member functions of parent
class and derived class are called from the same critical part of the program then it can beadvantageous to keep the two modules contiguous in program memory. This can be doneby controlling the order in which the modules are linked together. The link order is usually
the order in which the modules appear in the project window or makefile. You can check theorder of functions in memory by requesting a map file from the linker. The map file tells the92address of each function relative to the beginning of the program. The map file includes theaddresses of library functions linked from static libraries (.lib or .a), but not dynamic
libraries (.dll or .so). There is no easy way to control the addresses of dynamically linked
library functions.

9.4 Variables that are used together should be stored together
Cache misses are very expensive. A variable can be fetched from the cache in just a few
clock cycles, but it can take more than a hundred clock cycles to fetch the variable from
RAM memory if it is not in the cache.
The cache works most efficiently if pieces of data that are used together are stored neareach other in memory. Variables and objects should preferably be declared in the functionin which they are used. Such variables and objects will be stored on the stack, which is very
likely to be in the level-1 cache. The different kinds of variable storage are explained on
page 25. Avoid global and static variables if possible, and avoid dynamic memory allocation(new and delete).
Object oriented programming can be an efficient way of keeping data together. Data
members of a class (also called properties) are always stored together in an object of theclass. Data members of a parent class and a derived class are stored together in an objectof the derived class (see page 52).
The order in which data are stored can be important if you have big data structures. For
example, if a program has two arrays, a and b, and the elements are accessed in the ordera[0], b[0], a[1], b[1], ... then you may improve the performance by organizing the dataas an array of structures:// Example 9.1a
int Func(int);
const int size = 1024;
int a[size], b[size], i;...
for (i = 0; i < size; i++) {b[i] = Func(a[i]);}The data in this example can be accessed sequentially in memory if organized as follows:// Example 9.1b
int Func(int);
const int size = 1024;
struct Sab {int a; int b;};
Sab ab[size];
int i;...
for (i = 0; i < size; i++) {ab[i].b = Func(ab[i].a);}There will be no extra overhead in the program code for making the structure in example
9.1b. On the contrary, the code becomes simpler because it needs only calculate element
addresses for one array rather than two.
Some compilers will use different memory spaces for different arrays even if they are neverused at the same time. Example:// Example 9.2a
void F1(int
x[]);

93

void F2(float x[]);void F3(bool y) {if (y) {int a[1000];F1(a);}
else {
float b[1000];
F2(b);}}

Here it is possible to use the same memory area for a and b because their live ranges do
not overlap. You can save a lot of cache space by joining a and b in a union:// Example 9.2b
void F3(bool y) {union {
int
a[1000];float b[1000];};if (y) {F1(a);}
else {
F2(b);}}

Using a union is not a safe programming practice, of course, because you will get nowarning from the compiler if the uses of a and b overlap. You should use this method only
for big objects that take a lot of cache space. Putting simple variables into a union is not
optimal because it prevents the use of register variables.

9.5 Alignment of data
A variable is accessed most efficiently if it is stored at a memory address which is divisibleby the size of the variable. For example, a double takes 8 bytes of storage space. It should
therefore preferably be stored at an address divisible by 8. The size should always be a
power of 2. Objects bigger than 16 bytes should be stored at an address divisible by 16.
You can generally assume that the compiler takes care of this alignment automatically.
The alignment of structure and class members may cause a waste of cache space, as
explained in example 7.39 page 53.
You may choose to align large objects and arrays by the cache line size, which is typically
64 bytes. This makes sure that the beginning of the object or array coincides with the
beginning of a cache line. Some compilers will align large static arrays automatically but you
may as well specify the alignment explicitly by writing:alignas(64) int BigArray[1024];
See page 100 and 133 for discussion of aligning dynamically allocated memory.

94

9.6 Dynamic memory allocation
Objects and arrays can be allocated dynamically with new and delete, or malloc andfree. This can be useful when the amount of memory required is not known at compile
time. Four typical uses of dynamic memory allocation can be mentioned here:•

A large array can be allocated dynamically when the size of the array is not known atcompile time.

•

A variable number of objects can be allocated dynamically when the total number ofobjects is not known at compile time.

•

Text strings and similar objects of variable size can be allocated dynamically.

•

Arrays that are too large for the stack can be allocated dynamically.

The advantages of dynamic memory allocation are:•Gives a more clear program structure in some cases.

•

Does not allocate more space than needed. This makes data caching more efficientthan when a fixed-size array is made very big in order to cover the worst casesituation of the maximum possible memory requirement.

•

Useful when no reasonable upper limit to the required amount of memory space canbe given in advance.

The disadvantages of dynamic memory allocation are:•

The process of dynamic allocation and deallocation of memory takes much moretime than other kinds of storage. See page 25.

•

The heap space becomes fragmented when objects of different sizes are allocatedand deallocated in random order. This makes data caching inefficient.

•

An allocated array may need to be resized in the event that it becomes full. This may
require that a new bigger memory block is allocated and the entire contents copied
to the new block. Any pointers to data in the old block then become invalid.

•

The heap manager will start garbage collection when the heap space has become
too fragmented. This garbage collection may start at unpredictable times and causedelays in the program flow at inconvenient times when a user is waiting for
response.

•

It is the responsibility of the programmer to make sure that everything that has been
allocated is also deallocated. Failure to do so will cause the heap to be filled up. Thisis a common programming error known as memory leaks.

•

It is the responsibility of the programmer to make sure that no object is accessed
after it has been deallocated. Failure to do so is also a common programming error.

•

The allocated memory may not be optimally aligned. See page 133 for how to aligndynamically allocated memory.

•

It is difficult for the compiler to optimize code that uses pointers because it cannot
rule out aliasing (see page 81).
95

•A matrix or multidimensional array is less efficient when the row length is not knownat compile time because of the extra work needed for calculating row addresses at
each access. The compiler may not be able to optimize this with induction variables.

It is important to weigh the advantages over the disadvantages when deciding whether touse dynamic memory allocation. There is no reason to use dynamic memory allocation
when the size of an array or the number of objects is known at compile time or a reasonable
upper limit can be defined.
The cost of dynamic memory allocation is negligible when the number of allocations islimited. Dynamic memory allocation can therefore be advantageous when a program has
one or a few arrays of variable size. The alternative solution of making the arrays very big tocover the worst case situation is a waste of cache space. A situation where a program has
several large arrays and where the size of each array is a multiple of the critical stride (seeabove, page 91) is likely to cause contentions in the data cache.
If the number of elements in an array grows during program execution, then it is preferable
to allocate the final array size right from the beginning rather than allocating more space
step by step. In most systems, you cannot increase the size of a memory block that has
already been allocated. If the final size cannot be predicted or if the prediction turns out to
be too small, then it is necessary to allocate a new bigger memory block and copy thecontents of the old memory block into the beginning of the new bigger memory block. This isinefficient, of course, and causes the heap space to become fragmented. An alternative is tokeep multiple memory blocks, either in the form of a linked list or with an index of memory
blocks. A method with multiple memory blocks makes the access to individual arrayelements more complicated and time consuming.
A collection of a variable number of objects is often implemented as a linked list. Eachelement in a linked list has its own memory block and a pointer to the next block. A linked
list is less efficient than a linear array for the following reasons:•

Each object is allocated separately. The allocation, deallocation and garbage
collection takes a considerable amount of time.

•

The objects are not stored contiguously in the memory. This makes data cachingless efficient.

•

Extra memory space is used for the link pointers and for information stored by theheap manager for each allocated block.

•

Walking through a linked list takes more time than looping through a linear array. No
link pointer can be loaded until the previous link pointer has been loaded. This
makes a critical dependency chain which prevents out-of-order execution.

It is often more efficient to allocate one big block of memory for all the objects (memory
pooling) than to allocate a small block for each object.
A little-known alternative to using new and delete is to allocate variable-size arrays withalloca. This is a function that allocates memory on the stack rather than on the heap. The
space is automatically deallocated when returning from the function in which alloca wascalled. There is no need to deallocate the space explicitly when alloca is used. The
advantages of alloca over new and delete or malloc and free are:•

There is very little overhead to the allocation process because the microprocessor
has hardware support for the stack.

96

•The memory space never becomes fragmented thanks to the first-in-last-out natureof the stack.

•

Deallocation has no cost because it goes automatically when the function returns.
There is no need for garbage collection.

•

The allocated memory is contiguous with other objects on the stack, which makes
data caching very efficient.

The following example shows how to make a variable-size array with alloca:// Example 9.3#include <malloc.h>void SomeFunction (int n) {if (n > 0) {// Make dynamic array of n floats:float * DynamicArray = (float *)alloca(n * sizeof(float));// (Some compilers use the name _alloca)
for (int i = 0; i < n; i++) {DynamicArray[i] = WhateverFunction(i);// ...
}}}

Obviously, a function should never return any pointer or reference to anything it has
allocated with alloca, because it is deallocated when the function returns. alloca may
not be compatible with structured exception handling. See the manual for your compiler for
restrictions on using alloca.
The C99 extension supports variable-size arrays. This feature is controversial and is only
available in C, not in C++. You may use alloca instead of variable-size arrays since it
provides the same functionality.

9.7 Data structures and container classes
Whenever dynamic memory allocation is used, it is recommended to wrap the allocatedmemory into a container class. The container class must have a destructor to make sureeverything that is allocated is also de-allocated. This is the best way to prevent memory
leaks and other common programming errors associated with dynamic memory allocation. A
simple alternative to a container class is to use a smart pointer, as explained on page 37.
Container classes can also be convenient for adding bounds-checking to an array and formore advanced data structures with First-In-First-Out or Last-In-First-Out access, sort andsearch facilities, binary trees, hash maps etc.
It is common to make container classes in the form of templates where the type of objects
they contain is provided as a template parameter. There is no performance cost to using
templates.
Ready-made container class templates are available in standard C++ libraries with theC++11 and later standards. The advantage of using ready-made containers is that you do
not have to reinvent the wheel. The standard containers are universal, flexible, well tested,and very useful for many different purposes.
But while the standard containers are designed for generality and flexibility, efficiency has
been sacrificed. Execution speed, memory economy, cache efficiency, and code size have
got low priority. Especially the memory allocation is unnecessarily wasteful in many97

standard containers. Many container class templates are implemented as linked lists that
allocate one or more new memory blocks for every object in the container.
std::vector stores all the objects in one contiguous memory block, but this memory
block is re-allocated every time it is filled up, which happens quite often because the block
size grows by only 50% or less each time. An experiment where 10 elements were inserted,
one by one, into a std::vector turned up to cause seven memory allocations of sizes 1,
2, 3, 4, 6, 9 and 13 objects, respectively (MS Visual Studio 2019). This wasteful behavior
can be prevented by calling std::vector::reserve with a prediction or estimate of the
final size needed before adding the first object to the vector. Most other containers do nothave such a feature for reserving memory in advance.
The frequent allocation and de-allocation of memory with new and delete causes thememory to become fragmented so that caching is very inefficient. There is a large overhead
cost to memory management and garbage collection, as mentioned above.
The generality of the standard containers also costs in terms of code size. In fact, thestandard library has been criticized for code bloat and complexity(en.wikipedia.org/wiki/Standard_Template_Library). The objects stored in a standard
container are allowed to have constructors and destructors. The move constructors or copy
constructors and destructors of each object are called every time an object is moved, which
may happen quite often. This is necessary if the objects stored are containers themselves.
But implementing a matrix as a vector of vectors, as is often seen, is certainly a very
inefficient solution.
Many containers use linked lists. A linked list is a convenient way of making the container
expandable, but it is very inefficient. Linear arrays with contiguous memory are usuallymuch faster than linked lists.
The so-called iterators that are used in containers for accessing container elements arecumbersome to use for many programmers and they are not necessary if you can use alinear list with a simple index. A good compiler can optimize away the extra overhead of theiterators in some cases, but not all.
Fortunately, there are more efficient alternatives that can be used where execution speed,
memory economy and small code size has higher priority than code generality. The mostimportant remedy is memory pooling. It is more efficient to store many objects together in
one big memory block than to store each object in its own allocated memory block. A large
block containing many objects can be copied or moved with a single call to memcpy rather
than moving each object separately if there are no move constructors, copy constructors, or
destructors that need to be called. Some examples of efficient container classes are shown
below on page 101. A collection of container classes are provided atwww.agner.org/optimize/cppexamples.zip. Container classes for vectors and matrixes canbe found in the vector class library on github.
The following considerations should be taken into account when choosing a container for a
specific purpose:•

Contain one or multiple elements? If the container is to hold only one element thenyou should use local allocation or perhaps a smart pointer (see page 37).

•

Is the size known at compile time? If the maximum number of elements is known atcompile time or a not-too-big upper limit can be set, then the optimal solution is a
fixed size array. Dynamic memory allocation may be needed, however, if the array orcontainer is too big for the stack.

98•Is the size known before the first element is stored? If the total number of elements
to store is known before the first element is stored, or if a reasonable estimate or
upper limit can be made, then it is preferred to use a container such as
std::vector that allows you to reserve the amount of memory needed in advancerather than allocating piecewise or re-allocating when a memory block turns out to
be too small.

•Is the size big or small? A big and complex solution may not be worth the trouble fora small data structure, even if you need features for searching, adding, andremoving objects. Linear search in an unsorted array is certainly not the mostefficient algorithm, but it may still be the best solution if the array is small.

•

Are objects numbered consecutively? If objects are identified by consecutive indicesor by keys within a limited range then a simple array is the most efficient solution.

•

Is a multidimensional structure needed? A matrix or multidimensional array should
be stored in one contiguous memory block. Do not use one container for each row orcolumn. A nested container is certainly not the most efficient solution. The access isfaster if the number of elements per row is a constant known at compile time.

•

Are objects accessed in a FIFO manner? If objects are accessed on a First-In-FirstOut (FIFO) basis then use a queue. It is much more efficient to implement a queue
as a circular buffer than as a linked list. See example 9.6 page 103.

•

Are objects accessed in a LIFO manner? If objects are accessed on a Last-In-FirstOut (LIFO) basis then use a linear array with a top-of-stack index. See example 9.5
page 102.

•

Are objects identified by a key? If the key values are confined to a limited range thena simple array can be used. If the number of objects is high then the most efficientsolution may be a binary tree or a hash map.

•

Do objects have a natural ordering? If you need to do searches of the kind: "what is
the nearest element to x?" or "how many elements are there between x and y?" thenyou may use a sorted list or a binary tree.

•

Is searching needed after all objects have been added? If search facilities areneeded, but only after all objects have been stored in the container, then a linear
array will be an efficient solution. Sort the array after all elements have been added.
Then use binary search for finding elements. A sorted list may still be useful if adding
objects is rare while searching is frequent, and the list is not too big. A hash map
may also be an efficient solution.

•

Is searching needed before all objects have been added? If search facilities areneeded, and objects can be added or removed at any time, then the solution is more
complicated. If the total number of elements is small then a sorted list can still be themost efficient solution because of its simplicity. But a sorted list can be very
inefficient if the list is large. The insertion of a new element in the list causes all
subsequent elements in the sequence to be moved. A binary tree or a hash map ismore efficient in this case. A binary tree may be used if elements have a naturalorder and there are search requests for elements in a specific interval. A hash map
can be used if elements have no specific order but are identified by a unique key.

•

Do objects have mixed types or sizes? It is possible to store objects of different
types or strings of different lengths in the same memory pool. See
www.agner.org/optimize/cppexamples.zip. If the number and types of elements is99

known at compile time then there is no need to use a container or memory pool.
•

Alignment? Some applications require that data are aligned at round addresses.
Especially the use of intrinsic vectors requires alignment to addresses divisible by
16, 32, or 64. Alignment of data structures to addresses divisible by the cache line
size (typically 64) can improve performance in some cases. C++98 and later
standards guarantee that allocated memory is correctly aligned for the object typespecified to the new operator.

•

Multiple threads? Container classes are generally not thread safe if multiple threads
can add, remove, or modify objects simultaneously. It is more efficient to haveseparate containers for each thread in multithreaded applications than to temporarily
lock a container for exclusive access by each thread.

•

Pointers to contained objects? It may not be safe to make a pointer to an object
inside a container because the container may move the object in case memory reallocation is needed. Objects inside containers should be identified by their index or
key in the container rather than by pointers or references. It is OK, however, to passa pointer or reference to such an object to a function that does not add or remove
any objects if no other threads have access to the container.

•

Can the container be recycled? There is a significant cost to creating and deleting
containers. It may be more efficient to re-use a container than to delete it and create
a new one, if the program logic allows it.

Standard C++ containers
The standard C++ container class templates provide convenient and well-tested solutions tocommon problems of organizing data structures. Unfortunately, these containers are quite
complicated and not always very efficient. Linked lists, in particular, are inefficient because
the data objects are stored in fragmented bits of memory with poor caching as a consequence. The code is bulky with a lot of overhead. A good optimizing compiler can peel off
much of the overhead, but the code is still difficult to understand and difficult to debug
because of the excessive complexity. The most common C++ container class templates arelisted here:•

std::array. This works like a simple array. The size is fixed at compile time.
There is no bounds checking.

•std::vector. This works like a linear array. The elements are stored in one
contiguous memory block. The size can grow. The growth is organized is such a waythat a new memory block (50% bigger) is allocated when the previous memory block
becomes too small. All data have to be moved to the new memory block every time itis reallocated. It is important to use the reserve function with an estimate or upper
limit of the size needed before adding the first element. This will reduce the numberof reallocations. std::vector is the simplest and most efficient of the standard
containers with dynamic memory allocation.

•

std::deque. This is a double-ended queue. It can be used both as a linear array
and as a queue where elements can be inserted and removed efficiently in bothends. A deque may allocate fewer memory blocks than a linked list, because eachmemory block can contain multiple elements if the elements are small. It allows
random access to any element in the list, just like an array. A deque provides auseful compromise between a linear array and a linked list.

100

•

std::forward_list. This is a linked list. A new memory block is allocated foreach element. The elements can only be accessed sequentially. The list can besorted, but sorting is not particularly efficient.

•

std::list. This is a doubly linked list. Elements can be added and removed atboth ends. A list is similar to a forward_list, but the list can be traversed bothforwards and backwards.

•

std:stack. This is a Last-In-First-Out (LIFO) list. It is a special case of deque.

•std::set. This is an ordered list. The objects are identified by their value andordered by their value. Objects can be added and removed in random order at any
time. Searching for objects is efficient. A set is implemented as a self-balancing
binary tree (red-black tree). This is useful if objects are added in random order andsearched for in random order.

•

std::map. This is similar to std::set. The difference from std::set is that a map
contains key-value pairs. Objects are identified and ordered by their key while thevalue contains other information related to the key.

•

std::unordered_set. This can be used instead of std::set if the objects have nonatural order. It is implemented as a hash map. Objects can be added and removed
efficiently. Finding an object is efficient.

•

std::unordered_map. This is similar to an unordered_set, but contains keyvalue pairs.

Making your own container classes
An efficient container class should allocate as few memory blocks as possible. Preferably,
all data should be stored in one contiguous memory block. Dynamic memory allocation isnecessary only if a reasonable maximum size is not known at compile time. The type ofobjects that the container contains can be specified conveniently as a template parameter.
Remember to define a destructor to deallocate all allocated memory.
More examples of container class templates are provided atwww.agner.org/optimize/cppexamples.zip. Efficient containers for vectors and matrixes canbe found in the vector class library on github.
Example 9.4 shows a linear array where the size is specified in the constructor. Elements
are accessed with the [] operator. An error check for index out of range is added.
// Example 9.4// Template for simple array with bounds checking
template <typename T>// T is the element typeclass SafeArray {protected:
T * p = 0;// pointer to array
int size = 0;// array size
public:// Constructor
SafeArray(int n) {// n is the desired size
p = new T[n];// allocate memoryif (p == 0) {// .. error message here: memory allocation failed}
size = n;// save size}101

// Return the size of the arrayint getSize() const {return size;}// [] array index operator
T & operator[] (int i) {if ((unsigned int)i >= (unsigned int)size) {// Index out of range// .. error message here: index out of range
return p[0];}// No error
return p[i];// Return reference to element}~SafeArray() {// destructor
if (p) delete[] p;// free the memory}};// ...
// Example of use:
SafeArray<float> myArray(100);for (int i = 0; i < 100; i++) {myArray[i] = 0;}
myArray[2] = 9;

Example 9.5 shows a Last-In-First-Out list (also called a stack) where a fixed maximum sizeis specified as a template parameter. No dynamic memory allocation is needed.
// Example 9.5// Template for Last-In-First-Out list
template <typename T, int maxsize>class LIFOStack {protected:
int n = 0;// Number of objects in list
T list[maxsize];// Data buffer
public:LIFOStack() {// Constructorn = 0;}
bool put(T const & x) { // Put object into list
if (n >= maxsize) {return false;// Return false if list full}list[n] = x;n++;// Increment countreturn true;// Return true if success}T get() {// Get object from list
if (n <= 0) {// Error: list empty// Put error message here: list empty
return T(0);}n--;// Decrement countreturn list[n];// Return object}
int getNum() const {// Tell number of objects in list
return n;
}

102

};
// ...
// Example of use:
LIFOStack<int, 100> myQ;// put numbers infor (int i = 0; i < 5; i++) {myQ.put(i*10);}// get numbers out in reverse orderwhile (myQ.getNum() > 0) {printf("\n%i ", myQ.get());}

Example 9.6 shows a First-In-First-Out list (also called a queue) where a fixed maximum
size is specified as a template parameter. No dynamic memory allocation is needed. The list
is implemented as a circular buffer.
// Example 9.6// Template for First-In-First-Out list
template <typename T, int maxsize>class FIFOQueue {protected:
T * head, * tail;// Pointers to current head and tailint n;// Number of objects in list
T list[maxsize];// Circular buffer
public:FIFOQueue() {// Constructor
head = tail = list; // Initialize
n = 0;}
bool put(T const & x) { // Put object into list
if (n >= maxsize) {return false;// Return false if list full}n++;// Increment count*head = x;// Copy x to list
head++;// Increment head pointerif (head >= list + maxsize) {head = list;// Wrap around}return true;// Return true if success}T get() {// Get object from list
if (n <= 0) {// Put an error message here: list empty
return T(0);// return zero object if possible}n--;// Decrement countT * p = tail;// Pointer to object
tail++;// Increment tail pointerif (tail >= list + maxsize) {tail = list;// Wrap around}return *p;// Return object}
int getNum() const {// Tell number of objects in list
return n;}};// ...

103

// Example of use:
FIFOQueue<int, 100> myQ;// put numbers infor (int i = 0; i < 5; i++) {myQ.put(i*10);}// get numbers out in same orderwhile (myQ.getNum() > 0) {printf("\n%i ", myQ.get());}

The C++ standard does not currently define an efficient way of making a matrix withdimensions determined at run time. Example 9.7 shows a possible solution:// Example 9.7// Template for a matrix. The dimensions are decided at run time// This template makes a matrix of arbitrary dimensions// There is no bounds checking
template <typename T>class Matrix {public:// constructor with number of rows and columns
Matrix(int rows, int columns) {// save number of rows and columns
this->rows = rows;
this->columns = columns;// allocate memoryp = new T[rows * columns];
if (p == 0) {// .. error message here: allocation failed}
else {// initialize all elements to zerofor (int i = 0; i < rows * columns; i++) {p[i] = 0;}}}// get number of rows
int nrows() const {return rows;}// get number of columns
int ncols() const {return columns;}// destructor~Matrix() {if (p) delete[] p;// free the memory}// Operator [] gives a pointer to row number r.
// A second [] with the column number gives a single element
T * operator[] (int r) {// no bounds check
return p + r * columns;}
protected:
int rows;// number of rows
int columns;// number of columns
T * p;// pointer to memory};104

// ...
// Example of use:// Make a matrix of doubles with 8 rows and 10 columns
Matrix<double> mat(8,10);// Put a value in row 3, column 5
mat[3][5] = 12.34;// Print out the matrix
for (int r = 0; r < mat.nrows(); r++) {for (int c = 0; c < mat.ncols(); c++) {printf("%5.2f ", mat[r][c]);}printf("\n");}I have provided several other examples of container class templates inwww.agner.org/optimize/cppexamples.zip. These may be used as alternatives to thestandard C++ containers if the full generality and flexibility of the standard containers is notneeded. You may write your own container classes or modify the ones that are available tofit specific needs.

9.8 Strings
Text strings typically have variable lengths that are not known at compile time. The storageof text strings in classes like string, or wstring is using new and delete to allocate a
new memory block every time a string is created or modified. This can be quite inefficient if
a program creates or modifies many strings.
In most cases, the fastest way to handle strings is the old fashioned C style with character
arrays. Strings can be manipulated with C functions such as strcpy, strcat, strlen,
sprintf, etc. But beware that these functions have no check for overflow of the arrays. An
array overflow can cause unpredictable errors elsewhere in the program that are very
difficult to diagnose. It is the responsibility of the programmer to make sure the arrays are
sufficiently large to handle the strings including the terminating zero and to make overflow
checks where necessary. Fast versions of common string functions as well as efficient
functions for string searching and parsing are provided in the asmlib library at
www.agner.org/optimize/asmlib.zip.
If you want to improve speed without jeopardizing safety, you may store all strings in amemory pool, as explained above. Examples are provided in an appendix to this manual atwww.agner.org/optimize/cppexamples.zip.

9.9 Access data sequentially
A cache works most efficiently when the data are accessed sequentially. It works somewhat
less efficiently when data are accessed backwards and much less efficiently when data areaccessed in a random manner. This applies to reading as well as writing data.
Multidimensional arrays should be accessed with the last index changing in the innermost
loop. This reflects the order in which the elements are stored in memory. Example:// Example 9.8
const int NUMROWS = 100, NUMCOLUMNS = 100;
int matrix[NUMROWS][NUMCOLUMNS];
int row, column;for (row = 0; row < NUMROWS; row++)

105

for (column = 0; column < NUMCOLUMNS; column++)matrix[row][column] = row + column;

Do not swap the order of the two loops (except in Fortran where the storage order is
opposite).

9.10 Cache contentions in large data structures
It is not always possible to access a multidimensional array sequentially. Some applications(e.g. in linear algebra) require other access patterns. This can cause severe delays if the
distance between rows in a big matrix happen to be equal to the critical stride, as explainedon page 92. This will happen if the size of a matrix line (in bytes) is a high power of 2.
The following example illustrates this. My example is a function which transposes aquadratic matrix, i.e. each element matrix[r][c] is swapped with element
matrix[c][r].
// Example 9.9a
const int SIZE = 64;// number of rows/columns in matrix

void transpose(double a[SIZE][SIZE]) { // function to transpose matrix// define a macro to swap two array elements:#define swapd(x,y) {temp=x; x=y; y=temp;}
int r, c; double temp;
for (r = 1; r < SIZE; r++) {for (c = 0; c < r; c++) {swapd(a[r][c], a[c][r]);}}// loop through rows// loop columns below diagonal// swap elements}
void test () {alignas(64)double matrix[SIZE][SIZE];transpose(matrix);}// align by cache line size// define matrix// call transpose function

Transposing a matrix is the same as reflecting it at the diagonal. Each element
matrix[r][c] below the diagonal is swapped with element matrix[c][r] at its mirror
position above the diagonal. The c loop in example 9.9a goes from the leftmost column tothe diagonal. The elements at the diagonal remain unchanged.
The problem with this code is that if the elements matrix[r][c] below the diagonal areaccessed row-wise, then the mirror elements matrix[c][r] above the diagonal areaccessed column-wise.
Assume now that we are running this code with a 6464 matrix on a Pentium 4 computer
where the level-1 data cache is 8 kb = 8192 bytes, 4 ways, with a line size of 64. Eachcache line can hold 8 double's of 8 bytes each. The critical stride is 8192 / 4 = 2048 bytes= 4 rows.
Let's look at what happens inside the loop, for example when r = 28. We take the elementsfrom row 28 below the diagonal and swap these elements with column 28 above thediagonal. The first eight elements in row 28 share the same cache line. But these eightelements will go into eight different cache lines in column 28 because the cache lines follow
the rows, not the columns. Every fourth of these cache lines belong to the same set in thecache. When we reach element number 16 in column 28, the cache will evict the cache linethat was used by element 0 in this column. Number 17 will evict number 1. Number 18 will106

evict number 2, etc. This means that all the cache lines we used above the diagonal havebeen lost at the time we are swapping column 29 with line 29. Each cache line has to bereloaded eight times because it is evicted before we need the next element. I haveconfirmed this by measuring the time it takes to transpose a matrix using example 9.9a on a
Pentium 4 with different matrix sizes. The results of my experiment are given below. The
time unit is clock cycles per array element.
Matrix size

Total kilobytes

Time per element

6363
6464
6565
127127
128128129129511511512512513513

31
32
33
126
128
130
2040
2048
2056

11.6
16.4
11.8
12.2
17.4
14.4
38.7
230.7
38.1

Table 9.1. Time for transposition of different size matrices,
clock cycles per element.

The table shows that it takes 40% more time to transpose the matrix when the size of thematrix is a multiple of the level-1 cache size. This is because the critical stride is a multipleof the size of a matrix line. The delay is less than the time it takes to reload the level-1
cache from the level-2 cache because the out-of-order execution mechanism can prefetch
the data.
The effect is much more dramatic when contentions occur in the level-2 cache. The level-2
cache is 512 kb, 8 ways. The critical stride for the level-2 cache is 512 kb / 8 = 64 kb. This
corresponds to 16 lines in a 512512 matrix. My experimental results in table 9.1 show thatit takes six times as long time to transpose a matrix when contentions occur in the level-2
cache as when contentions do not occur. The reason why this effect is so much stronger forlevel-2 cache contentions than for level-1 cache contentions is that the level-2 cache cannot
prefetch more than one line at a time.
A simple way of solving the problem is to make the rows in the matrix longer than needed inorder to avoid that the critical stride is a multiple of the matrix line size. I tried to make thematrix 512520 and leave the last 8 columns unused. This removed the contentions and thetime consumption was down to 36.
There may be cases where it is not possible to add unused columns to a matrix. For
example, a library of math functions should work efficiently on all sizes of matrices. An
efficient solution in this case is to divide the matrix into smaller squares and handle one
square at a time. This is called square blocking or tiling. This technique is illustrated inexample 9.9b.
// Example 9.9b
void transpose(double a[SIZE][SIZE]) {// Define macro to swap two elements:#define swapd(x,y) {temp=x; x=y; y=temp;}// Check if level-2 cache contentions will occur:
if (SIZE > 256 && SIZE % 128 == 0) {// Cache contentions expected. Use square blocking:int r1, r2, c1, c2; double temp;// Define size of squares:
const int TILESIZE = 8;// SIZE must be divisible by TILESIZE// Loop r1 and c1 for all squares:107for (r1 = 0; r1 < SIZE; r1 += TILESIZE) {for (c1 = 0; c1 < r1; c1 += TILESIZE) {// Loop r2 and c2 for elements inside sqaure:for (r2 = r1; r2 < r1+TILESIZE; r2++) {for (c2 = c1; c2 < c1+TILESIZE; c2++) {swapd(a[r2][c2],a[c2][r2]);}}}// At the diagonal there is only half a square.
// This triangle is handled separately:for (r2 = r1+1; r2 < r1+TILESIZE; r2++) {for (c2 = r1; c2 < r2; c2++) {swapd(a[r2][c2],a[c2][r2]);}}}}
else {// No cache contentions. Use simple method.
// This is the code from example 9.9a:
int r, c; double temp;
for (r = 1; r < SIZE; r++) {// loop through rowsfor (c = 0; c < r; c++) {// loop columns below diagonal
swapd(a[r][c], a[c][r]); // swap elements}}}}

This code took 50 clock cycles per element for a 512512 matrix in my experiments.
Contentions in the level-2 cache are so expensive that it is very important to do something
about them. You should therefore be aware of situations where the number of columns in amatrix is a high power of 2. Contentions in the level-1 cache are less expensive. Using
complicated techniques like square blocking for the level-1 cache may not be worth theeffort.
Square blocking and similar methods are further described in the book "Performance
Optimization of Numerically Intensive Codes", by S. Goedecker and A. Hoisie, SIAM 2001.

9.11 Explicit cache control
Microprocessors with the SSE and later instruction sets have certain instructions that allow
you to manipulate the data cache. These instructions are accessible through intrinsic
functions.
Function

Prefetch
Store 4 bytes without cacheStore 8 bytes without cacheStore 16 bytes withoutcacheStore 16 bytes withoutcacheStore 16 bytes without
cache

Assembly name

Intrinsic function nameInstruction
set

PREFETCH
MOVNTIMOVNTIMOVNTPS_mm_prefetch_mm_stream_si32_mm_stream_si64_mm_stream_ps

SSE
SSE2
SSEs
SSE

MOVNTPD_mm_stream_pd

SSE2

MOVNTDQ_mm_stream_si128

SSE2

108

Table 9.2. Cache control instructions.

There are other cache control instructions than the ones mentioned in table 9.2, such as
flush and fence instructions, but these are hardly relevant to optimization.

Prefetching data
The prefetch instruction can be used for fetching a cache line that we expect to use later inthe program flow. However, this did not improve the execution speed in any of the examples
I have tested. The reason is that modern processors prefetch data automatically thanks toout-of-order execution and advanced prediction mechanisms. Modern microprocessors areable to automatically prefetch data for regular access patterns containing multiple streams
with different strides. Therefore, you do not have to prefetch data explicitly if data accesscan be arranged in regular patterns with fixed strides.

Uncached memory store
A write with a cache miss is more expensive than an uncached read because the write
causes an entire cache line to be read and written back.
The so-called nontemporal write instructions (MOVNT) are designed to solve this problem.
These instructions write directly to memory without loading a cache line. This isadvantageous in cases where we are writing to uncached memory and we do not expect toread from the same or a nearby address again before the cache line would be evicted. Do
not mix nontemporal writes with normal writes or reads to the same memory area.
The nontemporal write instructions are not suitable for example 9.9 because we are reading
and writing from the same address so a cache line will be loaded anyway. If we modify
example 9.9 so that it writes only, then the effect of nontemporal write instructions becomes
noticeable. The following example transposes a matrix and stores the result in a different
array.
// Example 9.10a
const int SIZE = 512; // number of rows and columns in matrix// function to transpose and copy matrix
void TransposeCopy(double a[SIZE][SIZE], double b[SIZE][SIZE]) {int r, c;for (r = 0; r < SIZE; r++) {for (c = 0; c < SIZE; c++) {a[c][r] = b[r][c];}}}This function writes to matrix a in a column-wise manner where the critical stride causes all
writes to load a new cache line in both the level-1 and the level-2 cache. Using thenontemporal write instruction prevents the level-2 cache from loading any cache lines formatrix a:// Example 9.10b.
#include "xmmintrin.h"// header for intrinsic functions// This function stores a double without loading a cache line:
static inline void StoreNTD(double * dest, double const & source) {_mm_stream_pi((__m64*)dest, *(__m64*)&source); // MOVNTQ_mm_empty();// EMMS}
const int SIZE = 512; // number of rows and columns in matrix

109

// function to transpose and copy matrix
void TransposeCopy(double a[SIZE][SIZE], double b[SIZE][SIZE]) {int r, c;for (r = 0; r < SIZE; r++) {for (c = 0; c < SIZE; c++) {StoreNTD(&a[c][r], b[r][c]);}}}The execution times per matrix cell for different matrix sizes were measured on a Pentium 4
computer. The measured results were as follows:
Matrix size

Time per element
Example 9.10aTime per element
Example 9.10b

6464
6565
512512513513

14.0
13.6
378.7
58.7

80.8
80.9
168.5
168.3

Table 9.3. Time for transposing and copying different size
matrices, clock cycles per element.

As table 9.3 shows, the method of storing data without caching is advantageous if, and only
if, a level-2 cache miss can be expected. The 6464 matrix size causes misses in the level1 cache. This has hardly any effect on the total execution time because the cache miss on astore operation does not delay the subsequent instructions. The 512512 matrix size
causes misses in the level-2 cache. This has a very dramatic effect on the execution time
because the memory bus is saturated. This can be ameliorated by using nontemporal
writes. If the cache contentions can be prevented in other ways, as explained in chapter
9.10, then the nontemporal write instructions are not optimal.
There are certain restrictions on using the instructions listed in table 9.2. All these
instructions require that the microprocessor has the SSE or SSE2 instruction set, as listed in
the table. The 16-byte instructions MOVNTPS, MOVNTPD and MOVNTDQ require that theoperating system has support for XMM registers; see page 135.

10 Multithreading
The clock frequency of the CPU is limited by physical factors. The way to increase thethroughput of CPU-intensive programs when the clock frequency is limited is to do multiple
things at the same time. There are three ways to do things in parallel:•

Using multiple CPUs or multi-core CPUs, as described in this chapter.

•

Using the out-of-order capabilities of modern CPUs, as described in chapter 11.

•

Using the vector operations of modern CPUs, as described in chapter 12.

Most modern CPUs have two or more cores, and it can be expected that the number ofcores will grow in the future. To use multiple CPUs or CPU cores, we need to divide thework into multiple threads. There are two main principles here: functional decompositionand data decomposition. Functional decomposition here means that different threads are
doing different kinds of jobs. For example, one thread can take care of the user interface,
another thread can take care of communication with a remote database, and a third thread
can do mathematical calculations. It is important that the user interface is not in the same110

thread as very time-consuming tasks because this would give annoyingly long and irregularresponse times. It is often useful to put time-consuming tasks into separate threads with low
priority.
In many cases, however, there is a single task that consumes most of the resources. In thiscase we need to split up the data into multiple blocks in order to utilize the multiple
processor cores. Each thread should then handle its own block of data. This is data
decomposition.
It is important to distinguish between coarse-grained parallelism and fine-grained parallelism
when deciding whether it is advantageous to do things in parallel. Coarse-grained
parallelism refers to the situation where a long sequence of operations can be carried out
independently of other tasks that are running in parallel. Fine-grained parallelism is thesituation where a task is divided into many small subtasks, but it is impossible to work forvery long on a particular subtask before coordination with other subtasks is necessary.
Multithreading works more efficiently with coarse-grained parallelism than with fine-grained
parallelism because communication and synchronization between the different threads isslow. If the granularity is too fine then it is not advantageous to split the tasks into multiple
threads. Out-of-order execution (chapter 11) and vector operations (chapter 12) are more
useful methods for exploiting fine-grained parallelism.
The way to use multiple CPU cores is to divide the work into multiple threads. The use of
threads is discussed on page 61. In the case of data decomposition, we should preferably
have no more threads with the same priority than the number of cores or logical processors
available in the system. The number of logical processors available can be determined by asystem call (e.g. GetProcessAffinityMask in Windows).
There are several ways to divide the workload between multiple CPU cores:•

Define multiple threads and put an equal amount of work into each thread. Thismethod works with all compilers.

•

Use automatic parallelization. The Gnu and Intel compilers can automatically detect
opportunities for parallelization in the code and divide it into multiple threads, but thecompiler may not be able to find the optimal decomposition of the data.

•

Use OpenMP directives. OpenMP is a standard for specifying parallel processing in
C++ and Fortran. These directives are supported by most compilers. See
www.openmp.org and the compiler manual for details.

•

Use std::thread. This has fewer features than OpenMP, but is standardized acrossplatforms.

•

Use function libraries with internal multi-threading, e.g. Intel Math Kernel Library.

The multiple CPU cores or logical processors usually share the same cache, at least at thelast cache level, and in some cases even the same level-1 cache. The advantage of sharing
the same cache is that communication between threads becomes faster and that threads
can share the same code and read-only data. The disadvantage is that the cache will befilled up if the threads use different memory areas, and there will be cache contentions if thethreads write to the same memory areas.
Data that are read-only can be shared between multiple threads, while data that aremodified should be separate for each thread. It is not good to have two or more threads
writing to the same cache line, because the threads will invalidate each other's caches andcause large delays. The easiest way to make thread-specific data is to declare it locally in111

the thread function so that it is stored on the stack. Each thread has its own stack.
Alternatively, you may define a structure or class for containing thread-specific data andmake one instance for each thread. This structure or class should be aligned by at least thecache line size in order to avoid multiple threads writing to the same cache line. The cacheline size is typically 64 bytes on contemporary processors. The cache line size may possibly
be more (128 or 256 bytes) on future processors.
There are various methods for communication and synchronization between threads, suchas semaphores, mutexes and message systems. All of these methods are time consuming.
Therefore, the data and resources should be organized so that the amount of necessarycommunication between threads is minimized. For example, if multiple threads are sharing
the same queue, list, database, or other data structure, then you may consider if it ispossible to give each thread its own data structure and then merge the multiple data
structures in the end when all threads have finished the time-consuming data processing.
Running multiple threads on a system with only one logical processor is not an advantage if
the threads are competing for the same resources. But it can be a good idea to put timeconsuming calculations into a separate thread with lower priority than the user interface. It isalso useful to put file access and network access in separate threads so that one thread can
do calculations while another thread is waiting for response from a hard disk or network.

10.1 Simultaneous multithreading
Many microprocessors are able to run two threads in each core. For example, a processor
with four cores can run eight threads simultaneously. This processor has four physicalprocessors but eight logical processors.
"Hyperthreading" is Intel's term for simultaneous multithreading. Two threads running in thesame core will always compete for the same resources, such as cache and execution units.
There is no advantage to using simultaneous multithreading if any of the shared resources
are limiting factors for the performance then. On the contrary, each thread may run at lessthan half speed because of cache evictions and other resource conflicts. But if a large
fraction of the time goes to cache misses, branch misprediction, or long dependency chains,then each thread will run at more than half the single-thread speed. In this case there is anadvantage to using simultaneous multithreading, but the performance is not doubled. A
thread that shares the resources of the core with another thread will always run slower thana thread that runs alone in the core.
It is often necessary to do experiments in order to determine whether it is advantageous to
use simultaneous multithreading or not in a particular application.
If simultaneous multithreading is not advantageous then it is necessary to query certain
operating system functions (e.g. GetLogicalProcessorInformation in Windows) todetermine if the processor has simultaneous multithreading. If so, then you can avoid
simultaneous multithreading by using only the even-numbered logical processors (0, 2, 4,etc.). Older operating systems lack the necessary functions for distinguishing between thenumber of physical processors and the number of logical processors.
There is no way to tell the processor to give higher priority to one thread than another.
Therefore, it can often happen that a low-priority thread steals resources from a higherpriority thread running in the same core. It is the responsibility of the operating system toavoid running two threads with widely different priority in the same processor core.
Unfortunately, contemporary operating systems are not able to handle this problem
adequately.

112

11 Out of order execution
All modern x86 CPUs can execute instructions out of order or do more than one thing at thesame time, except for some small low-power CPUs (Intel Atom). The following example
shows how to take advantage of this capability:// Example 11.1afloat a, b, c, d, y;y = a + b + c + d;

This expression is calculated as ((a+b)+c)+d. This is a dependency chain where each
addition has to wait for the result of the preceding one. You can improve this by writing:// Example 11.1b
float a, b, c, d, y;y = (a + b) + (c + d);

Now the two parentheses can be calculated independently. The CPU will start to calculate(c+d) before it has finished the calculation of (a+b). This can save several clock cycles.
You cannot assume that an optimizing compiler will change the code in example 11.1a to
11.1b automatically, although it appears to be an obvious thing to do. The reason why
compilers do not make this kind of optimizations on floating point expressions is that it may
cause a loss of precision, as explained on page 75. You have to set the parentheses
manually.
The effect of dependency chains is stronger when they are long. This is often the case inloops. Consider the following example, which calculates the sum of 100 numbers:// Example 11.2aconst int size = 100;
float list[size], sum = 0; int i;for (i = 0; i < size; i++) sum += list[i];This has a long dependency chain. If a floating point addition takes 5 clock cycles, then thisloop will take approximately 500 clock cycles. You can improve the performancedramatically by unrolling the loop and splitting the dependency chain in two:// Example 11.2b
const int size = 100;
float list[size], sum1 = 0, sum2 = 0;
for (i = 0; i < size; i += 2) {sum1 += list[i];sum2 += list[i+1];}
sum1 += sum2;

int i;

If a microprocessor with only one adder is doing an addition to sum1 from time T to T+5,
then it can do another addition to sum2 from time T+1 to T+6, and the whole loop will takeonly 256 clock cycles.
Calculations in a loop where each iteration needs the result of the preceding one is called aloop-carried dependency chain. Such dependency chains can be very long and very timeconsuming. There is a lot to gain if such dependency chains can be broken up. The twosummation variables sum1 and sum2 are called accumulators. Current CPUs have one or
two floating point addition units, and these units are pipelined, as explained above, so that it
can start a new addition before the preceding addition is finished.
The optimal number of accumulators for floating point addition and multiplication may bethree or four, depending on the CPU.
113

Unrolling a loop becomes a little more complicated if the number of iterations is not divisibleby the unroll factor. For example, if the number of elements in list in example 11.2b wasan odd number then we would have to add the last element outside the loop or add an extra
dummy element to list and make this extra element zero.
It is not necessary to unroll a loop and use multiple accumulators if there is no loop-carried
dependency chain. A microprocessor with out-of-order capabilities can overlap the iterations
and start the calculation of one iteration before the preceding iteration is finished. Example:// Example 11.3
const int size = 100; int i;
float a[size], b[size], c[size];float register temp;
for (i = 0; i < size; i++) {temp = a[i] + b[i];c[i] = temp * temp;}

Microprocessors with out-of-order capabilities are very smart. They can detect that the valueof register temp in one iteration of the loop in example 11.3 is independent of the value inthe previous iteration. This allows it to begin calculating a new value of temp before it isfinished using the previous value. It does this by assigning a new physical register to temp
even though the logical register that appears in the machine code is the same. This is called
register renaming. The CPU can hold many renamed instances of the same logical register.
This advantage comes automatically. There is no reason to unroll the loop and have atemp1 and temp2. Modern CPUs are capable of register renaming and doing multiple
calculations in parallel if certain conditions are satisfied. The conditions that make it possible
for the CPU to overlap the calculations of loop iterations are:•No loop-carried dependency chain. Nothing in the calculation of one iteration should
depend on the result of the previous iteration (except for the loop counter, which is
calculated fast if it is an integer).

•

All intermediate results should be saved in registers, not in memory. The renaming
mechanism works only on registers, not on variables in memory or cache. Most
compilers will make temp a register variable in example 11.3 even without theregister keyword.

•

The loop branch should be predicted. This is no problem if the repeat count is large
or constant. If the loop count is small and changing then the CPU may occasionallypredict that the loop exits, when in fact it does not, and therefore fail to start the next
calculation. However, the out-of-order mechanism allows the CPU to increment theloop counter ahead of time so that it may detect the misprediction before it is toolate. You should therefore not be too worried about this condition.

In general, the out-of-order execution mechanism works automatically. However, there are acouple of things that the programmer can do to take maximum advantage of out-of-order
execution. The most important thing is to avoid long dependency chains. Another thing thatyou can do is to mix different kinds of operations in order to divide the work evenly between
the different execution units in the CPU. It can be advantageous to mix integer and floating
point calculations as long as you do not need conversions between integers and floating
point numbers. It can also be advantageous to mix floating point addition with floating point
multiplication, to mix simple integer with vector integer operations, and to mix mathematical
calculations with memory access.
Very long dependency chains put a strain on the out-of-order resources of the CPU, even if
they do not carry into the next iteration of a loop. A modern CPU can typically handle more114

than a hundred pending operations (see manual 3: "The microarchitecture of Intel, AMD and
VIA CPUs"). It may be useful to split a loop in two and store intermediate results in order tobreak an extremely long dependency chain.

12 Using vector operations
Today's microprocessors have vector instructions that make it possible to do operations on
all elements of a vector simultaneously. This is also called Single-Instruction-Multiple-Data(SIMD) operations. The total size of each vector can be 64 bits (MMX), 128 bits (XMM), 256
bits (YMM), and 512 bits (ZMM).
Vector operations are useful when doing calculations on large data sets where the same
operation is performed on multiple data elements and the program logic allows parallel
calculations. Examples are image processing, sound processing, and mathematicaloperations on vectors and matrixes. Algorithms that are inherently serial, such as mostsorting algorithms, are not well suited for vector operations. Algorithms that rely heavily ontable lookup or require a lot of data permutation, such as many encryption algorithms, areperhaps less suited for vector operations.
The vector operations use a set of special vector registers. The maximum size of each
vector register is 128 bits (XMM) if the SSE2 instruction set is available, 256 bits (YMM) if
the AVX instruction set is available, and 512 bits when the AVX512 instruction set isavailable. The number of elements in each vector depends on the size and type of data
elements, as follows:

115

Type ofelements

Number ofelements

Total size ofvector, bits

Instruction
set

char
short int
int
int64_t

Size of eachelement,
bits
8
16
32
64

8
4
2
1

64
64
64
64

MMX
MMX
MMX
MMX

char
short int
int
int64_t_Float16
float
double

8
16
32
64
16
32
64

16
8
4
2
8
4
2

128
128
128
128
128
128
128

SSE2
SSE2
SSE2
SSE2
AVX512FP16
SSE
SSE2

char
short int
int
int64_t_Float16
float
double

8
16
32
64
16
32
64

32
16
8
4
16
8
4

256
256
256
256
256
256
256

AVX2
AVX2
AVX2
AVX2
AVX512FP16
AVX
AVX

char
short int
int
int64_t_Float16
float
double

8
16
32
64
16
32
64

64
32
16
8
32
16
8

512
512
512
512
512
512
512

AVX512BW
AVX512BW
AVX512F
AVX512F
AVX512FP16
AVX512F
AVX512F

Table 12.1. Vector sizes available in different instruction set extensions

For example, a 128-bit XMM register can be organized as a vector of eight 16-bit integers orfour float's when the SSE2 instruction set is available. The older MMX registers, whichare 64 bits wide, should be avoided because they cannot be mixed with x87 style floating
point code.
The 128-bit XMM vectors must be aligned by 16, i.e. stored at a memory address that isdivisible by 16 when compiling for an instruction set lower than AVX (see below). The 256bit YMM vectors are preferably aligned by 32 and the 512-bit ZMM registers by 64, but thealignment requirements are less strict when compiling for the AVX and later instruction sets.
Vector operations are particularly fast on newer processors. Many processors can calculate
a vector just as fast as a scalar (Scalar means a single element). The first generation ofprocessors that support a new vector size sometimes have execution units, memory ports,
etc. of only half the size of the largest vector. These units are used twice for handling a fullsize vector.
The use of vector operations is more advantageous the smaller the data elements are. For
example, you get sixteen float additions in the same time that it takes to do eight
additions with double's. It is almost always advantageous to use vector operations oncontemporary CPUs if the data fit nicely into the vector registers.
11612.1 AVX instruction set and YMM registers
The 128-bit XMM registers are extended to 256-bit registers named YMM in the AVX
instruction set. The main advantage of the AVX instruction set is that it allows larger floating
point vectors. There are also other advantages that may improve the performancesomewhat. The AVX2 instruction set also allows 256-bit integer vectors.
Code that is compiled for the AVX instruction set can run only if AVX is supported by both
the CPU and the operating system. Old operating systems without support for AVX are
rarely in use today, so this is not a problem. The AVX instruction set is supported in thelatest compilers from Microsoft, Intel, Gnu and Clang.
There is a problem when mixing code compiled with and without AVX support on some Intelprocessors. There is a performance penalty when going from AVX code to non-AVX code
because of a change in the YMM register state. This penalty should be avoided by callingthe intrinsic function _mm256_zeroupper() before any transition from AVX code to nonAVX code. This can be necessary in the following cases:•

If part of a program is compiled with AVX support and another part of the program iscompiled without AVX support then call _mm256_zeroupper() before leaving the
AVX part.

•

If a function is compiled in multiple versions with and without AVX using CPU
dispatching then call _mm256_zeroupper() before leaving the AVX part.

•

If a piece of code compiled with AVX support calls a function in a library other thanthe library that comes with the compiler, and the library has no AVX support, thencall _mm256_zeroupper() before calling the library function.

The compiler may or may not insert _mm256_zeroupper() automatically. The assembly
output from the compiler will tell what it does.

12.2 AVX512 instruction set and ZMM registers
The 256-bit YMM registers are extended to 512-bit registers named ZMM in the AVX512
instruction set. The number of vector registers is extended from 16 to 32 in 64 bit mode.
There are only 8 vector registers in 32-bit mode. Therefore, AVX512 code should preferably
be compiled for 64-bit mode.
The AVX512 instruction set also adds a set of mask registers. These are used as Boolean
vectors. Almost any vector instruction can be masked with a mask register so that each
vector element is calculated only if the corresponding bit in the mask register is 1. This
makes vectorization of code with branches more efficient.
There are many additional extensions to AVX512. All processors with AVX512 have someof these extensions, but no processor so far has them all. Some of the known and planned
extensions to AVX512 are:
•
•
•
•
•

AVX512F. Foundation. All AVX512 processors have this. Includes operations on 32bit and 64-bit integers, float and double in 512-bit vectors, including masked
operations.
AVX512VL. Includes the same operations on 128-bit and 256-bit vectors, including
masked operations and 32 vector registers.
AVX512BW. Operations on 8-bit and 16-bit integers in 512-bit vectors.
AVX512DQ. Multiplication and conversion instructions with 64-bit integers. Various
other instructions on float and double.
AVX512ER. Fast reciprocal, reciprocal square root, and exponential function.
Precise on float; approximate on double. Only available on few processors.
117

•
•
•
•
•
•
•
•

AVX512CD. Conflict detection. Find duplicate elements in a vector.
AVX512PF. Prefetch instructions with gather/scatter logic.
AVX512VBMI. Permutation and shift with 8-bit granularity.
AVX512VBMI2. Compress and expand sparse vectors with 8-bit and 16-bit integers.
Useful in text processing.
AVX512IFMA. Fused multiply-and-add on 52-bit integers.
AVX512_4VNNIW. Iterated dot product on 16-bit integers.
AVX512_4FMAPS. Iterated fused multiply-and-add, single precision.
AVX512_FP16. Vectors of half-precision floating point numbers.

This makes CPU dispatching more complicated. You may pick the extensions that are
useful for a particular task and make a code branch for processors that have this extension.
The use of _mm256_zeroupper() is less important in AVX512 code, but still
recommended. See manual 2: "Optimizing subroutines in assembly language" chapter 13.2
and manual 5: "Calling conventions" chapter 6.3.

12.3 Automatic vectorization
Good compilers can use vector operations automatically in cases where the parallelism isobvious. See the compiler documentation for detailed instructions. Example:// Example 12.1a. Automatic vectorization
const int size = 1024;
int a[size], b[size];// ...
for (int i = 0; i < size; i++) {a[i] = b[i] + 2;}

A good compiler will optimize this loop by using vector operations when the SSE2 or later
instruction set is specified. The code will read four, eight, or sixteen elements of b into avector register depending on the instruction set, and do an addition with another vector
register containing (2,2,2,...), and store the four results in a. This operation will then be
repeated as many times as the array size divided by the number of elements per vector.
The speed is improved accordingly. It is best when the loop count is divisible by the numberof elements per vector. You may add dummy elements at the end of the array to make thearray size a multiple of the vector size.
There is a disadvantage when the arrays are accessed through pointers, e.g.:// Example 12.1b. Vectorization with alignment problem
void AddTwo(int * __restrict aa, int * __restrict bb) {for (int i = 0; i < size; i++) {aa[i] = bb[i] + 2;}}

The performance is best if the arrays are aligned by the vector size, i.e. 16, 32 or 64 for
XMM, YMM, and ZMM registers respectively. Efficient vector operations under instruction
sets prior to AVX require that the arrays are aligned by addresses divisible by 16. In
example 12.1a, the compiler can align the arrays as required, but in example 12.1b, thecompiler cannot know for sure whether the arrays are properly aligned or not. The loop canstill be vectorized, but the code will be less efficient because the compiler has to take extra
precautions to account for unaligned arrays. There are various things you can do to makethe code more efficient when arrays are accessed through pointers or references:

118

•

If the Intel compiler is used, then use #pragma vector aligned or the__assume_aligned directive to tell the compiler that the arrays are aligned, andmake sure that they are.

•Declare the function inline. This may enable the compiler to reduce example
12.1b to 12.1a.

•

Enable the instruction set with the largest vector size if possible. The AVX and later
instruction sets have very few restrictions on alignment and the resultant code will beefficient whether the arrays are aligned or not.

The automatic vectorization works best if the following conditions are satisfied:
1. Use a compiler with good support for automatic vectorization, such as Gnu, Clang,or Intel.
2. Use the latest version of the compiler. The compilers are becoming better and betterat vectorization.
3. If the arrays or structures are accessed through pointers or references then tell thecompiler explicitly that pointers do not alias, if appropriate, using the __restrict or__restrict__ keyword.
4. Use appropriate compiler options to enable the desired instruction set(/arch:SSE2, /arch:AVX etc. for Windows, -msse2, -mavx512f, etc. for Linux)
5. Use the less restrictive floating point options. For Gnu and Clang compilers, use theoptions -O2 -fno-trapping-math -fno-math-errno -fno-signed-zeros(-ffast-math works as well, but functions like isnan(x) do not work under-ffast-math).
6. Align arrays and big structures by 16 for SSE2, preferably 32 for AVX and preferably
64 for AVX512.
7. The loop count should preferably be a constant that is divisible by the number of
elements in a vector.
8. If arrays are accessed through pointers so that the alignment is not visible in thescope of the function where you want vectorization then follow the advice given
above.
9. Minimize the use of branches at the vector element level.
10. Avoid table lookup at the vector element level.
You may look at the assembly output listing to see if the code is indeed vectorized asintended (see page 88).
The compiler may fail to vectorize the code, or make the code unnecessarily complicated fora number of reasons. The most important obstacles to automatic vectorization are listed
here:•

The compiler cannot rule out that data pointers are pointing to overlapping or
aliasing addresses.

119

•

The compiler cannot rule out that not-taken branches will generate exceptions orother side effects.

•

The compiler does not know whether the size of an array is a multiple of the vector
size.

•

The compiler does not know whether data structures are properly aligned.

•

Data need to be rearranged to fit into vectors.

•

The code is too complex.

•

The code calls external functions that are not available in vector versions.

•

The code uses lookup tables.

The compiler can also use vector operations where there is no loop if the same operation is
performed on a sequence of consecutive variables. Example:// Example 12.2
struct alignas(16) S1 {float a, b, c, d;};// Structure of 4 floats, aligned

void Func() {S1 x, y;...
x.a = y.a + 1.;
x.b = y.b + 2.;
x.c = y.c + 3.;
x.d = y.d + 4.;};

A structure of four float's fits into a 128-bit XMM register. In example 12.2, the optimized
code will load the structure y into a vector register, add the constant vector (1,2,3,4),
and store the result in x.
The compiler is not always able to predict correctly whether vectorization will beadvantageous or not. The compiler may have #pragma or other directives that can beused for telling the compiler which loops to vectorize. The pragmas must be placed
immediately before the loop or the series of statements that you want them to apply to.
There are complications if the code contains branches, as in example 12.4a below. The way
to handle branches is to calculate both sides of a branch and then merge the two result
vectors so that some vector elements are taken from one branch and the other elements
are taken from the other branch. The compiler will not vectorize code in this case if there isa theoretical possibility that a not-taken branch will generate a trap for an exception (suchas floating point overflow) or have other side effects. You can help the compiler to vectorize
the code by specifying the compiler options -fno-trapping-math and -fno-matherrno for Gnu and Clang compilers, or similar options for other compilers. It is notrecommended to use the option -ffast-math because it will disable the detection of
infinity and NAN (see page 66).
It is advantageous to use the smallest data size that fits the application. In example 12.3a,for example, you can double the speed by using short int instead of int. A short
int is 16 bits wide, while an int is 32 bits, so you can have eight numbers of type
short int in one vector, while you can only have four numbers of type int. Therefore, itis advantageous to use the smallest integer size that is big enough to hold the numbers in120

question without generating overflow. Likewise, it is advantageous to use float rather thandouble if the code can be vectorized, because a float uses 32 bits while a double uses
64 bits. The advantage of using lower precision may disappear if you need a lot of extra
code to convert between different precisions.
The SSE2 vector instruction set cannot multiply integers of any size other than short int(16 bits). There are no instructions for integer division in vectors, but the vector class library(see page 125) has functions for integer vector division.

12.4 Using intrinsic functions
It is difficult to predict whether the compiler will vectorize a loop or not. The following
example shows a code that the compiler may or may not vectorize automatically. The code
has a branch that chooses between two expressions for every element in the arrays:// Example 12.4a. Loop with branch// Loop with branchvoid SelectAddMul(short int aa[], short int bb[], short int cc[]) {for (int i = 0; i < 256; i++) {aa[i] = (bb[i] > 0) ? (cc[i] + 2) : (bb[i] * cc[i]);}}It is possible to vectorize code explicitly by using the so-called intrinsic functions. This isuseful in situations like example 12.4a where current compilers do not always vectorize thecode automatically. It is also useful in situations where automatic vectorization leads tosuboptimal code.
Intrinsic functions are somewhat similar to assembly programming in the sense that mostintrinsic function calls are translated to a specific machine instruction. Intrinsic functions areeasier and safer to use than assembly programming because the compiler takes care ofregister allocation, function calling conventions, etc. A further advantage is that the compiler
may be able to optimize the code further by reordering instructions, common subexpression
elimination, etc. Intrinsic functions are supported by the Gnu, Clang, Intel, and Microsoft
compilers. The best performance is obtained with Gnu and Clang compilers.
We want to vectorize the loop in example 12.4a so that we can handle eight elements at atime in vectors of eight 16-bit integers. The branch inside the loop can be implemented invarious ways depending on the available instruction set. The traditional way is to make a bitmask with all 1's when bb[i] > 0, and all 0's when this is false. The value of cc[i]+2 isAND'ed with this mask, and bb[i]*cc[i] is AND'ed with the inverted mask. The
expression that is AND'ed with all 1's is unchanged, while the expression that is AND'ed
with all 0's gives zero. An OR combination of these two gives the chosen expression.
Example 12.4b shows how this can be implemented with intrinsic functions for the SSE2
instruction set:// Example 12.4b. Vectorized with SSE2
#include <emmintrin.h>// Define SSE2 intrinsic functions// Function to load unaligned integer vector from arraystatic inline __m128i LoadVector(void const * p) {return _mm_loadu_si128((__m128i const*)p);}// Function to store unaligned integer vector into arraystatic inline void StoreVector(void * d, __m128i const & x) {121

_mm_storeu_si128((__m128i *)d, x);}// Branch/loop function vectorized:
void SelectAddMul(short int aa[], short int bb[], short int cc[]) {// Make a vector of (0,0,0,0,0,0,0,0)__m128i zero = _mm_setzero_si128();// Make a vector of (2,2,2,2,2,2,2,2)__m128i two = _mm_set1_epi16(2);// Roll out loop by eight to fit the eight-element vectors:for (int i = 0; i < 256; i += 8) {// Load eight consecutive elements from bb into vector b:__m128i b = LoadVector(bb + i);// Load eight consecutive elements from cc into vector c:__m128i c = LoadVector(cc + i);// Add 2 to each element in vector c__m128i c2 = _mm_add_epi16(c, two);// Multiply b and c__m128i bc = _mm_mullo_epi16 (b, c);// Compare each element in b to 0 and generate a bit-mask:__m128i mask = _mm_cmpgt_epi16(b, zero);// AND each element in vector c2 with the bit-mask:
c2 = _mm_and_si128(c2, mask);// AND each element in vector bc with the inverted bit-mask:
bc = _mm_andnot_si128(mask, bc);// OR the results of the two AND operations:__m128i a = _mm_or_si128(c2, bc);// Store the result vector in eight consecutive elements in aa:
StoreVector(aa + i, a);}}

The resulting code will be more efficient because it handles eight elements at a time, andbecause it avoids the branch inside the loop. Example 12.4b executes three to seven times
faster than example 12.4a, depending on how predictable the branch inside the loop is.
The type __m128i defines a 128 bit vector containing integers. It can contain either
sixteen integers of 8 bits each, eight integers of 16 bits each, four integers of 32 bits each,or two integers of 64 bits each. The type __m128 defines a 128 bit vector of four float.
The type __m128d defines a 128 bit vector of two double. The corresponding 256 bit
vector registers are called __m256i, __m256, and __m256d. The 512-bit registers arecalled __m512i, __m512, and __m512d.
The intrinsic vector functions have names that begin with _mm. These functions are listed in
the programming manual from Intel: "Intel 64 and IA-32 Architectures Software Developer
Manuals". They can also be found with the convenient search page Intel Intrinsics Guide.
There are thousands of different intrinsic functions and it can be difficult to find the rightfunction for a particular purpose.

122

The AND-OR construction in example 12.4b can be replaced by a blend instruction if theSSE4.1 instruction set is available:// Example 12.4c. Same example, vectorized with SSE4.1// Function to load unaligned integer vector from arraystatic inline __m128i LoadVector(void const * p) {return _mm_loadu_si128((__m128i const*)p);}// Function to store unaligned integer vector into arraystatic inline void StoreVector(void * d, __m128i const & x) {_mm_storeu_si128((__m128i *)d, x);}void SelectAddMul(short int aa[], short int bb[], short int cc[]) {// Make a vector of (0,0,0,0,0,0,0,0)__m128i zero = _mm_setzero_si128();// Make a vector of (2,2,2,2,2,2,2,2)__m128i two = _mm_set1_epi16(2);// Roll out loop by eight to fit the eight-element vectors:for (int i = 0; i < 256; i += 8) {// Load eight consecutive elements from bb into vector b:__m128i b = LoadVector(bb + i);// Load eight consecutive elements from cc into vector c:__m128i c = LoadVector(cc + i);// Add 2 to each element in vector c__m128i c2 = _mm_add_epi16(c, two);// Multiply b and c__m128i bc = _mm_mullo_epi16 (b, c);// Compare each element in b to 0 and generate a bit-mask:__m128i mask = _mm_cmpgt_epi16(b, zero);// Use mask to choose between c2 and bc for each element__m128i a = _mm_blendv_epi8(bc, c2, mask);// Store the result vector in eight consecutive elements in aa:
StoreVector(aa + i, a);}}

The AVX512 instruction set provides an even more efficient way of branching, using a mask
register and conditional instructions. It is possible to do calculation and branching in one
instruction:// Example 12.4d. Same example, vectorized with AVX512F
void SelectAddMul(short int aa[], short int bb[], short int cc[]) {// Make a vector of all 0__m512i zero = _mm512_setzero_si512();// Make a vector of all 2__m512i two = _mm512_set1_epi16(2);// Roll out loop by 32 to fit the 32-element vectors:for (int i = 0; i < 256; i += 32) {123

// Load 32 consecutive elements from bb into vector b:__m512i b = _mm512_loadu_si512(bb + i);// Load 32 consecutive elements from cc into vector c:__m512i c = _mm512_loadu_si512(cc + i);// Multiply b and c__m512i bc = _mm512_mullo_epi16(b, c);// Compare each element in b to 0 and generate a 32 bit mask// Each bit in mask is 1 for b[i] > 0__mmask32 mask = _mm512_cmp_epi16_mask(b, zero, 6);// Conditionally add 2 to each element in vector c.
// Select c+2 when mask bit = 1, bc when mask bit = 0__m512i r = _mm512_mask_add_epi16(bc, mask, c, two);// Store the result vector in 32 elements in aa:_mm512_storeu_epi16(aa + i, r);}}

You have to include the appropriate header file for the instruction set you are compiling for.
The header file immintrin.h or x86intrin.h covers all the instruction sets.
You have to make sure that the CPU supports the corresponding instruction set. The
program will crash if it contains an instruction that the CPU does not support. All compilers
except Microsoft allows you to specify on the command line which instruction sets you wantto compile for.
See page 135 for how to check which instruction sets the CPU supports.

Aligning data
Loading data into a vector goes faster if the data are aligned to an address divisible by thevector size (16, 32, or 64 bytes). This has a significant effect on older processors and onIntel Atom processors, but is less important on most newer processors. The following
example shows how to align arrays.
// Example 12.5. Aligned arrays
const int size = 256;// Array sizealignas(16) int16_t aa[size];// Make aligned array

Vectorized table lookup
Lookup tables can be useful for optimizing code, as explained on page 144. Unfortunately,table lookup is often an obstacle to vectorization. AVX2 and later instruction sets havegather instructions that are useful for table lookup. A vector of 32-bit or 64-bit integers
provides the indexes into a table in memory. The result is a vector of 32-bit or 64-bit
integers, float, or double values. The gather instruction takes several clock cycles because
the elements are being read one by one.
If the lookup table is so small that it can fit into one or a few vector registers, then it is more
efficient to use permute instructions for table lookup. The AVX instruction set permits
permutation of 32-bit floats in 256-bit vectors. Permutation with 16 bit integers requires the
AVX512BW instruction set. Permutation with 8 bit integers in tables with more than 16
elements requires the AVX512VBMI instruction set.
Using intrinsic functions can be quite tedious. The code becomes bulky and difficult to read.
It is often easier to use vector classes, as explained in the next section.
124

12.5 Using vector classes
Programming with intrinsic functions in the way of example 12.4b, c and d is quite tediousindeed. It is possible to write the same in a more clear and intelligible way by wrapping thevectors into C++ classes and using overloaded operators for things like adding vectors. The
operators are inlined so that the resulting machine code becomes the same as if you had
used intrinsic functions. It is just easier to write a + b than to write_mm512_add_epi16(a,b).
The advantages of vector classes are:•

You can specify explicitly what part of the code to vectorize, and how•

You can overcome the obstacles to automatic vectorization listed on page 119•

The code often becomes simpler than what the compiler would do with automatic
vectorization because the compiler has to take care of special cases that may not be
relevant in your case•

The code is simpler and more readable than assembly code or intrinsic functions, yetequally efficient

Various libraries of predefined vector classes are currently available, including one fromIntel and one from me. My vector class library (VCL) has many features, see
https://github.com/vectorclass. The Intel vector class library has few features and is rarely
updated.
Vector class library
Available from
Include file
Supported compilers
Supported operating systems
Instruction set control
LicenseIntel
Intel and Microsoft C++
compilersdvec.hIntel, Microsoft
Windows, Linux, Macnolicense included incompiler price

VCL (Agner)https://github.com/vectorclassvectorclass.hIntel, Microsoft, Gnu, Clang
Windows, Linux, Mac, BSD
yes
Apache 2.0

Table 12.2. Vector class libraries

The following table lists the available vector classes. Including the appropriate header file
will give you access to all of these classes.

125Size of eachelement,
bits
8
8
16
16
32
32
64
8
8
16
16
32
32
64
64
8
8
16
16
32
32
64
64
8
8
16
16
32
32
64
64
16
32
64
16
32
64
16
32
64

Number ofelements invector
8
8
4
4
2
2
1
16
16
8
8
4
4
2
2
32
32
16
16
8
8
4
4
64
64
32
32
16
16
8
8
8
4
2
16
8
4
32
16
8

Type ofelements

Total size ofvector, bits

Vectorclass, Intel

int8_tuint8_t
int16_tuint16_t
int32_tuint32_t
int64_t
int8_tuint8_t
int16_tuint16_t
int32_tuint32_t
int64_tuint64_t
int8_tuint8_t
int16_tuint16_t
int32_tuint32_t
int64_tuint64_t
int8_tuint8_t
int16_tuint16_t
int32_tuint32_t
int64_tuint64_t_Float16
float
double_Float16
float
double_Float16
float
double

64
64
64
64
64
64
64
128
128
128
128
128
128
128
128
256
256
256
256
256
256
256
256
512
512
512
512
512
512
512
512
128
128
128
256
256
256
512
512
512

Is8vec8
Iu8vec8
Is16vec4
Iu16vec4
Is32vec2
Iu32vec2
I64vec1
Is8vec16
Iu8vec16
Is16vec8
Iu16vec8Is32vec4
Iu32vec4
I64vec2

Vectorclass, VCL

I8vec64Iu8vec64
I16vec32
Iu16vec32
I32vec16
Iu32vec16
I64vec8
Iu64vec8
F32vec4
F64vec2
F32vec8
F64vec4
F32vec16
F64vec8

Vec16c
Vec16uc
Vec8s
Vec8us
Vec4i
Vec4ui
Vec2q
Vec2uqVec32c
Vec32uc
Vec16sVec16us
Vec8i
Vec8ui
Vec4q
Vec4uq
Vec64c
Vec64uc
Vec32s
Vec32us
Vec16i
Vec16ui
Vec8q
Vec8uq
Vec8h
Vec4f
Vec2d
Vec16hVec8f
Vec4d
Vec32hVec16f
Vec8d

Table 12.3. Vector classes defined in two libraries

It is not recommended to use the vectors of 64 bits total size, because these areincompatible with floating point code. If you do use the 64-bit vectors then you have to
execute _mm_empty() after the 64-bit vector operations and before any floating pointcode. The vectors of 128 bits and more do not have this problem.
Vectors of 256 and 512 bits size are only available if supported by the CPU and by theoperating system (see page 117). The VCL vector class library can emulate a 256-bit vector
as two 128-bit vectors and a 512-bit vector as two 256-bit vectors or four 128-bit vectors,
depending on the instruction set. Vectors of half precision float (_Float16) are only
efficient if the AVX512-FP16 instruction set is enabled.
126

The following example shows the same code as example 12.4b, rewritten with the use ofIntel vector classes:// Example 12.4d. Same example, using Intel vector classes#include <dvec.h> // Define vector classes// Function to load unaligned integer vector from arraystatic inline __m128i LoadVector(void const * p) {return _mm_loadu_si128((__m128i const*)p);}// Function to store unaligned integer vector into arraystatic inline void StoreVector(void * d, __m128i const & x) {_mm_storeu_si128((__m128i *)d, x);}void SelectAddMul(short int aa[], short int bb[], short int cc[]) {// Make a vector of (0,0,0,0,0,0,0,0)
Is16vec8 zero(0,0,0,0,0,0,0,0);// Make a vector of (2,2,2,2,2,2,2,2)
Is16vec8 two(2,2,2,2,2,2,2,2);// Roll out loop by eight to fit the eight-element vectors:for (int i = 0; i < 256; i += 8) {// Load eight consecutive elements from bb into vector b:
Is16vec8 b = LoadVector(bb + i);// Load eight consecutive elements from cc into vector c:
Is16vec8 c = LoadVector(cc + i);// result = b > 0 ? c + 2 : b * c;
Is16vec8 a = select_gt(b, zero, c + two, b * c);// Store the result vector in eight consecutive elements in aa:
StoreVector(aa + i, a);}}

The same example using VCL vector classes looks like this:// Example 12.4e. Same example, using VCL#include "vectorclass.h"// Define vector classes
void SelectAddMul(short int aa[], short int bb[], short int cc[]) {// Define vector objects
Vec16s a, b, c;// Roll out loop by eight to fit the eight-element vectors:for (int i = 0; i < 256; i += 16) {// Load eight consecutive elements from bb into vector b:b.load(bb+i);// Load eight consecutive elements from cc into vector c:c.load(cc+i);// result = b > 0 ? c + 2 : b * c;a = select(b > 0, c + 2, b * c);// Store the result vector in eight consecutive elements in aa:
a.store(aa+i);
}
}

127

The vector class library (VCL) includes many features such as vector permutation andmathematical functions. It also includes add-on packages for various purposes. See the
GitHub site for details.

CPU dispatching with vector classes
The VCL vector class library makes it possible to compile for different instruction sets fromthe same source code. The library has preprocessing directives that select the best
implementation for a given instruction set.
The following example shows how to make the SelectAddMul example (12.4e) with
automatic CPU dispatching. The code in this example should be compiled four times, one
for the SSE2 instruction set, one for SSE4.1, one for AVX2, and one for AVX512BW. Allfour versions are linked into the same executable. SSE2 is the minimum supported
instruction set for the vector class library, SSE4.1 gives an advantage in the select function,
the AVX2 instruction set gives the advantage of bigger vector registers, and AVX512 gives
still bigger vectors and more efficient branching.
The vector class library will use one 512-bit vector register, or two 256-bit vector registers,
or four 128-bit vector registers, depending on the instruction set.
The preprocessing macro INSTRSET is used for giving the function a different name foreach instruction set. A CPU dispatcher sets a function pointer to the best possible version
the first time the function is called. See the vectorclass manual for details.
// Example 12.6. Vector class code with automatic CPU dispatching#include "vectorclass.h" // vector class library#include <stdio.h>// define fprintf// define function typetypedef void FuncType(short int aa[], short int bb[], short int cc[]);// function prototypes for each version
FuncType SelectAddMul, SelectAddMul_SSE2, SelectAddMul_SSE41,
SelectAddMul_AVX2, SelectAddMul_AVX512BW, SelectAddMul_dispatch;// Define function name depending on instruction set#if
INSTRSET == 2// SSE2#define FUNCNAME SelectAddMul_SSE2#elif INSTRSET == 5// SSE4.1#define FUNCNAME SelectAddMul_SSE41#elif INSTRSET == 8// AVX2#define FUNCNAME SelectAddMul_AVX2#elif INSTRSET == 10// AVX512BW#define FUNCNAME SelectAddMul_AVX512BW#endif// specific version of the function. Compile once for each version
void FUNCNAME(short int aa[], short int bb[], short int cc[]) {Vec32s a, b, c; // Define biggest possible vector objects// Roll out loop by 32 to fit the biggest vectors:for (int i = 0; i < 256; i += 32) {b.load(bb+i);c.load(cc+i);a = select(b > 0, c + 2, b * c);a.store(aa+i);}}#if INSTRSET == 2// make dispatcher in only the lowest of the compiled versions#include "instrset_detect.cpp" // instrset_detect function

128

// Function pointer initially points to the dispatcher.
// After first call it points to the selected version
FuncType * SelectAddMul_pointer = &SelectAddMul_dispatch;// Dispatcher
void SelectAddMul_dispatch(
short int aa[], short int bb[],short int cc[]) {// Detect supported instruction set
int iset = instrset_detect();// Set function pointerif(iset >=10) SelectAddMul_pointer = &SelectAddMul_AVX512BW;
else if (iset >= 8) SelectAddMul_pointer = &SelectAddMul_AVX2;
else if (iset >= 5) SelectAddMul_pointer = &SelectAddMul_SSE41;
else if (iset >= 2) SelectAddMul_pointer = &SelectAddMul_SSE2;
else {// Error: lowest instruction set not supported
fprintf(stderr, "\nError: Instruction set lower than SSE2 notsupported");return;}// continue in dispatched version
return (*SelectAddMul_pointer)(aa, bb, cc);}// Entry to dispatched function callinline void SelectAddMul(
short int aa[], short int bb[], short int cc[]) {// go to dispatched version
return (*SelectAddMul_pointer)(aa, bb, cc);}#endif// INSTRSET == 2

12.6 Transforming serial code for vectorization
Not all code has a parallel structure that can easily be organized into vectors. A lot of code
is serial in the sense that each calculation depends on the previous one. It may nevertheless be possible to organize the code in a way that can be vectorized if the code isrepetitive. The simplest case is a sum of a long list of numbers:// Example 12.7a. Sum of a list
float a[100];
float sum = 0;
for (int i = 0; i < 100; i++) sum += a[i];The above code is serial because each value of sum depends on the preceding value ofsum. The trick is to roll out the loop by n and reorganize the code so that each value
depends on the value that is n places back, where n is the number of elements in a vector. If
n = 4, we have:// Example 12.7b. Sum of a list, rolled out by 4
float a[100];
float s0 = 0, s1 = 0, s2 = 0, s3 = 0, sum;for (int i = 0; i < 100; i += 4) {s0 += a[i];s1 += a[i+1];
s2 += a[i+2];
s3 += a[i+3];129}
sum = (s0+s1)+(s2+s3);

Now s0, s1, s2 and s3 can be combined into a 128-bit vector so that we can do four
additions in one operation. A good compiler will convert example 12.7a to 12.7b
automatically and vectorize the code if we specify the options for fast math and the SSE or
higher instruction set.
More complicated cases cannot be vectorized automatically. For example, let's look at theexample of a Taylor series. The exponential function can be calculated by the series:
∞
𝑥𝑛
𝑥
𝑒 =∑
𝑛!
𝑛=0

A C++ implementation may look like this:// Example 12.8a. Taylor series
float Exp(float x) {// Approximate exp(x) for small x
float xn = x;// x^nfloat sum = 1.f;// sum, initialize to x^0/0!
float nfac = 1.f;// n factorialfor (int n = 1; n <= 16; n++) {sum += xn / nfac;
xn *= x;nfac *= n+1;}
return sum;}

Here, each value xn is calculated from the previous value as xn = x∙xn-1, and each value of n!
is calculated from the previous value as n! = n∙(n-1)!. If we want to roll out the loop by four,
we will have to calculate each value from the value that is four places back. Thus, we willcalculate xn as x4∙xn-4. There is no easy way to roll out the calculation of the factorials, but
this is not necessary because the factorials do not depend on x so we can store the values
in a pre-calculated table. Even better: store the reciprocal factorials so that we do not haveto do the divisions (Division is slow, you know). The code can now be vectorized as follows(using the vector class library):// Example 12.8b. Taylor series, vectorized#include "vectorclass.h"// Vector class library// Approximate exp(x) for small x
float Exp(float x) {// table of 1/n!
alignas(16) const float coef[16] = {1., 1./2., 1./6., 1./24., 1./120., 1./720., 1./5040.,
1./40320., 1./362880., 1./3628800., 1./39916800.,
1./4.790016E8, 1./6.22702E9, 1./8.71782E10,
1./1.30767E12, 1./2.09227E13};
float x2 = x * x;// x^2
float x4 = x2 * x2;// x^4// Define vectors of four floats
Vec4f xxn(x4, x2*x, x2, x);// x^1, x^2, x^3, x^4
Vec4f xx4(x4);// x^4Vec4f s(0.f, 0.f, 0.f, 1.f);// initialize sumfor (int i = 0; i < 16; i += 4) {// Loop by 4
s += xxn * Vec4f().load(coef+i); // s += x^n/n!
xxn *= xx4;// next four x^n}return horizontal_add(s);// add the four sums}

130This loop calculates four consecutive terms in one vector. It may be worthwhile to unroll theloop further if the loop is long because the speed here is likely to be limited by the latency ofthe multiplication of xxn rather than the throughput (see p. 113). The table of coefficients is
calculated at compile time here. It may be more convenient to calculate the table at runtime,
if only you make sure it is only calculated once, rather than each time the function is called.

12.7 Mathematical functions for vectors
There are various function libraries for computing mathematical functions such as
logarithms, exponential functions, trigonometric functions, etc. in vectors. These function
libraries are useful for vectorizing mathematical code.
There are two different kinds of vector math libraries: long vector libraries and short vector
libraries. To explain the difference, let's say that you want to calculate the same function ona thousand numbers. With a long vector library, you are feeding an array of thousand
numbers as a parameter to the library function, and the function stores the thousand results
in another array. The disadvantage of using a long vector library is that if you are doing asequence of calculations then you have to store the intermediate result of each step of thesequence in a temporary array before calling the function for the next step. With a shortvector library, you divide the data set into sub-vectors that fit the size of the vector registers
in the CPU. If the vector registers can hold e.g. eight numbers, then you have to call thelibrary function 125 times with eight numbers at a time packed into a vector register. The
library function will return the result in a vector register which can be fed directly to the next
step in the sequence of calculations without the need to store intermediate results in RAM
memory. This may be faster despite the extra function calls, because the CPU can do
calculations while simultaneously prefetching the code of the next function. However, theshort vector method may be at a disadvantage if the sequence of calculations forms a longdependency chain. We want the CPU to start calculations on the second sub-vector before
it has finished the calculations on the first sub-vector. A long dependency chain may fill up
the queue of pending instructions in the CPU and prevent it from fully utilizing its out-oforder calculation capabilities.
Here is a list of some long vector math libraries:•••Intel vector math library (VML, MKL). Works with all x86 platforms. The performanceof this library on non-Intel CPUs is uncertain. See page 143.
Intel Performance Primitives (IPP). Works with all x86 platforms. Works well with
non-Intel CPUs. Includes many functions for statistics, signal processing and image
processing.
Yeppp. Open source library. Supports x86 and ARM platforms and various
programming languages (Is this still active?).

And here is a list of short vector math libraries:••••

Sleef library. Supports many different platforms. Open source. www.sleef.org
Intel short vector math library (SVML). This is supplied with Intel's compilers andinvoked with automatic vectorization. This library usually works well with non-Intel
CPUs if you are not using an Intel compiler. See page 143.
Most compilers include a vector math library that is used in automatic vectorizationof loops containing math functions. See discussion below.
VCL vector class library. Open source. Supports all x86 platforms. Microsoft, Intel,
Gnu and Clang compilers. The code is inlined so that there is no need to link withexternal libraries. https://github.com/vectorclass

All these libraries have very good performance and precision. The speed is many times
faster than any non-vector library.
131

The function names in the vector math libraries are not always well documented. The
examples in this table may be of some help if you want to call the library functions directly:
LibraryIntel SVML v.10.2 & earlierIntel SVML v.10.3 & later
Intel SVML + ia32intrin.h
AMD Core Math Library
AMD LIBM Library
Gnu libmvec
VCL vector class library

exp function of 4
exp function of 2 double
floatsvmlsExp4
vmldExp2__svml_expf4__svml_exp2_mm_exp_ps_mm_exp_pd__vrs4_expf__vrd2_exp
amd_vrs4_expf
amd_vrd2_exp
depends on instruction set
exp
exp

Using short vector math libraries with different compilers
Loops that contain mathematical functions can only be vectorized if there is a function
library that contains these functions in vector versions with vector input and vector output.
(The sqrt function does not require an external library).
Older versions of the Gnu compiler had an option for using an external library named SVML
(Short Vector Math Library) provided by Intel. Now, the Gnu compiler has its own vector
math library named libmvec that is linked in automatically if needed.
The Clang compiler currently has no vector math library. Clang version 12 and later has theoption fveclib=libmvec that makes it use the Gnu vector math library.
The Microsoft compiler (version 17, 2022) has its own vector math library that is linked in
automatically when needed.
All versions of the Intel compiler include the highly optimized SVML library that is linked in
automatically. The SVML library is optimized for Intel processors, but it gives satisfactory
performance on non-Intel processors as well, unless it is used with the legacy version of theIntel compiler (named “classic”).
Another solution for vectorizing math functions is to use the Vector Class Library (link)described on page 125. The vector class library provides inline math functions in vector
form. It also includes an optional interface to Intel’s SVML library. This works with all of thecompilers mentioned above.
The Gnu libmvec library currently contains only the most basic functions such as sin, cos,pow, log, exp. The other libraries also contain inverse trigonometric functions, hyperbolic
functions, etc. The SVML library contains the largest number of mathematical functions. The
SVML library can be used with other compilers than Intel, but the calling interface is poorly
documented. SVML also contains undocumented functions, for example sinpi.
The different libraries are using different calculation methods, but they all give a reasonably
good accuracy.
It is difficult to compare the performance of the different vector math libraries. All the vector
math libraries give good performance in the general case where data are within areasonable range, but they differ quite a lot in how efficiently they treat extreme data values
and special cases. The libmvec library is falling back on scalar functions in special cases inorder to assure correct handling of even the rarest cases. The Microsoft and Intel SVML
libraries also use large branches to handle special cases. Only the Vector Class Library ishandling special cases in the main program flow without branching. This gives superior
performance in all the special cases, but the code may overflow or underflow too early for
extreme input values.
132

To determine which vector math library is most efficient for a particular purpose, you need toconsider not only the performance on simple data values, but also how it performs with data
values such as INF, NAN, subnormal numbers, and extreme values if such values occur
commonly in the application.

12.8 Aligning dynamically allocated memory
Memory allocated with new or malloc is aligned by 8 or 16 depending on the platform. Thisis a problem with vector operations when alignment by 16 or more is required. The C++ 17
standard gives you the required alignment automatically when using operator new:// Example 12.9// Aligned memory allocation in C++17
int arraysize = 100;__m512 * pp = new __m512[arraysize];// pp will be aligned by alignof(__m512) if using C++17

12.9 Aligning RGB video or 3-dimensional vectors
RGB image data have three values per point. This does not fit into a vector of e.g. four
floats. The same applies to 3-dimensional geometry and other odd-sized vector data. The
data have to be aligned by the vector size for the sake of efficiency. Using unaligned reads
and writes may slow down the execution to the point where it is less advantageous to use
vector operations. You may choose one of the following solutions, depending on what fits
best into the algorithm in question:•

Put in an unused fourth value to make the data fit into the vector. This is a simple
solution, but it increases the amount of memory used. You may avoid this method if
memory access is a bottleneck.

•

Organize the data into groups of four (or eight) points with the four R value in one
vector, the four G values in the next vector, and the four B value in the last vector.

•

Organize the data with all the R values first, then all the G values, and last all the B
values.

The choice of which method to use depends on what fits best into the algorithm in question.
You may choose the method that gives the simplest code.
If the number of points is not divisible by the vector size then add a few unused points in theend in order to get an integral number of vectors.

12.10 Conclusion
There is a lot to gain in speed by using vector code if the algorithm allows parallel
calculation. The gain depends on the number of elements per vector. The simplest andmost clean solution is to rely on automatic vectorization by the compiler. The compiler will
vectorize the code automatically in simple cases where the parallelism is obvious and thecode contains only simple standard operations. All you have to do is to enable theappropriate instruction set and relevant compiler options.
However, there are many cases where the compiler is unable to vectorize the code
automatically or does so in a suboptimal way. Here you have to vectorize the code explicitly.
There are various ways to do this:

133

•

Use assembly language•

Use intrinsic functions•

Use predefined vector classes

The easiest way to vectorize code explicitly is by using the vector class library. You may
combine this with intrinsic functions if you need things that are not defined in the vector
class library. Whether you choose to use intrinsic functions or vector classes is just a matterof convenience - there is usually no difference in performance. Intrinsic functions have long
names that look clumsy and tedious. The code becomes more readable when you are using
vector classes and overloaded operators.
A good compiler is often able to optimize the code further after you have vectorized it
manually. The compiler can use optimization techniques such as function inlining, common
subexpression elimination, constant propagation, loop optimization, etc. These techniques
are rarely used in manual assembly coding because it makes the code unwieldy, error
prone, and almost impossible to maintain. The combination of manual vectorization withfurther optimization by the compiler can therefore give the best result in difficult cases.
Some experimentation may be needed to find the best solution. You may look at theassembly output or the disassembly display in a debugger to check what the compiler isdoing.
Vectorized code often contains a lot of extra instructions for converting the data to the rightformat and getting them into the right positions in the vectors. This data conversion andpermutation can sometimes take more time than the actual calculations. This should betaken into account when deciding whether it is profitable to use vectorized code or not. The
VCL Vector Class Library has some very useful permutation functions that automatically findthe optimal implementation of a particular permutation pattern.
I will conclude this section by summing up the factors that decide how advantageous
vectorization is.
Factors that make vectorization favorable:
•
•
•
•
•
•
•
•

Small data types: char, int16_t, float.
Similar operations on all data in large arrays.
Array size divisible by vector size.
Unpredictable branches that select between two simple expressions.
Operations that are only available with vector operands: minimum, maximum,
saturated addition, fast approximate reciprocal, fast approximate reciprocal squareroot, RGB color difference.
Vector instruction set available, e.g. AVX2, AVX-512
Mathematical vector function libraries.
Use Gnu or Clang compiler.

Factors that make vectorization less favorable:
•
•
•
•
•
•
•

Larger data types: int64_t, double.
Misaligned data.
Extra data conversion, permutation, packing, unpacking needed.
Predictable branches that can skip large expressions when not selected.
Compiler has insufficient information about pointer alignment and aliasing.
Operations that are missing in the instruction set for the appropriate type of vector,
such as 32-bit integer multiplication prior to SSE4.1, and integer division.
Older CPUs with execution units smaller than the vector register size.
134

Vectorized code is more difficult for the programmer to make and therefore more error
prone. The vectorized code should therefore preferably be put aside in reusable and welltested library modules and header files.

13 Making critical code in multiple versions for different
instruction sets
Microprocessor producers keep adding new instructions to the instruction set. These new
instructions can make certain kinds of code execute faster. The most important addition to
the instruction set is the vector operations mentioned in chapter 12.
If the code is compiled for a particular instruction set, then it will be compatible with all CPUs
that support this instruction set or any higher instruction set, but not with earlier CPUs. The
sequence of backwards compatible instruction sets is as follows:
Instruction set
Important features
80386
32 bit mode
SSE
128 bit float vectors
SSE2
128 bit integer and double vectors
SSE3
horizontal add, etc.
SSSE3
a few more integer vector instructions
SSE4.1some more vector instructions
SSE4.2string search instructions
AVX
256 bit float and double vectors
AVX2
256 bit integer vectors
FMA3
floating point multiply-and-add
AVX512F
512 bit integer and floating point vectors
AVX512 BW, DQ, VL
More 512-bit vector instructions
AVX512-FP16
Vectors of half-precision floating point numbers
Table 13.1. Instruction sets
A more detailed explanation of the instruction sets is provided in manual 4: "Instruction
tables". There are certain restrictions on mixing code compiled for AVX or later with code
compiled without AVX, as explained on page 117.
A disadvantage of using the newest instruction set is that the compatibility with older
microprocessors is lost. This dilemma can be solved by making the most critical parts of thecode in multiple versions for different CPUs. This is called CPU dispatching. For example,
you may want to make one version that takes advantage of the AVX512 instruction set,
another version for CPUs with only the AVX2 instruction set, and a generic version that iscompatible with old microprocessors without any of these instruction sets. The program
should automatically detect which instruction set is supported by the CPU and choose theappropriate version of the subroutine for the critical innermost loops.

13.1 CPU dispatch strategies
It is quite expensive – in terms of development, testing and maintenance – to make a pieceof code in multiple versions, each carefully optimized and fine-tuned for a particular set ofCPUs. These costs can be justified for general function libraries that are used in multiple
applications, but not always for application-specific code. If you consider making highly
optimized code with CPU dispatching, then it is advisable to make it in the form of a reusable library if possible. This also makes testing and maintenance more manageable.
I have done a good deal of research on CPU dispatching and discovered that many
common programs use inappropriate CPU dispatch methods.
135

The most common pitfalls of CPU dispatching are:•

Optimizing for present processors rather than future processors. Consider the time ittakes to develop and publish a function library with CPU dispatching. Add to this thetime it takes before the application programmer gets the new version of the library.
Add to this the time it takes to develop and market the application program that uses
the updated function library. Add to this the time before the end user gets the latest
version of the application program. All in all, it will often take several years before
your code is running in the majority of end user's computers. At this time, any
processor that you optimized for is likely to be obsolete. Programmers very often
underestimate this time lag.

•Thinking in terms of specific processor models rather than processor features. The
programmer typically thinks "what works best on processor X?" rather than "whatworks best on processors with this instruction set?". A list of which code branch touse for each processor model is going to be very long and difficult to maintain. And it
is unlikely that the end user will have an up-to-date version of the program. The CPU
dispatcher should not look at CPU brand names and model numbers, but on what
instruction sets and other features it has.

•

Assuming that processor model numbers form a logical sequence. If you know thatprocessor model N supports a particular instruction set, then you cannot assume
that model N+1 supports at least the same instruction set. Neither can you assume
that model N-1 is inferior. A model with a higher number is not necessarily newer.
The CPU family and model numbers are not always sequential, and you cannot
make any assumption about an unknown CPU based on its family and model
number.

•Failure to handle unknown processors properly. Many CPU dispatchers aredesigned to handle only known processors. Other brands or models that were
unknown at the time of programming will typically get the generic branch, which is
the one that gives the worst possible performance. We must bear in mind, that manyusers will prefer to run a speed-critical program on the newest CPU model, whichquite likely is a model that was unknown at the time of programming. The CPU
dispatcher should give a CPU of unknown brand or model number the best possible
branch if it has an instruction set that is compatible with that branch. The common
excuse that "we do not support processor X" is simply not appropriate here. It
reveals a fundamentally flawed approach to CPU dispatching.

•

Underestimating the cost of keeping a CPU dispatcher updated. It is tempting to finetune the code to a specific CPU model and then think that you can make an update
when the next new model comes on the market. But the cost of fine-tuning, testing,verifying and maintaining a new branch of code is so high that it is unrealistic thatyou can do this every time a new processor enters the market for years to come.
Even big software companies often fail to keep their CPU dispatchers up to date. A
more realistic goal is to make a new branch only when a new instruction set opens
the possibility for significant improvements.

•

Making too many branches. If you are making branches that are fine-tuned forspecific CPU brands or specific models, then you will soon get a lot of branches thattake up code space and are difficult to maintain. Any specific bottleneck or any
particularly slow instruction that you are dealing with in a particular CPU model islikely to be irrelevant within a year or two. Often, it is sufficient to have just two
branches: one for the latest instruction set and one that is compatible with CPUs thatare up to five or ten years old. The CPU market is developing so fast that what isbrand new today will be mainstream next year.
136

•

Ignoring virtualization. The time when the CPUID instruction was certain to truly
represent a known CPU model is over. Virtualization is quite common today. A
virtual processor may have a reduced number of cores in order to reserve resources
for other virtual processors on the same machine. The virtual processor may be
given a false model number to reflect this or for the sake of compatibility with some
legacy software. It may even have a false vendor string. In the future, we may also
see emulated processors and FPGA soft cores that do not correspond to any known
hardware CPU. These virtual processors can have any brand name and model
number. The only CPUID information that we can surely rely on is the feature
information, such as supported instruction sets and cache sizes.

Fortunately, the solution to these problems is quite simple in most cases: The CPU
dispatcher should have only a few branches, and the dispatching should be based on which
instruction sets the CPU supports, rather than its brand, family, and model number.
I have seen many examples of poor CPU dispatching. For example, Mathcad (v. 15.0) is
using a six years old version of Intel's Math Kernel Library (MKL v. 7.2). This library has a
CPU dispatcher that does not handle current CPUs optimally. The speed for certain tasks
on current Intel CPUs can be increased by more than 33% when the CPUID is artificiallychanged to the old Pentium 4. The reason is that the CPU dispatcher in the Intel MKL libraryrelies on the CPU family number, which is 15 on the old Pentium 4, while all newer Intel
CPUs have family number 6. The speed on non-Intel CPUs was more than doubled for thistask when the CPUID was manipulated to fake an Intel Pentium 4. Even worse, many
software products fail to recognize VIA processors because this brand was less popular at
the time the software was developed.
A CPU dispatch mechanism that treats different brands of CPUs unequally can become aserious legal issue, as you can read about in my blog. Here, you can also find more
examples of bad CPU dispatching.
Obviously, you should apply CPU dispatching only to the most critical part of the program preferably isolated into a separate function library. The radical solution of making the entire
program in multiple versions should be used only when instruction sets are mutually
incompatible. A function library with a well-defined functionality and a well-defined interface
to the calling program is more manageable and easier to test, maintain, and verify than aprogram where the dispatch branches are scattered everywhere in the source files.

13.2 Model-specific dispatching
There may be cases where a particular code implementation works particularly bad on aparticular processor model. You may ignore the problem and assume that the next
processor model will work better. If the problem is too important to ignore, then the solution
is to make a negative list of processor models on which this code version performs poorly. It
is not a good idea to make a positive list of processor models on which a code version
performs well. The reason is that a positive list needs to be updated every time a newprocessor appears on the market. Such a list is almost certain to become obsolete within
the lifetime of the software. A negative list, on the other hand, does not need updating in thelikely case that the next generation of processors is better. Whenever a processor has aparticular weakness or bottleneck, it is likely that the producer will try to fix the problem andmake the next model work better.
Remember again, that most software runs most of the time on processors that were
unknown at the time the software was coded. If the software contains a positive list of which
processor models to run the most advanced code version on, then it will run an inferiorversion on the processors that were unknown at the time it was programmed. But if thesoftware contains a negative list of which processor models to avoid running the advanced
137

version on, then it will run the advanced version on all newer models that were unknown at
the time of programming.

13.3 Difficult cases
In most cases, the optimal branch can be chosen based on the CPUID information about
supported instruction sets, cache size, etc. There are a few cases, however, where there
are different ways of doing the same thing and the CPUID instruction does not give thenecessary information about which implementation is best. These cases are sometimes
dealt with in assembly language. Here are some examples:•

strlen function. The string length function scans a string of bytes to find the first
byte of zero. A good implementation uses vector registers to test 16 bytes or more ata time and afterwards a BSF (bit scan forward) instruction to localize the first byte ofzero within a block of 16 bytes. Some older CPUs have particularly slow
implementations of this bit scan instruction. Programmers that have tested thestrlen function in isolation have been unsatisfied with the performance of thisfunction on CPUs with a slow bit scan instruction and have implemented a separate
version for specific CPU models. However, we must consider that the bit scan
instruction is executed only once for each function call so that you have to call thefunction billions of times before the performance even matters, which few programs
do. Hence, it is hardly worth the effort to make special versions of the strlen
function for CPUs with slow bit scan instructions. My recommendation is to use thebit scan instruction and expect this to be the fastest solution on future CPUs.

•

Half size execution units. The size of vector registers has been increased from 64-bit
MMX to 128-bit XMM, 256-bit YMM, and 512-bit ZMM registers. The first processorsthat supported 128-bit vector registers had in fact only 64-bit execution units. Each
128-bit operation was split into two 64-bit operations so that there was hardly any
speed advantage in using the larger vector size. Later models had the full 128-bit
execution units and hence higher speed. In the same way, the first processors that
supported 256-bit instructions were splitting 256-bit read operations into two 128-bit
reads. It is possible that we will see something similar with further expansions of theregister size in the future. Often, the full advantage of a new register size comes onlyin the second generation of processors that support it. There are situations where avector implementation is optimal only on CPUs with full-size execution units. The
problem is that it is difficult for the CPU dispatcher to know whether the largest
vector register size is handled at half speed or full speed. A simple solution to thisproblem is to use a new register size only when the next higher instruction set is
supported. For example, use AVX only when AVX2 is supported in suchapplications. Alternatively, use a negative list of processors on which it is notadvantageous to use the newest instruction set.

•High precision math. Libraries for high precision math allow addition of integers witha very large number of bits. This is usually done in a loop of ADC (add with carry)
instructions where the carry bit must be saved from one iteration to the next. The
carry bit can be saved either in the carry flag or in a register. If the carry bit is kept in
the carry flag then the loop branch must rely on instructions that use the zero flagand do not modify the carry flag (e.g. DEC, JNZ). This solution can incur a large
delay due to the so-called partial flags stall on some processors because the CPU
has problems separating the flags register into the carry and zero flags on older IntelCPUs, but not on AMD CPUs (See manual 3: "The microarchitecture of Intel, AMD
and VIA CPUs"). This is one of the few cases where it makes sense to dispatch by
CPU model. The version that works best on the newest CPU of a particular brand islikely to be the optimal choice for future models of the same brand. Newer
processors support the ADX instructions for high precision math.
138

Memory copying. There are several different ways of copying blocks of memory.
These methods are discussed in manual 2: "Optimizing subroutines in assembly
language", section 17.9: "Moving blocks of data", where it is also discussed which
method is fastest on different processors. In a C++ program, you should choose an
up-to-date function library that has a good implementation of the memcpy function.
There are so many different cases for different microprocessors, different alignments
and different sizes of the data block to copy that the only reasonable solution is tohave a standard function library that takes care of the CPU dispatching. This function
is so important and generally used that most function libraries have CPU dispatchingfor this function, though not all libraries have the best and most up-to-date solution.
The compiler is likely to use the memcpy function implicitly when copying a large
object, unless there is a copy constructor specifying otherwise.
In difficult cases like these, it is important to remember that your code is likely to run most of
its time on processors that were unknown at the time it was programmed. Therefore, it isimportant to consider which method is likely to work best on future processors, and choose
this method for all unknown processors that support the necessary instruction set. It is rarely
worth the effort to make a CPU dispatcher based on complicated criteria or lists of specific
CPU models if the problem is likely to go away in the future due to general improvements in
microprocessor hardware design.
The ultimate solution would be to include a performance test that measures the speed ofeach version of the critical code to see which solution is optimal on the actual processor.
However, this involves the problems that the clock frequency may vary dynamically and thatmeasurements are unstable due to interrupts and task switches, so that it is necessary totest the different versions alternatingly several times in order to make a reliable decision.

13.4 Test and maintenance
There are two things to test when software uses CPU dispatching:
1. How much you gain in speed by using a particular code version.
2. Check that all code versions work correctly.
The speed test should preferably be done on the type of CPU that each particular branch ofcode is intended for. In other words, you need to test on several different CPUs if you want
to optimize for several different CPUs.
On the other hand, it is not necessary to have many different CPUs to verify that all code
branches works correctly. A code branch for a low instruction set can still run on a CPU witha higher instruction set. Therefore, you only need a CPU with the highest instruction set inorder to test all branches for correctness. It is therefore recommended to put a test feature
into the code that allows you to override the CPU dispatching and run any code branch fortest purposes.
If the code is implemented as a function library or a separate module, then it is convenient
to make a test program that can call all code branches separately and test theirfunctionality. This will be very helpful for later maintenance. However, this is not a textbook
on test theory. Advice on how to test a software module for correctness must be foundelsewhere.

13.5 Implementation
The CPU dispatch mechanism can be implemented in different places making the dispatch
decision at different times:
139•

Dispatch on every call. A branch tree or switch statement leads to the appropriate
version of the critical function. The branching is done every time the critical function
is called. This has the disadvantage that the branching takes time.

•

Dispatch on first call. The function is called through a function pointer which initiallypoints to a dispatcher. The dispatcher changes the function pointer and makes it
point to the right version of the function. This has the advantage that it does not
spend time on deciding which version to use in case the function is never called.
This method is illustrated in example 13.1 below.

•

Make pointer at initialization. The program or library has an initialization routine thatis called before the first call to the critical function. The initialization routine sets afunction pointer to the right version of the function. This has the advantage that the
response time is consistent for the function call.

•

Load library at initialization. Each code version is implemented in a separate
dynamic link library (*.dll or *.so). The program has an initialization routine thatloads the appropriate version of the library. This method is useful if the library is verylarge or if different versions have to be compiled with different compilers.

•

Dispatch at load time. The program uses a procedure linkage table (PLT) that is
initialized when the program is loaded. This method requires OS support and isavailable in newer versions of Linux and perhaps Mac OS. See page 142 below.

•

Dispatch at installation time. Each code version is implemented in a separate
dynamic link library (*.dll or *.so). The installation program makes a symbolic
link to the appropriate version of the library. The application program loads thelibrary through the symbolic link.

•

Use different executables. This method can be used if instruction sets are mutually
incompatible. You may make separate executables for 32-bit and 64-bit systems.
The appropriate version of the program may be selected during the installation
process or by an executable file stub.

If different versions of the critical code are compiled with different compilers then it isrecommended to specify static linking for any library functions called by the critical code sothat you do not have to distribute all the dynamic libraries (*.dll or *.so) that belong toeach compiler with the application.
The availability of various instruction sets can be determined with system calls (e.g.
IsProcessorFeaturePresent in Windows). Alternatively, you may call the CPUID
instruction directly, or use the InstructionSet()in the library
www.agner.org/optimize/asmlib.zip or the instrset_detect() function fromhttps://github.com/vectorclass/version2/blob/master/instrset_detect.cpp.
The following example shows how to implement the dispatch on first call method using thefunction InstructionSet(). See also example 12.6 page 128 for CPU dispatching withthe vector class library.
// Example 13.1// CPU dispatching on first call// Header file for InstructionSet()#include "asmlib.h"// Define function type with desired parameters
typedef int CriticalFunctionType(int parm1, int parm2);// Function prototype

140

CriticalFunctionType CriticalFunction_Dispatch;// Function pointer serves as entry point.
// After first call it will point to the appropriate function version
CriticalFunctionType * CriticalFunction = &CriticalFunction_Dispatch;// Lowest version
int CriticalFunction_386(int parm1, int parm2) {...}// SSE2 version
int CriticalFunction_SSE2(int parm1, int parm2) {...}// AVX version
int CriticalFunction_AVX(int parm1, int parm2) {...}// Dispatcher. Will be called only first time
int CriticalFunction_Dispatch(int parm1, int parm2){// Get supported instruction set, using asmlib library
int level = InstructionSet();// Set pointer to the appropriate version (May use a table// of function pointers if there are many branches):
if (level >= 11){ // AVX supported
CriticalFunction = &CriticalFunction_AVX;}
else if (level >= 4){ // SSE2 supported
CriticalFunction = &CriticalFunction_SSE2;}
else{ // Generic version
CriticalFunction = &CriticalFunction_386;}// Now call the chosen version
return (*CriticalFunction)(parm1, parm2);}
int main(){
int a, b, c;...
// Call critical function through function pointera = (*CriticalFunction)(b, c);...
return 0;}

The InstructionSet() function is available in the function library asmlib, which isavailable in different versions for different compilers. This function is OS independent andchecks both the CPU and the operating system for support of the different instructions sets.
The different versions of CriticalFunction in example 13.1 can be placed in separate
modules if necessary, each compiled for the specific instruction set.

13.6 CPU dispatching at load time in LinuxA feature called "Gnu indirect function" has been introduced in Linux and supported by theGnu utilities in 2010 and by Clang in 2015. This feature calls a dispatch function at the time
the program is loaded. The dispatch function returns a pointer to the selected version of the141function, and this pointer is placed in an ordinary procedure linkage table (PLT). The indirectfunction feature requires support from both compiler, linker and loader (requires binutils
version 2.20, glibc version 2.11 ifunc branch). This feature works only on platforms that use
the ELF file format, i.e. Linux and FreeBSD, not Windows and MacOS.
Note that the dispatcher function is called before the program starts running and before any
constructors are called. Therefore, the dispatcher function cannot rely on anything else
being initialized, and it has no access to environment variables. The dispatcher function iscalled even if the dispatched function is never called. Example 13.2 show how to use theGnu indirect function feature.
// Example 13.2. CPU dispatching with Gnu indirect function// The function instrset_detect() is borrowed from:// github.com/vectorclass/version2/blob/master/instrset_detect.cpp#include "instrset_detect.cpp"// Declare myfunc as a dispatched function// The name of the dispatcher is given in quotes
int myfunc (int) __attribute__ ((ifunc ("myfunc_dispatch")));// Generic version
int myfuncGeneric (int a) {puts("Generic");
return a;}// AVX2 version
int myfuncAVX2 (int a) {puts("AVX2");
return a;}// AVX512 version
int myfuncAVX512 (int a) {puts("AVX512");return a;}// declare function type// (replace parameter types and return type to fit your case)
typedef int functype(int);// avoid name mangling
extern "C" {// The dispatch function returns a pointer to the selected version
functype * myfunc_dispatch() {// Detect supported instruction set
int instrset = instrset_detect();// Return a pointer to the selected function
if (instrset >= 10) {return myfuncAVX512;}
else if (instrset >= 8) {return myfuncAVX2;}
else {
return myfuncGeneric;
}
}
}

142

The indirect function feature is used in the Gnu C function library for functions that are
particularly critical.

13.7 CPU dispatching in Intel compiler
The Intel compiler comes in two versions, a legacy version called “Classic” and a newer
version labeled “LLVM-based” which is a forking of the Clang compiler.
The Intel compiler “Classic” has a feature for making multiple versions of a function fordifferent Intel CPUs. It uses the dispatch on every call method. When the function is called,
a dispatch is made to the desired version of the function. The automatic dispatching can bemade for all suitable functions in a module by compiling the module with, e.g. the option/QaxAVX or -axAVX. This will make multiple versions of all relevant functions. It is possible
to do the dispatching only for speed-critical functions by using the directive__declspec(cpu_dispatch(...)). See the Intel C++ Compiler Documentation for
details.
The Intel compiler Classic is currently the only compiler that can generate CPU dispatchingon user code automatically. Unfortunately, the CPU dispatch mechanism in the Intel
compiler works only for Intel CPUs, not for AMD or other brands.
The CPU dispatch mechanism provided by the Intel compiler is less efficient than the Gnu
compiler mechanism because it makes dispatching on every call of the critical function,
while the Gnu mechanism stores a pointer to the desired version in a procedure linkage
table. If a dispatched function calls another dispatched function with the Intel compiler thenthe dispatch branch of the latter is executed even though the CPU-type is already known at
this place. This can be avoided by inlining the latter function, but it may be better to do the
CPU dispatching explicitly as in example 13.1 on page 140.
Intel function libraries have features for automatic CPU dispatching. Many Intel libraryfunctions have several versions for different processors and instruction sets.
Unfortunately, the CPU detection mechanism in the Intel compiler Classic has several flaws:•

The best possible version of the code is chosen only when running on an Intelprocessor. The CPU dispatcher checks whether the processor is an Intel before it
checks which instruction set it supports. An inferior "generic" version of the code isselected if the processor is not an Intel, even if the processor is compatible with abetter version of the code. This can lead to a dramatic degradation of performanceon AMD and VIA processors.

•

Explicit CPU dispatching works only with Intel processors. A non-Intel processor
makes the dispatcher signal an error by performing an illegal operation that crashes
the program or by printing an error message.

•

The CPU dispatcher does not check if XMM registers are supported by the operating
system. It will crash on old operating systems that do not support SSE.

Several function libraries published by Intel have similar CPU dispatch mechanisms, andsome of these also treat non-Intel CPUs in a suboptimal way.
The fact that software built with Intel compilers or Intel function libraries performs poorly onother brands, has become a source of much controversy and legal battles. See my blog for
details.

143

The newer “LLVM-based” Intel compiler behaves better. This compiler does not check the
CPU brand, but only which instruction sets are supported. However, the “LLVM-based” Intel
compiler does not support automatic CPU dispatching of user code. Only the code in thefunction libraries is dispatched.
The CPU dispatchers in Intel function libraries have two versions, one version that checks
the CPU brand and gives an inferior version if the CPU is anything else than Intel, andanother version that checks only which instruction sets are supported. For example, theinternal dispatchers in the standard library are named __intel_cpu_features_init()
and __intel_cpu_features_init_x(), respectively, for the two versions. Other Intelfunction libraries have similar dispatchers, where the one that does not check the CPUbrand has suffix _x. The unfair versions that check the CPU brand are used with the Classic
version of the Intel compiler, while the _x version that treats non-Intel processors more fairly
is used with the LLVM-based Intel compiler and with non-Intel compilers. Note that thisinformation is based on my own experiments and may not apply in all cases.
The conclusion is that the Classic version of the Intel compiler should never be used forsoftware that may run on non-Intel processors. The LLVM-based Intel compiler may be
used for non-Intel processors, but you may as well use the plain Clang compiler since thetwo compilers are almost identical.
The manual for the LLVM-based Intel compiler states that code generated with the -m or/arch: option should execute on any compatible, non-Intel processor with support for thecorresponding instruction set. Code compiled with options beginning with -x or /Qx will run
only on Intel processors.
Intel function libraries are optimized for Intel processors, but the version included with the
LLVM-based Intel compiler seems to provide good performance on non-Intel processors as
well. However, this is based on my own limited experiments. There is so far no clear
confirmation from intel that their function libraries do not degrade performance on non-Intelprocessors (see discussion here).
I have previously published various tricks on how to override the unfair CPU dispatcher inIntel function libraries. These tricks are apparently no longer needed for the latest version of
the libraries as long as they are not used in connection with the Classic version of the Intel
compiler.

14 Specific optimization topics
14.1 Use lookup tables
Reading a value from a table of constants is very fast if the table is cached. Usually it takes
only a few clock cycles to read from a table in the level-1 cache. We can take advantage ofthis fact by replacing a function call with a table lookup if the function has only a limited
number of possible inputs.
Let's take the integer factorial function (n!) as an example. The only allowed inputs are theintegers from 0 to 12. Higher inputs give overflow and negative inputs give infinity. A typical
implementation of the factorial function looks like this:// Example 14.1a
int factorial (int n) {// n!
int i, f = 1;
for (i = 2; i <= n; i++) f *= i;return f;}

144

This calculation requires n-1 multiplications, which can take quite a long time. It is more
efficient to use a lookup table:// Example 14.1b
int factorial (int n) {// n!
// Table of factorials:
const int FactorialTable[13] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
if ((unsigned int)n < 13) {// Bounds checking (see page 147)
return FactorialTable[n]; // Table lookup}
else {
return 0;// return 0 if out of range}}

This implementation uses a lookup table instead of calculating the value each time thefunction is called. I have added a bounds check on n here because the consequence of nbeing out of range is possibly more serious when n is an array index than when n is a loop
count. The method of bounds checking is explained below on page 147.
The table should be declared const in order to enable constant propagation and otheroptimizations. You may declare the function inline.
Replacing a function with a lookup table is advantageous in most cases where the numberof possible inputs is limited and there are no cache problems. It is not advantageous to use
a lookup table if you expect the table to be evicted from the cache between each call, andthe time it takes to calculate the function is less than the time it takes to reload the value
from memory plus the costs to other parts of the program of occupying a cache line.
Table lookup cannot be vectorized with the current instruction set. Do not use lookup tables
if this prevents a faster vectorized code.
Storing something in static memory can cause caching problems because static data arelikely to be scattered around at different memory addresses. If caching is a problem then it
may be useful to copy the table from static memory to stack memory outside the innermost
loop. This is done by declaring the table inside a function but outside the innermost loop andwithout the static keyword:// Example 14.1c
void CriticalInnerFunction () {// Table of factorials:
const int FactorialTable[13] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};...
int i, a, b;// Critical innermost loop:for (i = 0; i < 1000; i++) {...
a = FactorialTable[b];...
}}The FactorialTable in example 14.1c is copied from static memory to the stack when
CriticalInnerFunction is called. The compiler will store the table in static memory andinsert a code that copies the table to stack memory at the start of the function. Copying thetable takes extra time, of course, but this is permissible when it is outside the criticalinnermost loop. The loop will use the copy of the table that is stored in stack memory which145is contiguous with other local variables and therefore likely to be cached more efficiently
than static memory.
If you do not care to calculate the table values by hand and insert the values in the code
then you may of course make the program do the calculations. The time it takes to calculate
the table is not significant as long as it is done only once. One may argue that it is safer tocalculate the table in the program than to type in the values because a typo in a handwritten table may go undetected.
The principle of table lookup can be used in any situation where a program chooses
between two or more constants. For example, a branch that chooses between two
constants can be replaced by a table with two entries. This may improve the performance if
the branch is poorly predictable. For example:// Example 14.2afloat a; int b;
a = (b == 0) ? 1.0f : 2.5f;

If we assume that b is always 0 or 1 and that the value is poorly predictable, then it isadvantageous to replace the branch by a table lookup:// Example 14.2b
float a; int b;
const float OneOrTwo5[2] = {1.0f, 2.5f};
a = OneOrTwo5[b & 1];

Here, I have AND'ed b with 1 for the sake of security. b & 1 is certain to have no othervalue than 0 or 1 (see page 147). This extra check on b can be omitted, of course, if thevalue of b is guaranteed to be 0 or 1. Writing a = OneOrTwo5[b!=0]; will also work,
although slightly less efficiently. This method is inefficient, however, when b is a float or
double because all the compilers I have tested implement OneOrTwo5[b!=0] as
OneOrTwo5[(b!=0) ? 1 : 0] in this case so we do not get rid of the branch. It may
seem illogical that the compiler uses a different implementation when b is floating point. The
reason is, I guess, that compiler makers assume that floating point comparisons are morepredictable than integer comparisons. The solution a = 1.0f + b * 1.5f; is efficientwhen b is a float, but not if b is an integer because the integer-to-float conversion takes
more time than the table lookup.
Lookup tables are particular advantageous as replacements for switch statements
because switch statements often suffer from poor branch prediction. Example:// Example 14.3a
int n;
switch (n) {case 0:
printf("Alpha");case 1:
printf("Beta");case 2:
printf("Gamma");case 3:printf("Delta");}break;
break;
break;
break;

This can be improved by using a lookup table:// Example 14.3b
int n;
char const * const Greek[4] = {146

"Alpha", "Beta", "Gamma", "Delta"};if ((unsigned int)n < 4) { // Check that index is not out of range
printf(Greek[n]);}The declaration of the table has const twice because both the pointers and the texts they
point to are constant.

14.2 Bounds checking
In C++, it is often necessary to check if an array index is out of range. This may typicallylook like this:// Example 14.4a
const int size = 16; int i;
float list[size];...
if (i < 0 || i >= size) {cout << "Error: Index out of range";}else {
list[i] += 1.0f;}

The two comparisons i < 0 and i >= size can be replaced by a single comparison:// Example 14.4b
if ((unsigned int)i >= (unsigned int)size) {cout << "Error: Index out of range";}else {
list[i] += 1.0f;}

A possible negative value of i will appear as a large positive number when i isinterpreted as an unsigned integer and this will trigger the error condition. Replacing two
comparisons by one makes the code faster because testing a condition is relatively
expensive, while the type conversion generates no extra code at all.
This method can be extended to the general case where you want to check whether an
integer is within a certain interval:// Example 14.5a
const int min = 100, max = 110;...
if (i >= min && i <= max) { ...

int i;

can be changed to:// Example 14.5b
if ((unsigned int)i - (unsigned int)min <= (unsigned int)(max - min)){ ...

There is an even faster way to limit the range of an integer if the length of the desiredinterval is a power of 2. Example:// Example 14.6
float list[16]; int i;...
list[i & 15] += 1.0f;

147This needs a little explanation. The value of i&15 is guaranteed to be in the interval from 0
to 15. If i is outside this interval, for example i = 18, then the & operator (bitwise and) will
cut off the binary value of i to four bits, and the result will be 2. The result is the same as i
modulo 16. This method is useful for preventing program errors in case the array index isout of range and we do not need an error message if it is. It is important to note that thismethod works only for powers of 2 (i.e. 2, 4, 8, 16, 32, 64, ...). We can make sure that avalue is less than 2n and not negative by AND'ing it with 2n -1. The bitwise AND operation
isolates the least significant n bits of the number and sets all other bits to zero.

14.3 Use bitwise operators for checking multiple values at once
The bitwise operators &, |, ^, ~, <<, >> can test or manipulate all the bits of an integer in
one operation. For example, if each bit of a 32-bit integer has a particular meaning, then you
can set multiple bits in a single operation using the | operator; you can clear or mask out
multiple bits with the & operator; and you can toggle multiple bits with the ^ operator.
The & operator is also useful for testing multiple conditions in a single operation. Example:// Example 14.7a. Testing multiple conditions
enum Weekdays {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
Weekdays Day;if (Day == Tuesday || Day == Wednesday || Day == Friday) {DoThisThreeTimesAWeek();}

The if statement in this example has three conditions which are implemented as threebranches. They can be joined into a single branch if the constants Sunday, Monday, etc.
are defined as powers of 2:// Example 14.7b. Testing multiple conditions using &enum Weekdays {Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8,
Thursday = 0x10, Friday = 0x20, Saturday = 0x40};
Weekdays Day;if (Day & (Tuesday | Wednesday | Friday)) {DoThisThreeTimesAWeek();}

By giving each constant a value that is a power of 2 in example 14.7b, we are in fact using
each bit in Day for signifying one of the weekdays. The maximum number of constants wecan define in this way is equal to the number of bits in an integer, usually 32. In 64-bit
systems we can use 64-bit integers with hardly any loss of efficiency.
The expression (Tuesday | Wednesday | Friday) in example 14.7b is converted by
the compiler to the value 0x2C so that the if condition can be calculated by a single &operation, which is very fast. The result of the & operation will be non-zero, and therefore
count as true, if any of the bits for Tuesday, Wednesday or Friday is set in the variable
Day.
Note the difference between the Boolean operators &&, ||, ! and the corresponding bitwise
operators &, |, ~. The Boolean operators produce a single result, true (1) or false (0); andthe second operand is evaluated only when needed. The bitwise operators produce 32
results when applied to 32-bit integers, and they always evaluate both operands.
Nevertheless, the bitwise operators are calculated much faster than the Boolean operators
148

because they do not use branches, provided that the operands are integer expressions
rather than Boolean expressions.
There are lots of things you can do with bitwise operators using integers as Boolean
vectors, and these operations are very fast. This can be useful in programs with many
Boolean expressions. Whether the constants are defined with enum, const, or #define
makes no difference for the performance.

14.4 Integer multiplication
Integer multiplication takes longer time than addition and subtraction (3 - 10 clock cycles,
depending on the processor). Optimizing compilers will often replace integer multiplication
by a constant with a combination of additions and shift operations. Multiplying by a power of
2 is faster than multiplying by other constants because it can be done as a shift operation.
For example, a * 16 is calculated as a << 4, and a * 17 is calculated as (a << 4) +a.
You can take advantage of this by preferably using powers of 2 when multiplying with aconstant. The compilers also have fast ways of multiplying by 3, 5 and 9.
Multiplications are done implicitly when calculating the address of an array element. In somecases this multiplication will be faster when the factor is a power of 2. Example:// Example 14.8
const int rows = 10, columns = 8;
float matrix[rows][columns];
int i, j;
int order(int x);...
for (i = 0; i < rows; i++) {j = order(i);matrix[j][0] = i;}Here, the address of matrix[j][0] is calculated internally as(int)&matrix[0][0] + j * (columns * sizeof(float)).
Now, the factor to multiply j by is (columns * sizeof(float)) = 8 * 4 = 32. Thisis a power of 2, so the compiler can replace j * 32 with j << 5. If columns had notbeen a power of 2 then the multiplication would take longer time. It can therefore be
advantageous to make the number of columns in a matrix a power of 2 if the rows areaccessed in a non-sequential order.
The same applies to an array of structure or class elements. The size of each object should
preferably be a power of 2 if the objects are accessed in a non-sequential order. Example:// Example 14.9
struct S1 {int a;
int b;
int c;
int UnusedFiller;};
int order(int x);
const int size = 100;
S1 list[size]; int i, j;...
for (i = 0; i < size; i++) {j = order(i);list[j].a = list[j].b + list[j].c;}149

Here, we have inserted UnusedFiller in the structure to make sure its size is a power of
2 in order to make the address calculation faster.
The advantage of using powers of 2 applies only when elements are accessed in nonsequential order. If the code in example 14.8 and 14.9 is changed so that it has i instead ofj as index then the compiler can see that the addresses are accessed in sequential orderand it can calculate each address by adding a constant to the preceding one (see page 72).
In this case it does not matter if the size is a power of 2 or not.
The advice of using powers of 2 does not apply to very big data structures. On the contrary,
you should by all means avoid powers of 2 if a matrix is so big that caching becomes aproblem. If the number of columns in a matrix is a power of 2 and the matrix is bigger thanthe cache then you can get very expensive cache contentions, as explained on page 106.

14.5 Integer division
Integer division takes much longer time than addition, subtraction and multiplication (27 - 80
clock cycles for 32-bit integers, depending on the processor).
Integer division by a power of 2 can be done with a shift operation, which is much faster.
Division by a constant is faster than division by a variable because optimizing compilers cancompute a / b as a * (2n / b) >> n with a suitable choice of n. The constant (2n / b) is
calculated in advance and the multiplication is done with an extended number of bits. The
method is somewhat more complicated because various corrections for sign and rounding
errors must be added. This method is described in more detail in manual 2: "Optimizing
subroutines in assembly language". The method is faster if the dividend is unsigned.
The following guidelines can be used for improving code that contains integer division:•

Integer division by a constant is faster than division by a variable. Make sure thevalue of the divisor is known at compile time.

•

Integer division by a constant is faster if the constant is a power of 2•

Integer division by a constant is faster if the dividend is unsigned

Examples:// Example 14.10
int a, b, c;
a = b / c;// This is slow
a = b / 10;// Division by a constant is fastera = (unsigned int)b / 10; // Still faster if unsigned
a = b / 16;// Faster if divisor is a power of 2
a = (unsigned int)b / 16; // Still faster if unsigned

The same rules apply to modulo calculations:// Example 14.11
int a, b, c;
a = b % c;// This is slow
a = b % 10;// Modulo by a constant is fastera = (unsigned int)b % 10; // Still faster if unsigned
a = b % 16;// Faster if divisor is a power of 2
a = (unsigned int)b % 16; // Still faster if unsigned

150

You can take advantage of these guidelines by using a constant divisor that is a power of 2
if possible and by changing the dividend to unsigned if you are sure that it will not be
negative.
The method described above can still be used if the value of the divisor is not known atcompile time, but the program is dividing repeatedly with the same divisor. In this case you
have to do the necessary calculations of (2n / b) etc. at compile time. The function library at
www.agner.org/optimize/asmlib.zip contains various functions for these calculations.
Division of a loop counter by a constant can be avoided by rolling out the loop by the same
constant. Example:// Example 14.12a
int list[300];
int i;for (i = 0; i < 300;
list[i] += i / 3;}i++) {

This can be replaced with:// Example 14.12b
int list[300];
int i, i_div_3;
for (i = i_div_3 = 0; i < 300;
list[i]+= i_div_3;
list[i+1] += i_div_3;
list[i+2] += i_div_3;}

i += 3, i_div_3++) {A similar method can be used to avoid modulo operations:// Example 14.13a
int list[300];
int i;for (i = 0; i < 300;
list[i] = i % 3;}i++) {

This can be replaced with:// Example 14.13b
int list[300];
int i;for (i = 0; i < 300;
list[i]= 0;list[i+1] = 1;
list[i+2] = 2;}i += 3) {The loop unrolling in example 14.12b and 14.13b works only if the loop count is divisible by
the unroll factor. If not, then you must do the extra operations outside the loop:// Example 14.13c
int list[301];
int i;for (i = 0; i < 300;
list[i]= 0;list[i+1] = 1;
list[i+2] = 2;}
list[300] = 0;i += 3) {151

14.6 Floating point division
Floating point division takes much longer time than addition, subtraction and multiplication(20 - 45 clock cycles).
Floating point division by a constant should be done by multiplying with the reciprocal:// Example 14.14adouble a, b;
a = b / 1.2345;

Change this to:// Example 14.14b
double a, b;
a = b * (1. / 1.2345);

The compiler will calculate (1./1.2345) at compile time and insert the reciprocal in thecode, so you will never spend time doing the division. Some compilers will replace the codein example 14.14a with 14.14b automatically but only if certain options are set to relax
floating point precision (see page 75). It is therefore safer to do this optimization explicitly.
Divisions can sometimes be eliminated completely. For example:// Example 14.15a
if (a > b / c)can sometimes be replaced by// Example 14.15b
if (a * c > b)

But beware of the pitfalls here: The inequality sign must be reversed if c < 0. The division is
inexact if b and c are integers, while the multiplication is exact.
Multiple divisions can be combined. For example:// Example 14.16adouble y, a1, a2, b1, b2;
y = a1/b1 + a2/b2;

Here we can eliminate one division by making a common denominator:// Example 14.16b
double y, a1, a2, b1, b2;
y = (a1*b2 + a2*b1) / (b1*b2);

The trick of using a common denominator can even be used on completely independent
divisions. Example:// Example 14.17adouble a1, a2, b1, b2, y1, y2;
y1 = a1 / b1;
y2 = a2 / b2;

This can be changed to:// Example 14.17b

152

double a1, a2, b1, b2, y1, y2, reciprocal_divisor;
reciprocal_divisor = 1. / (b1 * b2);
y1 = a1 * b2 * reciprocal_divisor;y2 = a2 * b1 * reciprocal_divisor;

14.7 Do not mix float and double
Floating point calculations usually take the same time regardless of whether you are using
single precision or double precision, but there is a penalty for mixing single and double
precision in programs compiled for 64-bit operating systems and programs compiled for the
instruction set SSE2 or later. Example:// Example 14.18a
float a, b;
a = b * 1.2;// Mixing float and double is bad

The C/C++ standard specifies that all floating point constants are double precision bydefault, so 1.2 in this example is a double precision constant. It is therefore necessary toconvert b from single precision to double precision before multiplying with the double
precision constant and then convert the result back to single precision. These conversions
take a lot of time. You can avoid the conversions and make the code up to 5 times faster
either by making the constant single precision or by making a and b double precision:// Example 14.18b
float a, b;
a = b * 1.2f;// everything is float// Example 14.18c
double a, b;
a = b * 1.2;// everything is double

There is no penalty for mixing different floating point precisions when the code is compiledfor old processors without the SSE2 instruction set, but it may be preferable to keep thesame precision in all operands in case the code is later ported to another platform.

14.8 Conversions between floating point numbers and integers
Conversion from floating point to integer
According to the standards for the C++ language, all conversions from floating point
numbers to integers use truncation towards zero, rather than rounding. This is unfortunate
because truncation takes much longer time than rounding unless the SSE2 instruction set isused. It is recommended to enable the SSE2 instruction set if possible. SSE2 is always
enabled in 64-bit mode.
A conversion from floating point to integer without SSE2 typically takes 40 clock cycles. If
you cannot avoid conversions from float or double to int in the critical part of thecode, then you may improve efficiency by using rounding instead of truncation. This isapproximately three times faster. The logic of the program may need modification tocompensate for the difference between rounding and truncation.
Efficient conversion from float or double to integer can be done with the functions
lrintf and lrint. Unfortunately, these functions are missing in many commercial
compilers due to controversies over the C99 standard. An implementation of the lrint
function is given in example 14.19 below. The function rounds a floating point number to thenearest integer. If two integers are equally near then the even integer is returned. There is
no check for overflow. This function is intended for 32-bit Windows and 32-bit Linux with
Microsoft, Intel and Gnu compilers.
153

// Example 14.19
static inline int lrint (double const x) { // Round to nearest integer
int n;#if defined(__unix__) || defined(__GNUC__)// 32-bit Linux, Gnu/AT&T syntax:__asm ("fldl %1 \n fistpl %0 " : "=m"(n) : "m"(x) : "memory" );#else// 32-bit Windows, Intel/MASM syntax:__asm fld qword ptr x;__asm fistp dword ptr n;#endifreturn n;}This code will work only on Intel/x86-compatible microprocessors. The function is alsoavailable in the function library at www.agner.org/optimize/asmlib.zip.
The following example shows how to use the lrint function:// Example 14.20
double d = 1.6;
int a, b;a = (int)d;// Truncation is slow. Value of a will be 1
b = lrint(d);// Rounding is fast. Value of b will be 2

In 64-bit mode or when the SSE2 instruction set is enabled there is no difference in speed
between rounding and truncation. The missing functions can be implemented as follows in
64-bit mode or when the SSE2 instruction set is enabled:// Example 14.21. // Only for SSE2 or x64#include <emmintrin.h>static inline int lrintf (float const x) {return _mm_cvtss_si32(_mm_load_ss(&x));}static inline int lrint (double const x) {return _mm_cvtsd_si32(_mm_load_sd(&x));}The code in example 14.21 is faster than other methods of rounding, but neither faster nor
slower than truncation when the SSE2 instruction set is enabled.

Conversion from integer to floating point
Conversion of signed integers to floating point is fast only when the SSE2 instruction set isenabled. Conversion of unsigned integers to floating point is faster only when the AVX512
instruction set is enabled. See page 41.

14.9 Using integer operations for manipulating floating point variables
Floating point numbers are stored in a binary representation according to the IEEE standard
754 (1985). This standard is used in almost all modern microprocessors and operating
systems (but not in some very old DOS compilers).
The representation of float, double and long double reflects the floating point value
written as 2eee1.fffff, where  is the sign, eee is the exponent, and fffff is thebinary decimals of the fraction. The sign is stored as a single bit which is 0 for positive and 1
for negative numbers. The exponent is stored as a biased binary integer, and the fraction isstored as the binary digits. The exponent is always normalized, if possible, so that the value
before the decimal point is 1. This '1' is not included in the representation, except in thelong double format. The formats can be expressed as follows:154

struct Sfloat {unsigned int fraction : 23; // fractional part
unsigned int exponent : 8; // exponent + 0x7F
unsigned int sign
: 1; // sign bit};
struct Sdouble {unsigned int fraction : 52; // fractional part
unsigned int exponent : 11; // exponent + 0x3FF
unsigned int sign
: 1; // sign bit};
struct Slongdouble {unsigned int fraction : 63; // fractional part
unsigned int one: 1; // always 1 if nonzero and normal
unsigned int exponent : 15; // exponent + 0x3FFF
unsigned int sign
: 1; // sign bit};

The values of nonzero floating point numbers can be calculated as follows:
𝑓𝑙𝑜𝑎𝑡𝑣𝑎𝑙𝑢𝑒 = (−1)𝑠𝑖𝑔𝑛 ⋅ 2𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡−127 ⋅ (1 + 𝑓𝑟𝑎𝑐𝑡𝑖𝑜𝑛 ⋅ 2−23 ),𝑑𝑜𝑢𝑏𝑙𝑒𝑣𝑎𝑙𝑢𝑒 = (−1)𝑠𝑖𝑔𝑛 ⋅ 2𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡−1023 ⋅ (1 + 𝑓𝑟𝑎𝑐𝑡𝑖𝑜𝑛 ⋅ 2−52 ),𝑙𝑜𝑛𝑔𝑑𝑜𝑢𝑏𝑙𝑒𝑣𝑎𝑙𝑢𝑒 = (−1)𝑠𝑖𝑔𝑛 ⋅ 2𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡−16383 ⋅ (𝑜𝑛𝑒 + 𝑓𝑟𝑎𝑐𝑡𝑖𝑜𝑛 ⋅ 2−63 ).
The value is zero if all bits except the sign bit are zero. Zero can be represented with orwithout the sign bit.
The fact that the floating point format is standardized allows us to manipulate the different
parts of the floating point representation directly with the use of integer operations. This canbe an advantage because integer operations are faster than floating point operations. You
should use such methods only if you are sure you know what you are doing. See the end of
this section for some caveats.
We can change the sign of a floating point number simply by inverting the sign bit:// Example 14.22
union {
float f;int i;} u;u.i ^= 0x80000000; // flip sign bit of u.f

We can take the absolute value by setting the sign bit to zero:// Example 14.23
union {
float f;int i;} u;u.i &= 0x7FFFFFFF; // set sign bit to zero

We can check if a floating point number is zero by testing all bits except the sign bit:// Example 14.24
union {
float f;int i;} u;
if (u.i & 0x7FFFFFFF) { // test bits 0 - 30// f is nonzero}

155

else {// f is zero}

We can multiply a nonzero floating point number by 2n by adding n to the exponent:// Example 14.25
union {
float f;int i;} u;
int n;
if (u.i & 0x7FFFFFFF) { // check if nonzero
u.i += n << 23;// add n to exponent}

Example 14.25 does not check for overflow and works only for positive n. You can divide by
2n by subtracting n from the exponent if there is no risk of underflow.
The fact that the representation of the exponent is biased allows us to compare two positive
floating point numbers simply by comparing them as integers:// Example 14.26
union {
float f;int i;} u, v;
if (u.i > v.i) {// u.f > v.f if both positive}

Example 14.26 assumes that we know that u.f and v.f are both positive. It will fail if bothare negative or if one is 0 and the other is -0 (zero with sign bit set).
We can shift out the sign bit to compare absolute values:// Example 14.27
union {
float f;unsigned int i;} u, v;
if (u.i * 2 > v.i * 2) {// abs(u.f) > abs(v.f)}

The multiplication by 2 in example 14.27 will shift out the sign bit so that the remaining bits
represent a monotonically increasing function of the absolute value of the floating point
number.
We can convert an integer in the interval 0 <= n < 223 to a floating point number in theinterval [1.0, 2.0) by setting the fraction bits:// Example 14.28
union {
float f;int i;} u;
int n;
u.i = (n & 0x7FFFFF) | 0x3F800000; // Now 1.0 <= u.f < 2.0This method is useful for random number generators.
156

In general, it is faster to access a floating point variable as an integer if it is stored inmemory, but not if it is a register variable. The union forces the variable to be stored inmemory, at least temporarily. Using the methods in the above examples will therefore be adisadvantage if other nearby parts of the code could benefit from using registers for thesame variables.
In these examples we are using unions rather than type casting of pointers because thismethod is safer. Type casting of pointers may not work on compilers that rely on the strictaliasing rule of standard C, specifying that pointers of different types cannot point to thesame object, except for char pointers.
The above examples all use single precision. Using double precision in 32-bit systems gives
rise to some extra complications. A double is represented with 64 bits, but 32-bit systems do
not have inherent support for 64-bit integers. Many 32-bit systems allow you to define 64-bit
integers, but they are in fact represented as two 32-bit integers, which is less efficient. You
may use the upper 32 bits of a double which gives access to the sign bit, the exponent,
and the most significant part of the fraction. For example, to test the sign of a double:// Example 14.22b
union {
double d;
int i[2];} u;
if (u.i[1] < 0) { // test sign bit// u.d is negative or -0}

It is not recommended to modify a double by modifying only half of it, for example if you
want to flip the sign bit in the above example with u.i[1] ^= 0x80000000; because thisis likely to generate a store forwarding delay in the CPU (See manual 3: "Themicroarchitecture of Intel, AMD and VIA CPUs"). This can be avoided in 64-bit systems byusing a 64-bit integer rather than two 32-bit integers to alias upon the double.
Another problem with accessing 32 bits of a 64-bit double is that it is not portable to systems
with big-endian storage. Example 14.22b and 14.29 will therefore need modification if
implemented on other platforms with big-endian storage. All x86 platforms (Windows, Linux,
BSD, Intel-based Mac OS, etc.) have little-endian storage, but other systems may have big
endian storage (e.g. PowerPC).
We can make an approximate comparison of doubles by comparing bits 32-62. This can be
useful for finding the numerically largest element in a matrix for use as pivot in a Gausselimination. The method in example 14.27 can be implemented like this in a pivot search:// Example 14.29
const int size = 100;// Array of 100 doubles:
union {double d; unsigned int u[2]} a[size];
unsigned int absvalue, largest_abs = 0;
int i, largest_index = 0;
for (i = 0; i < size; i++) {// Get upper 32 bits of a[i] and shift out sign bit:absvalue = a[i].u[1] * 2;// Find numerically largest element (approximately):if (absvalue > largest_abs) {largest_abs = absvalue;largest_index = i;
}
}

157

Example 14.29 finds the numerically largest element in an array, or approximately so. It may
fail to distinguish elements with a relative difference less than 2-20, but this is sufficiently
accurate for the purpose of finding a suitable pivot element. The integer comparison is likely
to be faster than a floating point comparison. On big endian systems you have to replaceu[1] by u[0].

14.10 Mathematical functions
The most common mathematical functions such as logarithms, exponential functions,
trigonometric functions, etc. are implemented in hardware in the x86 CPUs. However, asoftware implementation is faster than the hardware implementation in most cases when the
SSE2 instruction set is available. Most compilers use the software implementation if theSSE2 or later instruction set is enabled.
The advantage of using a software implementation rather than a hardware implementationof these functions is higher for single precision than for double precision. But the software
implementation is faster than the hardware implementation in most cases, even for double
precision.
You may use the Intel math function library with a different compiler by including the librarylibmmt.lib and the header file mathimf.h that come with the Intel C++ compiler. Thislibrary contains many useful mathematical functions. A lot of advanced mathematical
functions are supplied in Intel's Math Kernel Library, available from www.intel.com. The Intelfunction libraries are optimized for Intel processors, but they often give reasonable
performance on AMD and other processors as well (see page 143).
The vector class library includes optimized mathematical functions with vector input(https://github.com/vectorclass). See page 131 for further discussion of vector functions.

14.11 Static versus dynamic libraries
Function libraries can be implemented either as static link libraries (*.lib, *.a) or dynamic
link libraries, also called shared objects (*.dll, *.so). The mechanism of static linking isthat the linker extracts the functions that are needed from the library file and copies theminto the executable file. Only the executable file needs to be distributed to the end user.
Dynamic linking works differently. The link to a function in a dynamic library is resolved
when the library is loaded or at run time. Therefore, both the executable file and one ormore dynamic libraries are loaded into memory when the program is run. Both theexecutable file and all the dynamic libraries need to be distributed to the end user.
The advantages of using static linking rather than dynamic linking are:•

Static linking includes only the part of the library that is actually needed by theapplication, while dynamic linking makes the entire library (or at least a large part of it)
load into memory even when just a small fraction of the library code is needed.

•

All the code is included in a single executable file when static linking is used. Dynamic
linking makes it necessary to load several files when the program is started.

•

It takes longer time to call a function in a dynamic library than in a static link librarybecause it needs an extra jump through a pointer in an import table and possibly also alookup in a procedure linkage table (PLT).

•

The memory space becomes more fragmented when the code is distributed between
multiple dynamic libraries. The dynamic libraries are loaded at round memory addresses
divisible by the memory page size (4096). This will make all dynamic libraries contend
158

for the same cache lines. This makes code caching and data caching less efficient.
•

Dynamic libraries are less efficient in some systems because of the needs of positionindependent code, see below.

•

Installing a second application that uses a newer version of the same dynamic library
can change the behavior of the first application if dynamic linking is used, but not if static
linking is used.

The advantages of dynamic linking are:•Multiple applications running simultaneously can share the same dynamic libraries
without the need to load more than one instance of the library into memory. This isuseful on servers that run many processes simultaneously. Actually, only the code
section and read-only data sections can be shared. Any writable data section needs one
instance for each process.

•A dynamic library can be updated to a new version without the need to update theprogram that calls it.

•

A dynamic library can be called from programming languages that do not support static
linking.

•

A dynamic library can be useful for making plug-ins that add functionality to an existing
program.

Weighing the above advantages of each method, it is clear that static linking is preferable
for speed-critical functions. Many function libraries are available in both static and dynamic
versions. It is recommended to use the static version if speed is important.
Some systems allow lazy binding of function calls. The principle of lazy binding is that theaddress of a linked function is not resolved when the program is loaded, but waits until thefirst time the function is called. Lazy binding can be useful for large libraries where only few
of the functions are actually called in a single session. But lazy binding definitely degrades
performance for the functions that are called. A considerable delay comes when a function
is called for the first time because it needs to load the dynamic linker.
The delay on lazy binding leads to a usability problem in interactive programs because theresponse time to e.g. a menu click becomes inconsistent and sometimes unacceptably long.
Lazy binding should therefore be used only for very large libraries.
The memory address at which a dynamic library is loaded cannot be determined inadvance, because a fixed address might clash with another dynamic library requiring thesame address. There are two commonly used methods for dealing with this problem:
1. Relocation. All pointers and addresses in the code are modified, if necessary, to fit
the actual load address. Relocation is done by the linker and the loader.
2. Position-independent code. All addresses in the code are relative to the current
position. Address calculation is done at runtime.
Windows DLLs use relocation. The DLLs are relocated by the linker to a specific load
address. If this address is not vacant then the DLL is relocated (rebased) once more by theloader to a different address. A call from the main executable to a function in a DLL goes
through an import table or a pointer. A variable in a DLL can be accessed from mainthrough an imported pointer, but this feature is seldom used. It is more common to
exchange data or pointers to data through function calls. Internal references to data within
159the DLL use absolute references in 32 bit mode and mostly relative references in 64 bit
mode. The latter is slightly more efficient because relative references do not need relocation
at load time.
Shared objects in Unix-like systems use position-independent code by default. This is lessefficient than relocation, especially in 32-bit mode. The next chapter describes how thisworks and suggests methods for avoiding the costs of position-independent code.

14.12 Position-independent code
Shared objects in Linux, BSD and Mac systems normally use the so-called positionindependent code. The name "position-independent code" actually implies more than itsays. A code that is compiled as position-independent has the following features:•

The code section contains no absolute addresses that need relocation, but only selfrelative addresses. Therefore, the code section can be loaded at an arbitrary
memory address and shared between multiple processes.

•

The data section is not shared between multiple processes because it often contains
writeable data. Therefore, the data section may contain pointers or addresses thatneed relocation.

•

All public functions and public data can be overridden in Linux and BSD. If a functionin the main executable has the same name as a function in a shared object, then theversion in main will take precedence, not only when called from main, but also whencalled from the shared object. Likewise, when a global variable in main has the same
name as a global variable in the shared object, then the instance in main will be
used, even when accessed from the shared object. This so-called symbolinterposition is intended to mimic the behavior of static libraries. A shared object hasa table of pointers to its functions, called procedure linkage table (PLT) and a tableof pointers to its variables called global offset table (GOT) in order to implement this"override" feature. All accesses to functions and public variables go through the PLT
and GOT.

The symbol interposition feature that allows overriding of public functions and data in Linuxand BSD comes at a high price, and in most libraries it is never used. Whenever a function
in a shared object is called, it is necessary to look up the function address in the procedurelinkage table (PLT). And whenever a public variable in a shared object is accessed, it isnecessary to first look up the address of the variable in the global offset table (GOT). Thesetable lookups are needed even when the function or variable is accessed from within thesame shared object. Obviously, all these table lookup operations slow down the execution
considerably. A more detailed discussion can be found at
http://www.macieira.org/blog/2012/01/sorry-state-of-dynamic-libraries-on-linux/
Another serious burden is the calculation of self-relative references in 32-bit mode. The 32bit x86 instruction set has no instruction for self-relative addressing of data. The code goes
through the following steps to access a public data object: (1) get its own address through afunction call. (2) find the GOT through a self-relative address. (3) look up the address of thedata object in the GOT, and finally (4) access the data object through this address. Step (1)is not needed in 64-bit mode because the x86-64 instruction set supports self-relative
addressing.
In 32-bit Linux and BSD, the slow GOT lookup process is used for all static data, including
local data that do not need the "override" feature. This includes static variables, floating
point constants, string constants, and initialized arrays. I have no explanation why this
delaying process is used when it is not needed.
160

Obviously, the best way to avoid the burdensome position-independent code and tablelookup is to use static linking, as explained in the previous chapter (page 158). In the cases
where dynamic linking cannot be avoided, there are various ways to avoid the timeconsuming features of the position-independent code. These workaround methods dependon the system, as explained below.

Shared objects in 32 bit Linux
Shared objects are normally compiled with the option -fpic according to the Gnu
compiler manual. This option makes the code section position-independent, makes a PLT
for all functions and a GOT for all public and static data.
It is possible to compile a shared object without the -fpic option. Then we get rid of all
the problems mentioned above. Now the code will run faster because we can accessinternal variables and internal functions in a single step rather than the complicated address
calculation and table lookup mechanisms explained above. A shared object compiled
without -fpic is much faster, except perhaps for a very large shared object where most of
the functions are never called. The disadvantage of compiling without -fpic in 32-bit Linux
is that the loader will have more references to relocate, but these address calculations aredone only once, while the runtime address calculations have to be done at every access.
The code section needs one instance for each process when compiled without -fpic
because the relocations in the code section will be different for each process. Obviously, welose the ability to override public symbols, but this feature is rarely needed anyway.
You may preferably avoid global variables or hide them for the sake of portability to 64-bit
mode, as explained below.

Shared objects in 64 bit LinuxThe procedure to calculate self-relative addresses is much simpler in 64-bit mode because
the 64-bit instruction set has support for relative addressing of data. The need for special
position-independent code is smaller because relative addresses are often used by default
anyway in 64-bit code. However, we still want to get rid of the GOT and PLT lookups for
local references.
If we compile the shared object without -fpic in 64 bit mode, we encounter anotherproblem. The compiler sometimes uses 32-bit absolute addresses, mainly for static arrays.
This works in the main executable because it is sure to be loaded at an address below 2
GB, but not in a shared object which is typically loaded at a higher address which cannot bereached with a 32-bit (signed) address. The linker will generate an error message in thiscase. The best solution is to compile with the option -fpie instead of -fpic. This will
generate relative addresses in the code section, but it will not use GOT and PLT for internalreferences. Therefore, it will run faster than when compiled with -fpic and it will not havethe disadvantages mentioned above for the 32-bit case. The -fpie option is less useful in
32-bit mode, where it still uses a GOT.
Another possibility is to compile with -mcmodel=large, but this will use full 64-bit
addresses for everything, which is quite inefficient, and it will generate relocations in thecode section so that it cannot be shared.
You cannot have public variables in a 64-bit shared object made with option -fpie
because the linker makes an error message when it sees a relative reference to a publicvariable where it expects a GOT entry. You can avoid this error by avoiding any publicvariables. All global variables (i.e. variables defined outside any function) should be hidden
by using the declaration "static" or "__attribute__((visibility("hidden")))".

161

The gnu compiler version 5.1 and later has an option -fno-semantic-interposition,
which makes it avoid the use of PLT and GOT look, but only for references within the same
file. The same effect can be obtained by using inline assembly code to give the variable two
names, one global and one local, and use the local name for local references.
Despite these tricks, you may still get the error message: "relocation R_X86_64_PC32
against symbol `functionname' can not be used when making a shared object; recompile
with -fPIC", when the shared object is made from multiple modules (source files) and there
is a call from one module to another. I have not yet found a solution to this problem.

Shared objects in BSD
Shared objects in BSD work the same way as in Linux.

32-bit Mac OS X
Compilers for 32-bit Mac OS X make position-independent code and lazy binding by default,
even when shared objects are not used. The method currently used for calculating selfrelative addresses in 32-bit Mac code uses an unfortunate method that delays execution bycausing return addresses to be mispredicted (See manual 3: "The microarchitecture of Intel,
AMD and VIA CPUs" for an explanation of return prediction).
All code that is not part of a shared object can be speeded up significantly just by turning off
the position-independent code flag in the compiler. Remember, therefore, always to specify
the compiler option -fno-pic when compiling for 32-bit Mac OS X, unless you are making
a shared object.
It is possible to make shared objects without position-independent code when you compile
with the option -fno-pic and link with the option -read_only_relocs suppress.
GOT and PLT tables are not used for internal references.

64-bit Mac OS X
The code section is always position-independent because this is the most efficient solution
for the memory model used here. The compiler option -fno-pic apparently has no effect.
GOT and PLT tables are not used for internal references.
There is no need to take special precautions for speeding up 64-bit shared objects in Mac
OS X.

14.13 System programming
Device drivers, interrupt service routines, system core and high-priority threads are areas
where speed is particularly critical. A very time-consuming function in system code or in ahigh-priority thread can possibly block the execution of everything else.
System code has to obey certain rules about register use, as explained in the chapter"Register usage in kernel code" in manual 5: "Calling conventions for different C++
compilers and operating systems". For this reason, you can use only compilers and function
libraries that are intended for system code. System code should be written in C, C++ or
assembly language.
It is important to economize resource use in system code. Dynamic memory allocation isparticularly risky because it involves the risk of activating the very time-consuming garbage
collector at inconvenient times. A queue should be implemented as a circular buffer with
fixed size, not as a linked list. Do not use standard C++ containers (see page 95).
162

15 Metaprogramming
Metaprogramming means making code that makes code. For example, in interpreted script
languages, it is often possible to make a piece of code that produces a string and theninterpret this string as code.
Metaprogramming can be useful in compiled languages such as C++ for doing some
calculations at compile time rather than at runtime if all the inputs to the calculations areavailable at compile time. Of course there is no such advantage in interpreted languages
where everything happens at runtime.
The following techniques can be considered metaprogramming in C++:•

Preprocessor directives. For example use #if instead of if. This is a very efficient
way of removing superfluous code, but there are serious limitations to what the
preprocessor can do because it comes before the compiler and it understands only
the simplest expressions and operators.

•

Make a C++ program that produces another C++ program (or part of it). This can be
useful in some cases, for example to produce tables of mathematical functions thatyou want as constant arrays in the final program. This requires, of course, that you
compile the output of the first program.

•An optimizing compiler may try to do as much as possible at compile time. For
example, all good compilers will reduce int x = 2 * 5; to int x = 10;•

Templates are instantiated at compile time. A template instance has its parameters
replaced by their actual values before it is compiled. This is the reason why there isvirtually no cost to using templates (see p. 58). It is theoretically possible to express
almost any algorithm with template metaprogramming, but this method is extremelycomplicated and clumsy, and it may take a long time to compile.

•

Compile time branches. if constexpr (boolean expression) {}. The
boolean expression inside the parenthesis can be any expression containing values
known at compile time. The code inside the {} will only be included in the final
program if the bool is true. The code in a false branch is removed, but it still has tobe syntactically correct. The syntax check is less strict if it occurs inside a template.
This feature is useful for deciding which version of a code to use. The ifconstexpr feature is less useful for conditional declarations because the scope ofthe declaration is limited to the {}. Compile time branches require that the C++17
standard is activated in the compiler. See page 166 for details.

•

constexpr functions. A constexpr function can do arbitrary calculations at compile
time as long as it is called with parameters known at compile time. See page 167
below. This requires that the C++14 standard or higher is activated in the compiler.

15.1 Template metaprogramming
The following examples explain how metaprogramming can be used to speed up thecalculation of the power function when the exponent is an integer known at compile time.
// Example 15.1a. Calculate x to the power of 10
double xpow10(double x) {return pow(x,10);}

163

The pow function uses logarithms in the general case, but in this case it may recognize that
10 is an integer, so that the result can be calculated using multiplications only. The following
algorithm is used inside the pow function when the exponent is a positive integer:// Example 15.1b. Calculate integer power using loop
double ipow (double x, unsigned int n) {double y = 1.0;// used for multiplication
while (n != 0) {// loop for each bit in nn
if (n & 1) y *= x;// multiply if bit = 1
x *= x;// square xn >>= 1;// get next bit of n}
return y;// return y = pow(x,n)}
double xpow10(double x) {return ipow(x,10);}// ipow faster than pow

The method used in example 15.1b is easier to understand when we roll out the loop andreorganize:// Example 15.1c. Calculate integer power, loop unrolled
double xpow10(double x) {double x2 = x *x;// x^2
double x4 = x2*x2;// x^4
double x8 = x4*x4;// x^8
double x10 = x8*x2;// x^10return x10;// return x^10}

As we can see, it is possible to calculate pow(x,10) with only four multiplications. How
was it possible to come from example 15.1b to 15.1c? We took advantage of the fact that n
is known at compile time to eliminate everything that depends only on n, including thewhile loop, the if statement and all the integer calculations. The code in example 15.1c isfaster than 15.1b, and in this case it may be smaller as well.
The conversion from example 15.1b to 15.1c was done by me manually, but if we want togenerate a piece of code that works for any compile-time constant n, then we needmetaprogramming. Only the best compilers will convert example 15.1a or 15.1b to 15.1c
automatically. Metaprogramming is useful for cases that the compilers cannot reduce
automatically.
The next example shows this calculation implemented with template metaprogramming.
Don't panic if it looks too complicated. Template metaprogramming can be very
complicated. Fortunately, the new C++17 standard provides an easier way as we will see in
the next chapter.
I am giving this example only to show how tortuous and convoluted template
metaprogramming can be.
// Example 15.1d. Integer power using template metaprogramming// Template for pow(x,N) where N is a positive integer constant.
// General case, N is not a power of 2:
template <bool IsPowerOf2, int N>
class powN {public:static double p(double x) {// Remove right-most 1-bit in binary representation of N:#define N1 (N & (N-1))

164

return powN<(N1&(N1-1))==0,N1>::p(x) * powN<true,N-N1>::p(x);#undef N1}};// Partial template specialization for N a power of 2
template <int N>
class powN<true,N> {public:static double p(double x) {return powN<true,N/2>::p(x) * powN<true,N/2>::p(x);}};// Full template specialization for N = 1. This ends the recursion
template<>class powN<true,1> {public:static double p(double x) {return x;}};// Full template specialization for N = 0// This is used only for avoiding infinite loop if powN is// erroneously called with IsPowerOf2 = false where it should be true.
template<>class powN<true,0> {public:static double p(double x) {return 1.0;}};// Function template for x to the power of N
template <int N>
static inline double integerPower (double x) {// (N & N-1)==0 if N is a power of 2
return powN<(N & N-1)==0,N>::p(x);}// Use template to get x to the power of 10
double xpow10(double x) {return integerPower<10>(x);}If you want to know how this works, here's an explanation. Please skip the following
explanation if you are not sure you need it.
In C++ template metaprogramming, loops are implemented as recursive templates. The
powN template is calling itself in order to emulate the while loop in example 15.1b.
Branches are implemented by (partial) template specialization. This is how the if branch inexample 15.1b is implemented. The recursion must always end with a non-recursing
template specialization, not with a branch inside the template.
The powN template is a class template rather than a function template because partial
template specialization is allowed only for classes. The splitting of N into the individual bitsof its binary representation is particularly tricky. I have used the trick that N1 = N&(N-1)
gives the value of N with the rightmost 1-bit removed. If N is a power of 2 then N&(N-1)
is 0. The constant N1 could have been defined in other ways than by a macro, but themethod used here is the only one that works on all the compilers I have tried.
165

Good compilers are actually reducing example 15.1d to 15.1c as intended because they caneliminate common sub-expressions.
Why is template metaprogramming so complicated? Because the C++ template feature wasnever designed for this purpose. It just happened to be possible. Template metaprogramming is so complicated that I consider it unwise to use it except in the simplest
cases. Complicated code is a risk factor in itself, and the cost of verifying, debugging, andmaintaining such code is so high that it rarely justifies the gain in performance.

15.2 Metaprogramming with constexpr branches
Fortunately, metaprogramming has become simpler with the C++17 standard, which
provides compile-time branches with the constexpr keyword.
The following example shows the same algorithm as example 15.1, using compile-time
branches.
// Example 15.2. Calculate integer power using C++17// Recursive template, used below// Calculates y * pow(x,n)
template <int n>inline double ipow_step (double x, double y) {if constexpr ((n & 1) == 1) {y *= x;// multiply if bit = 1}
constexpr int n1 = n >> 1;// get next bit of n
if constexpr (n1 == 0) {return y;// finished}else {// square x and continue recursion
return ipow_step<n1>(x * x, y);}}// Efficient calculation of pow(x,n)
template <int n>double integerPower (double x) {if constexpr (n == 0) {return 1.;// pow(x,0) = 1}
else if constexpr (n < 0) {// x is negative
if constexpr ((unsigned int)n == 0x80000000u) {// -n overflows
return 0.;}// pow(x,n) = 1/pow(x,-n)
return 1. / integerPower<-n>(x);}// loop through recursion
return ipow_step<n>(x, 1.);}

Here, we still need a recursive template to roll out the loop, but it is easier to make branches
and to end the recursion. A good compiler will reduce example 15.2 to just the multiplication
instructions and nothing else. The not-taken branches will not be included in the final code.
Before compile-time branches were introduced with C++17, we had the problem that atemplate in a not-taken branch would be expanded, even if it was never used. This could
166

lead to infinite recursion or an exponentially growing number of unused branches. The only
way to end a template recursion prior to C++17 was with template specialization, as inexample 15.1d. A recursive template could take a long time to compile if it contained manybranches.
Compile time branches are more efficient, because a template in a not-taken branch will not
be expanded.

15.3 Metaprogramming with constexpr functions
A constexpr function is a function that can do almost any calculations at the time ofcompilation if the parameters are compile time constants. With the C++14 standard, you canhave branches, loops, etc. in a constexpr function.
This example finds the position of the most significant 1-bit in an integer. This is the sameas the bit scan reverse instruction, but calculated at compile time:// Example 15.3. Find most significant bit, using constexpr function
constexpr int bit_scan_reverse (uint64_t const n) {if (n == 0) return -1;
uint64_t a = n, b = 0, j = 64, k = 0;
do {j >>= 1;k = (uint64_t)1 << j;if (a >= k) {a >>= j;b += j;}} while (j > 0);
return int(b);}A good optimizing compiler will do simple calculations at compile time anyway, if all the
inputs are known constants, but few compilers are able to do more complicated calculations
at compile time if they involve branches, loops, function calls, etc. A constexpr function
can be useful to make sure that certain calculations are done at compile time.
The result of a constexpr function can be used wherever a compile time constant isrequired, for example for an array size or in a compile time branch.
While C++14 and C++17 provide important improvements to the metaprogramming
possibilities, there are still things that you cannot do with the C++ language. For example, it
is not possible to make a compile time loop that repeats ten times to generate ten functions
named func1, func2, ..., func10. This is possible with certain script languages andassemblers.

16 Testing speed
Testing the speed of a program is an important part of the optimization job. You have tocheck if your modifications actually increase the speed or not.
There are various profilers available which are useful for finding the hot spots andmeasuring the overall performance of a program. The profilers are not always accurate,
however, and it may be difficult to measure exactly what you want when the program
spends most of its time waiting for user input or reading disk files. See page 16 for a
discussion of profiling.
167

When a hot spot has been identified, then it may be useful to isolate the hot spot and makemeasurements on this part of the code only. This can be done with the resolution of the
CPU clock by using the so-called time stamp counter. This is a counter that measures thenumber of clock pulses since the CPU was started. The length of a clock cycle is thereciprocal of the clock frequency, as explained on page 15. If you read the value of the timestamp counter before and after executing a critical piece of code then you can get the exact
time consumption as the difference between the two clock counts.
The value of the time stamp counter can be obtained with the function ReadTSC listed
below in example 16.1. This code works only for compilers that support intrinsic functions.
Alternatively, you can use the header file timingtest.h fromwww.agner.org/optimize/testp.zip or get ReadTSC as a library function fromwww.agner.org/optimize/asmlib.zip.
// Example 16.1
#include <intrin.h>long long ReadTSC() {int dummy[4];volatile int DontSkip;long long clock;__cpuid(dummy, 0);
DontSkip = dummy[0];clock = __rdtsc();return clock;}// Or #include <ia32intrin.h> etc.
// Returns time stamp counter// For unused returns// Volatile to prevent optimizing// Time// Serialize// Prevent optimizing away cpuid// Read time

You can use this function to measure the clock count before and after executing the criticalcode. A test setup may look like this:// Example 16.2
#include <stdio.h>#include <asmlib.h>void CriticalFunction();// Use ReadTSC() from library asmlib..
// or from example 16.1// This is the function we want to measure...
const int NumberOfTests = 10;// Number of times to test
int i; long long time1;
long long timediff[NumberOfTests];// Time difference for each testfor (i = 0; i < NumberOfTests; i++) { // Repeat NumberOfTests times
time1 = ReadTSC();// Time before test

CriticalFunction();// Critical function to test

timediff[i] = ReadTSC() - time1;// (time after) - (time before)}
printf("\nResults:");// Print headingfor (i = 0; i < NumberOfTests; i++) { // Loop to print out results
printf("\n%2i %10I64i", i, timediff[i]);}The code in example 16.2 calls the critical function ten times and stores the time
consumption of each run in an array. The values are then output after the test loop. The
time that is measured in this way includes the time it takes to call the ReadTSC function.
You can subtract this value from the counts. It is measured simply by removing the call to
CriticalFunction in example 16.2.
168

The measured time is interpreted in the following way. The first count is usually higher than
the subsequent counts. This is the time it takes to execute CriticalFunction when codeand data are not cached. The subsequent counts give the execution time when code anddata are cached as good as possible. The first count and the subsequent counts represent
the “worst case” and “best case” values. Which of these two values is closest to the truth
depends on whether CriticalFunction is called once or multiple times in the final
program and whether there is other code that uses the cache in between the calls to
CriticalFunction. If your optimization effort is concentrated on CPU efficiency then it is
the “best case” counts that you should look at to see if a certain modification is profitable.
On the other hand, if your optimization effort is concentrated on arranging data in order toimprove cache efficiency, then you may also look at the “worst case” counts. In any event,the clock counts should be multiplied by the clock period and by the number of times
CriticalFunction is called in a typical application to calculate the time delay that theend user is likely to experience.
Occasionally, the clock counts that you measure are much higher than normal. This
happens when a task switch occurs during execution of CriticalFunction. You cannot
avoid this in a protected operating system, but you can reduce the problem by increasing
the thread priority before the test and setting the priority back to normal afterwards.
The clock counts are often fluctuating and it may be difficult to get reproducible results. Thisis because modern CPUs can change their clock frequency dynamically depending on thework load. The clock frequency is increased when the work load is high and decreased
when the work load is low in order to save power. There are various ways to get more
reproducible time measurements:•••

warm up the CPU by giving it some heavy work to do immediately before the code totest.
disable power-save options in the BIOS setup.
on Intel CPUs: use the core clock cycle counter (see below)

16.1 Using performance monitor counters
Many CPUs have a built-in test feature called performance monitor counters. A performancemonitor counter is a counter inside the CPU which can be set up to count certain events,
such as the number of machine instructions executed, cache misses, branchmispredictions, etc. These counters can be very useful for investigating performance
problems. The performance monitor counters are CPU-specific and each CPU model has its
own set of performance monitoring options.
CPU vendors are offering profiling tools that fit their CPUs. Intel's profiler is called VTune;
AMD's profiler is called CodeAnalyst. These profilers are useful for identifying hot spots inthe code.
For my own research, I have developed a test tool for using the performance monitor
counters. My test tool supports both Intel, AMD and VIA processors, and it is available fromwww.agner.org/optimize/testp.zip. This tool is not a profiler. It is not intended for finding hot
spots, but for studying a piece of code once the hot spots have been identified.
My test tool can be used in two ways. The first way is to insert the piece of code to test in
the test program itself and recompile it. I am using this for testing single assembly
instructions or small sequences of code. The second way is to set up the performancemonitor counters before running a program you want to optimize, and reading theperformance counters inside your program before and after the piece of code you want totest. You can use the same principle as in example 16.2 above, but read one or more
performance monitor counters instead of (or in addition to) the time stamp counter. The test
tool can set up and enable one or more performance monitor counters in all the CPU cores
169

and leave them enabled (there is one set of counters in each CPU core). The counters will
stay on until you turn them off or until the computer is reset or goes into sleep mode. See
the manual for my test tool for details (www.agner.org/optimize/testp.zip).
A particularly useful performance monitor counter in Intel processors is called core clock
cycles. The core clock cycles counter is counting clock cycles at the actual clock frequency
that the CPU core is running at, rather than the external clock. This gives a measure that is
almost independent of changes in the clock frequency. The core clock cycle counter is very
useful when testing which version of a piece of code is fastest because you can avoid the
problem that the clock frequency goes up and down.
Remember to insert a switch in your program to turn off the reading of the counters when
you are not testing. Trying to read the performance monitor counters when they are
disabled will crash the program.

16.2 The pitfalls of unit-testing
It is common practice to test each function or class separately in software development.
This unit-testing is necessary for verifying the functionality of an optimized function, but
unfortunately the unit-test does not give the full information about the performance of thefunction in terms of speed.
Assume that you have two different versions of a critical function and you want to find out
which one is fastest. The typical way to test this is to make a small test program that calls
the critical function many times with a suitable set of test data and measure how long time ittakes. The version that performs best under this unit-test may have a larger memory
footprint than the alternative version. The penalty of cache misses is not seen in the unittest because the total amount of code and data memory used by the test program is likely tobe less than the cache size.
When the critical function is inserted in the final program, it is very likely that code cache,micro-op cache, and data cache are critical resources. Modern CPUs are so fast that theclock cycles spent on executing instructions are less likely to be a bottleneck than memory
access and cache size. If this is the case then the optimal version of the critical function
may be the one that takes longer time in the unit-test but has a smaller memory footprint.
If, for example, you want to find out whether it is advantageous to roll out a big loop then
you cannot rely on a unit-test without taking cache effects into account.
You can calculate how much memory a function uses by looking at a link map or an
assembly listing. Use the “generate map file” option for the linker. Both code cache use anddata cache use can be critical. The branch target buffer is also a cache that can be critical.
Therefore, the number of jumps, calls, and branches in a function should also be
considered.
A realistic performance test should include not only a single function or hot spot but also theinnermost loop that includes the critical functions and hot spots. The test should be
performed with a realistic set of data in order to get reliable results for branchmispredictions. The performance measurement should not include any part of the programthat waits for user input. The time used for file input and output should be measured
separately.
The fallacy of measuring performance by unit-testing is unfortunately very common. Evensome of the best optimized function libraries available use excessive loop unrolling so thatthe memory footprint is unreasonably large.

170

16.3 Worst-case testing
Most performance tests are done under the best-case conditions. All disturbing influences
are removed, all resources are sufficient, and the caching conditions are optimal. Best-casetesting is useful because it gives more reliable and reproducible results. If you want tocompare the performance of two different implementations of the same algorithm, then you
need to remove all disturbing influences in order to make the measurements as accurate
and reproducible as possible.
However, there are cases where it is more relevant to test the performance under the worstcase conditions. For example, if you want to make sure that the response time to user input
never exceeds an acceptable limit, then you should test the response time under worst-caseconditions.
Programs that produce streaming audio or video should also be tested under worst-caseconditions in order to make sure that they always keep up with the expected real-time
speed. Delays or glitches in the output are unacceptable.
Each of the following methods could possibly be relevant when testing worst-caseperformance:•

The first time you activate a particular part of the program, it is likely to be slower
than the subsequent times because of lazy loading of the code, cache misses, andbranch mispredictions.

•

Test the whole software package, including all runtime libraries and frameworks,
rather than isolating a single function. Switch between different parts of the software
package in order to increase the likelihood that certain parts of the program code areuncached or even swapped to disk.

•

Software that relies on network resources and servers should be tested on a networkwith heavy traffic and a server in full use rather than a dedicated test server.

•

Use large data files and databases with lots of data.

•

Use an old computer with a slow CPU, an insufficient amount of RAM, a lot of
irrelevant software installed, a lot of background processes running, and a slow andfragmented hard disk.

•

Test with different brands of CPUs, different types of graphics cards, etc.

•

Use an antivirus program that scans all files on access.

•Run multiple processes or threads simultaneously. If the microprocessor has
hyperthreading, then try to run two threads in the same processor core.

•

Try to allocate more RAM than there is, in order to force the swapping of memory to
disk.

•

Provoke cache misses by making the code size or data used in the innermost loop
bigger than the cache size. Alternatively, you may actively invalidate the cache. The
operating system may have a function for this purpose, or you may use the_mm_clflush intrinsic function.

•

Provoke branch mispredictions by making the data more random than normal.

171

17 Optimization in embedded systems
Microcontrollers used in small embedded applications have less computing resources thanstandard PCs. The clock frequency may be a hundred or even a thousand times lower; andthe amount of RAM memory may even be a million times less than in a PC. Nevertheless, it
is possible to make software that runs quite fast on such small devices if you avoid the large
graphics frameworks, interpreters, just-in-time compilers, system database, and other extra
software layers and frameworks typically used on bigger systems.
The smaller the system, the more important it is to choose a software framework that uses
few resources. On the smallest devices, you do not even have an operating system.
The best performance is obtained by choosing a programming language that can be crosscompiled on a PC and then transferred as machine code to the device. Any language that
requires compilation or interpretation on the device itself is a big waste of resources. For
these reasons, the preferred language will often be C or C++. Critical device drivers mayneed assembly language.
C++ takes only slightly more resources than C if you follow the guidelines below. You may
choose either C or C++ based on what is most appropriate for the desired program
structure.
It is important to economize the use of RAM memory. Big arrays should be declared inside
the function they are used in so that they are deallocated when the function returns.
Alternatively, you may reuse the same array for multiple purposes.
All dynamic memory allocation using new/delete or malloc/free should be avoided
because of the large overhead of managing a memory heap. The heap manager has agarbage collector which is likely to consume time at unpredictable intervals which may
interfere with real time applications.
Remember that standard C++ container classes use dynamic memory allocation with new
and delete, and often excessively so. These containers should definitely be avoided
unless you have ample resources. For example, a FIFO queue should be implemented as a
circular buffer with fixed size to avoid dynamic memory allocation. Do not use a linked list
(see page 97).
All common implementations of string classes use dynamic memory allocation. You should
avoid these and handle text strings in the old fashioned C style as character arrays. Notethat the C style string functions have no check for overflow of the arrays. It is the
responsibility of the programmer to make sure the arrays are sufficiently large to handle thestrings including the terminating zero and to make overflow checks where necessary (seepage 105).
Virtual functions in C++ take more resources than non-virtual functions. Avoid virtualfunctions if possible.
Smaller microprocessors have no native floating point execution units. Any floating point
operation on such processors requires a big floating point library which is very time
consuming. Therefore, you should avoid any use of floating point expressions. For example,
a = b * 2.5 may be changed to a = b * 5 / 2 (be aware of possible overflow on
the intermediate expression b * 5). As soon as you have even a single constant with adecimal point in your program, you will be loading the entire floating point library. If you want
a number to be calculated with two decimals, for example, you should multiply it by 100 sothat it can be represented as an integer.
Integer variables can be 8, 16 or 32 bits (rarely 64). You may save RAM space, if
necessary, by using the smallest integer size that does not cause overflow in the particular172

application. The integer size is not standardized across platforms. See the compiler
documentation for the size of each integer type.
Interrupt service routines and device drivers are particularly critical because they can block
the execution of everything else. This normally belongs to the area of system programming,
but in applications without an operating system this is the job of the application programmer.
There is a higher risk that the programmer forgets that the system code is critical when
there is no operating system, and therefore the system code is not separated from theapplication code. An interrupt service routine should do as little work as possible. Typically it
should save one unit of received data in a static buffer or send data from a buffer. It should
never respond to a command or do other input/output than the specific event it is servicing.
A command received by an interrupt should preferably be responded to at a lower priority
level, typically in a message loop in the main program. See page 162 for further discussionof system code.
In this chapter, I have described some of the considerations that are particularly importanton small devices with limited resources. Most of the advice in the rest of the present manualis also relevant to small devices, but there are some differences due to the design of small
microcontrollers:•

Smaller microcontrollers have no branch prediction (see p. 43). There is no need totake branch prediction into account in the software.

•

Smaller microcontrollers have no cache (see p. 91). There is no need to organize
data to optimize caching.

•

Smaller microcontrollers have no out-of-order execution. There is no need to break
down dependency chains (see p. 21).

173

18 Overview of compiler options
Table 18.1. Command line options relevant to optimization
MS compiler
Windows

Gnu and ClangcompilerIntel compiler
Windows

Optimize for speed
Interprocedural
optimization
Whole program
optimization
No exception
handling
No f.p. exception
trapping/O2 or /Ox/Og-O3 or -Ofast

/O3

/GL

--combine/Qipo-fwhole-program

No stack frame/OyNo runtime typeidentification(RTTI)
Assume no pointeraliasing
Non-strict floating
point

/GR–

Intel
compiler
Linux
-O3

-ipo

/EHs/fp:except--fno-trappingmath-fno-math-errno-fomit-framepointer-fno-rtti

/GR-

/Oa
/fp:fast-fomitframepointer-fno-rtti-fno-alias-ffast-math/fp:fast/fp:fast=2-fp-model
fast, -fpmodel
fast=2-ffp-contract=fast-freciprocalmath

Fused multiply-add
Division by
constant = mul by
reciprocal
Assume nooverflow or NAN
Ignore sign of zero
Simple member
pointers/vms

Fastcall functions
Vectorcall
functions
Function levellinking (remove
unreferenced
functions)
SSE instruction set(128 bit float
vectors)
SSE2 instruction
set (128 vectors ofinteger or double)
SSE3 instruction
set

/Gr
/Gv

-ffinite-mathonly-fno-signedzeros-fno-completemember-pointers
/vms

/Gy

-ffunctionsections/Gy

ffunctionsections/arch:SSE-msse/arch:SSE-msse/arch:SSE2-msse2/arch:SSE2-msse2-msse3/arch:SSE3-msse3174

Suppl. SSE3 instr.
set
SSE4.1 instr. set/arch:AVX
AVX instr. set/arch:AVX2
AVX2 instr. set
AVX512F instr. set
AVX512VL/BW/DQ /arch:AVX512
instr. set
Automatic
vectorization
Automatic parallelization by multiple
threads
Parallelization by
OpenMP directives
32 bit code
64 bit code
Static linking(multithreaded)
Generate
assembly listing
Generate map file
Generate
optimization report/fp:fast/fp:except--mssse3/arch:SSSE3-mssse3-msse4.1-mAVX-mAVX2-mavx512f-mavx512vl-mavx512bw-mavx512dq-O2 -fnotrapping-math fno-math-errno
fveclib=libmvec/arch:SSE4.1/arch:AVX/arch:AVX-msse4.1-mAVX-mAVX2/arch:COREAVX512-mavx512vl-mavx512bw-mavx512dq/Qparallel-parallel/openmp-fopenmp/Qopenmp-openmp

/MT

-m32
-m64
-static/MT-static/FA-S -masm=intel

/FA

-S

/Qopt-report-optreport/Fm

Various floating point options represent different compromises between speed andprecision. This includes the following:-ffp-contract=fast: a*b+c is converted to a fused multiply-and-add instruction. Theintermediate (a*b) is calculated with unlimited precision.
-freciprocal-math: Dividing by a constant is replaced by multiplying with the reciprocal.
This improves speed with a minimum effect on precision.
-fassociative-math: (a+b)+c = a+(b+c). This can have severe consequences for
precision. Example: float a = 1.E10, b = (1+a)-a; This gives 0 with -fnoassociative-math and 1 with -fassociative-math. The programmer may prefer touse parentheses to indicate which order of calculation is desired rather than using thisoption.
-ffinite-math-only: Optimizations that fail to propagate NAN and INF. For example: xx can be optimized to 0. This will be wrong if x = INF because INF-INF = NAN. Warning:
Detection of NAN and INF may be impossible with this option.
-fno-trapping-math: Necessary for automatic vectorization. x = 0.0/0.0 is reduced to
x=NAN, giving no trap.
-fno-math-errno: Does not make a trap that sets errno for sqrt etc. This is necessary
for automatic vectorization of branches containing sqrt.
-fno-signed-zeros: Enable optimizations that ignore the sign of zero, such asx*0. = 0 and x+0. = x.
175

Table 18.2. Compiler directives and keywords relevant to optimization
MS compiler
Align by 16
Align by 16(C++11)
Assumepointer isalignedAssumepointer notaliasedAssumefunction ispureAssumefunction
does notthrow
exceptions
Assumefunctioncalled only
from same
moduleAssumememberfunctioncalled only
from same
moduleVectorize
Optimizefunction
Fastcallfunction
Vectorcall
function
Noncachedwrite__declspec(align(16))
alignas(16)#pragma
optimize("a",on)__restrict

Gnu and Clangcompiler__attribute((aligned(16)))alignas(16)__restrict

Intel compiler
Windows__declspec(align(16))
alignas(16)Intel compilerLinux__attribute((aligned(16)))alignas(16)#pragma vectoraligned#pragma vectoraligned__declspec(
noalias)__restrict#pragma ivdep__restrict#pragma ivdep__attribute((
const))__attribute((
const))

throw()

throw()

throw()

throw()

staticstaticstaticstatic__attribute__((visibility("internal")))__attribute__((visibility("internal")))#pragma vector
always#pragma
optimize(...)__fastcall__vectorcall__attribute((
fastcall))__vectorcall(Clang only)__fastcall__vectorcall#pragma vector
nontemporal176#pragma vector
always#pragma vector
nontemporalTable 18.3. Predefined macros

Compiler
identification

MS compiler
Windows

Gnu
compilerClangcompilerIntel
compiler
Windows

Intel
compiler
Linux_MSC_VER and__GNUC__ and__clang__

not

not

__INTEL_COMPILER or__INTEL_LLVM_COMPILER__INTEL_COMPILER or__INTEL_LLVM_COMPILER

n.a.
not _WIN64_WIN64_WIN32n.a.

__INTEL_COMPILER__INTEL_COMPILER and not__clang__

16 bit platform
32 bit platform
64 bit platform
Windows
platform
Linux platform

not _WIN32
not _WIN64_WIN64_WIN32

n.a.

n.a.

_LP64

_LP64

n.a.

__unix____linux____unix____linux__x86 platformx86-64platform_M_IX86_M_IX86 and_WIN64__unix____linux___M_IX86_M_X64

177_LP64_M_X64

19 Literature
Other manuals by Agner Fog
The present manual is number one in a series of five manuals. See page 3 for a list of titles.

Literature on code optimization
Intel: "Intel 64 and IA-32 Architectures Optimization Reference Manual".
developer.intel.com.
Many advices on optimization of C++ and assembly code for Intel CPUs. New
versions are produced regularly.
AMD: "Software Optimization Guide for AMD Family 15h Processors". www.amd.com.
Advices on optimization of C++ and assembly code for AMD CPUs. New versions
are produced regularly.
Intel: "Intel® C++ Compiler Documentation". Included with Intel C++ compiler, which isavailable from www.intel.com.
Manual on using the optimization features of Intel C++ compilers.
Wikipedia article on compiler optimization. en.wikipedia.org/wiki/Compiler_optimization.
ISO/IEC TR 18015, "Technical Report on C++ Performance". www.openstd.org/jtc1/sc22/wg21/docs/TR18015.pdf.
OpenMP. www.openmp.org. Documentation of the OpenMP directives for parallel
processing.
Scott Meyers: "Effective C++". Addison-Wesley. Third Edition, 2005; and "More Effective
C++". Addison-Wesley, 1996.
These two books contain many tips on advanced C++ programming, how to avoid
hard-to-find errors, and some tips on improving performance.
Stefan Goedecker and Adolfy Hoisie: "Performance Optimization of Numerically Intensive
Codes", SIAM 2001.
Advanced book on optimization of C++ and Fortran code. The main focus is on
mathematical applications with large data sets. Covers PC's, workstations andscientific vector processors.
Henry S. Warren, Jr.: "Hacker's Delight". Addison-Wesley, 2003.
Contains many bit manipulation tricks
Michael Abrash: "Zen of code optimization", Coriolis group books 1994.
Mostly obsolete.
Rick Booth: "Inner Loops: A sourcebook for fast 32-bit software development", AddisonWesley 1997.
Mostly obsolete.

Microprocessor documentation
Intel: "IA-32 Intel Architecture Software Developer’s Manual", Volume 1, 2A, 2B, and 3A and3B. developer.intel.com.
AMD: "AMD64 Architecture Programmer’s Manual", Volume 1 - 5. www.amd.com.
178

Internet forums
Several internet forums and newsgroups contain useful discussions about code
optimization. See www.agner.org/optimize and the FAQ for the newsgroupcomp.lang.asm.x86 for some links.

20 Copyright noticeThis series of five manuals is copyrighted by Agner Fog. Public distribution and mirroring is
not allowed. Non-public distribution to a limited audience for educational purposes isallowed. The code examples in these manuals can be used without restrictions. A creative
commons license CC-BY-SA shall automatically come into force when I die. See
https://creativecommons.org/licenses/by-sa/4.0/legalcode

179