Chapter 2 C and C++ Source-Level Optimizations 27
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
To avoid a comparison chain and its undesirable effects on branch prediction, replace the switch
statement with a series of if-else statements, as follows:
if (a==8) {
// Sequence for a==8
}
else if (a==16) {
// Sequence for a==16
}
...
else {
// Default sequence
}
Related Information
For information on preventing branch-prediction interference at the assembly-language level, see
“Density of Branches” on page 126.