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