Skip to main content

lib3mf_core/utils/
hardware.rs

1use serde::{Deserialize, Serialize};
2
3/// Hardware capabilities of the current system, used for informational statistics.
4#[derive(Debug, Clone, Default, Serialize, Deserialize)]
5pub struct HardwareCapabilities {
6    /// CPU architecture string (e.g., `"x86_64"`, `"aarch64"`).
7    pub architecture: String,
8    /// Number of available CPU threads (uses rayon thread count if parallel feature is enabled).
9    pub num_cpus: usize,
10    /// List of detected SIMD feature strings (e.g., `["sse", "sse2", "avx2"]`).
11    pub simd_features: Vec<String>,
12}
13
14/// Detects and returns the hardware capabilities of the current system.
15pub fn detect_capabilities() -> HardwareCapabilities {
16    #[allow(unused_mut)]
17    let mut features = Vec::new();
18
19    #[cfg(target_arch = "x86_64")]
20    {
21        if std::is_x86_feature_detected!("sse") {
22            features.push("sse".to_string());
23        }
24        if std::is_x86_feature_detected!("sse2") {
25            features.push("sse2".to_string());
26        }
27        if std::is_x86_feature_detected!("sse3") {
28            features.push("sse3".to_string());
29        }
30        if std::is_x86_feature_detected!("sse4.1") {
31            features.push("sse4.1".to_string());
32        }
33        if std::is_x86_feature_detected!("sse4.2") {
34            features.push("sse4.2".to_string());
35        }
36        if std::is_x86_feature_detected!("avx") {
37            features.push("avx".to_string());
38        }
39        if std::is_x86_feature_detected!("avx2") {
40            features.push("avx2".to_string());
41        }
42    }
43
44    #[cfg(target_arch = "aarch64")]
45    {
46        // On AArch64 neon is usually always present, but we can check specifically if needed
47        // using the cpufeatures crate or other methods.
48        features.push("neon".to_string());
49    }
50
51    HardwareCapabilities {
52        architecture: std::env::consts::ARCH.to_string(),
53        #[cfg(feature = "parallel")]
54        num_cpus: rayon::current_num_threads(),
55        #[cfg(not(feature = "parallel"))]
56        num_cpus: 1,
57        simd_features: features,
58    }
59}