4 ms·
> Much better :) But what about C? Let’s try it: > I tried both versions (modulo 2 and bitwise AND) and got the same result. I think the optimizer recognizes m
by Arcuru 2y ago
> Much better :) But what about C? Let’s try it:
> I tried both versions (modulo 2 and bitwise AND) and got the same result. I think the optimizer recognizes modulo 2 and converts it to bitwise AND.
Yes, even without specifying optimizations - https://godbolt.org/z/9se9c6qKT https://godbolt.org/z/9se9c6qKT
You can see that the output of the compiler is identical whether you use `i%2 == 0` or `(i&1) == 0`. The bitwise AND is instruction 12 in the output.
Using -O3 like in the post actually compiles to SIMD instructions on x86-64 - https://godbolt.org/z/dWbcK947G https://godbolt.org/z/dWbcK947G
- thaumasiotes 2y agoThat's how you check modulus for powers of 2. 2 is a power of 2. This barely even qualifies as an "optimization".
- ryan-c 2y agoWith i < 71, the compiler will just turn it into a constant value of 36. Switches to SIMD at 72, idk why.