25112 Rev. 3.06 September 2005

Software Optimization Guide for AMD64 Processors

Chapter 2 C and C++ Source-Level

Optimizations

Although C and C++ compilers generally produce very compact object code, many performance improvements are possible by careful source code optimization. Most such optimizations result from taking advantage of the underlying mechanisms used by C and C++ compilers to translate source code into sequences of AMD64 instructions. This chapter includes guidelines for writing C and C++ source code that result in the most efficiently optimized AMD64 code.

This chapter covers the following topics:

Topic

Page

 

 

Declarations of Floating-Point Values

9

 

 

Using Arrays and Pointers

10

 

 

Unrolling Small Loops

13

 

 

Expression Order in Compound Branch Conditions

14

 

 

Long Logical Expressions in If Statements

16

 

 

Arrange Boolean Operands for Quick Expression Evaluation

17

 

 

Dynamic Memory Allocation Consideration

19

 

 

Unnecessary Store-to-Load Dependencies

20

 

 

Matching Store and Load Size

22

 

 

SWITCH and Noncontiguous Case Expressions

25

 

 

Arranging Cases by Probability of Occurrence

28

 

 

Use of Function Prototypes

29

 

 

Use of const Type Qualifier

30

 

 

Generic Loop Hoisting

31

 

 

Local Static Functions

34

 

 

Explicit Parallelism in Code

35

 

 

Extracting Common Subexpressions

37

 

 

Sorting and Padding C and C++ Structures

39

 

 

Sorting Local Variables

41

 

 

Replacing Integer Division with Multiplication

43

 

 

Frequently Dereferenced Pointer Arguments

44

 

 

Array Indices

46

 

 

32-Bit Integral Data Types

47

 

 

Sign of Integer Operands

48

 

 

Chapter 2

C and C++ Source-Level Optimizations

7

Page 23
Image 23
AMD 250 manual C++ Source-Level Optimizations