4 ms·
Subtraction requires a branch, which could be worse (or not) depending on architecture.
by dllthomas 10y ago
Subtraction requires a branch, which could be worse (or not) depending on architecture.
- pklausler 10y agoOh, come on, you don't need a branch to do a conditional subtraction. Reify the condition to 0/1 and use multiplication, or use AND with a two's complement of the condition.
- dllthomas 10y agoGood point!
- avocad 10y agoThanks! This AND trick is great. Actually makes me want to go back to assembly-level programming.
- cksk 10y agoOK, my bit twiddling knowledge is weak, my google skills are weaker still, and now I'm curious: what does "AND with a two's complement of the condition" mean, exactly?
- jnordwick 10y agoThe branch will be properly predicted every time except for when it wraps. This should be faster than any of the alternatives.
- dllthomas 10y agoI fully believe that there are plenty of contexts where that's true - particularly in any throughput oriented system where the buffer is large. But if you care, measure.