const BMI2_BIT: u32 = 8;
const ADX_BIT: u32 = 19;
fn missing_features(extended_features: u32) -> Vec<&'static str> {
let forced_assembly = cfg!(all(feature = "x86_64-asm", not(feature = "portable")));
let required = [
(
"bmi2",
forced_assembly || cfg!(target_feature = "bmi2"),
BMI2_BIT,
),
(
"adx",
forced_assembly || cfg!(target_feature = "adx"),
ADX_BIT,
),
];
required
.iter()
.filter(|(_, required, bit)| *required && extended_features & (1 << bit) == 0)
.map(|(name, _, _)| *name)
.collect()
}
fn missing_pasta_assembly_features() -> Vec<&'static str> {
use core::arch::x86_64::{__cpuid, __cpuid_count};
if missing_features(0).is_empty() {
return Vec::new();
}
let extended_features = if __cpuid(0).eax >= 7 {
__cpuid_count(7, 0).ebx
} else {
0
};
missing_features(extended_features)
}
#[inline(never)]
#[allow(
clippy::print_stderr,
reason = "this runs before tracing is initialized, so stderr is the only output"
)]
pub(crate) fn exit_if_cpu_lacks_pasta_assembly_features() {
let missing = missing_pasta_assembly_features();
if missing.is_empty() {
return;
}
eprintln!(
"This Zakura binary requires CPU features this machine lacks: {}. \
Rebuild with `--features portable` and without CPU-specific compiler flags.",
missing.join(", ")
);
std::process::exit(1);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn supporting_cpu_never_reports_missing_extensions() {
assert!(missing_features((1 << BMI2_BIT) | (1 << ADX_BIT)).is_empty());
}
#[cfg(all(feature = "x86_64-asm", not(feature = "portable")))]
#[test]
fn forced_assembly_requires_both_extensions() {
assert_eq!(missing_features(0), ["bmi2", "adx"]);
assert_eq!(missing_features(1 << BMI2_BIT), ["adx"]);
assert_eq!(missing_features(1 << ADX_BIT), ["bmi2"]);
}
#[cfg(all(
not(all(feature = "x86_64-asm", not(feature = "portable"))),
not(target_feature = "bmi2"),
not(target_feature = "adx")
))]
#[test]
fn baseline_and_portable_builds_accept_cpus_without_extensions() {
assert!(missing_features(0).is_empty());
exit_if_cpu_lacks_pasta_assembly_features();
}
#[cfg(all(feature = "portable", target_feature = "bmi2", target_feature = "adx"))]
#[test]
fn portable_does_not_override_global_compiler_requirements() {
assert_eq!(missing_features(0), ["bmi2", "adx"]);
}
}