diff options
| author | gnzlbg <gonzalobg88@gmail.com> | 2019-09-16 17:42:00 +0200 |
|---|---|---|
| committer | gnzlbg <gnzlbg@users.noreply.github.com> | 2019-09-16 23:43:01 +0200 |
| commit | 226b3265c8fda97254c6b52f778ac7c54a76a3ad (patch) | |
| tree | 948283ed8fde44cb2da1f114c4c54a027b28c3ee /library/stdarch/crates/std_detect/src/detect | |
| parent | 599bcf28ad02c0ea7473a07b088553813a649487 (diff) | |
| download | rust-226b3265c8fda97254c6b52f778ac7c54a76a3ad.tar.gz rust-226b3265c8fda97254c6b52f778ac7c54a76a3ad.zip | |
Format
Diffstat (limited to 'library/stdarch/crates/std_detect/src/detect')
11 files changed, 86 insertions, 75 deletions
diff --git a/library/stdarch/crates/std_detect/src/detect/os/aarch64.rs b/library/stdarch/crates/std_detect/src/detect/os/aarch64.rs index dfb8c87707f..9adc938a264 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/aarch64.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/aarch64.rs @@ -16,7 +16,7 @@ //! - [Zircon implementation](https://fuchsia.googlesource.com/zircon/+/master/kernel/arch/arm64/feature.cpp) //! - [Linux documentation](https://www.kernel.org/doc/Documentation/arm64/cpu-feature-registers.txt) -use crate::detect::{Feature, cache}; +use crate::detect::{cache, Feature}; /// Try to read the features from the system registers. /// @@ -33,7 +33,9 @@ pub(crate) fn detect_features() -> cache::Initializer { // ID_AA64ISAR0_EL1 - Instruction Set Attribute Register 0 let aa64isar0: u64; - unsafe { asm!("mrs $0, ID_AA64ISAR0_EL1" : "=r"(aa64isar0)); } + unsafe { + asm!("mrs $0, ID_AA64ISAR0_EL1" : "=r"(aa64isar0)); + } let aes = bits_shift(aa64isar0, 7, 4) >= 1; let pmull = bits_shift(aa64isar0, 7, 4) >= 2; @@ -47,7 +49,9 @@ pub(crate) fn detect_features() -> cache::Initializer { // ID_AA64PFR0_EL1 - Processor Feature Register 0 let aa64pfr0: u64; - unsafe { asm!("mrs $0, ID_AA64PFR0_EL1" : "=r"(aa64pfr0)); } + unsafe { + asm!("mrs $0, ID_AA64PFR0_EL1" : "=r"(aa64pfr0)); + } let fp = bits_shift(aa64pfr0, 19, 16) < 0xF; let fphp = bits_shift(aa64pfr0, 19, 16) >= 1; @@ -60,12 +64,17 @@ pub(crate) fn detect_features() -> cache::Initializer { enable_feature(Feature::asimd, fp && asimd && (!fphp | asimdhp)); // SIMD extensions require SIMD support: enable_feature(Feature::rdm, asimd && bits_shift(aa64isar0, 31, 28) >= 1); - enable_feature(Feature::dotprod, asimd && bits_shift(aa64isar0, 47, 44) >= 1); + enable_feature( + Feature::dotprod, + asimd && bits_shift(aa64isar0, 47, 44) >= 1, + ); enable_feature(Feature::sve, asimd && bits_shift(aa64pfr0, 35, 32) >= 1); // ID_AA64ISAR1_EL1 - Instruction Set Attribute Register 1 let aa64isar1: u64; - unsafe { asm!("mrs $0, ID_AA64ISAR1_EL1" : "=r"(aa64isar1)); } + unsafe { + asm!("mrs $0, ID_AA64ISAR1_EL1" : "=r"(aa64isar1)); + } enable_feature(Feature::rcpc, bits_shift(aa64isar1, 23, 20) >= 1); } diff --git a/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs b/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs index 38e37721ef3..4c9d763b446 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for ARM on FreeBSD -use crate::detect::{Feature, cache}; -use super::{auxvec}; +use super::auxvec; +use crate::detect::{cache, Feature}; /// Try to read the features from the auxiliary vector pub(crate) fn detect_features() -> cache::Initializer { diff --git a/library/stdarch/crates/std_detect/src/detect/os/freebsd/auxvec.rs b/library/stdarch/crates/std_detect/src/detect/os/freebsd/auxvec.rs index a2bac767601..c595ec459b5 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/freebsd/auxvec.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/freebsd/auxvec.rs @@ -44,13 +44,13 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> { fn archauxv(key: usize) -> Result<usize, ()> { use crate::mem; - #[derive (Copy, Clone)] + #[derive(Copy, Clone)] #[repr(C)] pub struct Elf_Auxinfo { pub a_type: usize, pub a_un: unnamed, } - #[derive (Copy, Clone)] + #[derive(Copy, Clone)] #[repr(C)] pub union unnamed { pub a_val: libc::c_long, @@ -58,22 +58,30 @@ fn archauxv(key: usize) -> Result<usize, ()> { pub a_fcn: Option<unsafe extern "C" fn() -> ()>, } - let mut auxv: [Elf_Auxinfo; 27] = - [Elf_Auxinfo{a_type: 0, a_un: unnamed{a_val: 0,},}; 27]; + let mut auxv: [Elf_Auxinfo; 27] = [Elf_Auxinfo { + a_type: 0, + a_un: unnamed { a_val: 0 }, + }; 27]; let mut len: libc::c_uint = mem::size_of_val(&auxv) as libc::c_uint; unsafe { - let mut mib = [libc::CTL_KERN, libc::KERN_PROC, libc::KERN_PROC_AUXV, libc::getpid()]; - - let ret = libc::sysctl(mib.as_mut_ptr(), - mib.len() as u32, - &mut auxv as *mut _ as *mut _, - &mut len as *mut _ as *mut _, - 0 as *mut libc::c_void, - 0, - ); - + let mut mib = [ + libc::CTL_KERN, + libc::KERN_PROC, + libc::KERN_PROC_AUXV, + libc::getpid(), + ]; + + let ret = libc::sysctl( + mib.as_mut_ptr(), + mib.len() as u32, + &mut auxv as *mut _ as *mut _, + &mut len as *mut _ as *mut _, + 0 as *mut libc::c_void, + 0, + ); + if ret != -1 { for i in 0..auxv.len() { if auxv[i].a_type == key { diff --git a/library/stdarch/crates/std_detect/src/detect/os/freebsd/powerpc.rs b/library/stdarch/crates/std_detect/src/detect/os/freebsd/powerpc.rs index 52a71e672a3..6bfab631a9c 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/freebsd/powerpc.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/freebsd/powerpc.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for PowerPC on FreeBSD. -use crate::detect::{Feature, cache}; -use super::{auxvec}; +use super::auxvec; +use crate::detect::{cache, Feature}; pub(crate) fn detect_features() -> cache::Initializer { let mut value = cache::Initializer::default(); diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/aarch64.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/aarch64.rs index 43c0976daf3..b1b68f763e5 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/aarch64.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/aarch64.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for Aarch64 on Linux. -use crate::detect::{Feature, cache, bit}; use super::{auxvec, cpuinfo}; +use crate::detect::{bit, cache, Feature}; /// Try to read the features from the auxiliary vector, and if that fails, try /// to read them from /proc/cpuinfo. @@ -21,16 +21,16 @@ pub(crate) fn detect_features() -> cache::Initializer { /// /// [hwcap]: https://github.com/torvalds/linux/blob/master/arch/arm64/include/uapi/asm/hwcap.h struct AtHwcap { - fp: bool, // 0 + fp: bool, // 0 asimd: bool, // 1 // evtstrm: bool, // 2 - aes: bool, // 3 - pmull: bool, // 4 - sha1: bool, // 5 - sha2: bool, // 6 - crc32: bool, // 7 + aes: bool, // 3 + pmull: bool, // 4 + sha1: bool, // 5 + sha2: bool, // 6 + crc32: bool, // 7 atomics: bool, // 8 - fphp: bool, // 9 + fphp: bool, // 9 asimdhp: bool, // 10 // cpuid: bool, // 11 asimdrdm: bool, // 12 @@ -144,7 +144,10 @@ impl AtHwcap { enable_feature(Feature::sve, self.sve && asimd); // Crypto is specified as AES + PMULL + SHA1 + SHA2 per LLVM/hosts.cpp - enable_feature(Feature::crypto, self.aes && self.pmull && self.sha1 && self.sha2); + enable_feature( + Feature::crypto, + self.aes && self.pmull && self.sha1 && self.sha2, + ); } value } diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/arm.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/arm.rs index 95624e1285d..a1b28dad70f 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/arm.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/arm.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for ARM on Linux. -use crate::detect::{Feature, cache, bit}; use super::{auxvec, cpuinfo}; +use crate::detect::{bit, cache, Feature}; /// Try to read the features from the auxiliary vector, and if that fails, try /// to read them from /proc/cpuinfo. @@ -23,8 +23,11 @@ pub(crate) fn detect_features() -> cache::Initializer { } if let Ok(c) = cpuinfo::CpuInfo::new() { - enable_feature(&mut value, Feature::neon, c.field("Features").has("neon") && - !has_broken_neon(&c)); + enable_feature( + &mut value, + Feature::neon, + c.field("Features").has("neon") && !has_broken_neon(&c), + ); enable_feature(&mut value, Feature::pmull, c.field("Features").has("pmull")); return value; } diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/auxvec.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/auxvec.rs index 07b6432eafd..6ebae67fbf8 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/auxvec.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/auxvec.rs @@ -51,12 +51,12 @@ pub(crate) struct AuxVec { /// [auxvec_h]: https://github.com/torvalds/linux/blob/master/include/uapi/linux/auxvec.h /// [auxv_docs]: https://docs.rs/auxv/0.3.3/auxv/ pub(crate) fn auxv() -> Result<AuxVec, ()> { - #[cfg(feature = "std_detect_dlsym_getauxval")] { + #[cfg(feature = "std_detect_dlsym_getauxval")] + { // Try to call a dynamically-linked getauxval function. if let Ok(hwcap) = getauxval(AT_HWCAP) { // Targets with only AT_HWCAP: - #[cfg(any(target_arch = "aarch64", target_arch = "mips", - target_arch = "mips64"))] + #[cfg(any(target_arch = "aarch64", target_arch = "mips", target_arch = "mips64"))] { if hwcap != 0 { return Ok(AuxVec { hwcap }); @@ -74,22 +74,24 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> { } drop(hwcap); } - #[cfg(feature = "std_detect_file_io")] { + #[cfg(feature = "std_detect_file_io")] + { // If calling getauxval fails, try to read the auxiliary vector from // its file: auxv_from_file("/proc/self/auxv") } - #[cfg(not(feature = "std_detect_file_io"))] { + #[cfg(not(feature = "std_detect_file_io"))] + { Err(()) } } - #[cfg(not(feature = "std_detect_dlsym_getauxval"))] { + #[cfg(not(feature = "std_detect_dlsym_getauxval"))] + { let hwcap = unsafe { ffi_getauxval(AT_HWCAP) }; // Targets with only AT_HWCAP: - #[cfg(any(target_arch = "aarch64", target_arch = "mips", - target_arch = "mips64"))] + #[cfg(any(target_arch = "aarch64", target_arch = "mips", target_arch = "mips64"))] { if hwcap != 0 { return Ok(AuxVec { hwcap }); @@ -115,10 +117,7 @@ fn getauxval(key: usize) -> Result<usize, ()> { use libc; pub type F = unsafe extern "C" fn(usize) -> usize; unsafe { - let ptr = libc::dlsym( - libc::RTLD_DEFAULT, - "getauxval\0".as_ptr() as *const _, - ); + let ptr = libc::dlsym(libc::RTLD_DEFAULT, "getauxval\0".as_ptr() as *const _); if ptr.is_null() { return Err(()); } @@ -141,8 +140,7 @@ fn auxv_from_file(file: &str) -> Result<AuxVec, ()> { // 2*32 `usize` elements is enough to read the whole vector. let mut buf = [0_usize; 64]; { - let raw: &mut [u8; 64 * mem::size_of::<usize>()] = - unsafe { mem::transmute(&mut buf) }; + let raw: &mut [u8; 64 * mem::size_of::<usize>()] = unsafe { mem::transmute(&mut buf) }; file.read(raw).map_err(|_| ())?; } auxv_from_buf(&buf) @@ -153,8 +151,7 @@ fn auxv_from_file(file: &str) -> Result<AuxVec, ()> { #[cfg(feature = "std_detect_file_io")] fn auxv_from_buf(buf: &[usize; 64]) -> Result<AuxVec, ()> { // Targets with only AT_HWCAP: - #[cfg(any(target_arch = "aarch64", target_arch = "mips", - target_arch = "mips64"))] + #[cfg(any(target_arch = "aarch64", target_arch = "mips", target_arch = "mips64"))] { for el in buf.chunks(2) { match el[0] { @@ -193,8 +190,8 @@ mod tests { // using the auxv crate. #[cfg(feature = "std_detect_file_io")] fn auxv_crate_getprocfs(key: usize) -> Option<usize> { - use self::auxv_crate::AuxvType; use self::auxv_crate::procfs::search_procfs_auxv; + use self::auxv_crate::AuxvType; let k = key as AuxvType; match search_procfs_auxv(&[k]) { Ok(v) => Some(v[&k] as usize), @@ -206,8 +203,8 @@ mod tests { // using the auxv crate. #[cfg(not(any(target_arch = "mips", target_arch = "mips64")))] fn auxv_crate_getauxval(key: usize) -> Option<usize> { - use self::auxv_crate::AuxvType; use self::auxv_crate::getauxval::Getauxval; + use self::auxv_crate::AuxvType; let q = auxv_crate::getauxval::NativeGetauxval {}; match q.getauxval(key as AuxvType) { Ok(v) => Some(v as usize), diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/cpuinfo.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/cpuinfo.rs index b3168578537..f76c48a4b16 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/cpuinfo.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/cpuinfo.rs @@ -2,7 +2,7 @@ #![cfg_attr(not(target_arch = "arm"), allow(dead_code))] extern crate std; -use self::std::{prelude::v1::*, fs::File, io, io::Read}; +use self::std::{fs::File, io, io::Read, prelude::v1::*}; /// cpuinfo pub(crate) struct CpuInfo { @@ -150,8 +150,7 @@ power management: assert!(!cpuinfo.field("flags").has("avx")); } - const ARM_CORTEX_A53: &str = - r"Processor : AArch64 Processor rev 3 (aarch64) + const ARM_CORTEX_A53: &str = r"Processor : AArch64 Processor rev 3 (aarch64) processor : 0 processor : 1 processor : 2 diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/mips.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/mips.rs index 6486673f805..9c030f41a00 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/mips.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/mips.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for MIPS on Linux. -use crate::detect::{Feature, cache, bit}; use super::auxvec; +use crate::detect::{bit, cache, Feature}; /// Try to read the features from the auxiliary vector, and if that fails, try /// to read them from `/proc/cpuinfo`. diff --git a/library/stdarch/crates/std_detect/src/detect/os/linux/powerpc.rs b/library/stdarch/crates/std_detect/src/detect/os/linux/powerpc.rs index 5be35b96dda..97afe49fe50 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/linux/powerpc.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/linux/powerpc.rs @@ -1,7 +1,7 @@ //! Run-time feature detection for PowerPC on Linux. -use crate::detect::{Feature, cache}; use super::{auxvec, cpuinfo}; +use crate::detect::{cache, Feature}; /// Try to read the features from the auxiliary vector, and if that fails, try /// to read them from /proc/cpuinfo. diff --git a/library/stdarch/crates/std_detect/src/detect/os/x86.rs b/library/stdarch/crates/std_detect/src/detect/os/x86.rs index 6daeb2d1a1e..c653771c435 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/x86.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/x86.rs @@ -7,7 +7,7 @@ use crate::arch::x86_64::*; use crate::mem; -use crate::detect::{Feature, cache, bit}; +use crate::detect::{bit, cache, Feature}; /// Run-time feature detection on x86 works by using the CPUID instruction. /// @@ -73,8 +73,7 @@ pub(crate) fn detect_features() -> cache::Initializer { // EAX = 7, ECX = 0: Queries "Extended Features"; // Contains information about bmi,bmi2, and avx2 support. - let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7 - { + let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7 { let CpuidResult { ebx, ecx, .. } = unsafe { __cpuid(0x0000_0007_u32) }; (ebx, ecx) } else { @@ -210,11 +209,7 @@ pub(crate) fn detect_features() -> cache::Initializer { enable(extended_features_ebx, 30, Feature::avx512bw); enable(extended_features_ebx, 31, Feature::avx512vl); enable(extended_features_ecx, 1, Feature::avx512vbmi); - enable( - extended_features_ecx, - 14, - Feature::avx512vpopcntdq, - ); + enable(extended_features_ecx, 14, Feature::avx512vpopcntdq); } } } @@ -304,7 +299,10 @@ mod tests { fn compare_with_cupid() { let information = cupid::master().unwrap(); assert_eq!(is_x86_feature_detected!("aes"), information.aesni()); - assert_eq!(is_x86_feature_detected!("pclmulqdq"), information.pclmulqdq()); + assert_eq!( + is_x86_feature_detected!("pclmulqdq"), + information.pclmulqdq() + ); assert_eq!(is_x86_feature_detected!("rdrand"), information.rdrand()); assert_eq!(is_x86_feature_detected!("rdseed"), information.rdseed()); assert_eq!(is_x86_feature_detected!("tsc"), information.tsc()); @@ -358,13 +356,7 @@ mod tests { is_x86_feature_detected!("cmpxchg16b"), information.cmpxchg16b(), ); - assert_eq!( - is_x86_feature_detected!("adx"), - information.adx(), - ); - assert_eq!( - is_x86_feature_detected!("rtm"), - information.rtm(), - ); + assert_eq!(is_x86_feature_detected!("adx"), information.adx(),); + assert_eq!(is_x86_feature_detected!("rtm"), information.rtm(),); } } |
