From 54c893176860c15cb41be03412ab0bc30f20df25 Mon Sep 17 00:00:00 2001 From: gramar Date: Mon, 13 Feb 2023 15:03:41 +0100 Subject: [PATCH] impl: add compile time cpu feature detection when `no_std` 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 --- src/memchr/x86/mod.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/memchr/x86/mod.rs b/src/memchr/x86/mod.rs index aec35db..51450f7 100644 --- a/src/memchr/x86/mod.rs +++ b/src/memchr/x86/mod.rs @@ -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; @@ -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) }} }