Source
- URL: https://x.com/nicbarkeragain/status/1947506500194472221
- Author: Nic Barker (@nicbarkeragain)
- Posted: 2025-07-22 03:58:27
Thread
1/ @nicbarkeragain
“Branchless Programming” is a term used to describe a programming style that improves performance by avoiding branches (e.g. if statements and similar). One of the most common branchless tricks is to use multiplication in place of an if statement:
Branches: 2025-07-22-sreekotay-id-usually-use-or-but-same-idea, 2025-07-22-anicic_filip-a-more-compact-example-is-using-a-bitmask-integer, 2025-07-22-VPCOMPRESSB-im-branch-free-by-default-its-not-for, 2025-07-22-la_ledda-i-have-no-idea-if-it-improves-performance-in-the, 2025-07-22-viertausend1-great-post-which-color-scheme-font-is-used-in-the, 2025-07-22-gonenb-looks-pretty-useless-to-me-in-this-case-if-you, 2025-07-22-wcolefranks-1-if-x-else-0, 2025-07-22-augeeidos-its-also-a-key-component-in-developing-good, 2025-07-22-JustSlavic-one-additional-read-instead-of-using-the-result, 2025-07-22-RussellPolo-a-really-long-time-ago-programming-a-simple-game, 2025-07-22-macsmol-australia-represent-https-www-youtube-com-watch-v, 2025-07-22-stuartcarnie-it-also-matters-how-random-the-data-is-if-the, 2025-07-22-macsmol-you-nerd-sniped-me-to-actually-try-the-sth-out, 2025-07-22-jakubtomsu_-clang-failed-to-recognize-it-should-use-a-select, 2025-07-22-XylnZ-wait-i-am-cooked-are-you-trolling-or-serious-it, 2025-07-22-paulopacitti-10x-better-i-thought-branch-prediction-wouldnt, 2025-07-22-SMrrandom-why-should-you-do-something-that-your-compiler-do, 2025-07-22-feliperybas-so-programming-by-object-literals, 2025-07-22-RoseSilicon-if-you-are-doing-a-for-loop-you-are-still, 2025-07-22-yenwoda-what-kind-of-applications-is-this-practically, 2025-07-22-wetfsdffsdasd-compilers-already-inline-them-unless-you-are, 2025-07-22-weirdcuttlefish-this-will-be-slower-unless-your-compiler-is, 2025-07-22-RaykiruS-int-val-this-is-valid-syntax-c-is-wild-with-the, 2025-07-22-osoleve-is-it-for-performance-reasons-ive-only-seen-it-in, 2025-07-22-RaykiruS-thats-cool-but-also-compilers-know-how-to-do-it, 2025-07-22-narator287-you-mean-functional-programming, 2025-07-22-occidensus-what-is-the-measured-performance-impact, 2025-07-22-eigen_spectra-godbolt-example-of-branchless-vs-branchfull-if, 2025-07-22-_benwick-its-good-inside-hot-loops-and-shaders-but-its-not, 2025-07-22-tripvvire-id-also-maintain-state-machines-are-a-kind-of, 2025-07-22-skunk_works777-aka-erlang-prolog-etc, 2025-07-22-StatisticsFTW-why-cant-compilers-implement-this-optimization, 2025-07-22-jrswab-i-dont-mind-a-useful-if-statement-but-when-i-see, 2025-07-22-DeusIX_imxn-cool-but-i-prefer-to-be-able-to-read-my-code, 2025-07-22-AnshumanKhanna5-what-would-be-an-actual-real-life-use-case, 2025-07-22-0xE1-i-sometimes-use-that-principle-in-sql-so-theres, 2025-07-22-BonesMoses-i-thought-this-kind-of-masking-was-standard, 2025-07-22-RepealTCPA1947-i-dont-think-you-even-need-a-multiplication-just, 2025-07-22-RepealTCPA1947-what-colour-scheme-is-that-btw, 2025-07-22-m1nd_killer-it-can-be-done-by-tools-like-terser-or-by, 2025-07-22-Random832-of-course-its-important-to-be-careful-on-modern, 2025-07-22-awwscript-shouldnt-a-good-optimizer-already-do-this, 2025-07-22-Epiq_Phale-its-cool-but-unless-you-unroll-the-loop-fully-its, 2025-07-22-illwieckz-here-is-a-real-life-example-excuse-my-french, 2025-07-22-AstrodogX-its-a-beautiful-thing-but-also-pointless-and, 2025-07-22-sunblaze-gross-just-use-an-if-preferably-in-a-more, 2025-07-23-bigboosbro08-i-still-wonder-if-you-can-make-compiler-to-auto, 2025-07-23-_m16b-this-is-what-apple-does-with-the-npu-and-coreml, 2025-07-23-awksedgreep-google-tco-thank-me-later, 2025-07-23-BackwoodsEnginr-people-have-been-whipped-for-less-than-this-try, 2025-07-23-andrecasaldev-the-key-here-is-that-cpus-mostly-only-have-two, 2025-07-23-BenSimsTech-seems-like-the-problem-is-that-both-options-are, 2025-07-23-marcosdanix-close-enough-the-for-loop-is-a-branch-however-the, 2025-07-23-MrJayLC-i-love-using-arrays-as-jump-tables-for-operations, 2025-07-23-reidatcheson-i-ts-pretty-hard-to-tell-when-removing-a-branch, 2025-07-23-SYSUlindragon-remind-me-of-rust, 2025-07-26-MichaelDChaney-if-youre-handling-integers-just-use-a-full
2/ @nicbarkeragain
This approach is similar to how “jump tables” are implemented by the compiler to speed up switch statements. By using the enum value directly as an index into a table of addresses, we can avoid needing to compare / branch and instead just compute the address and jump directly.
3/ @nicbarkeragain
While this is a bit of a contrived example, branchless programming is a deep well full of really cool tricks, I’d encourage you to dig through some of the amazing things people have done if you haven’t heard of the term before 😁
Branches: 2025-07-22-zehavoc-you-can-find-a-lot-of-examples-of-this-method-in, 2025-07-22-daniel_collin-its-funny-when-some-people-say-branches-are, 2025-07-22-_benwick-i-think-its-a-cool-field-like-only-using-stack, 2025-07-23-ChristosArgyrop-ternary-operator-in-openmp-gpu-code-is-along-the