Skip to content

Commit 442176a

Browse files
authored
cpufeatures: fix usage on Musl environments; add NEON support (#403)
The `libc` crate doesn't presently define Linux's `HWCAP_*` in Musl-based environments: rust-lang/libc#2171 This PR (temporarily) copies the presently supported `HWCAP_*` constants directly into this crate. Additionally, it adds support for the `neon` target feature.
1 parent 80569e5 commit 442176a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

cpufeatures/src/aarch64.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ macro_rules! __detect_target_features {
5151
#[cfg(target_os = "linux")]
5252
macro_rules! __expand_check_macro {
5353
($(($name:tt, $hwcap:ident)),* $(,)?) => {
54-
$(
55-
pub use libc::$hwcap;
56-
)*
57-
5854
#[macro_export]
5955
#[doc(hidden)]
6056
macro_rules! check {
6157
$(
62-
($hwcaps:expr, $name) => { (($hwcaps & $crate::aarch64::$hwcap) != 0) };
58+
($hwcaps:expr, $name) => {
59+
(($hwcaps & $crate::aarch64::hwcaps::$hwcap) != 0)
60+
};
6361
)*
6462
}
6563
};
@@ -69,10 +67,23 @@ macro_rules! __expand_check_macro {
6967
#[cfg(target_os = "linux")]
7068
__expand_check_macro! {
7169
("aes", HWCAP_AES), // Enable AES support.
70+
("neon", HWCAP_NEON), // Enable Advanced SIMD instructions.
7271
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
7372
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
7473
}
7574

75+
/// Linux hardware capabilities
76+
///
77+
/// Workaround for these being missing from certain environments (i.e. Musl)
78+
/// See: <https://github.com/rust-lang/libc/issues/2171>
79+
#[cfg(target_os = "linux")]
80+
pub mod hwcaps {
81+
pub const HWCAP_AES: libc::c_ulong = 1 << 3;
82+
pub const HWCAP_NEON: libc::c_ulong = 1 << 12;
83+
pub const HWCAP_SHA2: libc::c_ulong = 1 << 6;
84+
pub const HWCAP_SHA3: libc::c_ulong = 1 << 17;
85+
}
86+
7687
// macOS `check!` macro.
7788
//
7889
// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
@@ -91,6 +102,9 @@ macro_rules! check {
91102
("aes") => {
92103
true
93104
};
105+
("neon") => {
106+
true
107+
};
94108
("sha2") => {
95109
true
96110
};

0 commit comments

Comments
 (0)