Commit 273bd18 for zlib

commit 273bd189aea5e726aa6c427857c86bc3c850ea9b
Author: Mark Adler <git@madler.net>
Date:   Sun Nov 22 21:35:53 2026 -0800

    Use a table of batch sizes for the ARM CRC-32 instructions.

    The three interleaved CRCs are now computed in batches of seven
    sizes, from 32592 bytes per stream down to 464. Each batch is now
    combined with two carry-less products each represented by a table
    of constants corresponding to the size.

    The batch sizes, chosen by measurement, are in crc32.c. The product
    tables are calculated from them and are in crc32.h, written by
    MAKECRCH, or computed at run time with DYNAMIC_CRC_TABLE.

    This is roughly 1.1 to 2.8 times faster for 1.4 KB to 19 KB, and up
    to 1.3 times faster for longer buffers, except on the Cortex-A76,
    Cortex-A720, and likely some others, where it can be up to a third
    slower in the range of 1.4 KB to 16 KB.

diff --git a/crc32.c b/crc32.c
index 07a6789..26aa1f5 100644
--- a/crc32.c
+++ b/crc32.c
@@ -36,7 +36,7 @@
 #  include "contrib/crc32vx/crc32_vx_hooks.h"
 #endif

- /*
+/*
   A CRC of a message is computed on N braids of words in the message, where
   each word consists of W bytes (4 or 8). If N is 3, for example, then three
   running sparse CRCs are calculated respectively on each braid, at these
@@ -331,16 +331,36 @@ local z_word_t byte_swap(z_word_t word) {
 }
 #endif

+#if defined(ARMCRC32) || defined(MAKECRCH)
+/*
+  batch[] is the count of bytes to process in each of the three interleaved
+  streams. Stepping down through these counts provides the benefits of
+  interleaving across whatever length is provided, while avoiding on-the-fly
+  calculations of multipliers. Instead, constant multipliers in batch_t[] are
+  used, which correspond to the fixed counts. The batch[] counts are each a bit
+  less than a power of two, so as to spread out the use of new cache lines,
+  with the amounts less (172 and 48) determined empirically on a Cortex-A72.
+  The batch_t[] multipliers are calculated from these batch lengths. If these
+  counts are changed, then MAKECRCH must be run to rebuild crc32.h.
+ */
+local const unsigned batch[] = {32592, 16208, 8016, 3920, 2000, 976, 464};
+#  define BATCHES (sizeof(batch) / sizeof(batch[0]))
+#endif
+
 #ifdef DYNAMIC_CRC_TABLE
 /* =========================================================================
- * Table of powers of x for combining CRC-32s, filled in by make_crc_table()
- * below.
+ * Table of powers of x for combining CRC-32s, and tables for combining the
+ * CRCs of the ARM batches, filled in by make_crc_table() below.
  */
    local z_crc_t FAR x2n_table[32];
+#  if defined(ARMCRC32) || defined(MAKECRCH)
+   local Z_U8 FAR batch_t[BATCHES][2][16];
+#  endif
 #else
 /* =========================================================================
- * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
- * of x for combining CRC-32s, all made by make_crc_table().
+ * Tables for byte-wise and braided CRC-32 calculations, a table of powers of x
+ * for combining CRC-32s, and tables for combining the CRCs of the ARM batches,
+ * all made by make_crc_table().
  */
 #  include "crc32.h"
 #endif
@@ -408,6 +428,22 @@ local z_crc_t FAR crc_table[256];
    local void write_table64(FILE *, const z_word_t FAR *, int);
 #endif /* MAKECRCH */

+#if defined(ARMCRC32) || defined(MAKECRCH)
+/* Set t[0..15] to the carry-less products of 0..15 and k. t is a table entry
+   in batch_t[] that represents the multiplier k. It is used in place of k to
+   speed up the multiplication. */
+local void batch_products(Z_U8 FAR *t, z_crc_t k) {
+    unsigned i, j;
+
+    for (i = 0; i < 16; i++) {
+        t[i] = 0;
+        for (j = 0; j < 4; j++)
+            if ((i >> j) & 1)
+                t[i] ^= (Z_U8)k << j;
+    }
+}
+#endif
+
 /* State for once(). */
 local z_once_t made = Z_ONCE_INIT;

@@ -457,6 +493,16 @@ local void make_crc_table(void) {
     for (n = 1; n < 32; n++)
         x2n_table[n] = p = (z_crc_t)multmodp(p, p);

+#if defined(ARMCRC32) || defined(MAKECRCH)
+    /* initialize the tables for combining the CRCs of the ARM batches */
+    for (n = 0; n < BATCHES; n++) {
+        z_crc_t k1 = (z_crc_t)x2nmodp(((z_off64_t)batch[n] << 3) - 33, 0);
+        z_crc_t k2 = (z_crc_t)x2nmodp(((z_off64_t)batch[n] << 4) - 33, 0);
+        batch_products(batch_t[n][0], k1);
+        batch_products(batch_t[n][1], k2);
+    }
+#endif
+
 #ifdef W
     /* initialize the braiding tables -- needs x2n_table[] */
     braid(BRAID_TABLE(N), BRAID_BIG_TABLE(N), N, W);
@@ -595,6 +641,24 @@ local void make_crc_table(void) {
         write_table(out, x2n_table, 32);
         fprintf(out,
             "};\n");
+
+        /* write out the tables for combining the CRCs of the ARM batches */
+        fprintf(out,
+            "\n"
+            "#ifdef ARMCRC32\n"
+            "\n"
+            "local const Z_U8 FAR batch_t[][2][16] = {\n");
+        for (n = 0; n < (int)BATCHES; n++) {
+            fprintf(out, "   {{");
+            write_table64(out, batch_t[n][0], 16);
+            fprintf(out, "},\n    {");
+            write_table64(out, batch_t[n][1], 16);
+            fprintf(out, "}}%s", n < (int)BATCHES - 1 ? ",\n" : "");
+        }
+        fprintf(out,
+            "};\n"
+            "\n"
+            "#endif\n");
         fclose(out);
     }
 #endif /* MAKECRCH */
@@ -818,9 +882,7 @@ local inline int arm_caps(void) {
 #  define ARMCRC32_ATTR __attribute__((target("arch=armv8-a+crc")))
 #endif

-/* Define CRC32W() to operate on a word and CRC32B() to operate on a byte.
-   Also define ARMCRC32_LOG, the log2 of the number of bits in a word for use
-   by x2nmodp() */
+/* Define CRC32W() to operate on a word and CRC32B() to operate on a byte. */
 #if ARMCRC32_WORD == 8
 typedef Z_U8 arm_word_t;
 #  if defined(_MSC_VER) && !defined(__clang__)
@@ -833,32 +895,73 @@ typedef Z_U8 arm_word_t;
 #    define CRC32W(c, w) \
         __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(c) : "r"(w))
 #  endif
-#  define ARMCRC32_LOG 6
+   /* set c to p times x^32, modulo the CRC polynomial */
+#  define CRC32_REDUCE(c, p) do { \
+        c = 0; \
+        CRC32W(c, p); \
+    } while (0)
 #else
 typedef Z_U4 arm_word_t;
 #  define CRC32B(c, b) (c = __builtin_arm_crc32b((unsigned)(c), \
                                                  (unsigned char)(b)))
 #  define CRC32W(c, w) (c = __builtin_arm_crc32w((unsigned)(c), w))
-#  define ARMCRC32_LOG 5
+    /* set c to p times x^32, modulo the CRC polynomial */
+#  define CRC32_REDUCE(c, p) do { \
+        c = 0; \
+        CRC32W(c, (Z_U4)(p)); \
+        CRC32W(c, (Z_U4)((p) >> 32)); \
+    } while (0)
 #endif

 /*
-   Constants empirically determined to maximize speed. These values are from
-   measurements on a Cortex-A57. Your mileage may vary. A batch is the same
-   number of bytes, 31920, for either word size.
+  The CRC is computed on three interleaved streams of words at once, to realize
+  a throughput of one CRC instruction per cycle, where each can have a latency
+  of up to three cycles. Do successively smaller batches so that any length
+  that is at least the smallest batch size will start with one of them. Below
+  that, do one word at a time in serial, and then one byte at a time for the
+  last few. The CRC from the first group is the CRC of all of the data through
+  that group. The other two CRCs are of those groups in isolation. Those two
+  are combined with the first CRC by multiplying them by the appropriate x^n
+  modulo the CRC polynomial, and then exclusive-oring the three together.
  */
-#define Z_BATCH (3990 * 8 / ARMCRC32_WORD)  /* number of words in a batch */
-#define Z_BATCH_ZEROS 0xa10d3d0c    /* computed from 31920 bytes */
-#define Z_BATCH_MIN (800 * 8 / ARMCRC32_WORD)  /* fewest words, final batch */

+/* Return the carry-less product of c and and the value represented by the
+   table t from batch_t[]. The table speeds up the multiplication by doing
+   four bits of c at a time. */
+local Z_U8 clmul_soft(z_crc_t c, const Z_U8 *t) {
+    Z_U8 p;
+    int j;
+
+    p = 0;
+    for (j = 0; j < 32; j += 4)
+        p ^= t[(c >> j) & 15] << j;
+    return p;
+}
+
+/* Return the CRC through three sequential streams of batch[k] bytes, given
+   crc, the running CRC through the first stream, and crc1 and crc2, the CRCs
+   of the second and third streams each starting from zero. */
+local ARMCRC32_ATTR uLong batch_soft(uLong crc, uLong crc1, uLong crc2,
+                                     unsigned k) {
+    Z_U8 p;
+    uLong c;
+
+    p = clmul_soft((z_crc_t)crc, batch_t[k][1]) ^
+        clmul_soft((z_crc_t)crc1, batch_t[k][0]);
+    CRC32_REDUCE(c, p);
+    return c ^ crc2;
+}
+
+/* Return the CRC-32 of len bytes at buf using ARM hardware instructions. */
 local ARMCRC32_ATTR uLong crc32_arm(uLong crc, const unsigned char FAR *buf,
                                     z_size_t len) {
     uLong val;
     uLong crc1, crc2;
     const arm_word_t *word;
     arm_word_t val0, val1, val2;
-    z_size_t last, last2, i;
+    z_size_t n, i;
     z_size_t num;
+    unsigned k;

     /* Return initial CRC, if requested. */
     if (buf == Z_NULL) return 0;
@@ -882,46 +985,26 @@ local ARMCRC32_ATTR uLong crc32_arm(uLong crc, const unsigned char FAR *buf,
     num = len / ARMCRC32_WORD;
     len &= ARMCRC32_WORD - 1;

-    /* Do three interleaved CRCs to realize the throughput of one CRC
-       instruction per cycle. Each CRC is calculated on Z_BATCH words. The
-       three CRCs are combined into a single CRC after each set of batches. */
-    while (num >= 3 * Z_BATCH) {
-        crc1 = 0;
-        crc2 = 0;
-        for (i = 0; i < Z_BATCH; i++) {
-            val0 = word[i];
-            val1 = word[i + Z_BATCH];
-            val2 = word[i + 2 * Z_BATCH];
-            CRC32W(crc, val0);
-            CRC32W(crc1, val1);
-            CRC32W(crc2, val2);
-        }
-        word += 3 * Z_BATCH;
-        num -= 3 * Z_BATCH;
-        crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
-        crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
-    }
-
-    /* Do one last smaller batch with the remaining words, if there are enough
-       to pay for the combination of CRCs. */
-    last = num / 3;
-    if (last >= Z_BATCH_MIN) {
-        last2 = last << 1;
-        crc1 = 0;
-        crc2 = 0;
-        for (i = 0; i < last; i++) {
-            val0 = word[i];
-            val1 = word[i + last];
-            val2 = word[i + last2];
-            CRC32W(crc, val0);
-            CRC32W(crc1, val1);
-            CRC32W(crc2, val2);
+    /* Do batches of three interleaved CRCs, from the largest size down. */
+    if (num >= 3 * (batch[BATCHES - 1] / ARMCRC32_WORD)) {
+        for (k = 0; k < BATCHES; k++) {
+            n = batch[k] / ARMCRC32_WORD;
+            while (num >= 3 * n) {
+                crc1 = 0;
+                crc2 = 0;
+                for (i = 0; i < n; i++) {
+                    val0 = word[i];
+                    val1 = word[i + n];
+                    val2 = word[i + 2 * n];
+                    CRC32W(crc, val0);
+                    CRC32W(crc1, val1);
+                    CRC32W(crc2, val2);
+                }
+                word += 3 * n;
+                num -= 3 * n;
+                crc = batch_soft(crc, crc1, crc2, k);
+            }
         }
-        word += 3 * last;
-        num -= 3 * last;
-        val = x2nmodp((int)last, ARMCRC32_LOG);
-        crc = multmodp(val, crc) ^ crc1;
-        crc = multmodp(val, crc) ^ crc2;
     }

     /* Compute the CRC on any remaining words. */
diff --git a/crc32.h b/crc32.h
index 67cd463..06f93df 100644
--- a/crc32.h
+++ b/crc32.h
@@ -9444,3 +9444,93 @@ local const z_crc_t FAR x2n_table[] = {
     0x9fec022a, 0x6c8dedc4, 0x15d6874d, 0x5fde7a4e, 0xbad90e37,
     0x2e4e5eef, 0x4eaba214, 0xa8a472c0, 0x429a969e, 0x148d302a,
     0xc40ba6d0, 0xc4e22c3c};
+
+#ifdef ARMCRC32
+
+local const Z_U8 FAR batch_t[][2][16] = {
+   {{0x0000000000000000, 0x000000001ec34ef1, 0x000000003d869de2,
+    0x000000002345d313, 0x000000007b0d3bc4, 0x0000000065ce7535,
+    0x00000000468ba626, 0x000000005848e8d7, 0x00000000f61a7788,
+    0x00000000e8d93979, 0x00000000cb9cea6a, 0x00000000d55fa49b,
+    0x000000008d174c4c, 0x0000000093d402bd, 0x00000000b091d1ae,
+    0x00000000ae529f5f},
+    {0x0000000000000000, 0x00000000b80a6da9, 0x000000017014db52,
+    0x00000001c81eb6fb, 0x00000002e029b6a4, 0x000000025823db0d,
+    0x00000003903d6df6, 0x000000032837005f, 0x00000005c0536d48,
+    0x00000005785900e1, 0x00000004b047b61a, 0x00000004084ddbb3,
+    0x00000007207adbec, 0x000000079870b645, 0x00000006506e00be,
+    0x00000006e8646d17}},
+   {{0x0000000000000000, 0x000000001bee170a, 0x0000000037dc2e14,
+    0x000000002c32391e, 0x000000006fb85c28, 0x0000000074564b22,
+    0x000000005864723c, 0x00000000438a6536, 0x00000000df70b850,
+    0x00000000c49eaf5a, 0x00000000e8ac9644, 0x00000000f342814e,
+    0x00000000b0c8e478, 0x00000000ab26f372, 0x000000008714ca6c,
+    0x000000009cfadd66},
+    {0x0000000000000000, 0x00000000167fe3c0, 0x000000002cffc780,
+    0x000000003a802440, 0x0000000059ff8f00, 0x000000004f806cc0,
+    0x0000000075004880, 0x00000000637fab40, 0x00000000b3ff1e00,
+    0x00000000a580fdc0, 0x000000009f00d980, 0x00000000897f3a40,
+    0x00000000ea009100, 0x00000000fc7f72c0, 0x00000000c6ff5680,
+    0x00000000d080b540}},
+   {{0x0000000000000000, 0x00000000e82fcd6b, 0x00000001d05f9ad6,
+    0x00000001387057bd, 0x00000003a0bf35ac, 0x000000034890f8c7,
+    0x0000000270e0af7a, 0x0000000298cf6211, 0x00000007417e6b58,
+    0x00000007a951a633, 0x000000069121f18e, 0x00000006790e3ce5,
+    0x00000004e1c15ef4, 0x0000000409ee939f, 0x00000005319ec422,
+    0x00000005d9b10949},
+    {0x0000000000000000, 0x000000006a88ed69, 0x00000000d511dad2,
+    0x00000000bf9937bb, 0x00000001aa23b5a4, 0x00000001c0ab58cd,
+    0x000000017f326f76, 0x0000000115ba821f, 0x0000000354476b48,
+    0x000000033ecf8621, 0x000000038156b19a, 0x00000003ebde5cf3,
+    0x00000002fe64deec, 0x0000000294ec3385, 0x000000022b75043e,
+    0x0000000241fde957}},
+   {{0x0000000000000000, 0x0000000030050de5, 0x00000000600a1bca,
+    0x00000000500f162f, 0x00000000c0143794, 0x00000000f0113a71,
+    0x00000000a01e2c5e, 0x00000000901b21bb, 0x0000000180286f28,
+    0x00000001b02d62cd, 0x00000001e02274e2, 0x00000001d0277907,
+    0x00000001403c58bc, 0x0000000170395559, 0x0000000120364376,
+    0x0000000110334e93},
+    {0x0000000000000000, 0x000000003896e336, 0x00000000712dc66c,
+    0x0000000049bb255a, 0x00000000e25b8cd8, 0x00000000dacd6fee,
+    0x0000000093764ab4, 0x00000000abe0a982, 0x00000001c4b719b0,
+    0x00000001fc21fa86, 0x00000001b59adfdc, 0x000000018d0c3cea,
+    0x0000000126ec9568, 0x000000011e7a765e, 0x0000000157c15304,
+    0x000000016f57b032}},
+   {{0x0000000000000000, 0x00000000ca6a5e14, 0x0000000194d4bc28,
+    0x000000015ebee23c, 0x0000000329a97850, 0x00000003e3c32644,
+    0x00000002bd7dc478, 0x0000000277179a6c, 0x000000065352f0a0,
+    0x000000069938aeb4, 0x00000007c7864c88, 0x000000070dec129c,
+    0x000000057afb88f0, 0x00000005b091d6e4, 0x00000004ee2f34d8,
+    0x0000000424456acc},
+    {0x0000000000000000, 0x00000000e074d17f, 0x00000001c0e9a2fe,
+    0x00000001209d7381, 0x0000000381d345fc, 0x0000000361a79483,
+    0x00000002413ae702, 0x00000002a14e367d, 0x0000000703a68bf8,
+    0x00000007e3d25a87, 0x00000006c34f2906, 0x00000006233bf879,
+    0x000000048275ce04, 0x0000000462011f7b, 0x00000005429c6cfa,
+    0x00000005a2e8bd85}},
+   {{0x0000000000000000, 0x00000000357b9517, 0x000000006af72a2e,
+    0x000000005f8cbf39, 0x00000000d5ee545c, 0x00000000e095c14b,
+    0x00000000bf197e72, 0x000000008a62eb65, 0x00000001abdca8b8,
+    0x000000019ea73daf, 0x00000001c12b8296, 0x00000001f4501781,
+    0x000000017e32fce4, 0x000000014b4969f3, 0x0000000114c5d6ca,
+    0x0000000121be43dd},
+    {0x0000000000000000, 0x00000000aaa18a37, 0x000000015543146e,
+    0x00000001ffe29e59, 0x00000002aa8628dc, 0x000000020027a2eb,
+    0x00000003ffc53cb2, 0x000000035564b685, 0x00000005550c51b8,
+    0x00000005ffaddb8f, 0x00000004004f45d6, 0x00000004aaeecfe1,
+    0x00000007ff8a7964, 0x00000007552bf353, 0x00000006aac96d0a,
+    0x000000060068e73d}},
+   {{0x0000000000000000, 0x000000006d40f445, 0x00000000da81e88a,
+    0x00000000b7c11ccf, 0x00000001b503d114, 0x00000001d8432551,
+    0x000000016f82399e, 0x0000000102c2cddb, 0x000000036a07a228,
+    0x000000030747566d, 0x00000003b0864aa2, 0x00000003ddc6bee7,
+    0x00000002df04733c, 0x00000002b2448779, 0x0000000205859bb6,
+    0x0000000268c56ff3},
+    {0x0000000000000000, 0x0000000081b6f443, 0x00000001036de886,
+    0x0000000182db1cc5, 0x0000000206dbd10c, 0x00000002876d254f,
+    0x0000000305b6398a, 0x000000038400cdc9, 0x000000040db7a218,
+    0x000000048c01565b, 0x000000050eda4a9e, 0x000000058f6cbedd,
+    0x000000060b6c7314, 0x000000068ada8757, 0x0000000708019b92,
+    0x0000000789b76fd1}}};
+
+#endif