Skip to content

Commit ef62c8e

Browse files
makotokatognzlbg
authored andcommitted
Run-time feature detection for Aarch64 on Windows.
1 parent fbb173a commit ef62c8e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

crates/std_detect/src/detect/mod.rs

Lines changed: 3 additions & 0 deletions
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;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Run-time feature detection for Aarch64 on Windows.
2+
3+
use crate::detect::{cache, Feature};
4+
5+
/// Try to read the features using IsProcessorFeaturePresent.
6+
pub(crate) fn detect_features() -> cache::Initializer {
7+
type DWORD = u32;
8+
type BOOL = i32;
9+
10+
const FALSE: BOOL = 0;
11+
// The following Microsoft documents isn't updated for aarch64.
12+
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
13+
// These are defined in winnt.h of Windows SDK
14+
const PF_ARM_NEON_INSTRUCTIONS_AVAILABLE: u32 = 19;
15+
const PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE: u32 = 30;
16+
const PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE: u32 = 31;
17+
18+
extern "system" {
19+
pub fn IsProcessorFeaturePresent(ProcessorFeature: DWORD) -> BOOL;
20+
}
21+
22+
let mut value = cache::Initializer::default();
23+
{
24+
let mut enable_feature = |f, enable| {
25+
if enable {
26+
value.set(f as u32);
27+
}
28+
};
29+
30+
// Some features such Feature::fp may be supported on current CPU,
31+
// but no way to detect it by OS API.
32+
// Also, we require unsafe block for the extern "system" calls.
33+
unsafe {
34+
enable_feature(
35+
Feature::asimd,
36+
IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != FALSE,
37+
);
38+
enable_feature(
39+
Feature::crc,
40+
IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) != FALSE,
41+
);
42+
// PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE means aes, sha1, sha2 and
43+
// pmull support
44+
enable_feature(
45+
Feature::crypto,
46+
IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != FALSE,
47+
);
48+
enable_feature(
49+
Feature::pmull,
50+
IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != FALSE,
51+
);
52+
}
53+
}
54+
value
55+
}

0 commit comments

Comments
 (0)