r/programming 1d ago

Branch‑Avoidant Programming

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

87 comments sorted by

View all comments

3

u/Nwallins 18h ago
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 12h ago

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.