On Tuesday, 19 May 2015 at 15:39:16 UTC, safety0ff wrote:
On Tuesday, 19 May 2015 at 08:28:11 UTC, John Colvin wrote:
I tested with a few different (modern) backends to see what
was generated, they all essentially give you this (gcc 5.1.0
-O3 -march=broadwell):
isPowerOf2:
xorl %eax, %eax
testl %edi, %edi
je .L5
blsr %edi, %edi
testl %edi, %edi
sete %al
.L5:
ret
I think you used:
return x && (x & (x - 1)) == 0;
instead of
return (x & (x - 1)) == 0 && x;
Which influences code generation (more weight on the x == 0
test,) hence the branch.
I used what andrei posted, but yes i do see the difference. How
infuriating. A compiler that will entirely restructure your code
leaving you with something unrecognisable in many cases, but in
others is sensitive to the order of operands around an &&.