adenilson / rpms / zlib

Forked from rpms/zlib 8 months ago
Clone
4e3973
From 2dfdc5b7d6943c0ac60eef63e361e2a50f9da610 Mon Sep 17 00:00:00 2001
4e3973
From: Ilya Leoshkevich <iii@linux.ibm.com>
4e3973
Date: Thu, 19 Mar 2020 11:52:03 +0100
4e3973
Subject: [PATCH] s390x: vectorize crc32
4e3973
4e3973
Use vector extensions when compiling for s390x and binutils knows
4e3973
about them. At runtime, check whether kernel supports vector
4e3973
extensions (it has to be not just the CPU, but also the kernel) and
4e3973
choose between the regular and the vectorized implementations.
4e3973
---
4e3973
 Makefile.in             |   9 ++
4e3973
 configure               |  28 ++++++
4e3973
 contrib/s390/crc32-vx.c | 195 ++++++++++++++++++++++++++++++++++++++++
4e3973
 crc32.c                 |  55 +++++++++++-
4e3973
 4 files changed, 285 insertions(+), 2 deletions(-)
4e3973
 create mode 100644 contrib/s390/crc32-vx.c
4e3973
4e3973
diff --git a/Makefile.in b/Makefile.in
4e3973
index 6070dcc..9e9743b 100644
4e3973
--- a/Makefile.in
4e3973
+++ b/Makefile.in
4e3973
@@ -29,6 +29,7 @@ LDFLAGS=
4e3973
 TEST_LDFLAGS=-L. libz.a
4e3973
 LDSHARED=$(CC)
4e3973
 CPP=$(CC) -E
4e3973
+VGFMAFLAG=
4e3973
 
4e3973
 STATICLIB=libz.a
4e3973
 SHAREDLIB=libz.so
4e3973
@@ -179,6 +180,9 @@ crc32_power8.o: $(SRCDIR)contrib/power8-crc/vec_crc32.c
4e3973
 crc32.o: $(SRCDIR)crc32.c
4e3973
 	$(CC) $(CFLAGS) $(ZINC) -c -o $@ $(SRCDIR)crc32.c
4e3973
 
4e3973
+crc32-vx.o: $(SRCDIR)contrib/s390/crc32-vx.c
4e3973
+	$(CC) $(CFLAGS) $(VGFMAFLAG) $(ZINC) -c -o $@ $(SRCDIR)contrib/s390/crc32-vx.c
4e3973
+
4e3973
 deflate.o: $(SRCDIR)deflate.c
4e3973
 	$(CC) $(CFLAGS) $(ZINC) -c -o $@ $(SRCDIR)deflate.c
4e3973
 
4e3973
@@ -234,6 +238,11 @@ crc32.lo: $(SRCDIR)crc32.c
4e3973
 	$(CC) $(SFLAGS) $(ZINC) -DPIC -c -o objs/crc32.o $(SRCDIR)crc32.c
4e3973
 	-@mv objs/crc32.o $@
4e3973
 
4e3973
+crc32-vx.lo: $(SRCDIR)contrib/s390/crc32-vx.c
4e3973
+	-@mkdir objs 2>/dev/null || test -d objs
4e3973
+	$(CC) $(SFLAGS) $(VGFMAFLAG) $(ZINC) -DPIC -c -o objs/crc32-vx.o $(SRCDIR)contrib/s390/crc32-vx.c
4e3973
+	-@mv objs/crc32-vx.o $@
4e3973
+
4e3973
 deflate.lo: $(SRCDIR)deflate.c
4e3973
 	-@mkdir objs 2>/dev/null || test -d objs
4e3973
 	$(CC) $(SFLAGS) $(ZINC) -DPIC -c -o objs/deflate.o $(SRCDIR)deflate.c
4e3973
diff --git a/configure b/configure
4e3973
index 70ed86b..7941f75 100755
4e3973
--- a/configure
4e3973
+++ b/configure
4e3973
@@ -923,6 +923,32 @@ EOF
4e3973
   fi
4e3973
 fi
4e3973
 
4e3973
+# check if we are compiling for s390 and binutils support vector extensions
4e3973
+VGFMAFLAG=-march=z13
4e3973
+cat > $test.c <
4e3973
+#ifndef __s390__
4e3973
+#error
4e3973
+#endif
4e3973
+EOF
4e3973
+if try $CC -c $CFLAGS $VGFMAFLAG $test.c; then
4e3973
+  CFLAGS="$CFLAGS -DHAVE_S390X_VX"
4e3973
+  SFLAGS="$SFLAGS -DHAVE_S390X_VX"
4e3973
+  OBJC="$OBJC crc32-vx.o"
4e3973
+  PIC_OBJC="$PIC_OBJC crc32-vx.lo"
4e3973
+  echo "Checking for s390 vector extensions... Yes." | tee -a configure.log
4e3973
+
4e3973
+  for flag in -mzarch -fzvector; do
4e3973
+    if try $CC -c $CFLAGS $VGFMAFLAG $flag $test.c; then
4e3973
+      VGFMAFLAG="$VGFMAFLAG $flag"
4e3973
+      echo "Checking for $flag... Yes." | tee -a configure.log
4e3973
+    else
4e3973
+      echo "Checking for $flag... No." | tee -a configure.log
4e3973
+    fi
4e3973
+  done
4e3973
+else
4e3973
+  echo "Checking for s390 vector extensions... No." | tee -a configure.log
4e3973
+fi
4e3973
+
4e3973
 # show the results in the log
4e3973
 echo >> configure.log
4e3973
 echo ALL = $ALL >> configure.log
4e3973
@@ -955,6 +981,7 @@ echo mandir = $mandir >> configure.log
4e3973
 echo prefix = $prefix >> configure.log
4e3973
 echo sharedlibdir = $sharedlibdir >> configure.log
4e3973
 echo uname = $uname >> configure.log
4e3973
+echo VGFMAFLAG = $VGFMAFLAG >> configure.log
4e3973
 
4e3973
 # udpate Makefile with the configure results
4e3973
 sed < ${SRCDIR}Makefile.in "
4e3973
@@ -964,6 +991,7 @@ sed < ${SRCDIR}Makefile.in "
4e3973
 /^LDFLAGS *=/s#=.*#=$LDFLAGS#
4e3973
 /^LDSHARED *=/s#=.*#=$LDSHARED#
4e3973
 /^CPP *=/s#=.*#=$CPP#
4e3973
+/^VGFMAFLAG *=/s#=.*#=$VGFMAFLAG#
4e3973
 /^STATICLIB *=/s#=.*#=$STATICLIB#
4e3973
 /^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
4e3973
 /^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
4e3973
diff --git a/contrib/s390/crc32-vx.c b/contrib/s390/crc32-vx.c
4e3973
new file mode 100644
4e3973
index 0000000..fa5387c
4e3973
--- /dev/null
4e3973
+++ b/contrib/s390/crc32-vx.c
4e3973
@@ -0,0 +1,195 @@
4e3973
+/*
4e3973
+ * Hardware-accelerated CRC-32 variants for Linux on z Systems
4e3973
+ *
4e3973
+ * Use the z/Architecture Vector Extension Facility to accelerate the
4e3973
+ * computing of bitreflected CRC-32 checksums.
4e3973
+ *
4e3973
+ * This CRC-32 implementation algorithm is bitreflected and processes
4e3973
+ * the least-significant bit first (Little-Endian).
4e3973
+ *
4e3973
+ * This code was originally written by Hendrik Brueckner
4e3973
+ * <brueckner@linux.vnet.ibm.com> for use in the Linux kernel and has been
4e3973
+ * relicensed under the zlib license.
4e3973
+ */
4e3973
+
4e3973
+#include "../../zutil.h"
4e3973
+
4e3973
+#include <stdint.h>
4e3973
+#include <vecintrin.h>
4e3973
+
4e3973
+typedef unsigned char uv16qi __attribute__((vector_size(16)));
4e3973
+typedef unsigned int uv4si __attribute__((vector_size(16)));
4e3973
+typedef unsigned long long uv2di __attribute__((vector_size(16)));
4e3973
+
4e3973
+uint32_t crc32_le_vgfm_16(uint32_t crc, const unsigned char *buf, size_t len) {
4e3973
+    /*
4e3973
+     * The CRC-32 constant block contains reduction constants to fold and
4e3973
+     * process particular chunks of the input data stream in parallel.
4e3973
+     *
4e3973
+     * For the CRC-32 variants, the constants are precomputed according to
4e3973
+     * these definitions:
4e3973
+     *
4e3973
+     *      R1 = [(x4*128+32 mod P'(x) << 32)]' << 1
4e3973
+     *      R2 = [(x4*128-32 mod P'(x) << 32)]' << 1
4e3973
+     *      R3 = [(x128+32 mod P'(x) << 32)]'   << 1
4e3973
+     *      R4 = [(x128-32 mod P'(x) << 32)]'   << 1
4e3973
+     *      R5 = [(x64 mod P'(x) << 32)]'       << 1
4e3973
+     *      R6 = [(x32 mod P'(x) << 32)]'       << 1
4e3973
+     *
4e3973
+     *      The bitreflected Barret reduction constant, u', is defined as
4e3973
+     *      the bit reversal of floor(x**64 / P(x)).
4e3973
+     *
4e3973
+     *      where P(x) is the polynomial in the normal domain and the P'(x) is the
4e3973
+     *      polynomial in the reversed (bitreflected) domain.
4e3973
+     *
4e3973
+     * CRC-32 (IEEE 802.3 Ethernet, ...) polynomials:
4e3973
+     *
4e3973
+     *      P(x)  = 0x04C11DB7
4e3973
+     *      P'(x) = 0xEDB88320
4e3973
+     */
4e3973
+    const uv16qi perm_le2be = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};  /* BE->LE mask */
4e3973
+    const uv2di r2r1 = {0x1C6E41596, 0x154442BD4};                                     /* R2, R1 */
4e3973
+    const uv2di r4r3 = {0x0CCAA009E, 0x1751997D0};                                     /* R4, R3 */
4e3973
+    const uv2di r5 = {0, 0x163CD6124};                                                 /* R5 */
4e3973
+    const uv2di ru_poly = {0, 0x1F7011641};                                            /* u' */
4e3973
+    const uv2di crc_poly = {0, 0x1DB710641};                                           /* P'(x) << 1 */
4e3973
+
4e3973
+    /*
4e3973
+     * Load the initial CRC value.
4e3973
+     *
4e3973
+     * The CRC value is loaded into the rightmost word of the
4e3973
+     * vector register and is later XORed with the LSB portion
4e3973
+     * of the loaded input data.
4e3973
+     */
4e3973
+    uv2di v0 = {0, 0};
4e3973
+    v0 = (uv2di)vec_insert(crc, (uv4si)v0, 3);
4e3973
+
4e3973
+    /* Load a 64-byte data chunk and XOR with CRC */
4e3973
+    uv2di v1 = vec_perm(((uv2di *)buf)[0], ((uv2di *)buf)[0], perm_le2be);
4e3973
+    uv2di v2 = vec_perm(((uv2di *)buf)[1], ((uv2di *)buf)[1], perm_le2be);
4e3973
+    uv2di v3 = vec_perm(((uv2di *)buf)[2], ((uv2di *)buf)[2], perm_le2be);
4e3973
+    uv2di v4 = vec_perm(((uv2di *)buf)[3], ((uv2di *)buf)[3], perm_le2be);
4e3973
+
4e3973
+    v1 ^= v0;
4e3973
+    buf += 64;
4e3973
+    len -= 64;
4e3973
+
4e3973
+    while (len >= 64) {
4e3973
+        /* Load the next 64-byte data chunk */
4e3973
+        uv16qi part1 = vec_perm(((uv16qi *)buf)[0], ((uv16qi *)buf)[0], perm_le2be);
4e3973
+        uv16qi part2 = vec_perm(((uv16qi *)buf)[1], ((uv16qi *)buf)[1], perm_le2be);
4e3973
+        uv16qi part3 = vec_perm(((uv16qi *)buf)[2], ((uv16qi *)buf)[2], perm_le2be);
4e3973
+        uv16qi part4 = vec_perm(((uv16qi *)buf)[3], ((uv16qi *)buf)[3], perm_le2be);
4e3973
+
4e3973
+        /*
4e3973
+         * Perform a GF(2) multiplication of the doublewords in V1 with
4e3973
+         * the R1 and R2 reduction constants in V0.  The intermediate result
4e3973
+         * is then folded (accumulated) with the next data chunk in PART1 and
4e3973
+         * stored in V1. Repeat this step for the register contents
4e3973
+         * in V2, V3, and V4 respectively.
4e3973
+         */
4e3973
+        v1 = (uv2di)vec_gfmsum_accum_128(r2r1, v1, part1);
4e3973
+        v2 = (uv2di)vec_gfmsum_accum_128(r2r1, v2, part2);
4e3973
+        v3 = (uv2di)vec_gfmsum_accum_128(r2r1, v3, part3);
4e3973
+        v4 = (uv2di)vec_gfmsum_accum_128(r2r1, v4, part4);
4e3973
+
4e3973
+        buf += 64;
4e3973
+        len -= 64;
4e3973
+    }
4e3973
+
4e3973
+    /*
4e3973
+     * Fold V1 to V4 into a single 128-bit value in V1.  Multiply V1 with R3
4e3973
+     * and R4 and accumulating the next 128-bit chunk until a single 128-bit
4e3973
+     * value remains.
4e3973
+     */
4e3973
+    v1 = (uv2di)vec_gfmsum_accum_128(r4r3, v1, (uv16qi)v2);
4e3973
+    v1 = (uv2di)vec_gfmsum_accum_128(r4r3, v1, (uv16qi)v3);
4e3973
+    v1 = (uv2di)vec_gfmsum_accum_128(r4r3, v1, (uv16qi)v4);
4e3973
+
4e3973
+    while (len >= 16) {
4e3973
+        /* Load next data chunk */
4e3973
+        v2 = vec_perm(*(uv2di *)buf, *(uv2di *)buf, perm_le2be);
4e3973
+
4e3973
+        /* Fold next data chunk */
4e3973
+        v1 = (uv2di)vec_gfmsum_accum_128(r4r3, v1, (uv16qi)v2);
4e3973
+
4e3973
+        buf += 16;
4e3973
+        len -= 16;
4e3973
+    }
4e3973
+
4e3973
+    /*
4e3973
+     * Set up a vector register for byte shifts.  The shift value must
4e3973
+     * be loaded in bits 1-4 in byte element 7 of a vector register.
4e3973
+     * Shift by 8 bytes: 0x40
4e3973
+     * Shift by 4 bytes: 0x20
4e3973
+     */
4e3973
+    uv16qi v9 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
4e3973
+    v9 = vec_insert((unsigned char)0x40, v9, 7);
4e3973
+
4e3973
+    /*
4e3973
+     * Prepare V0 for the next GF(2) multiplication: shift V0 by 8 bytes
4e3973
+     * to move R4 into the rightmost doubleword and set the leftmost
4e3973
+     * doubleword to 0x1.
4e3973
+     */
4e3973
+    v0 = vec_srb(r4r3, (uv2di)v9);
4e3973
+    v0[0] = 1;
4e3973
+
4e3973
+    /*
4e3973
+     * Compute GF(2) product of V1 and V0.  The rightmost doubleword
4e3973
+     * of V1 is multiplied with R4.  The leftmost doubleword of V1 is
4e3973
+     * multiplied by 0x1 and is then XORed with rightmost product.
4e3973
+     * Implicitly, the intermediate leftmost product becomes padded
4e3973
+     */
4e3973
+    v1 = (uv2di)vec_gfmsum_128(v0, v1);
4e3973
+
4e3973
+    /*
4e3973
+     * Now do the final 32-bit fold by multiplying the rightmost word
4e3973
+     * in V1 with R5 and XOR the result with the remaining bits in V1.
4e3973
+     *
4e3973
+     * To achieve this by a single VGFMAG, right shift V1 by a word
4e3973
+     * and store the result in V2 which is then accumulated.  Use the
4e3973
+     * vector unpack instruction to load the rightmost half of the
4e3973
+     * doubleword into the rightmost doubleword element of V1; the other
4e3973
+     * half is loaded in the leftmost doubleword.
4e3973
+     * The vector register with CONST_R5 contains the R5 constant in the
4e3973
+     * rightmost doubleword and the leftmost doubleword is zero to ignore
4e3973
+     * the leftmost product of V1.
4e3973
+     */
4e3973
+    v9 = vec_insert((unsigned char)0x20, v9, 7);
4e3973
+    v2 = vec_srb(v1, (uv2di)v9);
4e3973
+    v1 = vec_unpackl((uv4si)v1);  /* Split rightmost doubleword */
4e3973
+    v1 = (uv2di)vec_gfmsum_accum_128(r5, v1, (uv16qi)v2);
4e3973
+
4e3973
+    /*
4e3973
+     * Apply a Barret reduction to compute the final 32-bit CRC value.
4e3973
+     *
4e3973
+     * The input values to the Barret reduction are the degree-63 polynomial
4e3973
+     * in V1 (R(x)), degree-32 generator polynomial, and the reduction
4e3973
+     * constant u.  The Barret reduction result is the CRC value of R(x) mod
4e3973
+     * P(x).
4e3973
+     *
4e3973
+     * The Barret reduction algorithm is defined as:
4e3973
+     *
4e3973
+     *    1. T1(x) = floor( R(x) / x^32 ) GF2MUL u
4e3973
+     *    2. T2(x) = floor( T1(x) / x^32 ) GF2MUL P(x)
4e3973
+     *    3. C(x)  = R(x) XOR T2(x) mod x^32
4e3973
+     *
4e3973
+     *  Note: The leftmost doubleword of vector register containing
4e3973
+     *  CONST_RU_POLY is zero and, thus, the intermediate GF(2) product
4e3973
+     *  is zero and does not contribute to the final result.
4e3973
+     */
4e3973
+
4e3973
+    /* T1(x) = floor( R(x) / x^32 ) GF2MUL u */
4e3973
+    v2 = vec_unpackl((uv4si)v1);
4e3973
+    v2 = (uv2di)vec_gfmsum_128(ru_poly, v2);
4e3973
+
4e3973
+    /*
4e3973
+     * Compute the GF(2) product of the CRC polynomial with T1(x) in
4e3973
+     * V2 and XOR the intermediate result, T2(x), with the value in V1.
4e3973
+     * The final result is stored in word element 2 of V2.
4e3973
+     */
4e3973
+    v2 = vec_unpackl((uv4si)v2);
4e3973
+    v2 = (uv2di)vec_gfmsum_accum_128(crc_poly, v2, (uv16qi)v1);
4e3973
+
4e3973
+    return ((uv4si)v2)[2];
4e3973
+}
4e3973
diff --git a/crc32.c b/crc32.c
4e3973
index 34132ea..dfa33ef 100644
4e3973
--- a/crc32.c
4e3973
+++ b/crc32.c
4e3973
@@ -252,12 +252,54 @@ unsigned long crc32_vpmsum(unsigned long, const unsigned char FAR *, z_size_t);
4e3973
 #endif
4e3973
 #endif
4e3973
 
4e3973
+#ifdef HAVE_S390X_VX
4e3973
+#include <sys/auxv.h>
4e3973
+
4e3973
+#define VX_MIN_LEN 64
4e3973
+#define VX_ALIGNMENT 16L
4e3973
+#define VX_ALIGN_MASK (VX_ALIGNMENT - 1)
4e3973
+
4e3973
+unsigned int crc32_le_vgfm_16(unsigned int crc, const unsigned char FAR *buf, z_size_t len);
4e3973
+
4e3973
+local unsigned long s390_crc32_vx(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
4e3973
+{
4e3973
+    uint64_t prealign, aligned, remaining;
4e3973
+
4e3973
+    if (buf == Z_NULL) return 0UL;
4e3973
+
4e3973
+    if (len < VX_MIN_LEN + VX_ALIGN_MASK)
4e3973
+        return crc32_big(crc, buf, len);
4e3973
+
4e3973
+    if ((uintptr_t)buf & VX_ALIGN_MASK) {
4e3973
+        prealign = VX_ALIGNMENT - ((uintptr_t)buf & VX_ALIGN_MASK);
4e3973
+        len -= prealign;
4e3973
+        crc = crc32_big(crc, buf, prealign);
4e3973
+        buf += prealign;
4e3973
+    }
4e3973
+    aligned = len & ~VX_ALIGN_MASK;
4e3973
+    remaining = len & VX_ALIGN_MASK;
4e3973
+
4e3973
+    crc = crc32_le_vgfm_16(crc ^ 0xffffffff, buf, (size_t)aligned) ^ 0xffffffff;
4e3973
+
4e3973
+    if (remaining)
4e3973
+        crc = crc32_big(crc, buf + aligned, remaining);
4e3973
+
4e3973
+    return crc;
4e3973
+}
4e3973
+#endif
4e3973
+
4e3973
 /* due to a quirk of gnu_indirect_function - "local" (aka static) is applied to
4e3973
  * crc32_z which is not desired. crc32_z_ifunc is implictly "local" */
4e3973
 #ifndef Z_IFUNC_ASM
4e3973
 local
4e3973
 #endif
4e3973
-unsigned long (*(crc32_z_ifunc(void)))(unsigned long, const unsigned char FAR *, z_size_t)
4e3973
+unsigned long (*(crc32_z_ifunc(
4e3973
+#ifdef __s390__
4e3973
+unsigned long hwcap
4e3973
+#else
4e3973
+void
4e3973
+#endif
4e3973
+)))(unsigned long, const unsigned char FAR *, z_size_t)
4e3973
 {
4e3973
 #if _ARCH_PWR8==1
4e3973
 #if defined(__BUILTIN_CPU_SUPPORTS__)
4e3973
@@ -269,6 +311,11 @@ unsigned long (*(crc32_z_ifunc(void)))(unsigned long, const unsigned char FAR *,
4e3973
 #endif
4e3973
 #endif /* _ARCH_PWR8 */
4e3973
 
4e3973
+#ifdef HAVE_S390X_VX
4e3973
+    if (hwcap & HWCAP_S390_VX)
4e3973
+        return s390_crc32_vx;
4e3973
+#endif
4e3973
+
4e3973
 /* return a function pointer for optimized arches here */
4e3973
 
4e3973
 #ifdef DYNAMIC_CRC_TABLE
4e3973
@@ -301,7 +348,11 @@ unsigned long ZEXPORT crc32_z(crc, buf, len)
4e3973
     static unsigned long ZEXPORT (*crc32_func)(unsigned long, const unsigned char FAR *, z_size_t) = NULL;
4e3973
 
4e3973
     if (!crc32_func)
4e3973
-        crc32_func = crc32_z_ifunc();
4e3973
+        crc32_func = crc32_z_ifunc(
4e3973
+#ifdef __s390__
4e3973
+            getauxval(AT_HWCAP)
4e3973
+#endif
4e3973
+        );
4e3973
     return (*crc32_func)(crc, buf, len);
4e3973
 }
4e3973
 
4e3973
-- 
4e3973
2.25.1
4e3973