From a4207e54a17ef3dd38ec86a249ae8cb976bbd4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taras=20Alatmiu=C3=AB?= Date: Sat, 23 May 2026 16:49:08 +0800 Subject: [PATCH] fix(build): add architecture-aware guards for x86-64 assembly sources - config.m4: Add CPU architecture detection to include SSE/AVX assembly sources only on x86_64/amd64 targets. - blake3_dispatch.c: Add a preprocessor guard to disable all x86-64 SIMD dispatch paths on non-x86-64 platforms (including 32-bit x86), preventing build failures on ARM and other architectures as well as potential linker errors on i386/i686 targets. --- blake3_dispatch.c | 17 +++++++++++++++++ config.m4 | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/blake3_dispatch.c b/blake3_dispatch.c index 6518478..3b97e89 100644 --- a/blake3_dispatch.c +++ b/blake3_dispatch.c @@ -4,6 +4,23 @@ #include "blake3_impl.h" +/* Force disable x86-64 assembly paths if not building for x86-64 */ +#if !defined(__x86_64__) && !defined(_M_X64) + #ifndef BLAKE3_NO_SSE2 + #define BLAKE3_NO_SSE2 1 + #endif + #ifndef BLAKE3_NO_SSE41 + #define BLAKE3_NO_SSE41 1 + #endif + #ifndef BLAKE3_NO_AVX2 + #define BLAKE3_NO_AVX2 1 + #endif + #ifndef BLAKE3_NO_AVX512 + #define BLAKE3_NO_AVX512 1 + #endif + #undef IS_X86 +#endif + #if defined(IS_X86) #if defined(_MSC_VER) #include diff --git a/config.m4 b/config.m4 index 3b0e790..ef0a4f7 100644 --- a/config.m4 +++ b/config.m4 @@ -3,5 +3,19 @@ PHP_ARG_ENABLE(blake3, [--enable-blake3 Enable BLAKE3 Extension]) if test "$PHP_BLAKE3" != "no"; then - PHP_NEW_EXTENSION(blake3, php_blake3.c blake3.c blake3_dispatch.c blake3_portable.c blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S blake3_avx512_x86-64_unix.S, $ext_shared) -fi \ No newline at end of file + dnl Detect target CPU architecture + AC_MSG_CHECKING([for blake3 architecture optimization]) + case "$host_cpu" in + x86_64|amd64) + AC_MSG_RESULT([x86_64 (enabling SSE/AVX assembly)]) + BLAKE3_ASM_SOURCES="blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S blake3_avx512_x86-64_unix.S" + ;; + *) + AC_MSG_RESULT([$host_cpu (using portable C implementation)]) + BLAKE3_ASM_SOURCES="" + ;; + esac + + dnl Pass detected assembly sources to extension build + PHP_NEW_EXTENSION(blake3, php_blake3.c blake3.c blake3_dispatch.c blake3_portable.c $BLAKE3_ASM_SOURCES, $ext_shared) +fi