20
Consider Expression Order in Compound Branch Conditions
AMD Athlon™ Processor x86 Code Optimization
22007E/0—November 1999
Consider Expression Order in Compound Branch
Conditions
B ra n ch c o n d it i o n s i n C p rog ra m s a re o f t e n c o m po u n d
conditions consisting of multiple boolean expressions joined by
the boolean operators && and ||. C guarantees a short-circuit
evaluation of these operators. This means that in the case of ||,
the fi rs t operan d to eval uate to TRUE term inates the
evaluation, i.e., following operands are not evaluated at all.
Similarly for &&, the first operand to evaluate to FALSE
terminates the evaluation. Because of this short-circuit
evaluation, it is not always possible to swap the operands of ||
and &&. This is especially the case when the evaluation of one
of the operands causes a side effect. However, in most cases the
exchange of operands is possible.
When used to control conditional branches, expressions
involving || and && are translated into a series of conditional
branches. The ordering of the conditional branches is a function
of the ordering of the expressions in the compound condition,
and can have a significant impact on performance. It is
unfortunately not possible to give an easy, closed-form formula
on how to order the conditions. Overall performance is a
function of a variety of the following factors:
■
probability of a branch mispredict for each of the branches
generated
■
additional latency incurred due to a branch mispredict
■
cost of evaluating the conditions controlling each of the
branches generated
■
amount of parallelism that can be extracted in evaluating
the branch conditions
■
data stream consumed by an application (mostly due to the
dependence of mispredict probabilities on the nature of the
incoming data in data dependent branches)
It is therefore recommended to experiment with the ordering of
expressions in compound branch conditions in the most active
areas of a program (so called hot spots) where most of the
execution time is spent. Such hot spots can be found through
the use of profiling. A "typical" data stream should be fed to
the program while doing the experiments.
Summary of Contents for Athlon Processor x86
Page 1: ...AMD Athlon Processor x86 Code Optimization Guide TM...
Page 12: ...xii List of Figures AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 16: ...xvi Revision History AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 202: ...186 Page Attribute Table PAT AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 252: ...236 VectorPath Instructions AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 256: ...240 Index AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...