Skip to content

Commit ae0a3e6

Browse files
xiangzhaiheiher
authored andcommitted
std_detect: Add support for LoongArch
Co-authored-by: WANG Rui <[email protected]>
1 parent c42d479 commit ae0a3e6

File tree

8 files changed

+96
-2
lines changed

8 files changed

+96
-2
lines changed

crates/std_detect/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ crate from working on applications in which `std` is not available.
5353
[`cupid`](https://crates.io/crates/cupid) crate.
5454

5555
* Linux/Android:
56-
* `arm{32, 64}`, `mips{32,64}{,el}`, `powerpc{32,64}{,le}`, `riscv{32,64}`: `std_detect`
56+
* `arm{32, 64}`, `mips{32,64}{,el}`, `powerpc{32,64}{,le}`, `riscv{32,64}`, `loongarch64`: `std_detect`
5757
supports these on Linux by querying ELF auxiliary vectors (using `getauxval`
5858
when available), and if that fails, by querying `/proc/cpuinfo`.
5959
* `arm64`: partial support for doing run-time feature detection by directly
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Run-time feature detection on LoongArch.
2+
3+
features! {
4+
@TARGET: loongarch;
5+
@CFG: target_arch = "loongarch64";
6+
@MACRO_NAME: is_loongarch_feature_detected;
7+
@MACRO_ATTRS:
8+
/// Checks if `loongarch` feature is enabled.
9+
/// Supported arguments are:
10+
///
11+
/// * `"lam"`
12+
/// * `"ual"`
13+
/// * `"fpu"`
14+
/// * `"lsx"`
15+
/// * `"lasx"`
16+
/// * `"crc32"`
17+
/// * `"complex"`
18+
/// * `"crypto"`
19+
/// * `"lvz"`
20+
/// * `"lbtx86"`
21+
/// * `"lbtarm"`
22+
/// * `"lbtmips"`
23+
#[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")]
24+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lam: "lam";
25+
/// LAM
26+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] ual: "ual";
27+
/// UAL
28+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] fpu: "fpu";
29+
/// FPU
30+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lsx: "lsx";
31+
/// LSX
32+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lasx: "lasx";
33+
/// LASX
34+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] crc32: "crc32";
35+
/// FPU
36+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] complex: "complex";
37+
/// Complex
38+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] crypto: "crypto";
39+
/// Crypto
40+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lvz: "lvz";
41+
/// LVZ
42+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lbtx86: "lbtx86";
43+
/// LBT.X86
44+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lbtarm: "lbtarm";
45+
/// LBT.ARM
46+
@FEATURE: #[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")] lbtmips: "lbtmips";
47+
/// LBT.MIPS
48+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod powerpc64;
1919
mod mips;
2020
#[macro_use]
2121
mod mips64;
22+
#[macro_use]
23+
mod loongarch;
2224

2325
cfg_if! {
2426
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
@@ -45,6 +47,9 @@ cfg_if! {
4547
} else if #[cfg(target_arch = "mips64")] {
4648
#[unstable(feature = "stdarch_mips_feature_detection", issue = "111188")]
4749
pub use mips64::*;
50+
} else if #[cfg(target_arch = "loongarch64")] {
51+
#[unstable(feature = "stdarch_loongarch_feature_detection", issue = "117425")]
52+
pub use loongarch::*;
4853
} else {
4954
// Unimplemented architecture:
5055
#[doc(hidden)]

crates/std_detect/src/detect/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
9696
target_arch = "powerpc64",
9797
target_arch = "mips",
9898
target_arch = "mips64",
99+
target_arch = "loongarch64",
99100
))] {
100101
(0_u8..Feature::_last as u8).map(|discriminant: u8| {
101102
#[allow(bindings_with_variant_name)] // RISC-V has Feature::f

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
129129
target_arch = "riscv32",
130130
target_arch = "riscv64",
131131
target_arch = "mips",
132-
target_arch = "mips64"
132+
target_arch = "mips64",
133+
target_arch = "loongarch64",
133134
))]
134135
{
135136
let hwcap = unsafe { libc::getauxval(AT_HWCAP as libc::c_ulong) as usize };
@@ -225,6 +226,7 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
225226
target_arch = "riscv64",
226227
target_arch = "mips",
227228
target_arch = "mips64",
229+
target_arch = "loongarch64",
228230
))]
229231
{
230232
for el in buf.chunks(2) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Run-time feature detection for LoongArch on Linux.
2+
3+
use super::auxvec;
4+
use crate::detect::{bit, cache, Feature};
5+
6+
/// Try to read the features from the auxiliary vector.
7+
pub(crate) fn detect_features() -> cache::Initializer {
8+
let mut value = cache::Initializer::default();
9+
let enable_feature = |value: &mut cache::Initializer, feature, enable| {
10+
if enable {
11+
value.set(feature as u32);
12+
}
13+
};
14+
15+
// The values are part of the platform-specific [asm/hwcap.h][hwcap]
16+
//
17+
// [hwcap]: https://github.com/torvalds/linux/blob/master/arch/loongarch/include/uapi/asm/hwcap.h
18+
if let Ok(auxv) = auxvec::auxv() {
19+
enable_feature(&mut value, Feature::lam, bit::test(auxv.hwcap, 1));
20+
enable_feature(&mut value, Feature::ual, bit::test(auxv.hwcap, 2));
21+
enable_feature(&mut value, Feature::fpu, bit::test(auxv.hwcap, 3));
22+
enable_feature(&mut value, Feature::lsx, bit::test(auxv.hwcap, 4));
23+
enable_feature(&mut value, Feature::lasx, bit::test(auxv.hwcap, 5));
24+
enable_feature(&mut value, Feature::crc32, bit::test(auxv.hwcap, 6));
25+
enable_feature(&mut value, Feature::complex, bit::test(auxv.hwcap, 7));
26+
enable_feature(&mut value, Feature::crypto, bit::test(auxv.hwcap, 8));
27+
enable_feature(&mut value, Feature::lvz, bit::test(auxv.hwcap, 9));
28+
enable_feature(&mut value, Feature::lbtx86, bit::test(auxv.hwcap, 10));
29+
enable_feature(&mut value, Feature::lbtarm, bit::test(auxv.hwcap, 11));
30+
enable_feature(&mut value, Feature::lbtmips, bit::test(auxv.hwcap, 12));
31+
return value;
32+
}
33+
value
34+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ cfg_if::cfg_if! {
5454
} else if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] {
5555
mod powerpc;
5656
pub(crate) use self::powerpc::detect_features;
57+
} else if #[cfg(target_arch = "loongarch64")] {
58+
mod loongarch;
59+
pub(crate) use self::loongarch::detect_features;
5760
} else {
5861
use crate::detect::cache;
5962
/// Performs run-time feature detection.

crates/std_detect/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! * `mips64`: [`is_mips64_feature_detected`]
1212
//! * `powerpc`: [`is_powerpc_feature_detected`]
1313
//! * `powerpc64`: [`is_powerpc64_feature_detected`]
14+
//! * `loongarch`: [`is_loongarch_feature_detected`]
1415
1516
#![stable(feature = "stdsimd", since = "1.27.0")]
1617
#![feature(staged_api, doc_cfg, allow_internal_unstable)]

0 commit comments

Comments
 (0)