Checking if an Integer is an Exact Binary Power

Iain Buclaw via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 24 06:18:32 PDT 2016


On 23 April 2016 at 15:56, Andrei Alexandrescu via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> 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
>

That can easily be changed. ;-)



More information about the Digitalmars-d mailing list