I have two versions of a fast newline-counting routine. One runs on older hardware, while the other one runs much faster by using the POPCNT instruction, which is available on newer hardware (e.g. 6th generation Intel CPUs).
Now I'd like to use the best version for each CPU — how can I find out if it has a high-performance POPCNT implementation?
You could do like @kobrien said, or you could take a more civilised approach - the cpuid
crate.
To do that, add it to your Cargo.toml
and then, to check for availability of the 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 doesn't support the CPUID instruction or Some(hp)
, where hp
determines POPCNT's availability thereon.
Execute the cpuid instruction. Check bit 23 of ecx.
https://en.wikipedia.org/wiki/CPUID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With