From e38c493337d751f36cdb5fd33a2bc54326a60963 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Mon, 17 Jan 2022 18:47:23 -0800 Subject: [PATCH] Move UNALIGNED_OK detection to compile time instead of configure time. --- .github/workflows/cmake.yml | 7 +++--- CMakeLists.txt | 44 ------------------------------------- README.md | 2 -- arch/x86/compare256_avx2.c | 2 +- compare256.c | 4 ++-- configure | 20 ----------------- win32/Makefile.a64 | 2 -- win32/Makefile.arm | 1 - win32/Makefile.msc | 2 -- zbuild.h | 22 +++++++++++++++++++ 10 files changed, 28 insertions(+), 78 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index b37c84d3..5fdc38a1 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -41,21 +41,20 @@ jobs: cmake-args: -DZLIB_COMPAT=ON -DZLIB_SYMBOL_PREFIX=zTest_ codecov: ubuntu_gcc_compat_sprefix - - name: Ubuntu GCC OSB -O1 No Unaligned64 UBSAN + - name: Ubuntu GCC OSB -O1 UBSAN os: ubuntu-latest compiler: gcc cxx-compiler: g++ - cmake-args: -DWITH_UNALIGNED=ON -DUNALIGNED64_OK=OFF -DWITH_SANITIZER=Undefined + cmake-args: -DWITH_SANITIZER=Undefined build-dir: ../build build-src-dir: ../zlib-ng codecov: ubuntu_gcc_osb cflags: -O1 -g3 - - name: Ubuntu GCC -O3 No Unaligned + - name: Ubuntu GCC -O3 os: ubuntu-latest compiler: gcc cxx-compiler: g++ - cmake-args: -DWITH_UNALIGNED=OFF codecov: ubuntu_gcc_o3 cflags: -O3 diff --git a/CMakeLists.txt b/CMakeLists.txt index 60ccbe25..c41428c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,6 @@ option(WITH_MAINTAINER_WARNINGS "Build with project maintainer warnings" OFF) option(WITH_CODE_COVERAGE "Enable code coverage reporting" OFF) option(WITH_INFLATE_STRICT "Build with strict inflate distance checking" OFF) option(WITH_INFLATE_ALLOW_INVALID_DIST "Build with zero fill for inflate invalid distances" OFF) -option(WITH_UNALIGNED "Support unaligned reads on platforms that support it" ON) set(ZLIB_SYMBOL_PREFIX "" CACHE STRING "Give this prefix to all publicly exported symbols. Useful when embedding into a larger library. @@ -138,7 +137,6 @@ mark_as_advanced(FORCE WITH_POWER8 WITH_INFLATE_STRICT WITH_INFLATE_ALLOW_INVALID_DIST - WITH_UNALIGNED INSTALL_UTILS ) @@ -249,46 +247,6 @@ else() endif() endif() -# Set architecture alignment requirements -if(WITH_UNALIGNED) - if((BASEARCH_ARM_FOUND AND NOT "${ARCH}" MATCHES "armv[2-7]") OR (BASEARCH_PPC_FOUND AND "${ARCH}" MATCHES "powerpc64le") OR BASEARCH_X86_FOUND) - if(NOT DEFINED UNALIGNED_OK) - set(UNALIGNED_OK TRUE) - endif() - endif() - if(UNALIGNED_OK) - add_definitions(-DUNALIGNED_OK) - message(STATUS "Architecture supports unaligned reads") - endif() - if(BASEARCH_ARM_FOUND) - if(NOT DEFINED UNALIGNED64_OK) - if("${ARCH}" MATCHES "armv[2-7]") - set(UNALIGNED64_OK FALSE) - elseif("${ARCH}" MATCHES "(arm(v[8-9])?|aarch64)") - set(UNALIGNED64_OK TRUE) - endif() - endif() - endif() - if(BASEARCH_PPC_FOUND) - if(NOT DEFINED UNALIGNED64_OK) - if("${ARCH}" MATCHES "powerpc64le") - set(UNALIGNED64_OK TRUE) - endif() - endif() - endif() - if(BASEARCH_X86_FOUND) - if(NOT DEFINED UNALIGNED64_OK) - set(UNALIGNED64_OK TRUE) - endif() - endif() - if(UNALIGNED64_OK) - add_definitions(-DUNALIGNED64_OK) - message(STATUS "Architecture supports unaligned reads of > 4 bytes") - endif() -else() - message(STATUS "Unaligned reads manually disabled") -endif() - # Apply warning compiler flags if(WITH_MAINTAINER_WARNINGS) add_compile_options(${WARNFLAGS} ${WARNFLAGS_MAINTAINER} ${WARNFLAGS_DISABLE}) @@ -1459,8 +1417,6 @@ add_feature_info(WITH_MAINTAINER_WARNINGS WITH_MAINTAINER_WARNINGS "Build with p add_feature_info(WITH_CODE_COVERAGE WITH_CODE_COVERAGE "Enable code coverage reporting") add_feature_info(WITH_INFLATE_STRICT WITH_INFLATE_STRICT "Build with strict inflate distance checking") add_feature_info(WITH_INFLATE_ALLOW_INVALID_DIST WITH_INFLATE_ALLOW_INVALID_DIST "Build with zero fill for inflate invalid distances") -add_feature_info(WITH_UNALIGNED UNALIGNED_OK "Support unaligned reads on platforms that support it") -add_feature_info(WITH_UNALIGNED64 UNALIGNED64_OK "Support unaligned 64-bit reads on platforms that support it") if(BASEARCH_ARM_FOUND) add_feature_info(WITH_ACLE WITH_ACLE "Build with ACLE") diff --git a/README.md b/README.md index 0a801a7e..59899ad5 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,6 @@ Advanced Build Options | CMake | configure | Description | Default | |:--------------------------------|:----------------------|:--------------------------------------------------------------------|------------------------| | ZLIB_DUAL_LINK | | Dual link tests with system zlib | OFF | -| UNALIGNED_OK | | Allow unaligned reads | ON (x86, arm) | | FORCE_SSE2 | --force-sse2 | Skip runtime check for SSE2 instructions (Always on for x86_64) | OFF (x86) | | FORCE_TZCNT | --force-tzcnt | Skip runtime check for TZCNT instructions | OFF | | WITH_AVX2 | | Build with AVX2 intrinsics | ON | @@ -212,7 +211,6 @@ Advanced Build Options | WITH_CRC32_VX | --without-crc32-vx | Build with vectorized CRC32 on IBM Z | ON | | WITH_DFLTCC_DEFLATE | --with-dfltcc-deflate | Build with DFLTCC intrinsics for compression on IBM Z | OFF | | WITH_DFLTCC_INFLATE | --with-dfltcc-inflate | Build with DFLTCC intrinsics for decompression on IBM Z | OFF | -| WITH_UNALIGNED | | Allow optimizations that use unaligned reads if safe on current arch| ON | | WITH_INFLATE_STRICT | | Build with strict inflate distance checking | OFF | | WITH_INFLATE_ALLOW_INVALID_DIST | | Build with zero fill for inflate invalid distances | OFF | | INSTALL_UTILS | | Copy minigzip and minideflate during install | OFF | diff --git a/arch/x86/compare256_avx2.c b/arch/x86/compare256_avx2.c index 07f57f28..e25fa93e 100644 --- a/arch/x86/compare256_avx2.c +++ b/arch/x86/compare256_avx2.c @@ -14,7 +14,7 @@ # include #endif -/* UNALIGNED_OK, AVX2 intrinsic comparison */ +/* AVX2 unaligned intrinsic comparison */ static inline uint32_t compare256_unaligned_avx2_static(const uint8_t *src0, const uint8_t *src1) { uint32_t len = 0; diff --git a/compare256.c b/compare256.c index 1b693f27..609e6181 100644 --- a/compare256.c +++ b/compare256.c @@ -56,7 +56,7 @@ Z_INTERNAL uint32_t compare256_c(const uint8_t *src0, const uint8_t *src1) { #include "match_tpl.h" #ifdef UNALIGNED_OK -/* UNALIGNED_OK, 16-bit integer comparison */ +/* 16-bit unaligned integer comparison */ static inline uint32_t compare256_unaligned_16_static(const uint8_t *src0, const uint8_t *src1) { uint32_t len = 0; @@ -94,7 +94,7 @@ Z_INTERNAL uint32_t compare256_unaligned_16(const uint8_t *src0, const uint8_t * #include "match_tpl.h" #ifdef HAVE_BUILTIN_CTZ -/* UNALIGNED_OK, 32-bit integer comparison */ +/* 32-bit unaligned integer comparison */ static inline uint32_t compare256_unaligned_32_static(const uint8_t *src0, const uint8_t *src1) { uint32_t len = 0; diff --git a/configure b/configure index bd237236..17bd9fd8 100755 --- a/configure +++ b/configure @@ -1481,9 +1481,6 @@ case "${ARCH}" in i386 | i486 | i586 | i686 |x86_64) ARCHDIR=arch/x86 - CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - # Enable arch-specific optimizations if test $without_optimizations -eq 0; then CFLAGS="${CFLAGS} -DX86_FEATURES" @@ -1704,9 +1701,6 @@ EOF fi ;; armv6l | armv6hl) - CFLAGS="${CFLAGS} -DUNALIGNED_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK" - if test $without_optimizations -eq 0; then if test $buildacle -eq 1; then echo ACLE support not available @@ -1718,9 +1712,6 @@ EOF fi ;; arm | armv7*) - CFLAGS="${CFLAGS} -DUNALIGNED_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK" - if test $without_optimizations -eq 0; then if test $buildacle -eq 1; then echo ACLE support not available @@ -1745,9 +1736,6 @@ EOF fi ;; armv8-a | armv8-a+simd) - CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - if test $without_optimizations -eq 0; then if test $buildacle -eq 1; then echo ACLE support not available @@ -1772,9 +1760,6 @@ EOF fi ;; armv8-a+crc | armv8-a+crc+simd | armv8.[1234]-a | armv8.[1234]-a+simd) - CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - acleflag="-march=${ARCH}" if test $without_optimizations -eq 0; then @@ -1869,9 +1854,6 @@ EOF neonflag="-march=${ARCH}" acleflag="-march=${ARCH}" - - CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" ;; powerpc*) case "${ARCH}" in @@ -1883,8 +1865,6 @@ EOF ;; powerpc64le) [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=ppc64le - CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" - SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK" ;; esac diff --git a/win32/Makefile.a64 b/win32/Makefile.a64 index 4df6f3d0..29e66cee 100644 --- a/win32/Makefile.a64 +++ b/win32/Makefile.a64 @@ -30,8 +30,6 @@ WFLAGS = \ -D_CRT_NONSTDC_NO_DEPRECATE \ -DARM_NEON_HASLD4 \ -DARM_FEATURES \ - -DUNALIGNED_OK \ - -DUNALIGNED64_OK \ # LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest ARFLAGS = -nologo diff --git a/win32/Makefile.arm b/win32/Makefile.arm index 4cc90288..a43dc59b 100644 --- a/win32/Makefile.arm +++ b/win32/Makefile.arm @@ -30,7 +30,6 @@ WFLAGS = \ -D_CRT_NONSTDC_NO_DEPRECATE \ -DARM_FEATURES \ -DARM_NEON_HASLD4 \ - -DUNALIGNED_OK \ # LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest ARFLAGS = -nologo diff --git a/win32/Makefile.msc b/win32/Makefile.msc index 3d8f1b2e..9caa8211 100644 --- a/win32/Makefile.msc +++ b/win32/Makefile.msc @@ -35,8 +35,6 @@ WFLAGS = \ -DX86_AVX2 \ -DX86_AVX_CHUNKSET \ -DX86_SSE2_CHUNKSET \ - -DUNALIGNED_OK \ - -DUNALIGNED64_OK \ # LDFLAGS = -nologo -debug -incremental:no -opt:ref -manifest ARFLAGS = -nologo diff --git a/zbuild.h b/zbuild.h index 08bc7ef6..20e3e20e 100644 --- a/zbuild.h +++ b/zbuild.h @@ -194,6 +194,28 @@ # define Tracecv(c, x) #endif +#if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64) +# define UNALIGNED_OK +# define UNALIGNED64_OK +#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \ + defined(__i686__) || defined(_X86_) || defined(_M_IX86) +# define UNALIGNED_OK +#elif defined(__aarch64__) || defined(_M_ARM64) +# if (defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED)) || !defined(__GNUC__) +# define UNALIGNED_OK +# define UNALIGNED64_OK +# endif +#elif defined(__arm__) || (_M_ARM >= 7) +# if (defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED)) || !defined(__GNUC__) +# define UNALIGNED_OK +# endif +#elif defined(__powerpc64__) || defined(__ppc64__) +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define UNALIGNED_OK +# define UNALIGNED64_OK +# endif +#endif + /* Force compiler to emit unaligned memory accesses if unaligned access is supported on the architecture, otherwise don't assume unaligned access is supported. Older compilers don't optimize memcpy and memcmp calls to unaligned access instructions