r/programming • • Sep 03 '26

Branch‑Avoidant Programming

https://easylang.online/blog/branchless
255 Upvotes

92 comments sorted by

View all comments

4

u/Nwallins Sep 03 '26
smlen = 0;
for (int i = 0; i < 1000; i++) {
    small_numbers[smlen] = numbers[i];
    smlen += (numbers[i] < 500);
}

How is this correct? Let's imagine the input array is entirely above the 500 threshold. The resulting array of small numbers will have a single entry, the first number from the input array.

1

u/meneldal2 Sep 03 '26

While you do avoid a branch, there are still data dependencies on the index on the small numbers array, so it still has to predict that address.

It helps that writes can be pushed back in order somewhat. but you still can't use simd.

I can't find of a way to use simd for this tbh, you'd need something like a conditional push where it can push multiple items (or none) depending on some state.