Skip to content

Commit 896b671

Browse files
committed
RISC-V: Remove enable_features
This commit prepares common infrastructure for extension implication by removing `enable_features` closure which makes each feature test longer (because it needs extra `value` argument each time we test a feature). It comes with the overhead to enable each feature separately but later mitigated by the OS-independent extension implication logic.
1 parent 203faee commit 896b671

File tree

1 file changed

+13
-45
lines changed
  • crates/std_detect/src/detect/os/linux

1 file changed

+13
-45
lines changed

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

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,38 @@ use crate::detect::{Feature, bit, cache};
66
/// Read list of supported features from the auxiliary vector.
77
pub(crate) fn detect_features() -> cache::Initializer {
88
let mut value = cache::Initializer::default();
9-
let enable_feature = |value: &mut cache::Initializer, feature, enable| {
9+
let mut enable_feature = |feature, enable| {
1010
if enable {
1111
value.set(feature as u32);
1212
}
1313
};
14-
let enable_features = |value: &mut cache::Initializer, feature_slice: &[Feature], enable| {
15-
if enable {
16-
for feature in feature_slice {
17-
value.set(*feature as u32);
18-
}
19-
}
20-
};
2114

2215
// Use auxiliary vector to enable single-letter ISA extensions and Zicsr.
2316
// The values are part of the platform-specific [asm/hwcap.h][hwcap]
2417
//
2518
// [hwcap]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/riscv/include/uapi/asm/hwcap.h?h=v6.14
2619
let auxv = auxvec::auxv().expect("read auxvec"); // should not fail on RISC-V platform
2720
#[allow(clippy::eq_op)]
28-
enable_feature(
29-
&mut value,
30-
Feature::a,
31-
bit::test(auxv.hwcap, (b'a' - b'a').into()),
32-
);
33-
enable_feature(
34-
&mut value,
35-
Feature::c,
36-
bit::test(auxv.hwcap, (b'c' - b'a').into()),
37-
);
38-
enable_features(
39-
&mut value,
40-
&[Feature::d, Feature::f, Feature::zicsr],
41-
bit::test(auxv.hwcap, (b'd' - b'a').into()),
42-
);
43-
enable_features(
44-
&mut value,
45-
&[Feature::f, Feature::zicsr],
46-
bit::test(auxv.hwcap, (b'f' - b'a').into()),
47-
);
48-
enable_feature(
49-
&mut value,
50-
Feature::h,
51-
bit::test(auxv.hwcap, (b'h' - b'a').into()),
52-
);
53-
enable_feature(
54-
&mut value,
55-
Feature::m,
56-
bit::test(auxv.hwcap, (b'm' - b'a').into()),
57-
);
21+
enable_feature(Feature::a, bit::test(auxv.hwcap, (b'a' - b'a').into()));
22+
enable_feature(Feature::c, bit::test(auxv.hwcap, (b'c' - b'a').into()));
23+
let has_d = bit::test(auxv.hwcap, (b'd' - b'a').into());
24+
let has_f = bit::test(auxv.hwcap, (b'f' - b'a').into());
25+
enable_feature(Feature::d, has_d);
26+
enable_feature(Feature::f, has_d | has_f);
27+
enable_feature(Feature::zicsr, has_d | has_f);
28+
enable_feature(Feature::h, bit::test(auxv.hwcap, (b'h' - b'a').into()));
29+
enable_feature(Feature::m, bit::test(auxv.hwcap, (b'm' - b'a').into()));
5830

5931
// Handle base ISA.
6032
let has_i = bit::test(auxv.hwcap, (b'i' - b'a').into());
6133
// If future RV128I is supported, implement with `enable_feature` here
6234
#[cfg(target_pointer_width = "64")]
63-
enable_feature(&mut value, Feature::rv64i, has_i);
35+
enable_feature(Feature::rv64i, has_i);
6436
#[cfg(target_pointer_width = "32")]
65-
enable_feature(&mut value, Feature::rv32i, has_i);
37+
enable_feature(Feature::rv32i, has_i);
6638
// FIXME: e is not exposed in any of asm/hwcap.h, uapi/asm/hwcap.h, uapi/asm/hwprobe.h
6739
#[cfg(target_pointer_width = "32")]
68-
enable_feature(
69-
&mut value,
70-
Feature::rv32e,
71-
bit::test(auxv.hwcap, (b'e' - b'a').into()),
72-
);
40+
enable_feature(Feature::rv32e, bit::test(auxv.hwcap, (b'e' - b'a').into()));
7341

7442
// FIXME: Auxvec does not show supervisor feature support, but this mode may be useful
7543
// to detect when Rust is used to write Linux kernel modules.

0 commit comments

Comments
 (0)