| 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 hwcaps = $crate::loongarch64::getauxval_hwcap(); |
| 28 |
$($crate::check!(hwcaps, $tf) & )+ true |
| 29 |
}}; |
| 30 |
} |
| 31 |
|
| 32 |
/// Linux helper function for calling `getauxval` to get `AT_HWCAP`. |
| 33 |
#[cfg(target_os = "linux")] |
| 34 |
pub fn getauxval_hwcap() -> u64 { |
| 35 |
unsafe { libc::getauxval(libc::AT_HWCAP) } |
| 36 |
} |
| 37 |
|
| 38 |
// Linux `expand_check_macro` |
| 39 |
#[cfg(target_os = "linux")] |
| 40 |
macro_rules! __expand_check_macro { |
| 41 |
($(($name:tt, $hwcap:ident)),* $(,)?) => { |
| 42 |
#[macro_export] |
| 43 |
#[doc(hidden)] |
| 44 |
macro_rules! check { |
| 45 |
$( |
| 46 |
($hwcaps:expr, $name) => { |
| 47 |
(($hwcaps & $crate::loongarch64::hwcaps::$hwcap) != 0) |
| 48 |
}; |
| 49 |
)* |
| 50 |
} |
| 51 |
}; |
| 52 |
} |
| 53 |
|
| 54 |
// Linux `expand_check_macro` |
| 55 |
#[cfg(target_os = "linux")] |
| 56 |
__expand_check_macro! { |
| 57 |
("cpucfg", CPUCFG), // Enable CPUCFG support. |
| 58 |
("lam", LAM), // Enable LAM support. |
| 59 |
("ual", UAL), // Enable UAL support. |
| 60 |
("fpu", FPU), // Enable FPU support. |
| 61 |
("lsx", LSX), // Enable LSX support. |
| 62 |
("lasx", LASX), // Enable LASX support. |
| 63 |
("crc32", CRC32), // Enable CRC32 support. |
| 64 |
("complex", COMPLEX), // Enable COMPLEX support. |
| 65 |
("crypto", CRYPTO), // Enable CRYPTO support. |
| 66 |
("lvz", LVZ), // Enable LVZ support. |
| 67 |
("lbt.x86", LBT_X86), // Enable LBT_X86 support. |
| 68 |
("lbt.arm", LBT_ARM), // Enable LBT_ARM support. |
| 69 |
("lbt.mips", LBT_MIPS), // Enable LBT_MIPS support. |
| 70 |
("ptw", PTW), // Enable PTW support. |
| 71 |
} |
| 72 |
|
| 73 |
/// Linux hardware capabilities mapped to target features. |
| 74 |
/// |
| 75 |
/// Note that LLVM target features are coarser grained than what Linux supports |
| 76 |
/// and imply more capabilities under each feature. This module attempts to |
| 77 |
/// provide that mapping accordingly. |
| 78 |
#[cfg(target_os = "linux")] |
| 79 |
pub mod hwcaps { |
| 80 |
use libc::c_ulong; |
| 81 |
|
| 82 |
pub const CPUCFG: c_ulong = libc::HWCAP_CPUCFG; |
| 83 |
pub const LAM: c_ulong = libc::HWCAP_LAM; |
| 84 |
pub const UAL: c_ulong = libc::HWCAP_UAL; |
| 85 |
pub const FPU: c_ulong = libc::HWCAP_FPU; |
| 86 |
pub const LSX: c_ulong = libc::HWCAP_LSX; |
| 87 |
pub const LASX: c_ulong = libc::HWCAP_LASX; |
| 88 |
pub const CRC32: c_ulong = libc::HWCAP_CRC32; |
| 89 |
pub const COMPLEX: c_ulong = libc::HWCAP_COMPLEX; |
| 90 |
pub const CRYPTO: c_ulong = libc::HWCAP_CRYPTO; |
| 91 |
pub const LVZ: c_ulong = libc::HWCAP_LVZ; |
| 92 |
pub const LBT_X86: c_ulong = libc::HWCAP_LBT_X86; |
| 93 |
pub const LBT_ARM: c_ulong = libc::HWCAP_LBT_ARM; |
| 94 |
pub const LBT_MIPS: c_ulong = libc::HWCAP_LBT_MIPS; |
| 95 |
pub const PTW: c_ulong = libc::HWCAP_PTW; |
| 96 |
} |
| 97 |
|
| 98 |
// On other targets, runtime CPU feature detection is unavailable |
| 99 |
#[cfg(not(target_os = "linux"))] |
| 100 |
#[macro_export] |
| 101 |
#[doc(hidden)] |
| 102 |
macro_rules! __detect_target_features { |
| 103 |
($($tf:tt),+) => { |
| 104 |
false |
| 105 |
}; |
| 106 |
} |