diff options
| author | Nazar Mokrynskyi <[email protected]> | 2025-04-09 08:54:58 +0300 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2025-04-24 09:16:25 -0700 |
| commit | ed3fd0da1d049ab531bcd358630da02ece504464 (patch) | |
| tree | 57366a8f20b1e689cd07544ad4c477afc365af0f | |
| parent | df610ddc3b93841ffc59a87e3da659a15910eb46 (diff) | |
Check x86 features even in `no_std`
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/platform.rs | 68 |
2 files changed, 15 insertions, 56 deletions
@@ -120,6 +120,9 @@ rayon-core = { version = "1.12.1", optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } zeroize = { version = "1", default-features = false, optional = true } +[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies] +cpufeatures = "0.2.17" + [dev-dependencies] hmac = "0.12.0" hex = "0.4.2" diff --git a/src/platform.rs b/src/platform.rs index 51b3b7b..3a05420 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -408,7 +408,6 @@ impl Platform { #[cfg(blake3_avx512_ffi)] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] -#[allow(unreachable_code)] pub fn avx512_detected() -> bool { if cfg!(miri) { return false; @@ -418,24 +417,13 @@ pub fn avx512_detected() -> bool { if cfg!(feature = "no_avx512") { return false; } - // Static check, e.g. for building with target-cpu=native. - #[cfg(all(target_feature = "avx512f", target_feature = "avx512vl"))] - { - return true; - } - // Dynamic check, if std is enabled. - #[cfg(feature = "std")] - { - if is_x86_feature_detected!("avx512f") && is_x86_feature_detected!("avx512vl") { - return true; - } - } - false + + cpufeatures::new!(has_avx512, "avx512f", "avx512vl"); + has_avx512::get() } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] -#[allow(unreachable_code)] pub fn avx2_detected() -> bool { if cfg!(miri) { return false; @@ -445,24 +433,13 @@ pub fn avx2_detected() -> bool { if cfg!(feature = "no_avx2") { return false; } - // Static check, e.g. for building with target-cpu=native. - #[cfg(target_feature = "avx2")] - { - return true; - } - // Dynamic check, if std is enabled. - #[cfg(feature = "std")] - { - if is_x86_feature_detected!("avx2") { - return true; - } - } - false + + cpufeatures::new!(has_avx2, "avx2"); + has_avx2::get() } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] -#[allow(unreachable_code)] pub fn sse41_detected() -> bool { if cfg!(miri) { return false; @@ -472,24 +449,13 @@ pub fn sse41_detected() -> bool { if cfg!(feature = "no_sse41") { return false; } - // Static check, e.g. for building with target-cpu=native. - #[cfg(target_feature = "sse4.1")] - { - return true; - } - // Dynamic check, if std is enabled. - #[cfg(feature = "std")] - { - if is_x86_feature_detected!("sse4.1") { - return true; - } - } - false + + cpufeatures::new!(has_sse41, "sse4.1"); + has_sse41::get() } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] -#[allow(unreachable_code)] pub fn sse2_detected() -> bool { if cfg!(miri) { return false; @@ -499,19 +465,9 @@ pub fn sse2_detected() -> bool { if cfg!(feature = "no_sse2") { return false; } - // Static check, e.g. for building with target-cpu=native. - #[cfg(target_feature = "sse2")] - { - return true; - } - // Dynamic check, if std is enabled. - #[cfg(feature = "std")] - { - if is_x86_feature_detected!("sse2") { - return true; - } - } - false + + cpufeatures::new!(has_sse2, "sse2"); + has_sse2::get() } #[inline(always)] |
