0 is not a power of 2

safety0ff via Digitalmars-d digitalmars-d at puremagic.com
Tue May 19 08:39:15 PDT 2015


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.


More information about the Digitalmars-d mailing list