From 0a0bb7126e9207d3bb3d9af0f0b5ae646d532cf2 Mon Sep 17 00:00:00 2001
From: rsdy
Date: Thu, 7 Oct 2021 12:23:36 +0100
Subject: [PATCH] Implement better target detection for NEON
---
build.rs | 11 ++++++++++-
src/lib.rs | 2 +-
src/platform.rs | 18 +++++++++---------
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/build.rs b/build.rs
index 4fd3bae..c5677f6 100644
--- a/build.rs
+++ b/build.rs
@@ -44,6 +44,14 @@ fn is_x86_32() -> bool {
arch == "i386" || arch == "i586" || arch == "i686"
}
+fn is_arm() -> bool {
+ is_armv7() || is_aarch64() || target_components()[0] == "arm"
+}
+
+fn is_aarch64() -> bool {
+ target_components()[0] == "aarch64"
+}
+
fn is_armv7() -> bool {
target_components()[0] == "armv7"
}
@@ -237,7 +245,8 @@ fn main() -> Result<(), Box> {
}
}
- if is_neon() {
+ if (is_arm() && is_neon()) || (is_aarch64() && !is_pure()) {
+ println!("cargo:rustc-cfg=blake3_neon");
build_neon_c_intrinsics();
}
diff --git a/src/lib.rs b/src/lib.rs
index 4123a0b..31e5cd6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -94,7 +94,7 @@ mod avx2;
#[cfg(blake3_avx512_ffi)]
#[path = "ffi_avx512.rs"]
mod avx512;
-#[cfg(feature = "neon")]
+#[cfg(blake3_neon)]
#[path = "ffi_neon.rs"]
mod neon;
mod portable;
diff --git a/src/platform.rs b/src/platform.rs
index 1a732c5..00058b1 100644
--- a/src/platform.rs
+++ b/src/platform.rs
@@ -10,7 +10,7 @@ cfg_if::cfg_if! {
pub const MAX_SIMD_DEGREE: usize = 8;
}
}
- } else if #[cfg(feature = "neon")] {
+ } else if #[cfg(blake3_neon)] {
pub const MAX_SIMD_DEGREE: usize = 4;
} else {
pub const MAX_SIMD_DEGREE: usize = 1;
@@ -30,7 +30,7 @@ cfg_if::cfg_if! {
pub const MAX_SIMD_DEGREE_OR_2: usize = 8;
}
}
- } else if #[cfg(feature = "neon")] {
+ } else if #[cfg(blake3_neon)] {
pub const MAX_SIMD_DEGREE_OR_2: usize = 4;
} else {
pub const MAX_SIMD_DEGREE_OR_2: usize = 2;
@@ -49,7 +49,7 @@ pub enum Platform {
#[cfg(blake3_avx512_ffi)]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
AVX512,
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
NEON,
}
@@ -76,7 +76,7 @@ impl Platform {
}
// We don't use dynamic feature detection for NEON. If the "neon"
// feature is on, NEON is assumed to be supported.
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
{
return Platform::NEON;
}
@@ -95,7 +95,7 @@ impl Platform {
#[cfg(blake3_avx512_ffi)]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Platform::AVX512 => 16,
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
Platform::NEON => 4,
};
debug_assert!(degree <= MAX_SIMD_DEGREE);
@@ -129,7 +129,7 @@ impl Platform {
crate::avx512::compress_in_place(cv, block, block_len, counter, flags)
},
// No NEON compress_in_place() implementation yet.
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
Platform::NEON => portable::compress_in_place(cv, block, block_len, counter, flags),
}
}
@@ -161,7 +161,7 @@ impl Platform {
crate::avx512::compress_xof(cv, block, block_len, counter, flags)
},
// No NEON compress_xof() implementation yet.
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
Platform::NEON => portable::compress_xof(cv, block, block_len, counter, flags),
}
}
@@ -256,7 +256,7 @@ impl Platform {
)
},
// Assumed to be safe if the "neon" feature is on.
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
Platform::NEON => unsafe {
crate::neon::hash_many(
inputs,
@@ -315,7 +315,7 @@ impl Platform {
}
}
- #[cfg(feature = "neon")]
+ #[cfg(blake3_neon)]
pub fn neon() -> Option {
// Assumed to be safe if the "neon" feature is on.
Some(Self::NEON)