Detect x86_64 microarchitecture variant automatically

This commit is contained in:
Zanie Blue 2024-12-10 14:55:36 -06:00
parent b594c2d702
commit db98257f1a

View file

@ -17,7 +17,11 @@ pub enum Error {
UnsupportedVariant(String, String),
}
/// Architecture variants, e.g., with support for different instruction sets
/// Architecture variants supplementing the [`target_lexicon::Architecture`].
///
/// Currently these are only used for `x86_64` microarchitecture levels.
///
/// See the specification at <https://gitlab.com/x86-psABIs/x86-64-ABI>
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum ArchVariant {
/// Targets 64-bit Intel/AMD CPUs newer than Nehalem (2008).
@ -99,7 +103,7 @@ impl Arch {
pub fn from_env() -> Self {
Self {
family: target_lexicon::HOST.architecture,
variant: None,
variant: ArchVariant::from_env(),
}
}
@ -132,6 +136,45 @@ impl Arch {
}
}
impl ArchVariant {
#[cfg(target_arch = "x86_64")]
pub fn from_env() -> Option<Self> {
if is_x86_feature_detected!("avx512f")
&& is_x86_feature_detected!("avx512bw")
&& is_x86_feature_detected!("avx512cd")
&& is_x86_feature_detected!("avx512dq")
&& is_x86_feature_detected!("avx512vl")
{
Some(Self::V4)
} else if is_x86_feature_detected!("avx")
&& is_x86_feature_detected!("avx2")
&& is_x86_feature_detected!("bmi1")
&& is_x86_feature_detected!("bmi2")
&& is_x86_feature_detected!("f16c")
&& is_x86_feature_detected!("fma")
&& is_x86_feature_detected!("lzcnt")
&& is_x86_feature_detected!("movbe")
{
Some(Self::V3)
} else if is_x86_feature_detected!("cmpxchg16b")
&& is_x86_feature_detected!("popcnt")
&& is_x86_feature_detected!("sse3")
&& is_x86_feature_detected!("sse4.1")
&& is_x86_feature_detected!("sse4.2")
&& is_x86_feature_detected!("ssse3")
{
Some(Self::V2)
} else {
None
}
}
#[cfg(not(target_arch = "x86_64"))]
pub fn from_env() -> Option<Self> {
None
}
}
impl Display for Libc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {