68c528
From 14730a26e830eb2b09d1f7097910616f23c1476e Mon Sep 17 00:00:00 2001
68c528
From: Ilya Leoshkevich <iii@linux.ibm.com>
68c528
Date: Thu, 2 Feb 2023 19:40:32 +0100
68c528
Subject: [PATCH] 0001-PATCH-Preparation-for-Power-optimizations.patch
68c528
68c528
---
68c528
 CMakeLists.txt         | 67 ++++++++++++++++++++++++++++++++++++++++++
68c528
 configure              | 66 +++++++++++++++++++++++++++++++++++++++++
68c528
 contrib/README.contrib |  8 +++++
68c528
 contrib/gcc/zifunc.h   | 60 +++++++++++++++++++++++++++++++++++++
68c528
 contrib/power/power.h  |  4 +++
68c528
 5 files changed, 205 insertions(+)
68c528
 create mode 100644 contrib/gcc/zifunc.h
68c528
 create mode 100644 contrib/power/power.h
68c528
68c528
diff --git a/CMakeLists.txt b/CMakeLists.txt
68c528
index 0fe939d..e762023 100644
68c528
--- a/CMakeLists.txt
68c528
+++ b/CMakeLists.txt
68c528
@@ -7,6 +7,7 @@ set(VERSION "1.2.11")
68c528
 
68c528
 option(ASM686 "Enable building i686 assembly implementation")
68c528
 option(AMD64 "Enable building amd64 assembly implementation")
68c528
+option(POWER "Enable building power implementation")
68c528
 
68c528
 set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
68c528
 set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
68c528
@@ -140,6 +141,72 @@ if(CMAKE_COMPILER_IS_GNUCC)
68c528
 		add_definitions(-DASMV)
68c528
 		set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
68c528
 	endif()
68c528
+
68c528
+    # test to see if we can use a GNU indirect function to detect and load optimized code at runtime
68c528
+    CHECK_C_SOURCE_COMPILES("
68c528
+    static int test_ifunc_native(void)
68c528
+    {
68c528
+       return 1;
68c528
+    }
68c528
+    static int (*(check_ifunc_native(void)))(void)
68c528
+    {
68c528
+       return test_ifunc_native;
68c528
+    }
68c528
+    int test_ifunc(void) __attribute__ ((ifunc (\"check_ifunc_native\")));
68c528
+    int main(void)
68c528
+    {
68c528
+       return 0;
68c528
+    }
68c528
+    " HAS_C_ATTR_IFUNC)
68c528
+
68c528
+    if(HAS_C_ATTR_IFUNC)
68c528
+        add_definitions(-DHAVE_IFUNC)
68c528
+        set(ZLIB_PRIVATE_HDRS ${ZLIB_PRIVATE_HDRS} contrib/gcc/zifunc.h)
68c528
+    endif()
68c528
+
68c528
+    if(POWER)
68c528
+        # Test to see if we can use the optimizations for Power
68c528
+        CHECK_C_SOURCE_COMPILES("
68c528
+        #ifndef _ARCH_PPC
68c528
+            #error \"Target is not Power\"
68c528
+        #endif
68c528
+        #ifndef __BUILTIN_CPU_SUPPORTS__
68c528
+            #error \"Target doesn't support __builtin_cpu_supports()\"
68c528
+        #endif
68c528
+        int main() { return 0; }
68c528
+        " HAS_POWER_SUPPORT)
68c528
+
68c528
+        if(HAS_POWER_SUPPORT AND HAS_C_ATTR_IFUNC)
68c528
+            add_definitions(-DZ_POWER_OPT)
68c528
+
68c528
+            set(CMAKE_REQUIRED_FLAGS -mcpu=power8)
68c528
+            CHECK_C_SOURCE_COMPILES("int main(void){return 0;}" POWER8)
68c528
+
68c528
+            if(POWER8)
68c528
+                add_definitions(-DZ_POWER8)
68c528
+                set(ZLIB_POWER8 )
68c528
+
68c528
+                set_source_files_properties(
68c528
+                    ${ZLIB_POWER8}
68c528
+                    PROPERTIES COMPILE_FLAGS -mcpu=power8)
68c528
+            endif()
68c528
+
68c528
+            set(CMAKE_REQUIRED_FLAGS -mcpu=power9)
68c528
+            CHECK_C_SOURCE_COMPILES("int main(void){return 0;}" POWER9)
68c528
+
68c528
+            if(POWER9)
68c528
+                add_definitions(-DZ_POWER9)
68c528
+                set(ZLIB_POWER9 )
68c528
+
68c528
+                set_source_files_properties(
68c528
+                    ${ZLIB_POWER9}
68c528
+                    PROPERTIES COMPILE_FLAGS -mcpu=power9)
68c528
+            endif()
68c528
+
68c528
+            set(ZLIB_PRIVATE_HDRS ${ZLIB_PRIVATE_HDRS} contrib/power/power.h)
68c528
+            set(ZLIB_SRCS ${ZLIB_SRCS} ${ZLIB_POWER8} ${ZLIB_POWER9})
68c528
+        endif()
68c528
+    endif()
68c528
 endif()
68c528
 
68c528
 if(MSVC)
68c528
diff --git a/configure b/configure
68c528
index d026b35..0538d58 100755
68c528
--- a/configure
68c528
+++ b/configure
68c528
@@ -846,6 +846,72 @@ else
68c528
     echo "Checking for sys/sdt.h ... No." | tee -a configure.log
68c528
 fi
68c528
 
68c528
+# test to see if we can use a gnu indirection function to detect and load optimized code at runtime
68c528
+echo >> configure.log
68c528
+cat > $test.c <
68c528
+static int test_ifunc_native(void)
68c528
+{
68c528
+  return 1;
68c528
+}
68c528
+
68c528
+static int (*(check_ifunc_native(void)))(void)
68c528
+{
68c528
+  return test_ifunc_native;
68c528
+}
68c528
+
68c528
+int test_ifunc(void) __attribute__ ((ifunc ("check_ifunc_native")));
68c528
+EOF
68c528
+
68c528
+if tryboth $CC -c $CFLAGS $test.c; then
68c528
+  SFLAGS="${SFLAGS} -DHAVE_IFUNC"
68c528
+  CFLAGS="${CFLAGS} -DHAVE_IFUNC"
68c528
+  echo "Checking for attribute(ifunc) support... Yes." | tee -a configure.log
68c528
+else
68c528
+  echo "Checking for attribute(ifunc) support... No." | tee -a configure.log
68c528
+fi
68c528
+
68c528
+# Test to see if we can use the optimizations for Power
68c528
+echo >> configure.log
68c528
+cat > $test.c <
68c528
+#ifndef _ARCH_PPC
68c528
+  #error "Target is not Power"
68c528
+#endif
68c528
+#ifndef HAVE_IFUNC
68c528
+  #error "Target doesn't support ifunc"
68c528
+#endif
68c528
+#ifndef __BUILTIN_CPU_SUPPORTS__
68c528
+  #error "Target doesn't support __builtin_cpu_supports()"
68c528
+#endif
68c528
+EOF
68c528
+
68c528
+if tryboth $CC -c $CFLAGS $test.c; then
68c528
+  echo "int main(void){return 0;}" > $test.c
68c528
+
68c528
+  if tryboth $CC -c $CFLAGS -mcpu=power8 $test.c; then
68c528
+    POWER8="-DZ_POWER8"
68c528
+    PIC_OBJC="${PIC_OBJC}"
68c528
+    OBJC="${OBJC}"
68c528
+    echo "Checking for -mcpu=power8 support... Yes." | tee -a configure.log
68c528
+  else
68c528
+    echo "Checking for -mcpu=power8 support... No." | tee -a configure.log
68c528
+  fi
68c528
+
68c528
+  if tryboth $CC -c $CFLAGS -mcpu=power9 $test.c; then
68c528
+    POWER9="-DZ_POWER9"
68c528
+    PIC_OBJC="${PIC_OBJC}"
68c528
+    OBJC="${OBJC}"
68c528
+    echo "Checking for -mcpu=power9 support... Yes." | tee -a configure.log
68c528
+  else
68c528
+    echo "Checking for -mcpu=power9 support... No." | tee -a configure.log
68c528
+  fi
68c528
+
68c528
+  SFLAGS="${SFLAGS} ${POWER8} ${POWER9} -DZ_POWER_OPT"
68c528
+  CFLAGS="${CFLAGS} ${POWER8} ${POWER9} -DZ_POWER_OPT"
68c528
+  echo "Checking for Power optimizations support... Yes." | tee -a configure.log
68c528
+else
68c528
+  echo "Checking for Power optimizations support... No." | tee -a configure.log
68c528
+fi
68c528
+
68c528
 # show the results in the log
68c528
 echo >> configure.log
68c528
 echo ALL = $ALL >> configure.log
68c528
diff --git a/contrib/README.contrib b/contrib/README.contrib
68c528
index b4d3b18..2a53f90 100644
68c528
--- a/contrib/README.contrib
68c528
+++ b/contrib/README.contrib
68c528
@@ -19,6 +19,10 @@ asm686/     by Brian Raiter <breadbox@muppetlabs.com>
68c528
 blast/      by Mark Adler <madler@alumni.caltech.edu>
68c528
         Decompressor for output of PKWare Data Compression Library (DCL)
68c528
 
68c528
+gcc/        by Matheus Castanho <msc@linux.ibm.com>
68c528
+            and Rogerio Alves <rcardoso@linux.ibm.com>
68c528
+        Optimization helpers using GCC-specific extensions
68c528
+
68c528
 delphi/     by Cosmin Truta <cosmint@cs.ubbcluj.ro>
68c528
         Support for Delphi and C++ Builder
68c528
 
68c528
@@ -63,6 +67,10 @@ minizip/    by Gilles Vollant <info@winimage.com>
68c528
 pascal/     by Bob Dellaca <bobdl@xtra.co.nz> et al.
68c528
         Support for Pascal
68c528
 
68c528
+power/      by Matheus Castanho <msc@linux.ibm.com>
68c528
+            and Rogerio Alves <rcardoso@linux.ibm.com>
68c528
+        Optimized functions for Power processors
68c528
+
68c528
 puff/       by Mark Adler <madler@alumni.caltech.edu>
68c528
         Small, low memory usage inflate.  Also serves to provide an
68c528
         unambiguous description of the deflate format.
68c528
diff --git a/contrib/gcc/zifunc.h b/contrib/gcc/zifunc.h
68c528
new file mode 100644
68c528
index 0000000..daf4fe4
68c528
--- /dev/null
68c528
+++ b/contrib/gcc/zifunc.h
68c528
@@ -0,0 +1,60 @@
68c528
+/* Copyright (C) 2019 Matheus Castanho <msc@linux.ibm.com>, IBM
68c528
+ *               2019 Rogerio Alves    <rogerio.alves@ibm.com>, IBM
68c528
+ * For conditions of distribution and use, see copyright notice in zlib.h
68c528
+ */
68c528
+
68c528
+#ifndef Z_IFUNC_H_
68c528
+#define Z_IFUNC_H_
68c528
+
68c528
+/* Helpers for arch optimizations */
68c528
+
68c528
+#define Z_IFUNC(fname) \
68c528
+    typeof(fname) fname __attribute__ ((ifunc (#fname "_resolver"))); \
68c528
+    local typeof(fname) *fname##_resolver(void)
68c528
+/* This is a helper macro to declare a resolver for an indirect function
68c528
+ * (ifunc). Let's say you have function
68c528
+ *
68c528
+ *    int foo (int a);
68c528
+ *
68c528
+ * for which you want to provide different implementations, for example:
68c528
+ *
68c528
+ *    int foo_clever (int a) {
68c528
+ *      ... clever things ...
68c528
+ *    }
68c528
+ *
68c528
+ *    int foo_smart (int a) {
68c528
+ *      ... smart things ...
68c528
+ *    }
68c528
+ *
68c528
+ * You will have to declare foo() as an indirect function and also provide a
68c528
+ * resolver for it, to choose between foo_clever() and foo_smart() based on
68c528
+ * some criteria you define (e.g. processor features).
68c528
+ *
68c528
+ * Since most likely foo() has a default implementation somewhere in zlib, you
68c528
+ * may have to rename it so the 'foo' symbol can be used by the ifunc without
68c528
+ * conflicts.
68c528
+ *
68c528
+ *    #define foo foo_default
68c528
+ *    int foo (int a) {
68c528
+ *      ...
68c528
+ *    }
68c528
+ *    #undef foo
68c528
+ *
68c528
+ * Now you just have to provide a resolver function to choose which function
68c528
+ * should be used (decided at runtime on the first call to foo()):
68c528
+ *
68c528
+ *    Z_IFUNC(foo) {
68c528
+ *        if (... some condition ...)
68c528
+ *          return foo_clever;
68c528
+ *
68c528
+ *        if (... other condition ...)
68c528
+ *          return foo_smart;
68c528
+ *
68c528
+ *        return foo_default;
68c528
+ *    }
68c528
+ *
68c528
+ * All calls to foo() throughout the code can remain untouched, all the magic
68c528
+ * will be done by the linker using the resolver function.
68c528
+ */
68c528
+
68c528
+#endif /* Z_IFUNC_H_ */
68c528
diff --git a/contrib/power/power.h b/contrib/power/power.h
68c528
new file mode 100644
68c528
index 0000000..b42c7d6
68c528
--- /dev/null
68c528
+++ b/contrib/power/power.h
68c528
@@ -0,0 +1,4 @@
68c528
+/* Copyright (C) 2019 Matheus Castanho <msc@linux.ibm.com>, IBM
68c528
+ *               2019 Rogerio Alves    <rogerio.alves@ibm.com>, IBM
68c528
+ * For conditions of distribution and use, see copyright notice in zlib.h
68c528
+ */
68c528
-- 
68c528
2.39.1
68c528