"Masking" article
betsch85 opened this issue · 1 comments
betsch85 commented
https://github.com/algorithmica-org/algorithmica/blob/master/content/english/hpc/simd/masking.md claims that
// no branch:
for (int i = 0; i < N; i++)
s += (a[i] < 50) * a[i];
// also no branch:
for (int i = 0; i < N; i++)
s += (a[i] < 50 ? a[i] : 0);
are both branchless, while I'm not sure about the first one, the 2nd one is definitely not branchless. (cond?true:false)
is just syntactic sugar for if-then-else
sslotin commented
This is true, but compilers treat ternary operators differently from if-else constructions and are more likely to replace them with cmov-based predication. The article links to https://en.algorithmica.org/hpc/pipelining/branchless/, which elaborates on why this is happening.