如何检测CPU是否具有良好的POPCNT支持?

时间:2016-09-23 23:09:07

标签: performance rust cpu

我有fast newline-counting routine的两个版本。一个运行在旧硬件上,而另一个运行速度更快,使用POPCNT指令,该指令可用于较新的硬件(例如第六代Intel CPU)。

现在我想为每个CPU使用最好的版本 - 我怎样才能知道它是否具有高性能的POPCNT实现?

2 个答案:

答案 0 :(得分:8)

你可以做like @kobrien said,或者你可以采取更文明的方法 - cpuid crate

为此,请将其添加到Cargo.toml,然后检查POPCNT的可用性

extern crate cpuid;

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

如果CPU不支持CPUID指令,have_popcnt()函数将返回NoneSome(hp),其中hp确定POPCNT的可用性

答案 1 :(得分:1)

执行cpuid指令。检查ecx的第23位。

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

相关问题