Skip to content

Commit 0620a95

Browse files
committed
crc-fast-rust: fixing the dead_code issue with no_std builds (CI); dropped wasm64 in CI because it's just not readily available yet. sad day for WASM, honestly.
1 parent 4df52a9 commit 0620a95

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ jobs:
204204
# WASI preview 2 (32-bit) - nightly only (experimental)
205205
- target: wasm32-wasip2
206206
rust-toolchain: "nightly"
207-
# WASM 3.0 (64-bit address space) - nightly only (experimental)
208-
- target: wasm64-unknown-unknown
209-
rust-toolchain: "nightly"
207+
# Note: wasm64-unknown-unknown removed - not consistently available in nightly
210208
steps:
211209
- uses: actions/checkout@v4 # not pinning to commit hash since this is a GitHub action, which we trust
212210
- uses: actions-rust-lang/setup-rust-toolchain@9d7e65c320fdb52dcd45ffaa68deb6c02c8754d9 # v1.12.0

src/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static CACHE: Once<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>> = Once::new();
4848
///
4949
/// The cache key implements `Hash`, `Eq`, and `PartialEq` to enable efficient
5050
/// HashMap storage and lookup operations.
51+
#[cfg(any(feature = "std", feature = "cache"))]
5152
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5253
pub(crate) struct CrcParamsCacheKey {
5354
/// CRC width in bits (32 or 64)
@@ -58,6 +59,7 @@ pub(crate) struct CrcParamsCacheKey {
5859
pub reflected: bool,
5960
}
6061

62+
#[cfg(any(feature = "std", feature = "cache"))]
6163
impl CrcParamsCacheKey {
6264
/// Create a new cache key from CRC parameters
6365
///

src/feature_detection.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,41 @@
33
//! Feature detection system for safe and efficient hardware acceleration across different
44
//! platforms.
55
6-
#[cfg(not(feature = "std"))]
6+
#[cfg(all(
7+
not(feature = "std"),
8+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
9+
))]
710
use spin::Once;
8-
#[cfg(feature = "std")]
11+
#[cfg(all(
12+
feature = "std",
13+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
14+
))]
915
use std::sync::OnceLock;
1016

1117
#[cfg(all(feature = "alloc", not(feature = "std")))]
1218
extern crate alloc;
13-
#[cfg(all(feature = "alloc", not(feature = "std")))]
19+
#[cfg(all(
20+
feature = "alloc",
21+
not(feature = "std"),
22+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
23+
))]
1424
use alloc::string::{String, ToString};
15-
#[cfg(feature = "std")]
25+
#[cfg(all(
26+
feature = "std",
27+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
28+
))]
1629
use std::string::{String, ToString};
1730

1831
/// Global ArchOps instance cache - initialized once based on feature detection results
19-
#[cfg(feature = "std")]
32+
#[cfg(all(
33+
feature = "std",
34+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
35+
))]
2036
static ARCH_OPS_INSTANCE: OnceLock<ArchOpsInstance> = OnceLock::new();
21-
#[cfg(not(feature = "std"))]
37+
#[cfg(all(
38+
not(feature = "std"),
39+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
40+
))]
2241
static ARCH_OPS_INSTANCE: Once<ArchOpsInstance> = Once::new();
2342

2443
/// Performance tiers representing different hardware capability levels
@@ -61,7 +80,10 @@ pub struct ArchCapabilities {
6180

6281
/// Helper function to convert a performance tier to a human-readable target string
6382
/// Format: {architecture}-{intrinsics-family}-{intrinsics-features}
64-
#[cfg(feature = "alloc")]
83+
#[cfg(all(
84+
feature = "alloc",
85+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
86+
))]
6587
#[inline(always)]
6688
fn tier_to_target_string(tier: PerformanceTier) -> String {
6789
match tier {
@@ -79,6 +101,7 @@ fn tier_to_target_string(tier: PerformanceTier) -> String {
79101
///
80102
/// # Safety
81103
/// Uses runtime feature detection which may access CPU-specific registers
104+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
82105
unsafe fn detect_arch_capabilities() -> ArchCapabilities {
83106
#[cfg(target_arch = "aarch64")]
84107
{
@@ -258,6 +281,7 @@ pub(crate) fn select_performance_tier(capabilities: &ArchCapabilities) -> Perfor
258281

259282
/// Enum that holds the different ArchOps implementations for compile-time dispatch
260283
/// This avoids the need for trait objects while still providing factory-based selection
284+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
261285
#[rustversion::since(1.89)]
262286
#[derive(Debug, Clone, Copy)]
263287
pub enum ArchOpsInstance {
@@ -275,6 +299,7 @@ pub enum ArchOpsInstance {
275299
SoftwareFallback,
276300
}
277301

302+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
278303
#[rustversion::before(1.89)]
279304
#[derive(Debug, Clone, Copy)]
280305
pub enum ArchOpsInstance {
@@ -288,6 +313,7 @@ pub enum ArchOpsInstance {
288313
SoftwareFallback,
289314
}
290315

316+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
291317
impl ArchOpsInstance {
292318
#[inline(always)]
293319
#[rustversion::since(1.89)]
@@ -334,12 +360,18 @@ impl ArchOpsInstance {
334360
/// This function provides access to the cached ArchOps instance that was selected based on
335361
/// feature detection results at library initialization time, eliminating runtime feature
336362
/// detection overhead from hot paths.
337-
#[cfg(feature = "std")]
363+
#[cfg(all(
364+
feature = "std",
365+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
366+
))]
338367
pub fn get_arch_ops() -> &'static ArchOpsInstance {
339368
ARCH_OPS_INSTANCE.get_or_init(create_arch_ops)
340369
}
341370

342-
#[cfg(not(feature = "std"))]
371+
#[cfg(all(
372+
not(feature = "std"),
373+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
374+
))]
343375
pub fn get_arch_ops() -> &'static ArchOpsInstance {
344376
ARCH_OPS_INSTANCE.call_once(create_arch_ops)
345377
}
@@ -349,6 +381,7 @@ pub fn get_arch_ops() -> &'static ArchOpsInstance {
349381
/// This function uses the cached feature detection results to select the optimal
350382
/// architecture-specific implementation at library initialization time, eliminating
351383
/// runtime feature detection overhead from hot paths.
384+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
352385
fn create_arch_ops() -> ArchOpsInstance {
353386
let capabilities = unsafe { detect_arch_capabilities() };
354387
let tier = select_performance_tier(&capabilities);
@@ -358,6 +391,7 @@ fn create_arch_ops() -> ArchOpsInstance {
358391

359392
/// Helper function to create ArchOpsInstance from a performance tier for Rust 1.89+ (when AVX512
360393
/// stabilized)
394+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
361395
#[rustversion::since(1.89)]
362396
fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
363397
match tier {
@@ -400,6 +434,7 @@ fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
400434

401435
/// Helper function to create ArchOpsInstance from a performance tier for Rust <1.89 (before AVX512
402436
/// stabilized)
437+
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
403438
#[rustversion::before(1.89)]
404439
fn create_arch_ops_from_tier(tier: PerformanceTier) -> ArchOpsInstance {
405440
match tier {

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,28 @@ pub fn checksum_combine_with_params(
860860
/// // "x86_64-avx512-vpclmulqdq" - x86_64 with VPCLMULQDQ support
861861
/// // "x86_64-sse-pclmulqdq" - x86_64 baseline with SSE4.1 and PCLMULQDQ
862862
/// ```
863-
#[cfg(feature = "alloc")]
863+
#[cfg(all(
864+
feature = "alloc",
865+
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
866+
))]
864867
pub fn get_calculator_target(_algorithm: CrcAlgorithm) -> String {
865868
use crate::feature_detection::get_arch_ops;
866869

867870
let arch_ops = get_arch_ops();
868871
arch_ops.get_target_string()
869872
}
870873

874+
/// Fallback version of get_calculator_target for unsupported architectures
875+
#[cfg(all(
876+
feature = "alloc",
877+
not(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))
878+
))]
879+
pub fn get_calculator_target(_algorithm: CrcAlgorithm) -> String {
880+
extern crate alloc;
881+
use alloc::string::ToString;
882+
"software-fallback-tables".to_string()
883+
}
884+
871885
/// Returns the calculator function and parameters for the specified CRC algorithm.
872886
#[inline(always)]
873887
fn get_calculator_params(algorithm: CrcAlgorithm) -> (CalculatorFn, CrcParams) {

0 commit comments

Comments
 (0)