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.

Reply via email to