How to determine if a CPU has good POPCNT support?

I have two versions of a quick procedure for counting newlines . One runs on older hardware, and the other runs much faster using the POPCNT instruction, which is available on newer hardware (such as 6th generation Intel processors).

Now I would like to use the best version for each CPU - how can I find out if it has a highly efficient POPCNT implementation?

+6
source share
2 answers

You could do as @kobrien said , or you could take a more civilized approach - cpuid crate .

To do this, add it to your Cargo.toml and then to check for POPCNT do

 extern crate cpuid; fn have_popcnt() -> Option<bool> { cpuid::identify().ok().map(|ci| ci.has_feature(cpuid::CpuFeature::POPCNT)) } 

The have_popcnt() function will return None if the CPU does not support the CPUID or Some(hp) command, where hp determines the availability of POPCNT.

+8
source

Run the cpuid command. Check bit 23 ecx.

https://en.wikipedia.org/wiki/CPUID

+1
source

All Articles