Home / Forums / Branchless programming techniques in C++ using conditional moves (CMOV) [Part 5]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Branchless programming techniques in C++ using conditional moves (CMOV) [Part 5]

ClangTidyFan
Modern Code Quality
MEMBER
Rep: 94
Join Date: Oct 2023
Posts: 6
Thanks: 16
8mo ago · Dec 7, 2025 1:41 PM
#1
Branch mispredictions on modern pipelined CPUs cost 15 to 20 clock cycles. Branchless code uses arithmetic bitwise operations or ternary expressions that compile to CMOV instructions instead of jmp/je branches.
ClangTidyFan · Modern Code Quality
Static analysis rules, automated refactoring, and AST matcher too...
The following users thanked ClangTidyFan for this post:
FiberRunner
User-Mode Scheduling
MEMBER
Rep: 104
Join Date: Mar 2022
Posts: 6
Thanks: 15
8mo ago · Dec 7, 2025 2:59 PM
#2
For example: int maxVal = (a > b) ? a : b; compiles to a single cmp followed by cmovg on x86_64, eliminating branch misprediction penalties completely.
FiberRunner · User-Mode Scheduling
Fiber context switching, cooperative multitasking, and green thre...
AtomicFences
Memory Consistency
MEMBER
Rep: 419
Join Date: Nov 2024
Posts: 6
Thanks: 80
8mo ago · Dec 7, 2025 5:59 PM
#3
Branchless algorithms on sorted data or random game entity flags boost throughput tremendously in tight loops.
AtomicFences · Memory Consistency
Hardware fence instructions, Store-Load reordering, and Peterson ...