Branchless Programming in C++
summary
Concrete code transformations and measurements determine value: a simple if (b) s += x change yields 2.5x speedup on the presented machine; a poorly predicted if (a[i] && b[i]) case runs about 4x slower than a branchless rewrite. Benchmarks use perf + Google Benchmark and show branch-mispredictions dropping from ~10% mispredict to <0.1%, with wins from replacing short‑circuit logic with bitwise/arithmetic or using boolean-indexed arrays when the overall outcome is predictable.
AI-written summary · about
chapters (14)
- 0:00Hardware and pipelining
- 10:11Data dependencies
- 14:00Branch prediction
- 17:21Register aliasing
- 20:41Branch misprediction cost
- 23:10Branchless programming
- 25:23Misprediction benchmarks
- 30:42The CPU predictor
- 34:03Q&A and mitigations
- 38:22Logical branch elimination
- 43:23Branchless arithmetic
- 52:11Array indexing optimization
- 55:28Compiler optimization limits
- 1:00:16Summary and Q&A








