#[cfg(target_arch = "x86")]
use core::arch::x86::__cpuid_count;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::__cpuid_count;
use std::sync::atomic::{AtomicU64, Ordering};
#[must_use]
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CPUFeature(pub u32);
#[expect(
non_upper_case_globals,
reason = "C enumerator names such as kSSE2 keep their case"
)]
impl CPUFeature {
pub const kSSE2: Self = Self(0);
pub const kSSE3: Self = Self(1);
pub const kSlowSSSE3: Self = Self(2);
pub const kSSE4_1: Self = Self(3);
pub const kAVX: Self = Self(4);
pub const kAVX2: Self = Self(5);
pub const kNEON: Self = Self(6);
pub const kMIPS32: Self = Self(7);
pub const kMIPSdspR2: Self = Self(8);
pub const kMSA: Self = Self(9);
}
pub type VP8CPUInfo = fn(CPUFeature) -> bool;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn get_cpu_info(info_type: u32) -> [u32; 4] {
let result = __cpuid_count(info_type, 0);
[result.eax, result.ebx, result.ecx, result.edx]
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[must_use]
pub fn check_slow_model(info: u32) -> bool {
const SLOW_MODELS: [u32; 6] = [
0x37, 0x4a, 0x4d, 0x1c, 0x26, 0x27, ];
let model = ((info & 0xf_0000) >> 12) | ((info >> 4) & 0xf);
let family = (info >> 8) & 0xf;
if family == 0x06 {
return SLOW_MODELS.contains(&model);
}
false
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[expect(
clippy::cast_possible_wrap,
reason = "cpuid returns the highest leaf in the EAX of the C int that is compared with 1 and 7"
)]
fn x86cpu_info(feature: CPUFeature) -> bool {
const VENDOR_ID_INTEL_EBX: u32 = 0x756e_6547; const VENDOR_ID_INTEL_EDX: u32 = 0x4965_6e69; const VENDOR_ID_INTEL_ECX: u32 = 0x6c65_746e; let cpu_info = get_cpu_info(0);
let max_cpuid_value = cpu_info[0] as i32;
if max_cpuid_value < 1 {
return false;
}
let is_intel = cpu_info[1] == VENDOR_ID_INTEL_EBX
&& cpu_info[2] == VENDOR_ID_INTEL_ECX
&& cpu_info[3] == VENDOR_ID_INTEL_EDX;
let cpu_info = get_cpu_info(1);
if feature == CPUFeature::kSSE2 {
return cpu_info[3] & (1 << 26) != 0;
}
if feature == CPUFeature::kSSE3 {
return cpu_info[2] & (1 << 0) != 0;
}
if feature == CPUFeature::kSlowSSSE3 {
if is_intel && cpu_info[2] & (1 << 9) != 0 {
return check_slow_model(cpu_info[0]);
}
return false;
}
if feature == CPUFeature::kSSE4_1 {
return cpu_info[2] & (1 << 19) != 0;
}
if feature == CPUFeature::kAVX {
if cpu_info[2] & 0x1800_0000 == 0x1800_0000 {
return std::is_x86_feature_detected!("avx");
}
}
if feature == CPUFeature::kAVX2 && x86cpu_info(CPUFeature::kAVX) && max_cpuid_value >= 7 {
let cpu_info = get_cpu_info(7);
return cpu_info[1] & (1 << 5) != 0;
}
false
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[must_use]
pub fn vp8_get_cpu_info(feature: CPUFeature) -> bool {
#[cfg(feature = "oracle")]
if !gate_level_allows(feature) {
return false;
}
x86cpu_info(feature)
}
#[cfg(all(feature = "oracle", any(target_arch = "x86", target_arch = "x86_64")))]
fn gate_level_allows(feature: CPUFeature) -> bool {
match std::env::var("LIBWEBP_SIMD_LEVEL").as_deref() {
Err(_) | Ok("" | "avx2" | "neon") => true,
Ok("sse41") => feature <= CPUFeature::kSSE4_1,
Ok("sse2") => feature == CPUFeature::kSSE2,
Ok(level) => panic!("LIBWEBP_SIMD_LEVEL={level}: unknown level"),
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
fn arm_cpu_info(feature: CPUFeature) -> bool {
feature == CPUFeature::kNEON
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
#[must_use]
pub fn vp8_get_cpu_info(feature: CPUFeature) -> bool {
arm_cpu_info(feature)
}
#[cfg(not(any(
target_arch = "x86",
target_arch = "x86_64",
all(feature = "simd", target_arch = "aarch64")
)))]
#[must_use]
pub fn vp8_get_cpu_info(_feature: CPUFeature) -> bool {
false
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[expect(
clippy::struct_excessive_bools,
reason = "one flag per CPU feature, as the C query answers them"
)]
pub struct Features {
pub sse2: bool,
pub sse3: bool,
pub slow_ssse3: bool,
pub sse4_1: bool,
pub avx: bool,
pub avx2: bool,
pub neon: bool,
pub query_neon: bool,
}
impl Features {
const SSE2: u32 = 1 << 0;
const SSE3: u32 = 1 << 1;
const SLOW_SSSE3: u32 = 1 << 2;
const SSE4_1: u32 = 1 << 3;
const AVX: u32 = 1 << 4;
const AVX2: u32 = 1 << 5;
const NEON: u32 = 1 << 6;
const QUERY_NEON: u32 = 1 << 7;
const VALID: u32 = 1 << 31;
fn bits(self) -> u32 {
let mut bits = 0;
for (flag, bit) in [
(self.sse2, Self::SSE2),
(self.sse3, Self::SSE3),
(self.slow_ssse3, Self::SLOW_SSSE3),
(self.sse4_1, Self::SSE4_1),
(self.avx, Self::AVX),
(self.avx2, Self::AVX2),
(self.neon, Self::NEON),
(self.query_neon, Self::QUERY_NEON),
] {
if flag {
bits |= bit;
}
}
bits
}
#[inline]
fn from_bits(bits: u32) -> Self {
Self {
sse2: bits & Self::SSE2 != 0,
sse3: bits & Self::SSE3 != 0,
slow_ssse3: bits & Self::SLOW_SSSE3 != 0,
sse4_1: bits & Self::SSE4_1 != 0,
avx: bits & Self::AVX != 0,
avx2: bits & Self::AVX2 != 0,
neon: bits & Self::NEON != 0,
query_neon: bits & Self::QUERY_NEON != 0,
}
}
}
static FEATURES_CACHE: AtomicU64 = AtomicU64::new(0);
#[inline]
#[must_use]
pub fn features() -> Features {
let token = crate::abi::decode::cpu_query_token();
let cached = FEATURES_CACHE.load(Ordering::Relaxed);
#[expect(
clippy::cast_possible_truncation,
reason = "the low word of the cache holds the feature bits"
)]
let bits = cached as u32;
let cached_token = (cached >> 32) as u32;
if cached_token == token && bits & Features::VALID != 0 {
return Features::from_bits(bits);
}
refresh_features(token)
}
#[cold]
#[inline(never)]
fn refresh_features(token: u32) -> Features {
let computed = compute_features();
FEATURES_CACHE.store(
(u64::from(token) << 32) | u64::from(computed.bits() | Features::VALID),
Ordering::Relaxed,
);
computed
}
fn compute_features() -> Features {
let query =
|feature: CPUFeature| crate::abi::decode::cpu_feature_query(feature).unwrap_or(false);
Features {
sse2: query(CPUFeature::kSSE2),
sse3: query(CPUFeature::kSSE3),
slow_ssse3: query(CPUFeature::kSlowSSSE3),
sse4_1: query(CPUFeature::kSSE4_1),
avx: query(CPUFeature::kAVX),
avx2: query(CPUFeature::kAVX2),
neon: cfg!(target_arch = "aarch64"),
query_neon: query(CPUFeature::kNEON),
}
}