|
| 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