xref: /aosp_15_r20/external/zlib/patches/0010-cmake-enable-simd.patch (revision 86ee64e75fa5f8bce2c8c356138035642429cd05)
1diff --git a/third_party/zlib/CMakeLists.txt b/third_party/zlib/CMakeLists.txt
2index b412dc7feb732..0431278405046 100644
3--- a/third_party/zlib/CMakeLists.txt
4+++ b/third_party/zlib/CMakeLists.txt
5@@ -1,4 +1,4 @@
6-cmake_minimum_required(VERSION 2.4.4)
7+cmake_minimum_required(VERSION 3.0)
8 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
9
10 project(zlib C)
11@@ -21,6 +21,26 @@ check_include_file(sys/types.h HAVE_SYS_TYPES_H)
12 check_include_file(stdint.h    HAVE_STDINT_H)
13 check_include_file(stddef.h    HAVE_STDDEF_H)
14
15+option(ENABLE_SIMD_OPTIMIZATIONS "Enable all SIMD optimizations" OFF)
16+
17+# TODO(cavalcantii): add support for other OSes (e.g. Android, fuchsia, osx)
18+# and architectures (e.g. Arm).
19+if (ENABLE_SIMD_OPTIMIZATIONS)
20+   add_definitions(-DINFLATE_CHUNK_SIMD_SSE2)
21+   add_definitions(-DADLER32_SIMD_SSSE3)
22+   add_definitions(-DINFLATE_CHUNK_READ_64LE)
23+   add_definitions(-DCRC32_SIMD_SSE42_PCLMUL)
24+   add_definitions(-DDEFLATE_SLIDE_HASH_SSE2)
25+   add_compile_options(-msse4.2 -mpclmul)
26+   # Required by CPU features detection code.
27+   add_definitions(-DX86_NOT_WINDOWS)
28+   # Apparently some environments (e.g. CentOS) require to explicitly link
29+   # with pthread and that is required by the CPU features detection code.
30+   find_package (Threads REQUIRED)
31+   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
32+   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
33+endif()
34+
35 #
36 # Check to see if we have large file support
37 #
38@@ -120,10 +140,25 @@ set(ZLIB_SRCS
39     zutil.c
40 )
41
42-if(NOT MINGW)
43-    set(ZLIB_DLL_SRCS
44-        win32/zlib1.rc # If present will override custom build rule below.
45-    )
46+
47+#============================================================================
48+# Update list of source files if optimizations were enabled
49+#============================================================================
50+if (ENABLE_SIMD_OPTIMIZATIONS)
51+  list(REMOVE_ITEM ZLIB_SRCS inflate.c)
52+
53+  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.h)
54+  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/chunkcopy.h)
55+  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.h)
56+  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.h)
57+  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.h)
58+
59+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.c)
60+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.c)
61+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inflate.c)
62+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.c)
63+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.c)
64+  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc_folding.c)
65 endif()
66
67 # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
68@@ -191,23 +226,9 @@ if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
69 endif()
70
71 #============================================================================
72-# Example binaries
73+# Benchmarker
74 #============================================================================
75-
76-add_executable(example test/example.c)
77-target_link_libraries(example zlib)
78-add_test(example example)
79-
80-add_executable(minigzip test/minigzip.c)
81-target_link_libraries(minigzip zlib)
82-
83-if(HAVE_OFF64_T)
84-    add_executable(example64 test/example.c)
85-    target_link_libraries(example64 zlib)
86-    set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
87-    add_test(example64 example64)
88-
89-    add_executable(minigzip64 test/minigzip.c)
90-    target_link_libraries(minigzip64 zlib)
91-    set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
92-endif()
93+enable_language(CXX)
94+set(CMAKE_CXX_STANDARD 14) # workaround for older compilers (e.g. g++ 5.4).
95+add_executable(zlib_bench contrib/bench/zlib_bench.cc)
96+target_link_libraries(zlib_bench zlib)
97