Skip to content

Commit c8204ec

Browse files
committed
Run-time feature detection for Aarch64 on Windows.
1 parent 6a016c7 commit c8204ec

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

crates/std_detect/src/detect/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ cfg_if! {
106106
mod aarch64;
107107
#[path = "os/freebsd/mod.rs"]
108108
mod os;
109+
} else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
110+
#[path = "os/windows/aarch64.rs"]
111+
mod os;
109112
} else {
110113
#[path = "os/other.rs"]
111114
mod os;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! Run-time feature detection for Aarch64 on Windows.
2+
3+
use crate::detect::{cache, Feature};
4+
5+
/// Try to read the features from OS API.
6+
pub(crate) fn detect_features() -> cache::Initializer {
7+
const PF_ARM_NEON_INSTRUCTIONS_AVAILABLE: u32 = 19;
8+
const PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE: u32 = 30;
9+
const PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE: u32 = 31;
10+
11+
extern "system" {
12+
pub fn IsProcessorFeaturePresent(ProcessorFeature: u32) -> u32;
13+
}
14+
15+
let mut value = cache::Initializer::default();
16+
{
17+
let mut enable_feature = |f, enable| {
18+
if enable {
19+
value.set(f as u32);
20+
}
21+
};
22+
23+
// Some features such Feature::fp may be supported on current CPU,
24+
// but no way to detect it by OS API.
25+
26+
enable_feature(Feature::asimd, unsafe {
27+
IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0
28+
});
29+
enable_feature(Feature::crc, unsafe {
30+
IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) != 0
31+
});
32+
// PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE means aes, sha1, sha2 and
33+
// pmull supprot
34+
enable_feature(Feature::crypto, unsafe {
35+
IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0
36+
});
37+
enable_feature(Feature::pmull, unsafe {
38+
IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0
39+
});
40+
}
41+
value
42+
}

0 commit comments

Comments
 (0)