| 1 |
//! LoongArch64 CPU feature detection support. |
| 2 |
//! |
| 3 |
//! This implementation relies on OS-specific APIs for feature detection. |
| 4 |
|
| 5 |
// Evaluate the given `$body` expression any of the supplied target features |
| 6 |
// are not enabled. Otherwise returns true. |
| 7 |
#[macro_export] |
| 8 |
#[doc(hidden)] |
| 9 |
macro_rules! __unless_target_features { |
| 10 |
($($tf:tt),+ => $body:expr ) => { |
| 11 |
{ |
| 12 |
#[cfg(not(all($(target_feature=$tf,)*)))] |
| 13 |
$body |
| 14 |
|
| 15 |
#[cfg(all($(target_feature=$tf,)*))] |
| 16 |
true |
| 17 |
} |
| 18 |
}; |
| 19 |
} |
| 20 |
|
| 21 |
// Linux runtime detection of target CPU features using `getauxval`. |
| 22 |
#[cfg(target_os = "linux")] |
| 23 |
#[macro_export] |
| 24 |
#[doc(hidden)] |
| 25 |
macro_rules! __detect_target_features { |
| 26 |
($($tf:tt),+) => {{ |
| 27 |
let cpucfg1: usize; |
| 28 |
let cpucfg2: usize; |
| 29 |
let cpucfg3: usize; |
| 30 |
unsafe { |
| 31 |
std::arch::asm!( |
| 32 |
"cpucfg {}, {}", |
| 33 |
"cpucfg {}, {}", |
| 34 |
"cpucfg {}, {}", |
| 35 |
out(reg) cpucfg1, in(reg) 1, |
| 36 |
out(reg) cpucfg2, in(reg) 2, |
| 37 |
out(reg) cpucfg3, in(reg) 3, |
| 38 |
options(pure, nomem, preserves_flags, nostack) |
| 39 |
); |
| 40 |
} |
| 41 |
let hwcaps = $crate::loongarch64::getauxval_hwcap(); |
| 42 |
$($crate::check!(cpucfg1, cpucfg2, cpucfg3, hwcaps, $tf) & )+ true |
| 43 |
}}; |
| 44 |
} |
| 45 |
|
| 46 |
/// Linux helper function for calling `getauxval` to get `AT_HWCAP`. |
| 47 |
#[cfg(target_os = "linux")] |
| 48 |
pub fn getauxval_hwcap() -> u64 { |
| 49 |
unsafe { libc::getauxval(libc::AT_HWCAP) } |
| 50 |
} |
| 51 |
|
| 52 |
#[macro_export] |
| 53 |
#[doc(hidden)] |
| 54 |
macro_rules! check { |
| 55 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "32s") => { |
| 56 |
(($cpucfg1 & 1) != 0 || ($cpucfg1 & 2) != 0) |
| 57 |
}; |
| 58 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "f") => { |
| 59 |
(($cpucfg2 & 2) != 0 && ($hwcaps & $crate::loongarch64::hwcaps::FPU) != 0) |
| 60 |
}; |
| 61 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "d") => { |
| 62 |
(($cpucfg2 & 4) != 0 && ($hwcaps & $crate::loongarch64::hwcaps::FPU) != 0) |
| 63 |
}; |
| 64 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "frecipe") => { |
| 65 |
(($cpucfg2 & (1 << 25)) != 0) |
| 66 |
}; |
| 67 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "div32") => { |
| 68 |
(($cpucfg2 & (1 << 26)) != 0) |
| 69 |
}; |
| 70 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lsx") => { |
| 71 |
(($hwcaps & $crate::loongarch64::hwcaps::LSX) != 0) |
| 72 |
}; |
| 73 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lasx") => { |
| 74 |
(($hwcaps & $crate::loongarch64::hwcaps::LASX) != 0) |
| 75 |
}; |
| 76 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lam-bh") => { |
| 77 |
(($cpucfg2 & (1 << 27)) != 0) |
| 78 |
}; |
| 79 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lamcas") => { |
| 80 |
(($cpucfg2 & (1 << 28)) != 0) |
| 81 |
}; |
| 82 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "ld-seq-sa") => { |
| 83 |
(($cpucfg3 & (1 << 23)) != 0) |
| 84 |
}; |
| 85 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "scq") => { |
| 86 |
(($cpucfg2 & (1 << 30)) != 0) |
| 87 |
}; |
| 88 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lbt") => { |
| 89 |
(($hwcaps & $crate::loongarch64::hwcaps::LBT_X86) != 0 |
| 90 |
&& ($hwcaps & $crate::loongarch64::hwcaps::LBT_ARM) != 0 |
| 91 |
&& ($hwcaps & $crate::loongarch64::hwcaps::LBT_MIPS) != 0) |
| 92 |
}; |
| 93 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "lvz") => { |
| 94 |
(($hwcaps & $crate::loongarch64::hwcaps::LVZ) != 0) |
| 95 |
}; |
| 96 |
($cpucfg1:expr, $cpucfg2:expr, $cpucfg3:expr, $hwcaps:expr, "ual") => { |
| 97 |
(($hwcaps & $crate::loongarch64::hwcaps::UAL) != 0) |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
/// Linux hardware capabilities mapped to target features. |
| 102 |
/// |
| 103 |
/// Note that LLVM target features are coarser grained than what Linux supports |
| 104 |
/// and imply more capabilities under each feature. This module attempts to |
| 105 |
/// provide that mapping accordingly. |
| 106 |
#[cfg(target_os = "linux")] |
| 107 |
pub mod hwcaps { |
| 108 |
use libc::c_ulong; |
| 109 |
|
| 110 |
pub const UAL: c_ulong = libc::HWCAP_LOONGARCH_UAL; |
| 111 |
pub const FPU: c_ulong = libc::HWCAP_LOONGARCH_FPU; |
| 112 |
pub const LSX: c_ulong = libc::HWCAP_LOONGARCH_LSX; |
| 113 |
pub const LASX: c_ulong = libc::HWCAP_LOONGARCH_LASX; |
| 114 |
pub const LVZ: c_ulong = libc::HWCAP_LOONGARCH_LVZ; |
| 115 |
pub const LBT_X86: c_ulong = libc::HWCAP_LOONGARCH_LBT_X86; |
| 116 |
pub const LBT_ARM: c_ulong = libc::HWCAP_LOONGARCH_LBT_ARM; |
| 117 |
pub const LBT_MIPS: c_ulong = libc::HWCAP_LOONGARCH_LBT_MIPS; |
| 118 |
} |
| 119 |
|
| 120 |
// On other targets, runtime CPU feature detection is unavailable |
| 121 |
#[cfg(not(target_os = "linux"))] |
| 122 |
#[macro_export] |
| 123 |
#[doc(hidden)] |
| 124 |
macro_rules! __detect_target_features { |
| 125 |
($($tf:tt),+) => { |
| 126 |
false |
| 127 |
}; |
| 128 |
} |