OXYGENHUB
# 569802ebae0e oxygen / vendor / crc-fast / src / bin / arch-check.rs
3.4 KiB · Rust · last changed Strata: the cloud stores send through ureq, so every release target links 1 month ago source blame history raw
1
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
2
3
//! This is a simple program that checks if the target architecture supports certain features.
4
5
#[cfg(target_arch = "aarch64")]
6
use std::arch::is_aarch64_feature_detected;
7
8
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
9
use std::arch::is_x86_feature_detected;
10
11
use crc_fast::get_calculator_target;
12
use crc_fast::CrcAlgorithm::{Crc32Iscsi, Crc32IsoHdlc, Crc64Nvme};
13
14
fn main() {
15
    // Check the target architecture and call the appropriate function
16
    #[cfg(target_arch = "aarch64")]
17
    aarch64_features();
18
19
    #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
20
    x86_features();
21
22
    print_targets();
23
24
    print_cpu_info();
25
}
26
27
#[cfg(target_arch = "aarch64")]
28
fn aarch64_features() {
29
    let checkmark: char = '✓';
30
31
    println!("[AArch64] Checking for features...");
32
33
    if is_aarch64_feature_detected!("neon") {
34
        println!("  {checkmark} NEON",);
35
    } else {
36
        println!("  x NEON");
37
    }
38
39
    if is_aarch64_feature_detected!("crc") {
40
        println!("  {checkmark} CRC",);
41
    } else {
42
        println!("  x CRC");
43
    }
44
45
    if is_aarch64_feature_detected!("sha3") {
46
        println!("  {checkmark} SHA3\n",);
47
    } else {
48
        println!("  x SHA3\n");
49
    }
50
}
51
52
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
53
fn x86_features() {
54
    let checkmark: char = '✓';
55
56
    println!("[X86] Checking for features...");
57
58
    if is_x86_feature_detected!("sse2") {
59
        println!("  {checkmark} SSE2",);
60
    } else {
61
        println!("  x SSE2");
62
    }
63
64
    if is_x86_feature_detected!("sse4.1") {
65
        println!("  {checkmark} SSE4.1",);
66
    } else {
67
        println!("  x SSE4.1");
68
    }
69
70
    if is_x86_feature_detected!("pclmulqdq") {
71
        println!("  {checkmark} PCLMULQDQ",);
72
    } else {
73
        println!("  x PCLMULQDQ");
74
    }
75
76
    if is_x86_feature_detected!("avx2") {
77
        println!("  {checkmark} AVX2",);
78
    } else {
79
        println!("  x AVX2");
80
    }
81
82
    if is_x86_feature_detected!("vpclmulqdq") {
83
        println!("  {checkmark} VPCLMULQDQ",);
84
    } else {
85
        println!("  x VPCLMULQDQ");
86
    }
87
88
    if is_x86_feature_detected!("avx512f") {
89
        println!("  {checkmark} AVX512F",);
90
    } else {
91
        println!("  x AVX512F");
92
    }
93
94
    if is_x86_feature_detected!("avx512vl") {
95
        println!("  {checkmark} AVX512VL\n",);
96
    } else {
97
        println!("  x AVX512VL\n");
98
    }
99
}
100
101
/// Print the acceleration targets
102
fn print_targets() {
103
    let checkmark: char = '✓';
104
105
    println!("[Acceleration targets]");
106
107
    println!(
108
        "  {} CRC-32/ISCSI target: {}",
109
        checkmark,
110
        get_calculator_target(Crc32Iscsi)
111
    );
112
    println!(
113
        "  {} CRC-32/ISO-HDLC target: {}",
114
        checkmark,
115
        get_calculator_target(Crc32IsoHdlc)
116
    );
117
    println!(
118
        "  {} CRC-64/NVME target: {}\n",
119
        checkmark,
120
        get_calculator_target(Crc64Nvme)
121
    );
122
}
123
124
/// Print the first entry of /proc/cpuinfo if it's available
125
fn print_cpu_info() {
126
    println!("\n[CPU Info]");
127
    if let Ok(cpuinfo) = std::fs::read_to_string("/proc/cpuinfo") {
128
        // Split the content by double newlines and take the first entry
129
        if let Some(first_cpu) = cpuinfo.split("\n\n").next() {
130
            println!("{first_cpu}",);
131
        } else {
132
            println!("No CPU information found.");
133
        }
134
    } else {
135
        println!("Failed to read /proc/cpuinfo. This may not be available on your platform.\n");
136
    }
137
}