60c5b2
diff --git a/Makefile.am b/Makefile.am
60c5b2
index 025c914..8b4120c 100644
60c5b2
--- a/Makefile.am
60c5b2
+++ b/Makefile.am
60c5b2
@@ -55,6 +55,9 @@ gzip_SOURCES = \
60c5b2
   trees.c unlzh.c unlzw.c unpack.c unzip.c util.c zip.c
60c5b2
 gzip_LDADD = libver.a lib/libgzip.a
60c5b2
 gzip_LDADD += $(LIB_CLOCK_GETTIME)
60c5b2
+if IBM_Z_DFLTCC
60c5b2
+gzip_SOURCES += dfltcc.c
60c5b2
+endif
60c5b2
 
60c5b2
 BUILT_SOURCES += version.c
60c5b2
 version.c: Makefile
60c5b2
diff --git a/bits.c b/bits.c
60c5b2
index b0df2fe..9effc32 100644
60c5b2
--- a/bits.c
60c5b2
+++ b/bits.c
60c5b2
@@ -78,7 +78,7 @@
60c5b2
 
60c5b2
 local file_t zfile; /* output gzip file */
60c5b2
 
60c5b2
-local unsigned short bi_buf;
60c5b2
+unsigned short bi_buf;
60c5b2
 /* Output buffer. bits are inserted starting at the bottom (least significant
60c5b2
  * bits).
60c5b2
  */
60c5b2
@@ -88,7 +88,7 @@ local unsigned short bi_buf;
60c5b2
  * more than 16 bits on some systems.)
60c5b2
  */
60c5b2
 
60c5b2
-local int bi_valid;
60c5b2
+int bi_valid;
60c5b2
 /* Number of valid bits in bi_buf.  All bits above the last valid bit
60c5b2
  * are always zero.
60c5b2
  */
60c5b2
diff --git a/configure.ac b/configure.ac
60c5b2
index 9a2b635..76ac26f 100644
60c5b2
--- a/configure.ac
60c5b2
+++ b/configure.ac
60c5b2
@@ -78,6 +78,16 @@ AC_ARG_ENABLE([gcc-warnings],
60c5b2
    fi]
60c5b2
 )
60c5b2
 
60c5b2
+AC_ARG_ENABLE([dfltcc],
60c5b2
+  [AS_HELP_STRING([--enable-dfltcc],
60c5b2
+     [use DEFLATE COMPRESSION CALL instruction on IBM Z])],
60c5b2
+  [case $enableval in
60c5b2
+     yes|no) gl_dfltcc=$enableval ;;
60c5b2
+     *)      AC_MSG_ERROR([bad value $enableval for dfltcc option]) ;;
60c5b2
+   esac],
60c5b2
+  [gl_dfltcc=no]
60c5b2
+)
60c5b2
+
60c5b2
 # gl_GCC_VERSION_IFELSE([major], [minor], [run-if-found], [run-if-not-found])
60c5b2
 # ------------------------------------------------
60c5b2
 # If $CPP is gcc-MAJOR.MINOR or newer, then run RUN-IF-FOUND.
60c5b2
@@ -188,6 +198,12 @@ if test "$gl_gcc_warnings" = yes; then
60c5b2
   AC_SUBST([GNULIB_WARN_CFLAGS])
60c5b2
 fi
60c5b2
 
60c5b2
+if test "$gl_dfltcc" = yes; then
60c5b2
+  AC_DEFINE([IBM_Z_DFLTCC], ,
60c5b2
+    [Use DEFLATE COMPRESSION CALL instruction on IBM Z machines.])
60c5b2
+fi
60c5b2
+AM_CONDITIONAL([IBM_Z_DFLTCC],  [test "$gl_dfltcc" = yes])
60c5b2
+
60c5b2
 # cc -E produces incorrect asm files on SVR4, so postprocess it.
60c5b2
 ASCPPPOST="sed '/^ *\\#/d; s,//.*,,; s/% /%/g; s/\\. /./g'"
60c5b2
 AC_SUBST([ASCPPPOST])
60c5b2
diff --git a/deflate.c b/deflate.c
60c5b2
index 8ffff3a..869b902 100644
60c5b2
--- a/deflate.c
60c5b2
+++ b/deflate.c
60c5b2
@@ -123,10 +123,6 @@
60c5b2
 #define NIL 0
60c5b2
 /* Tail of hash chains */
60c5b2
 
60c5b2
-#define FAST 4
60c5b2
-#define SLOW 2
60c5b2
-/* speed options for the general purpose bit flag */
60c5b2
-
60c5b2
 #ifndef TOO_FAR
60c5b2
 #  define TOO_FAR 4096
60c5b2
 #endif
60c5b2
@@ -215,9 +211,6 @@ local unsigned int max_lazy_match;
60c5b2
  * max_insert_length is used only for compression levels <= 3.
60c5b2
  */
60c5b2
 
60c5b2
-local int compr_level;
60c5b2
-/* compression level (1..9) */
60c5b2
-
60c5b2
 unsigned good_match;
60c5b2
 /* Use a faster search when the previous match is longer than this */
60c5b2
 
60c5b2
@@ -308,14 +301,12 @@ local  void check_match (IPos start, IPos match, int length);
60c5b2
 /* ===========================================================================
60c5b2
  * Initialize the "longest match" routines for a new file
60c5b2
  */
60c5b2
-void lm_init (pack_level, flags)
60c5b2
+void lm_init (pack_level)
60c5b2
     int pack_level; /* 0: store, 1: best speed, 9: best compression */
60c5b2
-    ush *flags;     /* general purpose bit flag */
60c5b2
 {
60c5b2
     register unsigned j;
60c5b2
 
60c5b2
     if (pack_level < 1 || pack_level > 9) gzip_error ("bad pack level");
60c5b2
-    compr_level = pack_level;
60c5b2
 
60c5b2
     /* Initialize the hash table. */
60c5b2
 #if defined MAXSEG_64K && HASH_BITS == 15
60c5b2
@@ -337,11 +328,6 @@ void lm_init (pack_level, flags)
60c5b2
     nice_match       = configuration_table[pack_level].nice_length;
60c5b2
 #endif
60c5b2
     max_chain_length = configuration_table[pack_level].max_chain;
60c5b2
-    if (pack_level == 1) {
60c5b2
-       *flags |= FAST;
60c5b2
-    } else if (pack_level == 9) {
60c5b2
-       *flags |= SLOW;
60c5b2
-    }
60c5b2
     /* ??? reduce max_chain_length for binary files */
60c5b2
 
60c5b2
     strstart = 0;
60c5b2
@@ -732,7 +718,7 @@ local off_t deflate_fast()
60c5b2
  * evaluation for matches: a match is finally adopted only if there is
60c5b2
  * no better match at the next window position.
60c5b2
  */
60c5b2
-off_t deflate()
60c5b2
+off_t deflate(int pack_level)
60c5b2
 {
60c5b2
     IPos hash_head;          /* head of hash chain */
60c5b2
     IPos prev_match;         /* previous match */
60c5b2
@@ -740,7 +726,8 @@ off_t deflate()
60c5b2
     int match_available = 0; /* set if previous match exists */
60c5b2
     register unsigned match_length = MIN_MATCH-1; /* length of best match */
60c5b2
 
60c5b2
-    if (compr_level <= 3) return deflate_fast(); /* optimized for speed */
60c5b2
+    lm_init(pack_level);
60c5b2
+    if (pack_level <= 3) return deflate_fast(); /* optimized for speed */
60c5b2
 
60c5b2
     /* Process the input block. */
60c5b2
     while (lookahead != 0) {
60c5b2
diff --git a/dfltcc.c b/dfltcc.c
60c5b2
new file mode 100644
60c5b2
index 0000000..9010475
60c5b2
--- /dev/null
60c5b2
+++ b/dfltcc.c
60c5b2
@@ -0,0 +1,429 @@
60c5b2
+/* dfltcc.c -- compress data using IBM Z DEFLATE COMPRESSION CALL
60c5b2
+
60c5b2
+   Copyright (C) 2019 Free Software Foundation, Inc.
60c5b2
+
60c5b2
+   This program is free software; you can redistribute it and/or modify
60c5b2
+   it under the terms of the GNU General Public License as published by
60c5b2
+   the Free Software Foundation; either version 3, or (at your option)
60c5b2
+   any later version.
60c5b2
+
60c5b2
+   This program is distributed in the hope that it will be useful,
60c5b2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
60c5b2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60c5b2
+   GNU General Public License for more details.
60c5b2
+
60c5b2
+   You should have received a copy of the GNU General Public License
60c5b2
+   along with this program; if not, write to the Free Software Foundation,
60c5b2
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
60c5b2
+
60c5b2
+#include <config.h>
60c5b2
+#include <stdlib.h>
60c5b2
+#ifdef DFLTCC_USDT
60c5b2
+#include <sys/sdt.h>
60c5b2
+#endif
60c5b2
+
60c5b2
+#include "tailor.h"
60c5b2
+#include "gzip.h"
60c5b2
+
60c5b2
+#ifdef DYN_ALLOC
60c5b2
+    error: DYN_ALLOC is not supported by DFLTCC
60c5b2
+#endif
60c5b2
+
60c5b2
+/* ===========================================================================
60c5b2
+ * C wrappers for the DEFLATE CONVERSION CALL instruction.
60c5b2
+ */
60c5b2
+
60c5b2
+typedef enum
60c5b2
+{
60c5b2
+    DFLTCC_CC_OK = 0,
60c5b2
+    DFLTCC_CC_OP1_TOO_SHORT = 1,
60c5b2
+    DFLTCC_CC_OP2_TOO_SHORT = 2,
60c5b2
+    DFLTCC_CC_OP2_CORRUPT = 2,
60c5b2
+    DFLTCC_CC_AGAIN = 3,
60c5b2
+} dfltcc_cc;
60c5b2
+
60c5b2
+#define DFLTCC_QAF 0
60c5b2
+#define DFLTCC_GDHT 1
60c5b2
+#define DFLTCC_CMPR 2
60c5b2
+#define DFLTCC_XPND 4
60c5b2
+#define HBT_CIRCULAR (1 << 7)
60c5b2
+//#define HB_BITS 15
60c5b2
+//#define HB_SIZE (1 << HB_BITS)
60c5b2
+#define DFLTCC_FACILITY 151
60c5b2
+#define DFLTCC_FMT0 0
60c5b2
+#define CVT_CRC32 0
60c5b2
+#define HTT_FIXED 0
60c5b2
+#define HTT_DYNAMIC 1
60c5b2
+
60c5b2
+struct dfltcc_qaf_param
60c5b2
+{
60c5b2
+    char fns[16];
60c5b2
+    char reserved1[8];
60c5b2
+    char fmts[2];
60c5b2
+    char reserved2[6];
60c5b2
+};
60c5b2
+
60c5b2
+struct dfltcc_param_v0
60c5b2
+{
60c5b2
+    unsigned short pbvn;               /* Parameter-Block-Version Number */
60c5b2
+    unsigned char mvn;                 /* Model-Version Number */
60c5b2
+    unsigned char ribm;                /* Reserved for IBM use */
60c5b2
+    unsigned reserved32 : 31;
60c5b2
+    unsigned cf : 1;                   /* Continuation Flag */
60c5b2
+    unsigned char reserved64[8];
60c5b2
+    unsigned nt : 1;                   /* New Task */
60c5b2
+    unsigned reserved129 : 1;
60c5b2
+    unsigned cvt : 1;                  /* Check Value Type */
60c5b2
+    unsigned reserved131 : 1;
60c5b2
+    unsigned htt : 1;                  /* Huffman-Table Type */
60c5b2
+    unsigned bcf : 1;                  /* Block-Continuation Flag */
60c5b2
+    unsigned bcc : 1;                  /* Block Closing Control */
60c5b2
+    unsigned bhf : 1;                  /* Block Header Final */
60c5b2
+    unsigned reserved136 : 1;
60c5b2
+    unsigned reserved137 : 1;
60c5b2
+    unsigned dhtgc : 1;                /* DHT Generation Control */
60c5b2
+    unsigned reserved139 : 5;
60c5b2
+    unsigned reserved144 : 5;
60c5b2
+    unsigned sbb : 3;                  /* Sub-Byte Boundary */
60c5b2
+    unsigned char oesc;                /* Operation-Ending-Supplemental Code */
60c5b2
+    unsigned reserved160 : 12;
60c5b2
+    unsigned ifs : 4;                  /* Incomplete-Function Status */
60c5b2
+    unsigned short ifl;                /* Incomplete-Function Length */
60c5b2
+    unsigned char reserved192[8];
60c5b2
+    unsigned char reserved256[8];
60c5b2
+    unsigned char reserved320[4];
60c5b2
+    unsigned short hl;                 /* History Length */
60c5b2
+    unsigned reserved368 : 1;
60c5b2
+    unsigned short ho : 15;            /* History Offset */
60c5b2
+    unsigned int cv;                   /* Check Value */
60c5b2
+    unsigned eobs : 15;                /* End-of-block Symbol */
60c5b2
+    unsigned reserved431 : 1;
60c5b2
+    unsigned char eobl : 4;            /* End-of-block Length */
60c5b2
+    unsigned reserved436 : 12;
60c5b2
+    unsigned reserved448 : 4;
60c5b2
+    unsigned short cdhtl : 12;         /* Compressed-Dynamic-Huffman Table
60c5b2
+                                          Length */
60c5b2
+    unsigned char reserved464[6];
60c5b2
+    unsigned char cdht[288];
60c5b2
+    unsigned char reserved[32];
60c5b2
+    unsigned char csb[1152];
60c5b2
+};
60c5b2
+
60c5b2
+static int is_bit_set(const char *bits, int n)
60c5b2
+{
60c5b2
+    return bits[n / 8] & (1 << (7 - (n % 8)));
60c5b2
+}
60c5b2
+
60c5b2
+static int is_dfltcc_enabled(void)
60c5b2
+{
60c5b2
+    const char *env;
60c5b2
+    char facilities[((DFLTCC_FACILITY / 64) + 1) * 8];
60c5b2
+    register int r0 __asm__("r0");
60c5b2
+
60c5b2
+    env = getenv("DFLTCC");
60c5b2
+    if (env && !strcmp(env, "0")) {
60c5b2
+        return 0;
60c5b2
+    }
60c5b2
+
60c5b2
+    r0 = sizeof(facilities) / 8;
60c5b2
+    __asm__("stfle %[facilities]\n"
60c5b2
+            : [facilities] "=Q"(facilities) : [r0] "r"(r0) : "cc", "memory");
60c5b2
+    return is_bit_set((const char *) facilities, DFLTCC_FACILITY);
60c5b2
+}
60c5b2
+
60c5b2
+static dfltcc_cc dfltcc(int fn, void *param,
60c5b2
+                        uch **op1, size_t *len1,
60c5b2
+                        const uch **op2, size_t *len2,
60c5b2
+                        void *hist)
60c5b2
+{
60c5b2
+    uch *t2 = op1 ? *op1 : NULL;
60c5b2
+    size_t t3 = len1 ? *len1 : 0;
60c5b2
+    const uch *t4 = op2 ? *op2 : NULL;
60c5b2
+    size_t t5 = len2 ? *len2 : 0;
60c5b2
+    register int r0 __asm__("r0") = fn;
60c5b2
+    register void *r1 __asm__("r1") = param;
60c5b2
+    register uch *r2 __asm__("r2") = t2;
60c5b2
+    register size_t r3 __asm__("r3") = t3;
60c5b2
+    register const uch *r4 __asm__("r4") = t4;
60c5b2
+    register size_t r5 __asm__("r5") = t5;
60c5b2
+    int cc;
60c5b2
+
60c5b2
+    __asm__ volatile(
60c5b2
+#ifdef DFLTCC_USDT
60c5b2
+                     STAP_PROBE_ASM(zlib, dfltcc_entry,
60c5b2
+                                    STAP_PROBE_ASM_TEMPLATE(5))
60c5b2
+#endif
60c5b2
+                     ".insn rrf,0xb9390000,%[r2],%[r4],%[hist],0\n"
60c5b2
+#ifdef DFLTCC_USDT
60c5b2
+                     STAP_PROBE_ASM(zlib, dfltcc_exit,
60c5b2
+                                    STAP_PROBE_ASM_TEMPLATE(5))
60c5b2
+#endif
60c5b2
+                     "ipm %[cc]\n"
60c5b2
+                     : [r2] "+r" (r2)
60c5b2
+                     , [r3] "+r" (r3)
60c5b2
+                     , [r4] "+r" (r4)
60c5b2
+                     , [r5] "+r" (r5)
60c5b2
+                     , [cc] "=r" (cc)
60c5b2
+                     : [r0] "r" (r0)
60c5b2
+                     , [r1] "r" (r1)
60c5b2
+                     , [hist] "r" (hist)
60c5b2
+#ifdef DFLTCC_USDT
60c5b2
+                     , STAP_PROBE_ASM_OPERANDS(5, r2, r3, r4, r5, hist)
60c5b2
+#endif
60c5b2
+                     : "cc", "memory");
60c5b2
+    t2 = r2; t3 = r3; t4 = r4; t5 = r5;
60c5b2
+
60c5b2
+    if (op1)
60c5b2
+        *op1 = t2;
60c5b2
+    if (len1)
60c5b2
+        *len1 = t3;
60c5b2
+    if (op2)
60c5b2
+        *op2 = t4;
60c5b2
+    if (len2)
60c5b2
+        *len2 = t5;
60c5b2
+    return (cc >> 28) & 3;
60c5b2
+}
60c5b2
+
60c5b2
+static void dfltcc_qaf(struct dfltcc_qaf_param *param)
60c5b2
+{
60c5b2
+    dfltcc(DFLTCC_QAF, param, NULL, NULL, NULL, NULL, NULL);
60c5b2
+}
60c5b2
+
60c5b2
+static void dfltcc_gdht(struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+    const uch *next_in = inbuf + inptr;
60c5b2
+    size_t avail_in = insize - inptr;
60c5b2
+
60c5b2
+    dfltcc(DFLTCC_GDHT, param, NULL, NULL, &next_in, &avail_in, NULL);
60c5b2
+}
60c5b2
+
60c5b2
+static off_t total_in;
60c5b2
+
60c5b2
+static dfltcc_cc dfltcc_cmpr_xpnd(struct dfltcc_param_v0 *param, int fn)
60c5b2
+{
60c5b2
+    uch *next_out = outbuf + outcnt;
60c5b2
+    size_t avail_out = OUTBUFSIZ - outcnt;
60c5b2
+    const uch *next_in = inbuf + inptr;
60c5b2
+    size_t avail_in = insize - inptr;
60c5b2
+    off_t consumed_in;
60c5b2
+    dfltcc_cc cc;
60c5b2
+
60c5b2
+    cc = dfltcc(fn | HBT_CIRCULAR, param,
60c5b2
+                &next_out, &avail_out,
60c5b2
+                &next_in, &avail_in,
60c5b2
+                window);
60c5b2
+    consumed_in = next_in - (inbuf + inptr);
60c5b2
+    inptr += consumed_in;
60c5b2
+    total_in += consumed_in;
60c5b2
+    outcnt += ((OUTBUFSIZ - outcnt) - avail_out);
60c5b2
+    return cc;
60c5b2
+}
60c5b2
+
60c5b2
+__attribute__((aligned(8)))
60c5b2
+static struct context
60c5b2
+{
60c5b2
+    union
60c5b2
+    {
60c5b2
+        struct dfltcc_qaf_param af;
60c5b2
+        struct dfltcc_param_v0 param;
60c5b2
+    };
60c5b2
+} ctx;
60c5b2
+
60c5b2
+static struct dfltcc_param_v0 *init_param(struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+    const char *s;
60c5b2
+
60c5b2
+    memset(param, 0, sizeof(*param));
60c5b2
+#ifndef DFLTCC_RIBM
60c5b2
+#define DFLTCC_RIBM 0
60c5b2
+#endif
60c5b2
+    s = getenv("DFLTCC_RIBM");
60c5b2
+    param->ribm = (s && *s) ? strtoul(s, NULL, 0) : DFLTCC_RIBM;
60c5b2
+    param->nt = 1;
60c5b2
+    param->cvt = CVT_CRC32;
60c5b2
+    param->cv = __builtin_bswap32(getcrc());
60c5b2
+    return param;
60c5b2
+}
60c5b2
+
60c5b2
+static void bi_close_block(struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+    bi_valid = param->sbb;
60c5b2
+    bi_buf = bi_valid == 0 ? 0 : outbuf[outcnt] & ((1 << bi_valid) - 1);
60c5b2
+    send_bits(
60c5b2
+        bi_reverse(param->eobs >> (15 - param->eobl), param->eobl),
60c5b2
+        param->eobl);
60c5b2
+    param->bcf = 0;
60c5b2
+}
60c5b2
+
60c5b2
+static void close_block(struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+    bi_close_block(param);
60c5b2
+    bi_windup();
60c5b2
+    param->sbb = (param->sbb + param->eobl) % 8;
60c5b2
+    if (param->sbb != 0) {
60c5b2
+        Assert(outcnt > 0, "outbuf must have enough space for EOBS");
60c5b2
+        outcnt--;
60c5b2
+    }
60c5b2
+}
60c5b2
+
60c5b2
+static void close_stream(struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+    if (param->bcf) {
60c5b2
+        bi_close_block(param);
60c5b2
+    }
60c5b2
+    send_bits(1, 3); /* BFINAL=1, BTYPE=00 */
60c5b2
+    bi_windup();
60c5b2
+    put_short(0x0000);
60c5b2
+    put_short(0xFFFF);
60c5b2
+}
60c5b2
+
60c5b2
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
60c5b2
+
60c5b2
+/* ===========================================================================
60c5b2
+ * Compress ifd into ofd in hardware or fall back to software.
60c5b2
+ */
60c5b2
+int dfltcc_deflate(int pack_level)
60c5b2
+{
60c5b2
+    const char *s;
60c5b2
+    unsigned long level_mask;
60c5b2
+    unsigned long block_size;
60c5b2
+    off_t block_threshold;
60c5b2
+    struct dfltcc_param_v0 *param;
60c5b2
+    int extra;
60c5b2
+
60c5b2
+    /* Check whether we can use hardware compression */
60c5b2
+    if (!is_dfltcc_enabled() || getenv("SOURCE_DATE_EPOCH")) {
60c5b2
+        return deflate(pack_level);
60c5b2
+    }
60c5b2
+#ifndef DFLTCC_LEVEL_MASK
60c5b2
+#define DFLTCC_LEVEL_MASK 0x2
60c5b2
+#endif
60c5b2
+    s = getenv("DFLTCC_LEVEL_MASK");
60c5b2
+    level_mask = (s && *s) ? strtoul(s, NULL, 0) : DFLTCC_LEVEL_MASK;
60c5b2
+    if ((level_mask & (1 << pack_level)) == 0) {
60c5b2
+        return deflate(pack_level);
60c5b2
+    }
60c5b2
+    dfltcc_qaf(&ctx.af);
60c5b2
+    if (!is_bit_set(ctx.af.fns, DFLTCC_CMPR) ||
60c5b2
+        !is_bit_set(ctx.af.fns, DFLTCC_GDHT) ||
60c5b2
+        !is_bit_set(ctx.af.fmts, DFLTCC_FMT0)) {
60c5b2
+        return deflate(pack_level);
60c5b2
+    }
60c5b2
+
60c5b2
+    /* Initialize tuning parameters */
60c5b2
+#ifndef DFLTCC_BLOCK_SIZE
60c5b2
+#define DFLTCC_BLOCK_SIZE 1048576
60c5b2
+#endif
60c5b2
+    s = getenv("DFLTCC_BLOCK_SIZE");
60c5b2
+    block_size = (s && *s) ? strtoul(s, NULL, 0) : DFLTCC_BLOCK_SIZE;
60c5b2
+    (void)block_size;
60c5b2
+#ifndef DFLTCC_FIRST_FHT_BLOCK_SIZE
60c5b2
+#define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
60c5b2
+#endif
60c5b2
+    s = getenv("DFLTCC_FIRST_FHT_BLOCK_SIZE");
60c5b2
+    block_threshold = (s && *s) ? strtoul(s, NULL, 0) :
60c5b2
+                                  DFLTCC_FIRST_FHT_BLOCK_SIZE;
60c5b2
+
60c5b2
+    /* Compress ifd into ofd in a loop */
60c5b2
+    param = init_param(&ctx.param);
60c5b2
+    while (1) {
60c5b2
+        /* Flush the output data */
60c5b2
+        if (outcnt > OUTBUFSIZ - 8) {
60c5b2
+            flush_outbuf();
60c5b2
+        }
60c5b2
+
60c5b2
+        /* Close the block */
60c5b2
+        if (param->bcf && total_in == block_threshold && !param->cf) {
60c5b2
+            close_block(param);
60c5b2
+            block_threshold += block_size;
60c5b2
+        }
60c5b2
+
60c5b2
+        /* Read the input data */
60c5b2
+        if (inptr == insize) {
60c5b2
+            if (fill_inbuf(1) == EOF && !param->cf) {
60c5b2
+                break;
60c5b2
+            }
60c5b2
+            inptr = 0;
60c5b2
+        }
60c5b2
+
60c5b2
+        /* Temporarily mask some input data */
60c5b2
+        extra = MAX(0, total_in + (insize - inptr) - block_threshold);
60c5b2
+        insize -= extra;
60c5b2
+
60c5b2
+        /* Start a new block */
60c5b2
+        if (!param->bcf) {
60c5b2
+            if (total_in == 0 && block_threshold > 0) {
60c5b2
+                param->htt = HTT_FIXED;
60c5b2
+            } else {
60c5b2
+                param->htt = HTT_DYNAMIC;
60c5b2
+                dfltcc_gdht(param);
60c5b2
+            }
60c5b2
+        }
60c5b2
+
60c5b2
+        /* Compress inbuf into outbuf */
60c5b2
+        dfltcc_cmpr_xpnd(param, DFLTCC_CMPR);
60c5b2
+
60c5b2
+        /* Unmask the input data */
60c5b2
+        insize += extra;
60c5b2
+
60c5b2
+        /* Continue the block */
60c5b2
+        param->bcf = 1;
60c5b2
+    }
60c5b2
+    close_stream(param);
60c5b2
+    setcrc(__builtin_bswap32(param->cv));
60c5b2
+    return 0;
60c5b2
+}
60c5b2
+
60c5b2
+/* ===========================================================================
60c5b2
+ * Decompress ifd into ofd in hardware or fall back to software.
60c5b2
+ */
60c5b2
+int dfltcc_inflate(void)
60c5b2
+{
60c5b2
+    struct dfltcc_param_v0 *param;
60c5b2
+    dfltcc_cc cc;
60c5b2
+
60c5b2
+    /* Check whether we can use hardware decompression */
60c5b2
+    if (!is_dfltcc_enabled()) {
60c5b2
+        return inflate();
60c5b2
+    }
60c5b2
+    dfltcc_qaf(&ctx.af);
60c5b2
+    if (!is_bit_set(ctx.af.fns, DFLTCC_XPND)) {
60c5b2
+        return inflate();
60c5b2
+    }
60c5b2
+
60c5b2
+    /* Decompress ifd into ofd in a loop */
60c5b2
+    param = init_param(&ctx.param);
60c5b2
+    while (1) {
60c5b2
+        /* Perform I/O */
60c5b2
+        if (outcnt == OUTBUFSIZ) {
60c5b2
+            flush_outbuf();
60c5b2
+        }
60c5b2
+        if (inptr == insize) {
60c5b2
+            if (fill_inbuf(1) == EOF) {
60c5b2
+                /* Premature EOF */
60c5b2
+                return 2;
60c5b2
+            }
60c5b2
+            inptr = 0;
60c5b2
+        }
60c5b2
+        /* Decompress inbuf into outbuf */
60c5b2
+        cc = dfltcc_cmpr_xpnd(param, DFLTCC_XPND);
60c5b2
+        if (cc == DFLTCC_CC_OK) {
60c5b2
+            /* The entire deflate stream has been successfully decompressed */
60c5b2
+            break;
60c5b2
+        }
60c5b2
+        if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0) {
60c5b2
+            /* The deflate stream is corrupted */
60c5b2
+            return 2;
60c5b2
+        }
60c5b2
+        /* There must be more data to decompress */
60c5b2
+    }
60c5b2
+    if (param->sbb != 0) {
60c5b2
+        /* The deflate stream has ended in the middle of a byte - go to the next
60c5b2
+         * byte boundary, so that unzip() can read CRC and length.
60c5b2
+         */
60c5b2
+        inptr++;
60c5b2
+    }
60c5b2
+    setcrc(__builtin_bswap32(param->cv)); /* set CRC value for unzip() */
60c5b2
+    flush_outbuf(); /* update bytes_out for unzip() */
60c5b2
+    return 0;
60c5b2
+}
60c5b2
diff --git a/gzip.c b/gzip.c
60c5b2
index 3ddfb7f..4fffc4f 100644
60c5b2
--- a/gzip.c
60c5b2
+++ b/gzip.c
60c5b2
@@ -128,9 +128,21 @@ static char const *const license_msg[] = {
60c5b2
 
60c5b2
                 /* global buffers */
60c5b2
 
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+/* DEFLATE COMPRESSION CALL works faster with page-aligned input buffers */
60c5b2
+__attribute__((aligned(4096)))
60c5b2
+#endif
60c5b2
 DECLARE(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+/* DEFLATE COMPRESSION CALL works faster with page-aligned output buffers */
60c5b2
+__attribute__((aligned(4096)))
60c5b2
+#endif
60c5b2
 DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
60c5b2
 DECLARE(ush, d_buf,  DIST_BUFSIZE);
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+/* DEFLATE COMPRESSION CALL works only with page-aligned windows */
60c5b2
+__attribute__((aligned(4096)))
60c5b2
+#endif
60c5b2
 DECLARE(uch, window, 2L*WSIZE);
60c5b2
 #ifndef MAXSEG_64K
60c5b2
     DECLARE(ush, tab_prefix, 1L<
60c5b2
diff --git a/gzip.h b/gzip.h
60c5b2
index 46bbac9..0c59cc2 100644
60c5b2
--- a/gzip.h
60c5b2
+++ b/gzip.h
60c5b2
@@ -74,7 +74,10 @@ extern int method;         /* compression method *   /
60c5b2
  */
60c5b2
 
60c5b2
 #ifndef	INBUFSIZ
60c5b2
-#  ifdef SMALL_MEM
60c5b2
+#  ifdef IBM_Z_DFLTCC
60c5b2
+/* DEFLATE COMPRESSION CALL works faster with larger input buffers */
60c5b2
+#    define INBUFSIZ  0x40000
60c5b2
+#  elif defined SMALL_MEM
60c5b2
 #    define INBUFSIZ  0x2000  /* input buffer size */
60c5b2
 #  else
60c5b2
 #    define INBUFSIZ  0x8000  /* input buffer size */
60c5b2
@@ -83,7 +86,10 @@ extern int method;         /* compression method */
60c5b2
 #define INBUF_EXTRA  64     /* required by unlzw() */
60c5b2
 
60c5b2
 #ifndef	OUTBUFSIZ
60c5b2
-#  ifdef SMALL_MEM
60c5b2
+#  ifdef IBM_Z_DFLTCC
60c5b2
+/* DEFLATE COMPRESSION CALL works faster with larger output buffers */
60c5b2
+#    define OUTBUFSIZ   0x40000
60c5b2
+#  elif defined SMALL_MEM
60c5b2
 #    define OUTBUFSIZ   8192  /* output buffer size */
60c5b2
 #  else
60c5b2
 #    define OUTBUFSIZ  16384  /* output buffer size */
60c5b2
@@ -275,8 +281,8 @@ extern int unlzh      (int in, int out);
60c5b2
 extern noreturn void abort_gzip (void);
60c5b2
 
60c5b2
         /* in deflate.c */
60c5b2
-extern void lm_init (int pack_level, ush *flags);
60c5b2
-extern off_t deflate (void);
60c5b2
+extern void lm_init (int pack_level);
60c5b2
+extern off_t deflate (int pack_level);
60c5b2
 
60c5b2
         /* in trees.c */
60c5b2
 extern void ct_init     (ush *attr, int *method);
60c5b2
@@ -284,6 +290,8 @@ extern int  ct_tally    (int dist, int lc);
60c5b2
 extern off_t flush_block (char *buf, ulg stored_len, int pad, int eof);
60c5b2
 
60c5b2
         /* in bits.c */
60c5b2
+extern unsigned short bi_buf;
60c5b2
+extern int            bi_valid;
60c5b2
 extern void     bi_init    (file_t zipfile);
60c5b2
 extern void     send_bits  (int value, int length);
60c5b2
 extern unsigned bi_reverse (unsigned value, int length) _GL_ATTRIBUTE_CONST;
60c5b2
@@ -293,7 +301,9 @@ extern int     (*read_buf) (char *buf, unsigned size);
60c5b2
 
60c5b2
         /* in util.c: */
60c5b2
 extern int copy           (int in, int out);
60c5b2
-extern ulg  updcrc        (uch *s, unsigned n);
60c5b2
+extern ulg  updcrc        (const uch *s, unsigned n);
60c5b2
+extern ulg  getcrc        (void) _GL_ATTRIBUTE_PURE;
60c5b2
+extern void setcrc        (ulg c);
60c5b2
 extern void clear_bufs    (void);
60c5b2
 extern int  fill_inbuf    (int eof_ok);
60c5b2
 extern void flush_outbuf  (void);
60c5b2
@@ -315,3 +325,9 @@ extern void fprint_off    (FILE *, off_t, int);
60c5b2
 
60c5b2
         /* in inflate.c */
60c5b2
 extern int inflate (void);
60c5b2
+
60c5b2
+        /* in dfltcc.c */
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+extern int dfltcc_deflate (int pack_level);
60c5b2
+extern int dfltcc_inflate (void);
60c5b2
+#endif
60c5b2
diff --git a/tests/znew-k b/tests/znew-k
60c5b2
index eeb7b29..d43246b 100755
60c5b2
--- a/tests/znew-k
60c5b2
+++ b/tests/znew-k
60c5b2
@@ -29,12 +29,13 @@ chmod +x compress || framework_failure_
60c5b2
 # Note that the basename must have a length of 6 or greater.
60c5b2
 # Otherwise, "test -f $name" below would fail.
60c5b2
 name=123456.Z
60c5b2
+gzname=123456.gz
60c5b2
 
60c5b2
 printf '%1012977s' ' ' | gzip -c > $name || framework_failure_
60c5b2
 
60c5b2
 fail=0
60c5b2
 
60c5b2
 znew -K $name || fail=1
60c5b2
-test -f $name || fail=1
60c5b2
+test -f $name || test -f $gzname || fail=1
60c5b2
 
60c5b2
 Exit $fail
60c5b2
diff --git a/unzip.c b/unzip.c
60c5b2
index a7255d4..86ef664 100644
60c5b2
--- a/unzip.c
60c5b2
+++ b/unzip.c
60c5b2
@@ -129,7 +129,11 @@ int unzip(in, out)
60c5b2
     /* Decompress */
60c5b2
     if (method == DEFLATED)  {
60c5b2
 
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+        int res = dfltcc_inflate();
60c5b2
+#else
60c5b2
         int res = inflate();
60c5b2
+#endif
60c5b2
 
60c5b2
         if (res == 3) {
60c5b2
             xalloc_die ();
60c5b2
diff --git a/util.c b/util.c
60c5b2
index 41e50d7..dc00f4a 100644
60c5b2
--- a/util.c
60c5b2
+++ b/util.c
60c5b2
@@ -96,6 +96,11 @@ static const ulg crc_32_tab[] = {
60c5b2
   0x2d02ef8dL
60c5b2
 };
60c5b2
 
60c5b2
+/* ========================================================================
60c5b2
+ * Shift register contents
60c5b2
+ */
60c5b2
+static ulg crc = (ulg)0xffffffffL;
60c5b2
+
60c5b2
 /* ===========================================================================
60c5b2
  * Copy input to output unchanged: zcat == cat with --force.
60c5b2
  * IN assertion: insize bytes have already been read in inbuf and inptr bytes
60c5b2
@@ -126,13 +131,11 @@ int copy(in, out)
60c5b2
  * Return the current crc in either case.
60c5b2
  */
60c5b2
 ulg updcrc(s, n)
60c5b2
-    uch *s;                 /* pointer to bytes to pump through */
60c5b2
+    const uch *s;           /* pointer to bytes to pump through */
60c5b2
     unsigned n;             /* number of bytes in s[] */
60c5b2
 {
60c5b2
     register ulg c;         /* temporary variable */
60c5b2
 
60c5b2
-    static ulg crc = (ulg)0xffffffffL; /* shift register contents */
60c5b2
-
60c5b2
     if (s == NULL) {
60c5b2
         c = 0xffffffffL;
60c5b2
     } else {
60c5b2
@@ -145,6 +148,23 @@ ulg updcrc(s, n)
60c5b2
     return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
60c5b2
 }
60c5b2
 
60c5b2
+/* ===========================================================================
60c5b2
+ * Return a current CRC value.
60c5b2
+ */
60c5b2
+ulg getcrc()
60c5b2
+{
60c5b2
+    return crc ^ 0xffffffffL;
60c5b2
+}
60c5b2
+
60c5b2
+/* ===========================================================================
60c5b2
+ * Set a new CRC value.
60c5b2
+ */
60c5b2
+void setcrc(c)
60c5b2
+    ulg c;
60c5b2
+{
60c5b2
+    crc = c ^ 0xffffffffL;
60c5b2
+}
60c5b2
+
60c5b2
 /* ===========================================================================
60c5b2
  * Clear input and output buffers
60c5b2
  */
60c5b2
@@ -238,7 +258,9 @@ void flush_outbuf()
60c5b2
 {
60c5b2
     if (outcnt == 0) return;
60c5b2
 
60c5b2
-    write_buf(ofd, (char *)outbuf, outcnt);
60c5b2
+    if (!test) {
60c5b2
+        write_buf(ofd, (char *)outbuf, outcnt);
60c5b2
+    }
60c5b2
     bytes_out += (off_t)outcnt;
60c5b2
     outcnt = 0;
60c5b2
 }
60c5b2
diff --git a/zip.c b/zip.c
60c5b2
index 1bd4c78..ace7e5e 100644
60c5b2
--- a/zip.c
60c5b2
+++ b/zip.c
60c5b2
@@ -23,9 +23,12 @@
60c5b2
 #include "tailor.h"
60c5b2
 #include "gzip.h"
60c5b2
 
60c5b2
-local ulg crc;       /* crc on uncompressed file data */
60c5b2
 off_t header_bytes;   /* number of bytes in gzip header */
60c5b2
 
60c5b2
+#define FAST 4
60c5b2
+#define SLOW 2
60c5b2
+/* speed options for the general purpose bit flag */
60c5b2
+
60c5b2
 /* ===========================================================================
60c5b2
  * Deflate in to out.
60c5b2
  * IN assertions: the input and output buffers are cleared.
60c5b2
@@ -68,11 +71,15 @@ int zip(in, out)
60c5b2
     put_long (stamp);
60c5b2
 
60c5b2
     /* Write deflated file to zip file */
60c5b2
-    crc = updcrc(0, 0);
60c5b2
+    updcrc(NULL, 0);
60c5b2
 
60c5b2
     bi_init(out);
60c5b2
     ct_init(&attr, &method);
60c5b2
-    lm_init(level, &deflate_flags);
60c5b2
+    if (level == 1) {
60c5b2
+        deflate_flags |= FAST;
60c5b2
+    } else if (level == 9) {
60c5b2
+        deflate_flags |= SLOW;
60c5b2
+    }
60c5b2
 
60c5b2
     put_byte((uch)deflate_flags); /* extra flags */
60c5b2
     put_byte(OS_CODE);            /* OS identifier */
60c5b2
@@ -85,7 +92,11 @@ int zip(in, out)
60c5b2
     }
60c5b2
     header_bytes = (off_t)outcnt;
60c5b2
 
60c5b2
-    (void)deflate();
60c5b2
+#ifdef IBM_Z_DFLTCC
60c5b2
+    (void)dfltcc_deflate(level);
60c5b2
+#else
60c5b2
+    (void)deflate(level);
60c5b2
+#endif
60c5b2
 
60c5b2
 #ifndef NO_SIZE_CHECK
60c5b2
   /* Check input size
60c5b2
@@ -98,7 +109,7 @@ int zip(in, out)
60c5b2
 #endif
60c5b2
 
60c5b2
     /* Write the crc and uncompressed size */
60c5b2
-    put_long(crc);
60c5b2
+    put_long(getcrc());
60c5b2
     put_long((ulg)bytes_in);
60c5b2
     header_bytes += 2*4;
60c5b2
 
60c5b2
@@ -126,7 +137,7 @@ int file_read(buf, size)
60c5b2
         read_error();
60c5b2
     }
60c5b2
 
60c5b2
-    crc = updcrc((uch*)buf, len);
60c5b2
+    updcrc((uch*)buf, len);
60c5b2
     bytes_in += (off_t)len;
60c5b2
     return (int)len;
60c5b2
 }
60c5b2
2.21.0