Checking if an Integer is an Exact Binary Power

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 23 16:41:09 PDT 2016


On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote:
> On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote:
>> CPUID: https://en.wikipedia.org/wiki/CPUID.
>> You can check for the presence of a lot of instructions with 
>> this instruction.
>> However this will only work on x86 and only run-time.
>
> Code you give a complete code example in D, please or point out 
> a suitable place in druntime/phobos?

core.bitop.popcnt() uses core.cpuid.hasPopcnt() to choose between 
the intrinsic _popcnt() and a software fallback algorithm at 
runtime:
     
https://github.com/dlang/druntime/blob/master/src/core/bitop.d#L343

The relative complexity of softPopcnt() also shows why you don't 
really want to use popcnt() for this task unless you know the 
intrinsic will be available.


More information about the Digitalmars-d mailing list