Consider Expression Order In Compound Branch Conditions - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization
Consider Expression Order in Compound Branch
Conditions
20
Branch c ondit ions in C prog rams are oft en com pound
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 first operand to evaluate 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.

Consider Expression Order in Compound Branch Conditions

22007E/0—November 1999

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Athlon Processor x86 and is the answer not in the manual?

Questions and answers

Table of Contents