Skip to content

Commit 6381cf7

Browse files
heiherAmanieu
authored andcommitted
std_detect: loongarch: Add runtime detectable features from LLVM
* f * d * frecipe * lbt
1 parent 4f17ac5 commit 6381cf7

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

crates/std_detect/src/detect/arch/loongarch.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,29 @@ features! {
88
/// Checks if `loongarch` feature is enabled.
99
/// Supported arguments are:
1010
///
11-
/// * `"ual"`
11+
/// * `"f"`
12+
/// * `"d"`
13+
/// * `"frecipe"`
1214
/// * `"lsx"`
1315
/// * `"lasx"`
16+
/// * `"lbt"`
1417
/// * `"lvz"`
18+
/// * `"ual"`
1519
#[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")]
16-
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] ual: "ual";
17-
/// UAL
20+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] f: "f";
21+
/// F
22+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] d: "d";
23+
/// D
24+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] frecipe: "frecipe";
25+
/// Frecipe
1826
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lsx: "lsx";
1927
/// LSX
2028
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lasx: "lasx";
2129
/// LASX
30+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lbt: "lbt";
31+
/// LBT
2232
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lvz: "lvz";
2333
/// LVZ
34+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] ual: "ual";
35+
/// UAL
2436
}

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use super::auxvec;
44
use crate::detect::{bit, cache, Feature};
5+
use core::arch::asm;
56

67
/// Try to read the features from the auxiliary vector.
78
pub(crate) fn detect_features() -> cache::Initializer {
@@ -12,14 +13,42 @@ pub(crate) fn detect_features() -> cache::Initializer {
1213
}
1314
};
1415

16+
// The values are part of the platform-specific [cpucfg]
17+
//
18+
// [cpucfg]: LoongArch Reference Manual Volume 1: Basic Architecture v1.1
19+
let cpucfg2: usize;
20+
unsafe {
21+
asm!(
22+
"cpucfg {}, {}",
23+
out(reg) cpucfg2, in(reg) 2,
24+
options(pure, nomem, preserves_flags, nostack)
25+
);
26+
}
27+
enable_feature(&mut value, Feature::frecipe, bit::test(cpucfg2, 25));
28+
1529
// The values are part of the platform-specific [asm/hwcap.h][hwcap]
1630
//
1731
// [hwcap]: https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
1832
if let Ok(auxv) = auxvec::auxv() {
19-
enable_feature(&mut value, Feature::ual, bit::test(auxv.hwcap, 2));
33+
enable_feature(
34+
&mut value,
35+
Feature::f,
36+
bit::test(cpucfg2, 1) && bit::test(auxv.hwcap, 3),
37+
);
38+
enable_feature(
39+
&mut value,
40+
Feature::d,
41+
bit::test(cpucfg2, 2) && bit::test(auxv.hwcap, 3),
42+
);
2043
enable_feature(&mut value, Feature::lsx, bit::test(auxv.hwcap, 4));
2144
enable_feature(&mut value, Feature::lasx, bit::test(auxv.hwcap, 5));
45+
enable_feature(
46+
&mut value,
47+
Feature::lbt,
48+
bit::test(auxv.hwcap, 10) && bit::test(auxv.hwcap, 11) && bit::test(auxv.hwcap, 12),
49+
);
2250
enable_feature(&mut value, Feature::lvz, bit::test(auxv.hwcap, 9));
51+
enable_feature(&mut value, Feature::ual, bit::test(auxv.hwcap, 2));
2352
return value;
2453
}
2554
value

0 commit comments

Comments
 (0)