Checking if an Integer is an Exact Binary Power

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 23 06:56:06 PDT 2016


On 4/23/16 9:54 AM, Andrei Alexandrescu wrote:
> On 4/23/16 9:06 AM, Vladimir Panteleev wrote:
>> On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote:
>>> Wanted: CT-trait and run-time predicate for checking whether its
>>> single integer parameter is an exact power of two.
>>
>> popcnt(x) <= 1 ?
>
> Would be interesting to see how it compares to the handwritten version
> on different machines. popcnt has a reputation of being slow in older
> CPUs, in recent ones it has a latency of 3 cycles and a throughput of 1
> cycle. -- Andrei

Oh, and the bummer is that on gdc it's a function call: 
https://godbolt.org/g/dEYCcG -- Andrei



More information about the Digitalmars-d mailing list