From ed3fd0da1d049ab531bcd358630da02ece504464 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 9 Apr 2025 08:54:58 +0300 Subject: Check x86 features even in `no_std` --- Cargo.toml | 3 +++ src/platform.rs | 68 ++++++++++----------------------------------------------- 2 files changed, 15 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25ab345..bdd719f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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)] -- cgit v1.2.3