1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-04-28 17:55:08 +02:00

Implement better target detection for NEON

This commit is contained in:
rsdy 2021-10-07 12:23:36 +01:00
parent db436a50c2
commit 0a0bb7126e
3 changed files with 20 additions and 11 deletions

View File

@ -44,6 +44,14 @@ fn is_x86_32() -> bool {
arch == "i386" || arch == "i586" || arch == "i686" 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 { fn is_armv7() -> bool {
target_components()[0] == "armv7" target_components()[0] == "armv7"
} }
@ -237,7 +245,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
if is_neon() { if (is_arm() && is_neon()) || (is_aarch64() && !is_pure()) {
println!("cargo:rustc-cfg=blake3_neon");
build_neon_c_intrinsics(); build_neon_c_intrinsics();
} }

View File

@ -94,7 +94,7 @@ mod avx2;
#[cfg(blake3_avx512_ffi)] #[cfg(blake3_avx512_ffi)]
#[path = "ffi_avx512.rs"] #[path = "ffi_avx512.rs"]
mod avx512; mod avx512;
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
#[path = "ffi_neon.rs"] #[path = "ffi_neon.rs"]
mod neon; mod neon;
mod portable; mod portable;

View File

@ -10,7 +10,7 @@ cfg_if::cfg_if! {
pub const MAX_SIMD_DEGREE: usize = 8; pub const MAX_SIMD_DEGREE: usize = 8;
} }
} }
} else if #[cfg(feature = "neon")] { } else if #[cfg(blake3_neon)] {
pub const MAX_SIMD_DEGREE: usize = 4; pub const MAX_SIMD_DEGREE: usize = 4;
} else { } else {
pub const MAX_SIMD_DEGREE: usize = 1; pub const MAX_SIMD_DEGREE: usize = 1;
@ -30,7 +30,7 @@ cfg_if::cfg_if! {
pub const MAX_SIMD_DEGREE_OR_2: usize = 8; 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; pub const MAX_SIMD_DEGREE_OR_2: usize = 4;
} else { } else {
pub const MAX_SIMD_DEGREE_OR_2: usize = 2; pub const MAX_SIMD_DEGREE_OR_2: usize = 2;
@ -49,7 +49,7 @@ pub enum Platform {
#[cfg(blake3_avx512_ffi)] #[cfg(blake3_avx512_ffi)]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
AVX512, AVX512,
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
NEON, NEON,
} }
@ -76,7 +76,7 @@ impl Platform {
} }
// We don't use dynamic feature detection for NEON. If the "neon" // We don't use dynamic feature detection for NEON. If the "neon"
// feature is on, NEON is assumed to be supported. // feature is on, NEON is assumed to be supported.
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
{ {
return Platform::NEON; return Platform::NEON;
} }
@ -95,7 +95,7 @@ impl Platform {
#[cfg(blake3_avx512_ffi)] #[cfg(blake3_avx512_ffi)]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Platform::AVX512 => 16, Platform::AVX512 => 16,
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
Platform::NEON => 4, Platform::NEON => 4,
}; };
debug_assert!(degree <= MAX_SIMD_DEGREE); debug_assert!(degree <= MAX_SIMD_DEGREE);
@ -129,7 +129,7 @@ impl Platform {
crate::avx512::compress_in_place(cv, block, block_len, counter, flags) crate::avx512::compress_in_place(cv, block, block_len, counter, flags)
}, },
// No NEON compress_in_place() implementation yet. // 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), 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) crate::avx512::compress_xof(cv, block, block_len, counter, flags)
}, },
// No NEON compress_xof() implementation yet. // No NEON compress_xof() implementation yet.
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
Platform::NEON => portable::compress_xof(cv, block, block_len, counter, flags), 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. // Assumed to be safe if the "neon" feature is on.
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
Platform::NEON => unsafe { Platform::NEON => unsafe {
crate::neon::hash_many( crate::neon::hash_many(
inputs, inputs,
@ -315,7 +315,7 @@ impl Platform {
} }
} }
#[cfg(feature = "neon")] #[cfg(blake3_neon)]
pub fn neon() -> Option<Self> { pub fn neon() -> Option<Self> {
// Assumed to be safe if the "neon" feature is on. // Assumed to be safe if the "neon" feature is on.
Some(Self::NEON) Some(Self::NEON)