Checking if an Integer is an Exact Binary Power

Lass Safin via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 25 09:48:14 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?

I just found this: 
https://dlang.org/phobos/core_cpuid.html#.hasPopcnt! It does 
exactly as it says: checks if the system has popcnt.
Though read the top of https://dlang.org/phobos/core_cpuid.html 
before you use it:
> Bugs:
> Currently only works on x86 and Itanium CPUs. Many processors 
> have bugs in their
> microcode for the CPUID instruction, so sometimes the cache 
> information may be
> incorrect.

Example;

import core.bitop;
import core.cpuid;

int count;
if(hasPopcnt)
     count = _popcnt; // Uses x86-instruction "popcnt".
else
     count = popcnt; // Phobos's software implementation.

// Do stuff with count



More information about the Digitalmars-d mailing list