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