Conditional select instructions

So far, we have seen examples that use branches to handle decisions. The A64 instruction set also provides conditional select instructions. In many cases, these instructions can be used as an alternative to branches.

There are many variants, but the basic form is:

CSEL	Xd, Xn, Xm, cond

This means that:

if cond then
  Xd = Xn
else
  Xd = Xm

You can see an example in this code:

CMP	W1, #0
CSEL W5, W6, W7, EQ

Which gives the same result as:

if (W1==0) then
  W5 = W6
 else
  W5 = W7

There are variants that combine another operation with the conditional select. For example, CSINC performs a select and addition:

CSINC	Xd, Xn, Xm, cond

This means that:

 if cond then
  Xd = Xn
 else
  Xd = Xm + 1

To just conditionally increment, you could write:

 CSINC	X0, X0, X0, cond

Which equates to:

if cond then
  X0 = X0
 else
  X0 = X0 + 1

However, the architecture provides an alias, CINC, which is the equivalent to this command.

Compilers choose the most efficient method to implement the functionality in your program. Compilers will often use a conditional select for small if ... else statements performing simple operations, because conditional selects can be more efficient than branches.

Here are some simple if ... else examples that compare implementations using branches to equivalent implementations using conditional select instructions:

C Branching Conditional select
if (a != 0)
  b = b + 1;
 CMP W0, #0
 B.EQ else
 ADD W1, W1, #1
else:
 …
CMP W0, #0
CINC W0, W1, NE
if (a == 0)
  y = y + 1;
else
  y = y - 1;
 CMP W0, #0
 B.NE else
 ADD W1, W1, #1
 B end
else:
 SUB W1, W1, #1
end:
 …
 CMP W0, #0
 SUB W2, W1, #1
 CSINC W1, W2, W1, NE

In these types of examples, conditional selects have some advantages. The sequences are shorter and take the same number of instructions, regardless of the outcome.

Importantly, conditional selects also remove the need to branch. In modern processors, this kind of branch can be difficult for the branch prediction logic to predict correctly. A mispredicted branch can have a negative effect on performance, it is better that you remove branches where possible.

Previous Next