liveBook · Manning

Conditional move

As explained in Hacking Cryptography

A conditional move is a processor instruction that transfers a value when a Boolean predicate holds without branching to a different instruction path.

A conditional move transfers a value only when a Boolean predicate holds, while executing the same instruction sequence whether the predicate is true or false. After CMP, CMOVG moves its source to the destination when the compared value is greater. This avoids branch-path divergence and removes the processor’s need to predict a direction. section 10.5

Summing values above a threshold

A branching loop adds array[i] only when array[i] > threshold. A branchless replacement initializes increment to zero, compares array[i] with threshold, and uses CMOVG to replace increment with the element when the predicate holds. On a 100-million-element array, the unsorted branchless run takes 0.379 seconds versus 0.915 seconds with branching; after sorting, branching takes 0.302 seconds versus 0.362 seconds branchless.

How Hacking Cryptography covers it

How deep it goes
Explains CMP and CMOVG semantics, supplies GCC assembly, and benchmarks branching against branchless execution.
Examples
Uses threshold-based array summation, GCC inline assembly, random and sorted inputs, and measured execution times.
What it assumes
Assumes familiarity with C loops and comparisons while defining predicates, CMP, CMOVG, and branch prediction in context.
What you can do after
Apply conditional moves via GCC inline assembly to replace a data-dependent branch with a branchless update.
How it connects
Connects conditional moves to branching, branch prediction, branchless programming, and constant-time execution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.