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