Re:Wc in D: 712 Characters Without a Single Branch
by gpderetta on Tuesday January 28, 2020 @04:35PM
(#22174307)
Not really:
Should compile down to no branches (not even cmov) by summing the value of the flag register directly. Would be a it hard to stop without taking a branch though. Would you consider function pointer calls a branch? If that's too easy, what about taking a segfault?
[ Parent
| on HN ]
Re:Wc in D: 712 Characters Without a Single Branch
by BeeOnRope on Tuesday January 28, 2020 @08:19PM
(#22176167)
Even if you do the branchiest thing possible in the source, no bitwise or "looks cmov-y" stuff, all compilers that matter will do this without a branch with optimization on:
Only icc vectorizes this at -O2 (still branchless, of course), but clang and gcc vectorize it if you go to -O3.
Re:Wc in D: 712 Characters Without a Single Branch
by gpderetta on Tuesday January 28, 2020 @08:45PM
(#22176345)
Sure, but where's the code golfing fun in there ;)
Re:Wc in D: 712 Characters Without a Single Branch
by BeeOnRope on Tuesday January 28, 2020 @09:44PM
(#22176746)
:) I guess I'm not fun at [code golfing] parties.
I just want to gently push back on the notion that the source level branchy-ness is tightly tied to the generated assembly level branchiness.
Sometimes, it is - but it is a long topic to characterize when. For simple things like counters based on a condition, you'll almost always get branch-free. Same for assignment/return value based on a condition, where the possibilities don't involve lots of asymmetric work (or a asymmetric memory access).