3 ms·
Some of the suggested ways to avoid the branch are bit manipulation (which is not necessarily portable) and the ternary operator (which seems hardly different f
by ramshorns 9y ago
Some of the suggested ways to avoid the branch are bit manipulation (which is not necessarily portable) and the ternary operator (which seems hardly different from an if-else, though maybe the compiler usually treats it differently). It seems like another way would be
sum += data[c] * (data[c] >= 128);
which adds 0 when the condition is false.
- mark-r 9y agoMany processors have an instruction that will select between two operands depending on a condition. That can be faster than a branch. Although a sufficiently intelligent compiler would be able to determine that the instruction could be used in an if/else situation.