#[cfg(any(
all(target_arch = "aarch64", target_feature = "neon"),
any(target_arch = "x86", target_arch = "x86_64")
))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
#[allow(dead_code)]
fn apple_has_cpu_feature(feature_name: &str) -> bool {
use libc::{c_int, sysctlbyname};
use std::ffi::CString;
use std::ptr;
let c_feature_name = CString::new(feature_name).expect("CString::new failed");
let mut result: c_int = 0;
let mut size = std::mem::size_of::<c_int>();
unsafe {
if sysctlbyname(
c_feature_name.as_ptr(),
&mut result as *mut _ as *mut libc::c_void,
&mut size,
ptr::null_mut(),
0,
) == 0
{
result != 0
} else {
false
}
}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[allow(dead_code)]
pub fn is_x86_avx512dq_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.avx512dq")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
std::arch::is_x86_feature_detected!("avx512dq")
}
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[allow(dead_code)]
pub fn is_x86_avx512vl_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.avx512vl")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
std::arch::is_x86_feature_detected!("avx512vl")
}
}
#[cfg(not(any(
all(target_arch = "aarch64", target_feature = "neon"),
any(target_arch = "x86", target_arch = "x86_64")
)))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
#[allow(dead_code)]
fn apple_has_cpu_feature(_feature_name: &str) -> bool {
false
}
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
#[allow(dead_code)]
pub(crate) fn is_aarch_f16_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.arm.FEAT_FP16")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
std::arch::is_aarch64_feature_detected!("fp16")
}
}
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
#[allow(dead_code)]
pub(crate) fn is_aarch_f16c_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.AdvSIMD_HPFPCvt")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
true
}
}
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
#[allow(dead_code)]
pub(crate) fn is_aarch_fhm_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.arm.FEAT_FHM")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
std::arch::is_aarch64_feature_detected!("fhm")
}
}
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
#[allow(dead_code)]
pub(crate) fn is_aarch_rdm_supported() -> bool {
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
{
apple_has_cpu_feature("hw.optional.arm.FEAT_RDM")
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
{
std::arch::is_aarch64_feature_detected!("rdm")
}
}