From 24387c234b4eed1ef9a7eaa107391740b4095568 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Thu, 23 May 2024 15:15:18 +0300 Subject: [PATCH] CMake: Add manual support for 32-bit x86 assembly files One has to pass -DENABLE_X86_ASM=ON to cmake to enable the CRC assembly code. Autodetection isn't done. Looking at CMAKE_SYSTEM_PROCESSOR might not work as it comes from uname unless cross-compilation is done using a CMake toolchain file. On top of this, if the code is run on modern processors that support the CLMUL instruction, then the C code should be faster (but then one should also be using a x86-64 build if possible). --- CMakeLists.txt | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 445822b8..59ecf010 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,17 @@ # # A few things are still missing compared to the Autotools-based build: # -# - 32-bit x86 assembly code for CRC32 and CRC64 isn't used. +# - 32-bit x86 assembly code for CRC32 and CRC64 isn't used by default. +# Use the option -DENABLE_X86_ASM=ON on the CMake command line to +# enable the assembly files. They are compatible with Linux, *BSDs, +# Cygwin, MinGW-w64, and Darwin. They are NOT compatible with MSVC. +# +# NOTE: The C code includes a generic version compatible with all +# processors and CLMUL version that requires a new enough processor +# with the PCLMULQDQ instruction. If the 32-bit x86 assembly files +# are used, the CLMUL version in the C code is NOT built. On modern +# processors with CLMUL support, the C code should be faster than +# the assembly code while on old processors the assembly code wins. # # - External SHA-256 code isn't supported but it's disabled by # default in the Autotools build too (--enable-external-sha256). @@ -149,6 +159,14 @@ endif() set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) +# Support 32-bit x86 assembly files. +if(NOT MSVC) + option(ENABLE_X86_ASM "Enable 32-bit x86 assembly code" OFF) + if(ENABLE_X86_ASM) + enable_language(ASM) + endif() +endif() + # On Apple OSes, don't build executables as bundles: set(CMAKE_MACOSX_BUNDLE OFF) @@ -482,11 +500,16 @@ if(ENABLE_SMALL) target_sources(liblzma PRIVATE src/liblzma/check/crc32_small.c) else() target_sources(liblzma PRIVATE - src/liblzma/check/crc32_fast.c src/liblzma/check/crc32_table.c src/liblzma/check/crc32_table_be.h src/liblzma/check/crc32_table_le.h ) + + if(ENABLE_X86_ASM) + target_sources(liblzma PRIVATE src/liblzma/check/crc32_x86.S) + else() + target_sources(liblzma PRIVATE src/liblzma/check/crc32_fast.c) + endif() endif() if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES) @@ -496,11 +519,16 @@ if("crc64" IN_LIST ADDITIONAL_CHECK_TYPES) target_sources(liblzma PRIVATE src/liblzma/check/crc64_small.c) else() target_sources(liblzma PRIVATE - src/liblzma/check/crc64_fast.c src/liblzma/check/crc64_table.c src/liblzma/check/crc64_table_be.h src/liblzma/check/crc64_table_le.h ) + + if(ENABLE_X86_ASM) + target_sources(liblzma PRIVATE src/liblzma/check/crc64_x86.S) + else() + target_sources(liblzma PRIVATE src/liblzma/check/crc64_fast.c) + endif() endif() endif()