Skip to content

Commit b346384

Browse files
committed
Reformat std_detect
1 parent 0efd402 commit b346384

File tree

13 files changed

+60
-206
lines changed

13 files changed

+60
-206
lines changed

library/std_detect/src/detect/cache.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
44
#![allow(dead_code)] // not used on all platforms
55

6-
use core::sync::atomic::Ordering;
7-
8-
use core::sync::atomic::AtomicUsize;
6+
use core::sync::atomic::{AtomicUsize, Ordering};
97

108
/// Sets the `bit` of `x`.
119
#[inline]
@@ -40,31 +38,22 @@ impl Initializer {
4038
/// Tests the `bit` of the cache.
4139
#[inline]
4240
pub(crate) fn test(self, bit: u32) -> bool {
43-
debug_assert!(
44-
bit < CACHE_CAPACITY,
45-
"too many features, time to increase the cache size!"
46-
);
41+
debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
4742
test_bit(self.0, bit)
4843
}
4944

5045
/// Sets the `bit` of the cache.
5146
#[inline]
5247
pub(crate) fn set(&mut self, bit: u32) {
53-
debug_assert!(
54-
bit < CACHE_CAPACITY,
55-
"too many features, time to increase the cache size!"
56-
);
48+
debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
5749
let v = self.0;
5850
self.0 = set_bit(v, bit);
5951
}
6052

6153
/// Unsets the `bit` of the cache.
6254
#[inline]
6355
pub(crate) fn unset(&mut self, bit: u32) {
64-
debug_assert!(
65-
bit < CACHE_CAPACITY,
66-
"too many features, time to increase the cache size!"
67-
);
56+
debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
6857
let v = self.0;
6958
self.0 = unset_bit(v, bit);
7059
}
@@ -73,11 +62,7 @@ impl Initializer {
7362
/// This global variable is a cache of the features supported by the CPU.
7463
// Note: the third slot is only used in x86
7564
// Another Slot can be added if needed without any change to `Initializer`
76-
static CACHE: [Cache; 3] = [
77-
Cache::uninitialized(),
78-
Cache::uninitialized(),
79-
Cache::uninitialized(),
80-
];
65+
static CACHE: [Cache; 3] = [Cache::uninitialized(), Cache::uninitialized(), Cache::uninitialized()];
8166

8267
/// Feature cache with capacity for `size_of::<usize>() * 8 - 1` features.
8368
///
@@ -104,19 +89,14 @@ impl Cache {
10489
#[inline]
10590
pub(crate) fn test(&self, bit: u32) -> Option<bool> {
10691
let cached = self.0.load(Ordering::Relaxed);
107-
if cached == 0 {
108-
None
109-
} else {
110-
Some(test_bit(cached as u128, bit))
111-
}
92+
if cached == 0 { None } else { Some(test_bit(cached as u128, bit)) }
11293
}
11394

11495
/// Initializes the cache.
11596
#[inline]
11697
fn initialize(&self, value: usize) -> usize {
11798
debug_assert_eq!((value & !Cache::MASK), 0);
118-
self.0
119-
.store(value | Cache::INITIALIZED_BIT, Ordering::Relaxed);
99+
self.0.store(value | Cache::INITIALIZED_BIT, Ordering::Relaxed);
120100
value
121101
}
122102
}
@@ -217,7 +197,5 @@ pub(crate) fn test(bit: u32) -> bool {
217197
} else {
218198
(bit - 2 * Cache::CAPACITY, 2)
219199
};
220-
CACHE[idx]
221-
.test(relative_bit)
222-
.unwrap_or_else(|| detect_and_initialize().test(bit))
200+
CACHE[idx].test(relative_bit).unwrap_or_else(|| detect_and_initialize().test(bit))
223201
}

library/std_detect/src/detect/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ mod arch;
2929
#[doc(hidden)]
3030
#[unstable(feature = "stdarch_internal", issue = "none")]
3131
pub use self::arch::__is_feature_detected;
32-
3332
pub(crate) use self::arch::Feature;
3433

3534
mod bit;

library/std_detect/src/detect/os/aarch64.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
//! - [Linux documentation](https://www.kernel.org/doc/Documentation/arm64/cpu-feature-registers.txt)
1818
//! - [ARM documentation](https://developer.arm.com/documentation/ddi0601/2022-12/AArch64-Registers?lang=en)
1919
20-
use crate::detect::{Feature, cache};
2120
use core::arch::asm;
2221

22+
use crate::detect::{Feature, cache};
23+
2324
/// Try to read the features from the system registers.
2425
///
2526
/// This will cause SIGILL if the current OS is not trapping the mrs instruction.
@@ -104,10 +105,7 @@ pub(crate) fn parse_system_registers(
104105
let sha2 = bits_shift(aa64isar0, 15, 12) >= 1;
105106
enable_feature(Feature::sha2, asimd && sha1 && sha2);
106107
enable_feature(Feature::rdm, asimd && bits_shift(aa64isar0, 31, 28) >= 1);
107-
enable_feature(
108-
Feature::dotprod,
109-
asimd && bits_shift(aa64isar0, 47, 44) >= 1,
110-
);
108+
enable_feature(Feature::dotprod, asimd && bits_shift(aa64isar0, 47, 44) >= 1);
111109
enable_feature(Feature::sve, asimd && bits_shift(aa64pfr0, 35, 32) >= 1);
112110
}
113111

library/std_detect/src/detect/os/darwin/aarch64.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//!
33
//! <https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics>
44
5-
use crate::detect::{Feature, cache};
65
use core::ffi::CStr;
76

7+
use crate::detect::{Feature, cache};
8+
89
#[inline]
910
fn _sysctlbyname(name: &CStr) -> bool {
1011
use libc;
@@ -14,13 +15,7 @@ fn _sysctlbyname(name: &CStr) -> bool {
1415
let enabled_ptr = &mut enabled as *mut i32 as *mut libc::c_void;
1516

1617
let ret = unsafe {
17-
libc::sysctlbyname(
18-
name.as_ptr(),
19-
enabled_ptr,
20-
&mut enabled_len,
21-
core::ptr::null_mut(),
22-
0,
23-
)
18+
libc::sysctlbyname(name.as_ptr(), enabled_ptr, &mut enabled_len, core::ptr::null_mut(), 0)
2419
};
2520

2621
match ret {

library/std_detect/src/detect/os/freebsd/auxvec.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ fn archauxv(key: libc::c_int) -> usize {
5454
// https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/auxv.h
5555
// FreeBSD 11 support in std has been removed in Rust 1.75 (https://github.com/rust-lang/rust/pull/114521),
5656
// so we can safely use this function.
57-
let res = libc::elf_aux_info(
58-
key,
59-
&mut out as *mut libc::c_ulong as *mut libc::c_void,
60-
OUT_LEN,
61-
);
57+
let res =
58+
libc::elf_aux_info(key, &mut out as *mut libc::c_ulong as *mut libc::c_void, OUT_LEN);
6259
// If elf_aux_info fails, `out` will be left at zero (which is the proper default value).
6360
debug_assert!(res == 0 || out == 0);
6461
}

library/std_detect/src/detect/os/linux/aarch64.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,8 @@ impl AtHwcap {
343343
enable_feature(Feature::sve2, sve2);
344344
enable_feature(Feature::sve2p1, self.sve2p1 && sve2);
345345
// SVE2 extensions require SVE2 and crypto features
346-
enable_feature(
347-
Feature::sve2_aes,
348-
self.sveaes && self.svepmull && sve2 && self.aes,
349-
);
350-
enable_feature(
351-
Feature::sve2_sm4,
352-
self.svesm4 && sve2 && self.sm3 && self.sm4,
353-
);
346+
enable_feature(Feature::sve2_aes, self.sveaes && self.svepmull && sve2 && self.aes);
347+
enable_feature(Feature::sve2_sm4, self.svesm4 && sve2 && self.sm3 && self.sm4);
354348
enable_feature(
355349
Feature::sve2_sha3,
356350
self.svesha3 && sve2 && self.sha512 && self.sha3 && self.sha1 && self.sha2,

library/std_detect/src/detect/os/linux/loongarch.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Run-time feature detection for LoongArch on Linux.
22
3+
use core::arch::asm;
4+
35
use super::auxvec;
46
use crate::detect::{Feature, bit, cache};
5-
use core::arch::asm;
67

78
/// Try to read the features from the auxiliary vector.
89
pub(crate) fn detect_features() -> cache::Initializer {
@@ -43,16 +44,8 @@ pub(crate) fn detect_features() -> cache::Initializer {
4344
//
4445
// [hwcap]: https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
4546
if let Ok(auxv) = auxvec::auxv() {
46-
enable_feature(
47-
&mut value,
48-
Feature::f,
49-
bit::test(cpucfg2, 1) && bit::test(auxv.hwcap, 3),
50-
);
51-
enable_feature(
52-
&mut value,
53-
Feature::d,
54-
bit::test(cpucfg2, 2) && bit::test(auxv.hwcap, 3),
55-
);
47+
enable_feature(&mut value, Feature::f, bit::test(cpucfg2, 1) && bit::test(auxv.hwcap, 3));
48+
enable_feature(&mut value, Feature::d, bit::test(cpucfg2, 2) && bit::test(auxv.hwcap, 3));
5649
enable_feature(&mut value, Feature::lsx, bit::test(auxv.hwcap, 4));
5750
enable_feature(&mut value, Feature::lasx, bit::test(auxv.hwcap, 5));
5851
enable_feature(

library/std_detect/src/detect/os/linux/riscv.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@ fn _riscv_hwprobe(out: &mut [riscv_hwprobe]) -> bool {
112112
cpus: *mut libc::c_ulong,
113113
flags: libc::c_uint,
114114
) -> libc::c_long {
115-
unsafe {
116-
libc::syscall(
117-
__NR_riscv_hwprobe,
118-
pairs,
119-
pair_count,
120-
cpu_set_size,
121-
cpus,
122-
flags,
123-
)
124-
}
115+
unsafe { libc::syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_set_size, cpus, flags) }
125116
}
126117

127118
let len = out.len();
@@ -157,26 +148,11 @@ pub(crate) fn detect_features() -> cache::Initializer {
157148
// performance-related capabilities.
158149
'hwprobe: {
159150
let mut out = [
160-
riscv_hwprobe {
161-
key: RISCV_HWPROBE_KEY_BASE_BEHAVIOR,
162-
value: 0,
163-
},
164-
riscv_hwprobe {
165-
key: RISCV_HWPROBE_KEY_IMA_EXT_0,
166-
value: 0,
167-
},
168-
riscv_hwprobe {
169-
key: RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF,
170-
value: 0,
171-
},
172-
riscv_hwprobe {
173-
key: RISCV_HWPROBE_KEY_MISALIGNED_VECTOR_PERF,
174-
value: 0,
175-
},
176-
riscv_hwprobe {
177-
key: RISCV_HWPROBE_KEY_CPUPERF_0,
178-
value: 0,
179-
},
151+
riscv_hwprobe { key: RISCV_HWPROBE_KEY_BASE_BEHAVIOR, value: 0 },
152+
riscv_hwprobe { key: RISCV_HWPROBE_KEY_IMA_EXT_0, value: 0 },
153+
riscv_hwprobe { key: RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF, value: 0 },
154+
riscv_hwprobe { key: RISCV_HWPROBE_KEY_MISALIGNED_VECTOR_PERF, value: 0 },
155+
riscv_hwprobe { key: RISCV_HWPROBE_KEY_CPUPERF_0, value: 0 },
180156
];
181157
if !_riscv_hwprobe(&mut out) {
182158
break 'hwprobe;

library/std_detect/src/detect/os/openbsd/aarch64.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
//! https://github.com/openbsd/src/commit/d335af936b9d7dd9cf655cae1ce19560c45de6c8
55
//! https://github.com/golang/go/commit/cd54ef1f61945459486e9eea2f016d99ef1da925
66
7+
use core::mem::MaybeUninit;
8+
use core::ptr;
9+
710
use crate::detect::cache;
8-
use core::{mem::MaybeUninit, ptr};
911

1012
// Defined in machine/cpu.h.
1113
// https://github.com/openbsd/src/blob/72ccc03bd11da614f31f7ff76e3f6fce99bc1c79/sys/arch/arm64/include/cpu.h#L25-L40

library/std_detect/src/detect/os/x86.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use core::arch::x86::*;
55
#[cfg(target_arch = "x86_64")]
66
use core::arch::x86_64::*;
7-
87
use core::mem;
98

109
use crate::detect::{Feature, bit, cache};
@@ -42,12 +41,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
4241
// 0x8000_0000]. - The vendor ID is stored in 12 u8 ascii chars,
4342
// returned in EBX, EDX, and ECX (in that order):
4443
let (max_basic_leaf, vendor_id) = unsafe {
45-
let CpuidResult {
46-
eax: max_basic_leaf,
47-
ebx,
48-
ecx,
49-
edx,
50-
} = __cpuid(0);
44+
let CpuidResult { eax: max_basic_leaf, ebx, ecx, edx } = __cpuid(0);
5145
let vendor_id: [[u8; 4]; 3] = [ebx.to_ne_bytes(), edx.to_ne_bytes(), ecx.to_ne_bytes()];
5246
let vendor_id: [u8; 12] = mem::transmute(vendor_id);
5347
(max_basic_leaf, vendor_id)
@@ -60,11 +54,8 @@ pub(crate) fn detect_features() -> cache::Initializer {
6054

6155
// EAX = 1, ECX = 0: Queries "Processor Info and Feature Bits";
6256
// Contains information about most x86 features.
63-
let CpuidResult {
64-
ecx: proc_info_ecx,
65-
edx: proc_info_edx,
66-
..
67-
} = unsafe { __cpuid(0x0000_0001_u32) };
57+
let CpuidResult { ecx: proc_info_ecx, edx: proc_info_edx, .. } =
58+
unsafe { __cpuid(0x0000_0001_u32) };
6859

6960
// EAX = 7: Queries "Extended Features";
7061
// Contains information about bmi,bmi2, and avx2 support.
@@ -76,11 +67,8 @@ pub(crate) fn detect_features() -> cache::Initializer {
7667
extended_features_edx_leaf_1,
7768
) = if max_basic_leaf >= 7 {
7869
let CpuidResult { ebx, ecx, edx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
79-
let CpuidResult {
80-
eax: eax_1,
81-
edx: edx_1,
82-
..
83-
} = unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
70+
let CpuidResult { eax: eax_1, edx: edx_1, .. } =
71+
unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
8472
(ebx, ecx, edx, eax_1, edx_1)
8573
} else {
8674
(0, 0, 0, 0, 0) // CPUID does not support "Extended Features"
@@ -89,10 +77,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
8977
// EAX = 0x8000_0000, ECX = 0: Get Highest Extended Function Supported
9078
// - EAX returns the max leaf value for extended information, that is,
9179
// `cpuid` calls in range [0x8000_0000; u32::MAX]:
92-
let CpuidResult {
93-
eax: extended_max_basic_leaf,
94-
..
95-
} = unsafe { __cpuid(0x8000_0000_u32) };
80+
let CpuidResult { eax: extended_max_basic_leaf, .. } = unsafe { __cpuid(0x8000_0000_u32) };
9681

9782
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
9883
// Bits"
@@ -208,10 +193,8 @@ pub(crate) fn detect_features() -> cache::Initializer {
208193
// Processor Extended State Enumeration Sub-leaf (EAX = 0DH,
209194
// ECX = 1):
210195
if max_basic_leaf >= 0xd {
211-
let CpuidResult {
212-
eax: proc_extended_state1_eax,
213-
..
214-
} = unsafe { __cpuid_count(0xd_u32, 1) };
196+
let CpuidResult { eax: proc_extended_state1_eax, .. } =
197+
unsafe { __cpuid_count(0xd_u32, 1) };
215198
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
216199
enable(proc_extended_state1_eax, 1, Feature::xsavec);
217200
enable(proc_extended_state1_eax, 3, Feature::xsaves);
@@ -269,10 +252,8 @@ pub(crate) fn detect_features() -> cache::Initializer {
269252
enable(extended_features_edx_leaf_1, 8, Feature::amx_complex);
270253

271254
if max_basic_leaf >= 0x1e {
272-
let CpuidResult {
273-
eax: amx_feature_flags_eax,
274-
..
275-
} = unsafe { __cpuid_count(0x1e_u32, 1) };
255+
let CpuidResult { eax: amx_feature_flags_eax, .. } =
256+
unsafe { __cpuid_count(0x1e_u32, 1) };
276257

277258
enable(amx_feature_flags_eax, 4, Feature::amx_fp8);
278259
enable(amx_feature_flags_eax, 5, Feature::amx_transpose);

0 commit comments

Comments
 (0)