Skip to content

Commit

Permalink
impl: add compile time cpu feature detection when no_std
Browse files Browse the repository at this point in the history
This permits one to run the AVX memchr routines on x86-64 even when
'std' is not enabled. Probably something similar should be done for
memmem, but I think I have to insist on its conditional compilation
being simplified/centralized first.

Closes #120
  • Loading branch information
MarcusGrass authored and BurntSushi committed Jul 11, 2023
1 parent 7af67ce commit 54c8931
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/memchr/x86/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::fallback;

// We only use AVX when we can detect at runtime whether it's available, which
// requires std.
#[cfg(feature = "std")]
#[cfg(any(feature = "std", all(memchr_runtime_avx, target_feature = "avx2")))]
mod avx;
mod sse2;

Expand Down Expand Up @@ -83,11 +83,19 @@ macro_rules! unsafe_ifunc {
#[cfg(not(feature = "std"))]
macro_rules! unsafe_ifunc {
($fnty:ty, $name:ident, $haystack:ident, $($needle:ident),+) => {{
if cfg!(memchr_runtime_sse2) {
unsafe { sse2::$name($($needle),+, $haystack) }
} else {
fallback::$name($($needle),+, $haystack)
}
#[cfg(all(memchr_runtime_avx, target_feature = "avx2"))]
unsafe { avx::$name($($needle),+, $haystack) }
#[cfg(all(
memchr_runtime_sse2,
not(all(memchr_runtime_avx, target_feature = "avx2"))
))]
unsafe { sse2::$name($($needle),+, $haystack) }
#[cfg(all(
not(memchr_runtime_sse2),
not(memchr_runtime_avx),
not(target_feature = "avx2"),
))]
fallback::$name($($needle),+, $haystack)
}}
}

Expand Down

0 comments on commit 54c8931

Please sign in to comment.