> you have to be careful with assuming that x mod n will be optimized to an and 
> operation where n is a power of 2

That is a very good hint indeed. In C code often unsigned ints are used, so its 
a plain "and" op, but in Nim we most often use signed ints.

I have not benchmarked these statements myself, and speed was not really a 
concern. It was more about simple code in this case, I typed
    
    
    i = (i + 1) mod 4
    
    
    Run

for the forward loop, as it looks simpler for me than
    
    
    j -= 1
    if j < 0:
      j = 3
    
    
    Run

and I assumed that the mod operation at least would not be slower, which seems 
to be wrong indeed. Indeed simple if ... else statements can be very fast due 
to cmov instruction, I tested it recently:

[https://forum.nim-lang.org/t/4142#25901](https://forum.nim-lang.org/t/4142#25901)

Reply via email to