From aff5c686b4ad7a7899436121d46c1a74b2f63cc7 Mon Sep 17 00:00:00 2001 From: liuweilw Date: Mon, 19 Oct 2026 11:42:47 +0800 Subject: [PATCH] [bugfix] Fix x86_64 CRC32 SIMD build guards and detection - Detect x86_64 with check_c_source_compiles instead of CMAKE_SYSTEM_PROCESSOR, so the CMake decision always agrees with the __x86_64__/__amd64__ guards in the C sources (cross builds, -m32, and targets reporting "AMD64"/"amd64" no longer produce a mismatch that leaves undefined symbols at link time) - Enable the VPCLMULQDQ target attribute for clang: clang defines __GNUC__ as 4, so the previous GCC >= 8 check silently disabled the AVX-512 CRCs under clang - Drop _M_X64 from the C guards, matching the build check and the fact that cl.exe cannot compile arch_x86.c's GNU inline asm and target attributes - Align the gunit architecture and compiler guards with the implementation guards --- extra/zlib/zlib-1.3.1/CMakeLists.txt | 8 +++++++- extra/zlib/zlib-1.3.1/arch_x86.h | 4 ++-- extra/zlib/zlib-1.3.1/crc32.c | 2 +- unittest/gunit/mysys_my_checksum-t.cc | 12 ++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/extra/zlib/zlib-1.3.1/CMakeLists.txt b/extra/zlib/zlib-1.3.1/CMakeLists.txt index bb9baaf9db8..9145c764c9c 100644 --- a/extra/zlib/zlib-1.3.1/CMakeLists.txt +++ b/extra/zlib/zlib-1.3.1/CMakeLists.txt @@ -136,7 +136,13 @@ set(ZLIB_SRCS zutil.c ) -if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") +check_c_source_compiles(" + #if !defined(__x86_64__) && !defined(__amd64__) + # error not an x86_64 target + #endif + int main(void) { return 0; }" ZLIB_TARGET_X86_64) + +if(ZLIB_TARGET_X86_64) LIST(APPEND ZLIB_PRIVATE_HDRS arch_x86.h crc32_x86.h) diff --git a/extra/zlib/zlib-1.3.1/arch_x86.h b/extra/zlib/zlib-1.3.1/arch_x86.h index 103e2182ab3..e950c3126f0 100644 --- a/extra/zlib/zlib-1.3.1/arch_x86.h +++ b/extra/zlib/zlib-1.3.1/arch_x86.h @@ -4,8 +4,8 @@ #define ZLIB_X86_64_CRC32_PCLMUL 1 -/* Check GCC version and only apply vpclmulqdq target attribute when compiler is GCC 8 or later */ -#if defined(__GNUC__) && (__GNUC__ >= 8) +/* Apply vpclmulqdq target attribute when compiler is GCC 8 or later, or clang */ +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 8)) # define HAVE_VPCLMULQDQ_ATTRIBUTE 1 #endif diff --git a/extra/zlib/zlib-1.3.1/crc32.c b/extra/zlib/zlib-1.3.1/crc32.c index b7fe5d30674..ebd4559c813 100644 --- a/extra/zlib/zlib-1.3.1/crc32.c +++ b/extra/zlib/zlib-1.3.1/crc32.c @@ -29,7 +29,7 @@ #include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */ -#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) +#if defined(__x86_64__) || defined(__amd64__) #include "arch_x86.h" #endif diff --git a/unittest/gunit/mysys_my_checksum-t.cc b/unittest/gunit/mysys_my_checksum-t.cc index a67a838c69a..8b215580ae9 100644 --- a/unittest/gunit/mysys_my_checksum-t.cc +++ b/unittest/gunit/mysys_my_checksum-t.cc @@ -36,13 +36,13 @@ using namespace mycrc32; namespace mysys_my_checksum { -#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) +#if defined(__x86_64__) || defined(__amd64__) extern "C" { int has_crc32_x86_avx(void); int has_crc32_x86_avx512(void); unsigned long crc32_z_impl_x86_avx(unsigned long crc, const unsigned char FAR *buf, z_size_t len); -#if defined(__GNUC__) && (__GNUC__ >= 8) +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 8)) unsigned long crc32_z_impl_x86_avx512(unsigned long crc, const unsigned char FAR *buf, z_size_t len) __attribute__((weak)); @@ -71,7 +71,7 @@ std::uint32_t TableCrc32Reference(std::uint32_t crc_seed, return crc ^ 0xffffffffU; } -#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) +#if defined(__x86_64__) || defined(__amd64__) void VerifyAgainstTableAndZlib( const std::vector &data, std::size_t offset, std::size_t len, std::uint32_t seed, @@ -166,7 +166,7 @@ TEST(MysysMyChecksum, IntegerCrc32_64bit) { EXPECT_EQ(IntegerCrc32(crc, value), ~zres); } -#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) +#if defined(__x86_64__) || defined(__amd64__) TEST(MysysMyChecksum, Crc32ZMatchesAvxImplementationWhenSupported) { if (!has_crc32_x86_avx()) { GTEST_SKIP() << "CPU/OS does not support AVX+PCLMUL CRC32"; @@ -195,7 +195,7 @@ TEST(MysysMyChecksum, Crc32ZDispatchesToAvxImplementationWhenSupported) { if (!has_crc32_x86_avx()) { GTEST_SKIP() << "CPU/OS does not support AVX+PCLMUL CRC32"; } -#if defined(__GNUC__) && (__GNUC__ >= 8) +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 8)) if (crc32_z_impl_x86_avx512 != nullptr && has_crc32_x86_avx512()) { GTEST_SKIP() << "crc32_z uses AVX512 path on this CPU"; } @@ -273,7 +273,7 @@ TEST(MysysMyChecksum, SixteenByteBoundaryWithVariousOffsets) { } } -#if defined(__GNUC__) && (__GNUC__ >= 8) +#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 8)) TEST(MysysMyChecksum, Crc32ZDispatchesToAvx512ImplementationWhenSupported) { if (crc32_z_impl_x86_avx512 == nullptr) { GTEST_SKIP() << "AVX512 crc32 implementation is not linked in this build";