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