DEV Community

Discussion on: Branchless programming. Does it really matter?

Collapse
 
pokemaster7 profile image
pokemaster7

Example 3 is not correct. The modulo operator is not branchless. The only way to make this branchless is to have the second argument be a power of 2 (in this case, modulos can be calculated by bit-shifting). Instead, do something like:

return x[i - (i >= lenX) * lenX]

Of course, we are assuming i is less than lenX

Some comments have been hidden by the post's author - find out more