Skip to content

cpufeatures: fix usage on Musl environments; add NEON support #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cpufeatures/src/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ macro_rules! __detect_target_features {
#[cfg(target_os = "linux")]
macro_rules! __expand_check_macro {
($(($name:tt, $hwcap:ident)),* $(,)?) => {
$(
pub use libc::$hwcap;
)*

#[macro_export]
#[doc(hidden)]
macro_rules! check {
$(
($hwcaps:expr, $name) => { (($hwcaps & $crate::aarch64::$hwcap) != 0) };
($hwcaps:expr, $name) => {
(($hwcaps & $crate::aarch64::hwcaps::$hwcap) != 0)
};
)*
}
};
Expand All @@ -69,10 +67,23 @@ macro_rules! __expand_check_macro {
#[cfg(target_os = "linux")]
__expand_check_macro! {
("aes", HWCAP_AES), // Enable AES support.
("neon", HWCAP_NEON), // Enable Advanced SIMD instructions.
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
}

/// Linux hardware capabilities
///
/// Workaround for these being missing from certain environments (i.e. Musl)
/// See: <https://github.com/rust-lang/libc/issues/2171>
#[cfg(target_os = "linux")]
pub mod hwcaps {
pub const HWCAP_AES: libc::c_ulong = 1 << 3;
pub const HWCAP_NEON: libc::c_ulong = 1 << 12;
pub const HWCAP_SHA2: libc::c_ulong = 1 << 6;
pub const HWCAP_SHA3: libc::c_ulong = 1 << 17;
}

// macOS `check!` macro.
//
// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
Expand All @@ -91,6 +102,9 @@ macro_rules! check {
("aes") => {
true
};
("neon") => {
true
};
("sha2") => {
true
};
Expand Down