THE WEIRD CONCEPT OF BRANCHLESS PROGRAMMING
Note
I studied branch prediction awhile back. it's a beautiful art hammered into hardware that has powered so much cou architecture innovation.
it's fun to see how much works and hoops and hurdles one must jump through to eek out performance when branch prediction gets it wrong just enough.
I wonder if the field of branch prediction has much more to go or if these optimizations have made enough tradeoffs that there isn't more juice to squeeze?
Published
2025
Summary
Branchless programming is a technique that rewrites conditional logic (like if/else statements) into arithmetic and bit operations to avoid CPU branches. Modern CPUs rely on branch prediction for performance, but mispredictions in unpredictable scenarios (e.g., user input, shuffled arrays) can significantly slow down execution. By eliminating branches, this method can lead to smoother, faster, and more deterministic code, especially beneficial for hot loops, cryptographic operations, and SIMD/vectorization. While improving performance, it can make code less readable and turn logic into 'incomprehensible bit spaghetti' if misused.
Image

