16 C and C++ Source-Level Optimizations Chapter 2
25112 Rev. 3.06 September 2005
Software Optimization Guide for AMD64 Processors
2.5 Long Logical Expressions in If Statements
Optimization
In if statements, avoid long logical expressions that can generate dense conditional branches that
violate the guideline described in “Density of Branches” on page 126.
Application
This optimization applies to:
• 32-bit software
• 64-bit software
Rationale
Listing 1. Preferred for Data that Falls Mostly Within the Range
if (a <= max && a >= min && b <= max && b >= min)
If most of the data falls within the range, the branches will not be taken, so the above code is
preferred. Otherwise, the following code is preferred.
Listing 2. Preferred for Data that Does Not Fall Mostly Within the Range
if (a > max || a < min || b > max || b < min)