Home | History | Annotate | Line # | Download | only in zlib
      1  1.1  christos /* crc32.c -- compute the CRC-32 of a data stream
      2  1.3  christos  * Copyright (C) 1995-2022 Mark Adler
      3  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      4  1.1  christos  *
      5  1.3  christos  * This interleaved implementation of a CRC makes use of pipelined multiple
      6  1.3  christos  * arithmetic-logic units, commonly found in modern CPU cores. It is due to
      7  1.3  christos  * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
      8  1.1  christos  */
      9  1.1  christos 
     10  1.3  christos /* @(#) Id */
     11  1.1  christos 
     12  1.1  christos /*
     13  1.1  christos   Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
     14  1.1  christos   protection on the static variables used to control the first-use generation
     15  1.3  christos   of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
     16  1.1  christos   first call get_crc_table() to initialize the tables before allowing more than
     17  1.1  christos   one thread to use crc32().
     18  1.1  christos 
     19  1.3  christos   MAKECRCH can be #defined to write out crc32.h. A main() routine is also
     20  1.3  christos   produced, so that this one source file can be compiled to an executable.
     21  1.1  christos  */
     22  1.1  christos 
     23  1.1  christos #ifdef MAKECRCH
     24  1.1  christos #  include <stdio.h>
     25  1.1  christos #  ifndef DYNAMIC_CRC_TABLE
     26  1.1  christos #    define DYNAMIC_CRC_TABLE
     27  1.1  christos #  endif /* !DYNAMIC_CRC_TABLE */
     28  1.1  christos #endif /* MAKECRCH */
     29  1.1  christos 
     30  1.3  christos #include "zutil.h"      /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */
     31  1.1  christos 
     32  1.3  christos  /*
     33  1.3  christos   A CRC of a message is computed on N braids of words in the message, where
     34  1.3  christos   each word consists of W bytes (4 or 8). If N is 3, for example, then three
     35  1.3  christos   running sparse CRCs are calculated respectively on each braid, at these
     36  1.3  christos   indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
     37  1.3  christos   This is done starting at a word boundary, and continues until as many blocks
     38  1.3  christos   of N * W bytes as are available have been processed. The results are combined
     39  1.3  christos   into a single CRC at the end. For this code, N must be in the range 1..6 and
     40  1.3  christos   W must be 4 or 8. The upper limit on N can be increased if desired by adding
     41  1.3  christos   more #if blocks, extending the patterns apparent in the code. In addition,
     42  1.3  christos   crc32.h would need to be regenerated, if the maximum N value is increased.
     43  1.3  christos 
     44  1.3  christos   N and W are chosen empirically by benchmarking the execution time on a given
     45  1.3  christos   processor. The choices for N and W below were based on testing on Intel Kaby
     46  1.3  christos   Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64
     47  1.3  christos   Octeon II processors. The Intel, AMD, and ARM processors were all fastest
     48  1.3  christos   with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4.
     49  1.3  christos   They were all tested with either gcc or clang, all using the -O3 optimization
     50  1.3  christos   level. Your mileage may vary.
     51  1.3  christos  */
     52  1.3  christos 
     53  1.3  christos /* Define N */
     54  1.3  christos #ifdef Z_TESTN
     55  1.3  christos #  define N Z_TESTN
     56  1.1  christos #else
     57  1.3  christos #  define N 5
     58  1.3  christos #endif
     59  1.3  christos #if N < 1 || N > 6
     60  1.3  christos #  error N must be in 1..6
     61  1.3  christos #endif
     62  1.1  christos 
     63  1.3  christos /*
     64  1.3  christos   z_crc_t must be at least 32 bits. z_word_t must be at least as long as
     65  1.3  christos   z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and
     66  1.3  christos   that bytes are eight bits.
     67  1.3  christos  */
     68  1.3  christos 
     69  1.3  christos /*
     70  1.3  christos   Define W and the associated z_word_t type. If W is not defined, then a
     71  1.3  christos   braided calculation is not used, and the associated tables and code are not
     72  1.3  christos   compiled.
     73  1.3  christos  */
     74  1.3  christos #ifdef Z_TESTW
     75  1.3  christos #  if Z_TESTW-1 != -1
     76  1.3  christos #    define W Z_TESTW
     77  1.3  christos #  endif
     78  1.3  christos #else
     79  1.3  christos #  ifdef MAKECRCH
     80  1.3  christos #    define W 8         /* required for MAKECRCH */
     81  1.3  christos #  else
     82  1.3  christos #    if defined(__x86_64__) || defined(__aarch64__)
     83  1.3  christos #      define W 8
     84  1.3  christos #    else
     85  1.3  christos #      define W 4
     86  1.3  christos #    endif
     87  1.3  christos #  endif
     88  1.3  christos #endif
     89  1.3  christos #ifdef W
     90  1.3  christos #  if W == 8 && defined(Z_U8)
     91  1.3  christos      typedef Z_U8 z_word_t;
     92  1.3  christos #  elif defined(Z_U4)
     93  1.3  christos #    undef W
     94  1.3  christos #    define W 4
     95  1.3  christos      typedef Z_U4 z_word_t;
     96  1.3  christos #  else
     97  1.3  christos #    undef W
     98  1.3  christos #  endif
     99  1.3  christos #endif
    100  1.1  christos 
    101  1.3  christos /* If available, use the ARM processor CRC32 instruction. */
    102  1.3  christos #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
    103  1.3  christos #  define ARMCRC32
    104  1.3  christos #endif
    105  1.3  christos 
    106  1.3  christos #if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
    107  1.3  christos /*
    108  1.3  christos   Swap the bytes in a z_word_t to convert between little and big endian. Any
    109  1.3  christos   self-respecting compiler will optimize this to a single machine byte-swap
    110  1.3  christos   instruction, if one is available. This assumes that word_t is either 32 bits
    111  1.3  christos   or 64 bits.
    112  1.3  christos  */
    113  1.4  christos local z_word_t byte_swap(z_word_t word) {
    114  1.3  christos #  if W == 8
    115  1.3  christos     return
    116  1.3  christos         (word & 0xff00000000000000) >> 56 |
    117  1.3  christos         (word & 0xff000000000000) >> 40 |
    118  1.3  christos         (word & 0xff0000000000) >> 24 |
    119  1.3  christos         (word & 0xff00000000) >> 8 |
    120  1.3  christos         (word & 0xff000000) << 8 |
    121  1.3  christos         (word & 0xff0000) << 24 |
    122  1.3  christos         (word & 0xff00) << 40 |
    123  1.3  christos         (word & 0xff) << 56;
    124  1.3  christos #  else   /* W == 4 */
    125  1.3  christos     return
    126  1.3  christos         (word & 0xff000000) >> 24 |
    127  1.3  christos         (word & 0xff0000) >> 8 |
    128  1.3  christos         (word & 0xff00) << 8 |
    129  1.3  christos         (word & 0xff) << 24;
    130  1.3  christos #  endif
    131  1.3  christos }
    132  1.3  christos #endif
    133  1.3  christos 
    134  1.4  christos #ifdef DYNAMIC_CRC_TABLE
    135  1.4  christos /* =========================================================================
    136  1.4  christos  * Table of powers of x for combining CRC-32s, filled in by make_crc_table()
    137  1.4  christos  * below.
    138  1.4  christos  */
    139  1.4  christos    local z_crc_t FAR x2n_table[32];
    140  1.4  christos #else
    141  1.4  christos /* =========================================================================
    142  1.4  christos  * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
    143  1.4  christos  * of x for combining CRC-32s, all made by make_crc_table().
    144  1.4  christos  */
    145  1.4  christos #  include "crc32.h"
    146  1.4  christos #endif
    147  1.4  christos 
    148  1.3  christos /* CRC polynomial. */
    149  1.3  christos #define POLY 0xedb88320         /* p(x) reflected, with x^32 implied */
    150  1.1  christos 
    151  1.4  christos /*
    152  1.4  christos   Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
    153  1.4  christos   reflected. For speed, this requires that a not be zero.
    154  1.4  christos  */
    155  1.4  christos local z_crc_t multmodp(z_crc_t a, z_crc_t b) {
    156  1.4  christos     z_crc_t m, p;
    157  1.4  christos 
    158  1.4  christos     m = (z_crc_t)1 << 31;
    159  1.4  christos     p = 0;
    160  1.4  christos     for (;;) {
    161  1.4  christos         if (a & m) {
    162  1.4  christos             p ^= b;
    163  1.4  christos             if ((a & (m - 1)) == 0)
    164  1.4  christos                 break;
    165  1.4  christos         }
    166  1.4  christos         m >>= 1;
    167  1.4  christos         b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
    168  1.4  christos     }
    169  1.4  christos     return p;
    170  1.4  christos }
    171  1.4  christos 
    172  1.4  christos /*
    173  1.4  christos   Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
    174  1.4  christos   initialized.
    175  1.4  christos  */
    176  1.4  christos local z_crc_t x2nmodp(z_off64_t n, unsigned k) {
    177  1.4  christos     z_crc_t p;
    178  1.4  christos 
    179  1.4  christos     p = (z_crc_t)1 << 31;           /* x^0 == 1 */
    180  1.4  christos     while (n) {
    181  1.4  christos         if (n & 1)
    182  1.4  christos             p = multmodp(x2n_table[k & 31], p);
    183  1.4  christos         n >>= 1;
    184  1.4  christos         k++;
    185  1.4  christos     }
    186  1.4  christos     return p;
    187  1.4  christos }
    188  1.4  christos 
    189  1.1  christos #ifdef DYNAMIC_CRC_TABLE
    190  1.4  christos /* =========================================================================
    191  1.4  christos  * Build the tables for byte-wise and braided CRC-32 calculations, and a table
    192  1.4  christos  * of powers of x for combining CRC-32s.
    193  1.4  christos  */
    194  1.3  christos local z_crc_t FAR crc_table[256];
    195  1.3  christos #ifdef W
    196  1.3  christos    local z_word_t FAR crc_big_table[256];
    197  1.3  christos    local z_crc_t FAR crc_braid_table[W][256];
    198  1.3  christos    local z_word_t FAR crc_braid_big_table[W][256];
    199  1.4  christos    local void braid(z_crc_t [][256], z_word_t [][256], int, int);
    200  1.3  christos #endif
    201  1.1  christos #ifdef MAKECRCH
    202  1.4  christos    local void write_table(FILE *, const z_crc_t FAR *, int);
    203  1.4  christos    local void write_table32hi(FILE *, const z_word_t FAR *, int);
    204  1.4  christos    local void write_table64(FILE *, const z_word_t FAR *, int);
    205  1.1  christos #endif /* MAKECRCH */
    206  1.3  christos 
    207  1.3  christos /*
    208  1.3  christos   Define a once() function depending on the availability of atomics. If this is
    209  1.3  christos   compiled with DYNAMIC_CRC_TABLE defined, and if CRCs will be computed in
    210  1.3  christos   multiple threads, and if atomics are not available, then get_crc_table() must
    211  1.3  christos   be called to initialize the tables and must return before any threads are
    212  1.3  christos   allowed to compute or combine CRCs.
    213  1.3  christos  */
    214  1.3  christos 
    215  1.3  christos /* Definition of once functionality. */
    216  1.3  christos typedef struct once_s once_t;
    217  1.3  christos 
    218  1.3  christos /* Check for the availability of atomics. */
    219  1.3  christos #if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
    220  1.3  christos     !defined(__STDC_NO_ATOMICS__)
    221  1.3  christos 
    222  1.3  christos #include <stdatomic.h>
    223  1.3  christos 
    224  1.3  christos /* Structure for once(), which must be initialized with ONCE_INIT. */
    225  1.3  christos struct once_s {
    226  1.3  christos     atomic_flag begun;
    227  1.3  christos     atomic_int done;
    228  1.3  christos };
    229  1.3  christos #define ONCE_INIT {ATOMIC_FLAG_INIT, 0}
    230  1.3  christos 
    231  1.3  christos /*
    232  1.3  christos   Run the provided init() function exactly once, even if multiple threads
    233  1.3  christos   invoke once() at the same time. The state must be a once_t initialized with
    234  1.3  christos   ONCE_INIT.
    235  1.3  christos  */
    236  1.4  christos local void once(once_t *state, void (*init)(void)) {
    237  1.3  christos     if (!atomic_load(&state->done)) {
    238  1.3  christos         if (atomic_flag_test_and_set(&state->begun))
    239  1.3  christos             while (!atomic_load(&state->done))
    240  1.3  christos                 ;
    241  1.3  christos         else {
    242  1.3  christos             init();
    243  1.3  christos             atomic_store(&state->done, 1);
    244  1.3  christos         }
    245  1.3  christos     }
    246  1.3  christos }
    247  1.3  christos 
    248  1.3  christos #else   /* no atomics */
    249  1.3  christos 
    250  1.3  christos /* Structure for once(), which must be initialized with ONCE_INIT. */
    251  1.3  christos struct once_s {
    252  1.3  christos     volatile int begun;
    253  1.3  christos     volatile int done;
    254  1.3  christos };
    255  1.3  christos #define ONCE_INIT {0, 0}
    256  1.3  christos 
    257  1.3  christos /* Test and set. Alas, not atomic, but tries to minimize the period of
    258  1.3  christos    vulnerability. */
    259  1.4  christos local int test_and_set(int volatile *flag) {
    260  1.3  christos     int was;
    261  1.3  christos 
    262  1.3  christos     was = *flag;
    263  1.3  christos     *flag = 1;
    264  1.3  christos     return was;
    265  1.3  christos }
    266  1.3  christos 
    267  1.3  christos /* Run the provided init() function once. This is not thread-safe. */
    268  1.4  christos local void once(once_t *state, void (*init)(void)) {
    269  1.3  christos     if (!state->done) {
    270  1.3  christos         if (test_and_set(&state->begun))
    271  1.3  christos             while (!state->done)
    272  1.3  christos                 ;
    273  1.3  christos         else {
    274  1.3  christos             init();
    275  1.3  christos             state->done = 1;
    276  1.3  christos         }
    277  1.3  christos     }
    278  1.3  christos }
    279  1.3  christos 
    280  1.3  christos #endif
    281  1.3  christos 
    282  1.3  christos /* State for once(). */
    283  1.3  christos local once_t made = ONCE_INIT;
    284  1.3  christos 
    285  1.1  christos /*
    286  1.1  christos   Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
    287  1.1  christos   x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
    288  1.1  christos 
    289  1.1  christos   Polynomials over GF(2) are represented in binary, one bit per coefficient,
    290  1.3  christos   with the lowest powers in the most significant bit. Then adding polynomials
    291  1.1  christos   is just exclusive-or, and multiplying a polynomial by x is a right shift by
    292  1.3  christos   one. If we call the above polynomial p, and represent a byte as the
    293  1.1  christos   polynomial q, also with the lowest power in the most significant bit (so the
    294  1.3  christos   byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p,
    295  1.1  christos   where a mod b means the remainder after dividing a by b.
    296  1.1  christos 
    297  1.1  christos   This calculation is done using the shift-register method of multiplying and
    298  1.3  christos   taking the remainder. The register is initialized to zero, and for each
    299  1.1  christos   incoming bit, x^32 is added mod p to the register if the bit is a one (where
    300  1.3  christos   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x
    301  1.3  christos   (which is shifting right by one and adding x^32 mod p if the bit shifted out
    302  1.3  christos   is a one). We start with the highest power (least significant bit) of q and
    303  1.3  christos   repeat for all eight bits of q.
    304  1.3  christos 
    305  1.3  christos   The table is simply the CRC of all possible eight bit values. This is all the
    306  1.3  christos   information needed to generate CRCs on data a byte at a time for all
    307  1.3  christos   combinations of CRC register values and incoming bytes.
    308  1.3  christos  */
    309  1.3  christos 
    310  1.4  christos local void make_crc_table(void) {
    311  1.3  christos     unsigned i, j, n;
    312  1.3  christos     z_crc_t p;
    313  1.1  christos 
    314  1.3  christos     /* initialize the CRC of bytes tables */
    315  1.3  christos     for (i = 0; i < 256; i++) {
    316  1.3  christos         p = i;
    317  1.3  christos         for (j = 0; j < 8; j++)
    318  1.3  christos             p = p & 1 ? (p >> 1) ^ POLY : p >> 1;
    319  1.3  christos         crc_table[i] = p;
    320  1.3  christos #ifdef W
    321  1.3  christos         crc_big_table[i] = byte_swap(p);
    322  1.3  christos #endif
    323  1.3  christos     }
    324  1.1  christos 
    325  1.3  christos     /* initialize the x^2^n mod p(x) table */
    326  1.3  christos     p = (z_crc_t)1 << 30;         /* x^1 */
    327  1.3  christos     x2n_table[0] = p;
    328  1.3  christos     for (n = 1; n < 32; n++)
    329  1.3  christos         x2n_table[n] = p = multmodp(p, p);
    330  1.3  christos 
    331  1.3  christos #ifdef W
    332  1.3  christos     /* initialize the braiding tables -- needs x2n_table[] */
    333  1.3  christos     braid(crc_braid_table, crc_braid_big_table, N, W);
    334  1.3  christos #endif
    335  1.1  christos 
    336  1.1  christos #ifdef MAKECRCH
    337  1.1  christos     {
    338  1.3  christos         /*
    339  1.3  christos           The crc32.h header file contains tables for both 32-bit and 64-bit
    340  1.3  christos           z_word_t's, and so requires a 64-bit type be available. In that case,
    341  1.3  christos           z_word_t must be defined to be 64-bits. This code then also generates
    342  1.3  christos           and writes out the tables for the case that z_word_t is 32 bits.
    343  1.3  christos          */
    344  1.3  christos #if !defined(W) || W != 8
    345  1.3  christos #  error Need a 64-bit integer type in order to generate crc32.h.
    346  1.3  christos #endif
    347  1.1  christos         FILE *out;
    348  1.3  christos         int k, n;
    349  1.3  christos         z_crc_t ltl[8][256];
    350  1.3  christos         z_word_t big[8][256];
    351  1.1  christos 
    352  1.1  christos         out = fopen("crc32.h", "w");
    353  1.1  christos         if (out == NULL) return;
    354  1.3  christos 
    355  1.3  christos         /* write out little-endian CRC table to crc32.h */
    356  1.3  christos         fprintf(out,
    357  1.3  christos             "/* crc32.h -- tables for rapid CRC calculation\n"
    358  1.3  christos             " * Generated automatically by crc32.c\n */\n"
    359  1.3  christos             "\n"
    360  1.3  christos             "local const z_crc_t FAR crc_table[] = {\n"
    361  1.3  christos             "    ");
    362  1.3  christos         write_table(out, crc_table, 256);
    363  1.3  christos         fprintf(out,
    364  1.3  christos             "};\n");
    365  1.3  christos 
    366  1.3  christos         /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */
    367  1.3  christos         fprintf(out,
    368  1.3  christos             "\n"
    369  1.3  christos             "#ifdef W\n"
    370  1.3  christos             "\n"
    371  1.3  christos             "#if W == 8\n"
    372  1.3  christos             "\n"
    373  1.3  christos             "local const z_word_t FAR crc_big_table[] = {\n"
    374  1.3  christos             "    ");
    375  1.3  christos         write_table64(out, crc_big_table, 256);
    376  1.3  christos         fprintf(out,
    377  1.3  christos             "};\n");
    378  1.3  christos 
    379  1.3  christos         /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */
    380  1.3  christos         fprintf(out,
    381  1.3  christos             "\n"
    382  1.3  christos             "#else /* W == 4 */\n"
    383  1.3  christos             "\n"
    384  1.3  christos             "local const z_word_t FAR crc_big_table[] = {\n"
    385  1.3  christos             "    ");
    386  1.3  christos         write_table32hi(out, crc_big_table, 256);
    387  1.3  christos         fprintf(out,
    388  1.3  christos             "};\n"
    389  1.3  christos             "\n"
    390  1.3  christos             "#endif\n");
    391  1.3  christos 
    392  1.3  christos         /* write out braid tables for each value of N */
    393  1.3  christos         for (n = 1; n <= 6; n++) {
    394  1.3  christos             fprintf(out,
    395  1.3  christos             "\n"
    396  1.3  christos             "#if N == %d\n", n);
    397  1.3  christos 
    398  1.3  christos             /* compute braid tables for this N and 64-bit word_t */
    399  1.3  christos             braid(ltl, big, n, 8);
    400  1.3  christos 
    401  1.3  christos             /* write out braid tables for 64-bit z_word_t to crc32.h */
    402  1.3  christos             fprintf(out,
    403  1.3  christos             "\n"
    404  1.3  christos             "#if W == 8\n"
    405  1.3  christos             "\n"
    406  1.3  christos             "local const z_crc_t FAR crc_braid_table[][256] = {\n");
    407  1.3  christos             for (k = 0; k < 8; k++) {
    408  1.3  christos                 fprintf(out, "   {");
    409  1.3  christos                 write_table(out, ltl[k], 256);
    410  1.3  christos                 fprintf(out, "}%s", k < 7 ? ",\n" : "");
    411  1.3  christos             }
    412  1.3  christos             fprintf(out,
    413  1.3  christos             "};\n"
    414  1.3  christos             "\n"
    415  1.3  christos             "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
    416  1.3  christos             for (k = 0; k < 8; k++) {
    417  1.3  christos                 fprintf(out, "   {");
    418  1.3  christos                 write_table64(out, big[k], 256);
    419  1.3  christos                 fprintf(out, "}%s", k < 7 ? ",\n" : "");
    420  1.3  christos             }
    421  1.3  christos             fprintf(out,
    422  1.3  christos             "};\n");
    423  1.3  christos 
    424  1.3  christos             /* compute braid tables for this N and 32-bit word_t */
    425  1.3  christos             braid(ltl, big, n, 4);
    426  1.3  christos 
    427  1.3  christos             /* write out braid tables for 32-bit z_word_t to crc32.h */
    428  1.3  christos             fprintf(out,
    429  1.3  christos             "\n"
    430  1.3  christos             "#else /* W == 4 */\n"
    431  1.3  christos             "\n"
    432  1.3  christos             "local const z_crc_t FAR crc_braid_table[][256] = {\n");
    433  1.3  christos             for (k = 0; k < 4; k++) {
    434  1.3  christos                 fprintf(out, "   {");
    435  1.3  christos                 write_table(out, ltl[k], 256);
    436  1.3  christos                 fprintf(out, "}%s", k < 3 ? ",\n" : "");
    437  1.3  christos             }
    438  1.3  christos             fprintf(out,
    439  1.3  christos             "};\n"
    440  1.3  christos             "\n"
    441  1.3  christos             "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
    442  1.3  christos             for (k = 0; k < 4; k++) {
    443  1.3  christos                 fprintf(out, "   {");
    444  1.3  christos                 write_table32hi(out, big[k], 256);
    445  1.3  christos                 fprintf(out, "}%s", k < 3 ? ",\n" : "");
    446  1.3  christos             }
    447  1.3  christos             fprintf(out,
    448  1.3  christos             "};\n"
    449  1.3  christos             "\n"
    450  1.3  christos             "#endif\n"
    451  1.3  christos             "\n"
    452  1.3  christos             "#endif\n");
    453  1.1  christos         }
    454  1.3  christos         fprintf(out,
    455  1.3  christos             "\n"
    456  1.3  christos             "#endif\n");
    457  1.3  christos 
    458  1.3  christos         /* write out zeros operator table to crc32.h */
    459  1.3  christos         fprintf(out,
    460  1.3  christos             "\n"
    461  1.3  christos             "local const z_crc_t FAR x2n_table[] = {\n"
    462  1.3  christos             "    ");
    463  1.3  christos         write_table(out, x2n_table, 32);
    464  1.3  christos         fprintf(out,
    465  1.3  christos             "};\n");
    466  1.1  christos         fclose(out);
    467  1.1  christos     }
    468  1.1  christos #endif /* MAKECRCH */
    469  1.1  christos }
    470  1.1  christos 
    471  1.1  christos #ifdef MAKECRCH
    472  1.3  christos 
    473  1.3  christos /*
    474  1.3  christos    Write the 32-bit values in table[0..k-1] to out, five per line in
    475  1.3  christos    hexadecimal separated by commas.
    476  1.3  christos  */
    477  1.4  christos local void write_table(FILE *out, const z_crc_t FAR *table, int k) {
    478  1.1  christos     int n;
    479  1.1  christos 
    480  1.3  christos     for (n = 0; n < k; n++)
    481  1.3  christos         fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : "    ",
    482  1.1  christos                 (unsigned long)(table[n]),
    483  1.3  christos                 n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
    484  1.1  christos }
    485  1.3  christos 
    486  1.3  christos /*
    487  1.3  christos    Write the high 32-bits of each value in table[0..k-1] to out, five per line
    488  1.3  christos    in hexadecimal separated by commas.
    489  1.3  christos  */
    490  1.4  christos local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) {
    491  1.3  christos     int n;
    492  1.3  christos 
    493  1.3  christos     for (n = 0; n < k; n++)
    494  1.3  christos         fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : "    ",
    495  1.3  christos                 (unsigned long)(table[n] >> 32),
    496  1.3  christos                 n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
    497  1.3  christos }
    498  1.3  christos 
    499  1.3  christos /*
    500  1.3  christos   Write the 64-bit values in table[0..k-1] to out, three per line in
    501  1.3  christos   hexadecimal separated by commas. This assumes that if there is a 64-bit
    502  1.3  christos   type, then there is also a long long integer type, and it is at least 64
    503  1.3  christos   bits. If not, then the type cast and format string can be adjusted
    504  1.3  christos   accordingly.
    505  1.3  christos  */
    506  1.4  christos local void write_table64(FILE *out, const z_word_t FAR *table, int k) {
    507  1.3  christos     int n;
    508  1.3  christos 
    509  1.3  christos     for (n = 0; n < k; n++)
    510  1.3  christos         fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : "    ",
    511  1.3  christos                 (unsigned long long)(table[n]),
    512  1.3  christos                 n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", "));
    513  1.3  christos }
    514  1.3  christos 
    515  1.3  christos /* Actually do the deed. */
    516  1.4  christos int main(void) {
    517  1.3  christos     make_crc_table();
    518  1.3  christos     return 0;
    519  1.3  christos }
    520  1.3  christos 
    521  1.1  christos #endif /* MAKECRCH */
    522  1.1  christos 
    523  1.3  christos #ifdef W
    524  1.3  christos /*
    525  1.3  christos   Generate the little and big-endian braid tables for the given n and z_word_t
    526  1.3  christos   size w. Each array must have room for w blocks of 256 elements.
    527  1.3  christos  */
    528  1.4  christos local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) {
    529  1.3  christos     int k;
    530  1.3  christos     z_crc_t i, p, q;
    531  1.3  christos     for (k = 0; k < w; k++) {
    532  1.3  christos         p = x2nmodp((n * w + 3 - k) << 3, 0);
    533  1.3  christos         ltl[k][0] = 0;
    534  1.3  christos         big[w - 1 - k][0] = 0;
    535  1.3  christos         for (i = 1; i < 256; i++) {
    536  1.3  christos             ltl[k][i] = q = multmodp(i << 24, p);
    537  1.3  christos             big[w - 1 - k][i] = byte_swap(q);
    538  1.3  christos         }
    539  1.3  christos     }
    540  1.3  christos }
    541  1.3  christos #endif
    542  1.3  christos 
    543  1.1  christos #endif /* DYNAMIC_CRC_TABLE */
    544  1.1  christos 
    545  1.1  christos /* =========================================================================
    546  1.3  christos  * This function can be used by asm versions of crc32(), and to force the
    547  1.3  christos  * generation of the CRC tables in a threaded application.
    548  1.1  christos  */
    549  1.4  christos const z_crc_t FAR * ZEXPORT get_crc_table(void) {
    550  1.1  christos #ifdef DYNAMIC_CRC_TABLE
    551  1.3  christos     once(&made, make_crc_table);
    552  1.1  christos #endif /* DYNAMIC_CRC_TABLE */
    553  1.1  christos     return (const z_crc_t FAR *)crc_table;
    554  1.1  christos }
    555  1.1  christos 
    556  1.3  christos /* =========================================================================
    557  1.3  christos  * Use ARM machine instructions if available. This will compute the CRC about
    558  1.3  christos  * ten times faster than the braided calculation. This code does not check for
    559  1.3  christos  * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will
    560  1.3  christos  * only be defined if the compilation specifies an ARM processor architecture
    561  1.3  christos  * that has the instructions. For example, compiling with -march=armv8.1-a or
    562  1.3  christos  * -march=armv8-a+crc, or -march=native if the compile machine has the crc32
    563  1.3  christos  * instructions.
    564  1.3  christos  */
    565  1.3  christos #ifdef ARMCRC32
    566  1.3  christos 
    567  1.3  christos /*
    568  1.3  christos    Constants empirically determined to maximize speed. These values are from
    569  1.3  christos    measurements on a Cortex-A57. Your mileage may vary.
    570  1.3  christos  */
    571  1.3  christos #define Z_BATCH 3990                /* number of words in a batch */
    572  1.3  christos #define Z_BATCH_ZEROS 0xa10d3d0c    /* computed from Z_BATCH = 3990 */
    573  1.3  christos #define Z_BATCH_MIN 800             /* fewest words in a final batch */
    574  1.1  christos 
    575  1.4  christos unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
    576  1.4  christos                               z_size_t len) {
    577  1.3  christos     z_crc_t val;
    578  1.3  christos     z_word_t crc1, crc2;
    579  1.3  christos     const z_word_t *word;
    580  1.3  christos     z_word_t val0, val1, val2;
    581  1.3  christos     z_size_t last, last2, i;
    582  1.3  christos     z_size_t num;
    583  1.3  christos 
    584  1.3  christos     /* Return initial CRC, if requested. */
    585  1.3  christos     if (buf == Z_NULL) return 0;
    586  1.1  christos 
    587  1.1  christos #ifdef DYNAMIC_CRC_TABLE
    588  1.3  christos     once(&made, make_crc_table);
    589  1.1  christos #endif /* DYNAMIC_CRC_TABLE */
    590  1.1  christos 
    591  1.3  christos     /* Pre-condition the CRC */
    592  1.4  christos     crc = (~crc) & 0xffffffff;
    593  1.3  christos 
    594  1.3  christos     /* Compute the CRC up to a word boundary. */
    595  1.3  christos     while (len && ((z_size_t)buf & 7) != 0) {
    596  1.3  christos         len--;
    597  1.3  christos         val = *buf++;
    598  1.3  christos         __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
    599  1.3  christos     }
    600  1.3  christos 
    601  1.3  christos     /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */
    602  1.3  christos     word = (z_word_t const *)buf;
    603  1.3  christos     num = len >> 3;
    604  1.3  christos     len &= 7;
    605  1.3  christos 
    606  1.3  christos     /* Do three interleaved CRCs to realize the throughput of one crc32x
    607  1.4  christos        instruction per cycle. Each CRC is calculated on Z_BATCH words. The
    608  1.4  christos        three CRCs are combined into a single CRC after each set of batches. */
    609  1.3  christos     while (num >= 3 * Z_BATCH) {
    610  1.3  christos         crc1 = 0;
    611  1.3  christos         crc2 = 0;
    612  1.3  christos         for (i = 0; i < Z_BATCH; i++) {
    613  1.3  christos             val0 = word[i];
    614  1.3  christos             val1 = word[i + Z_BATCH];
    615  1.3  christos             val2 = word[i + 2 * Z_BATCH];
    616  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
    617  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
    618  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
    619  1.3  christos         }
    620  1.3  christos         word += 3 * Z_BATCH;
    621  1.3  christos         num -= 3 * Z_BATCH;
    622  1.3  christos         crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
    623  1.3  christos         crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
    624  1.3  christos     }
    625  1.1  christos 
    626  1.3  christos     /* Do one last smaller batch with the remaining words, if there are enough
    627  1.3  christos        to pay for the combination of CRCs. */
    628  1.3  christos     last = num / 3;
    629  1.3  christos     if (last >= Z_BATCH_MIN) {
    630  1.3  christos         last2 = last << 1;
    631  1.3  christos         crc1 = 0;
    632  1.3  christos         crc2 = 0;
    633  1.3  christos         for (i = 0; i < last; i++) {
    634  1.3  christos             val0 = word[i];
    635  1.3  christos             val1 = word[i + last];
    636  1.3  christos             val2 = word[i + last2];
    637  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
    638  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
    639  1.3  christos             __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
    640  1.3  christos         }
    641  1.3  christos         word += 3 * last;
    642  1.3  christos         num -= 3 * last;
    643  1.3  christos         val = x2nmodp(last, 6);
    644  1.3  christos         crc = multmodp(val, crc) ^ crc1;
    645  1.3  christos         crc = multmodp(val, crc) ^ crc2;
    646  1.3  christos     }
    647  1.3  christos 
    648  1.3  christos     /* Compute the CRC on any remaining words. */
    649  1.3  christos     for (i = 0; i < num; i++) {
    650  1.3  christos         val0 = word[i];
    651  1.3  christos         __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
    652  1.1  christos     }
    653  1.3  christos     word += num;
    654  1.3  christos 
    655  1.3  christos     /* Complete the CRC on any remaining bytes. */
    656  1.3  christos     buf = (const unsigned char FAR *)word;
    657  1.3  christos     while (len) {
    658  1.3  christos         len--;
    659  1.3  christos         val = *buf++;
    660  1.3  christos         __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
    661  1.1  christos     }
    662  1.3  christos 
    663  1.3  christos     /* Return the CRC, post-conditioned. */
    664  1.3  christos     return crc ^ 0xffffffff;
    665  1.1  christos }
    666  1.1  christos 
    667  1.3  christos #else
    668  1.2  christos 
    669  1.3  christos #ifdef W
    670  1.1  christos 
    671  1.2  christos /*
    672  1.3  christos   Return the CRC of the W bytes in the word_t data, taking the
    673  1.3  christos   least-significant byte of the word as the first byte of data, without any pre
    674  1.3  christos   or post conditioning. This is used to combine the CRCs of each braid.
    675  1.2  christos  */
    676  1.4  christos local z_crc_t crc_word(z_word_t data) {
    677  1.3  christos     int k;
    678  1.3  christos     for (k = 0; k < W; k++)
    679  1.3  christos         data = (data >> 8) ^ crc_table[data & 0xff];
    680  1.3  christos     return (z_crc_t)data;
    681  1.3  christos }
    682  1.3  christos 
    683  1.4  christos local z_word_t crc_word_big(z_word_t data) {
    684  1.3  christos     int k;
    685  1.3  christos     for (k = 0; k < W; k++)
    686  1.3  christos         data = (data << 8) ^
    687  1.3  christos             crc_big_table[(data >> ((W - 1) << 3)) & 0xff];
    688  1.3  christos     return data;
    689  1.3  christos }
    690  1.2  christos 
    691  1.3  christos #endif
    692  1.1  christos 
    693  1.1  christos /* ========================================================================= */
    694  1.4  christos unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
    695  1.4  christos                               z_size_t len) {
    696  1.3  christos     /* Return initial CRC, if requested. */
    697  1.3  christos     if (buf == Z_NULL) return 0;
    698  1.3  christos 
    699  1.3  christos #ifdef DYNAMIC_CRC_TABLE
    700  1.3  christos     once(&made, make_crc_table);
    701  1.3  christos #endif /* DYNAMIC_CRC_TABLE */
    702  1.3  christos 
    703  1.3  christos     /* Pre-condition the CRC */
    704  1.4  christos     crc = (~crc) & 0xffffffff;
    705  1.3  christos 
    706  1.3  christos #ifdef W
    707  1.3  christos 
    708  1.3  christos     /* If provided enough bytes, do a braided CRC calculation. */
    709  1.3  christos     if (len >= N * W + W - 1) {
    710  1.3  christos         z_size_t blks;
    711  1.3  christos         z_word_t const *words;
    712  1.3  christos         unsigned endian;
    713  1.3  christos         int k;
    714  1.3  christos 
    715  1.3  christos         /* Compute the CRC up to a z_word_t boundary. */
    716  1.3  christos         while (len && ((z_size_t)buf & (W - 1)) != 0) {
    717  1.3  christos             len--;
    718  1.3  christos             crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    719  1.3  christos         }
    720  1.3  christos 
    721  1.3  christos         /* Compute the CRC on as many N z_word_t blocks as are available. */
    722  1.3  christos         blks = len / (N * W);
    723  1.3  christos         len -= blks * N * W;
    724  1.3  christos         words = (z_word_t const *)buf;
    725  1.3  christos 
    726  1.3  christos         /* Do endian check at execution time instead of compile time, since ARM
    727  1.4  christos            processors can change the endianness at execution time. If the
    728  1.4  christos            compiler knows what the endianness will be, it can optimize out the
    729  1.3  christos            check and the unused branch. */
    730  1.3  christos         endian = 1;
    731  1.3  christos         if (*(unsigned char *)&endian) {
    732  1.3  christos             /* Little endian. */
    733  1.3  christos 
    734  1.3  christos             z_crc_t crc0;
    735  1.3  christos             z_word_t word0;
    736  1.3  christos #if N > 1
    737  1.3  christos             z_crc_t crc1;
    738  1.3  christos             z_word_t word1;
    739  1.3  christos #if N > 2
    740  1.3  christos             z_crc_t crc2;
    741  1.3  christos             z_word_t word2;
    742  1.3  christos #if N > 3
    743  1.3  christos             z_crc_t crc3;
    744  1.3  christos             z_word_t word3;
    745  1.3  christos #if N > 4
    746  1.3  christos             z_crc_t crc4;
    747  1.3  christos             z_word_t word4;
    748  1.3  christos #if N > 5
    749  1.3  christos             z_crc_t crc5;
    750  1.3  christos             z_word_t word5;
    751  1.3  christos #endif
    752  1.3  christos #endif
    753  1.3  christos #endif
    754  1.3  christos #endif
    755  1.3  christos #endif
    756  1.3  christos 
    757  1.3  christos             /* Initialize the CRC for each braid. */
    758  1.3  christos             crc0 = crc;
    759  1.3  christos #if N > 1
    760  1.3  christos             crc1 = 0;
    761  1.3  christos #if N > 2
    762  1.3  christos             crc2 = 0;
    763  1.3  christos #if N > 3
    764  1.3  christos             crc3 = 0;
    765  1.3  christos #if N > 4
    766  1.3  christos             crc4 = 0;
    767  1.3  christos #if N > 5
    768  1.3  christos             crc5 = 0;
    769  1.3  christos #endif
    770  1.3  christos #endif
    771  1.3  christos #endif
    772  1.3  christos #endif
    773  1.3  christos #endif
    774  1.1  christos 
    775  1.3  christos             /*
    776  1.3  christos               Process the first blks-1 blocks, computing the CRCs on each braid
    777  1.3  christos               independently.
    778  1.3  christos              */
    779  1.3  christos             while (--blks) {
    780  1.3  christos                 /* Load the word for each braid into registers. */
    781  1.3  christos                 word0 = crc0 ^ words[0];
    782  1.3  christos #if N > 1
    783  1.3  christos                 word1 = crc1 ^ words[1];
    784  1.3  christos #if N > 2
    785  1.3  christos                 word2 = crc2 ^ words[2];
    786  1.3  christos #if N > 3
    787  1.3  christos                 word3 = crc3 ^ words[3];
    788  1.3  christos #if N > 4
    789  1.3  christos                 word4 = crc4 ^ words[4];
    790  1.3  christos #if N > 5
    791  1.3  christos                 word5 = crc5 ^ words[5];
    792  1.3  christos #endif
    793  1.3  christos #endif
    794  1.3  christos #endif
    795  1.3  christos #endif
    796  1.3  christos #endif
    797  1.3  christos                 words += N;
    798  1.3  christos 
    799  1.3  christos                 /* Compute and update the CRC for each word. The loop should
    800  1.3  christos                    get unrolled. */
    801  1.3  christos                 crc0 = crc_braid_table[0][word0 & 0xff];
    802  1.3  christos #if N > 1
    803  1.3  christos                 crc1 = crc_braid_table[0][word1 & 0xff];
    804  1.3  christos #if N > 2
    805  1.3  christos                 crc2 = crc_braid_table[0][word2 & 0xff];
    806  1.3  christos #if N > 3
    807  1.3  christos                 crc3 = crc_braid_table[0][word3 & 0xff];
    808  1.3  christos #if N > 4
    809  1.3  christos                 crc4 = crc_braid_table[0][word4 & 0xff];
    810  1.3  christos #if N > 5
    811  1.3  christos                 crc5 = crc_braid_table[0][word5 & 0xff];
    812  1.3  christos #endif
    813  1.3  christos #endif
    814  1.3  christos #endif
    815  1.3  christos #endif
    816  1.3  christos #endif
    817  1.3  christos                 for (k = 1; k < W; k++) {
    818  1.3  christos                     crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
    819  1.3  christos #if N > 1
    820  1.3  christos                     crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
    821  1.3  christos #if N > 2
    822  1.3  christos                     crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
    823  1.3  christos #if N > 3
    824  1.3  christos                     crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
    825  1.3  christos #if N > 4
    826  1.3  christos                     crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
    827  1.3  christos #if N > 5
    828  1.3  christos                     crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
    829  1.3  christos #endif
    830  1.3  christos #endif
    831  1.3  christos #endif
    832  1.3  christos #endif
    833  1.3  christos #endif
    834  1.3  christos                 }
    835  1.3  christos             }
    836  1.3  christos 
    837  1.3  christos             /*
    838  1.3  christos               Process the last block, combining the CRCs of the N braids at the
    839  1.3  christos               same time.
    840  1.3  christos              */
    841  1.3  christos             crc = crc_word(crc0 ^ words[0]);
    842  1.3  christos #if N > 1
    843  1.3  christos             crc = crc_word(crc1 ^ words[1] ^ crc);
    844  1.3  christos #if N > 2
    845  1.3  christos             crc = crc_word(crc2 ^ words[2] ^ crc);
    846  1.3  christos #if N > 3
    847  1.3  christos             crc = crc_word(crc3 ^ words[3] ^ crc);
    848  1.3  christos #if N > 4
    849  1.3  christos             crc = crc_word(crc4 ^ words[4] ^ crc);
    850  1.3  christos #if N > 5
    851  1.3  christos             crc = crc_word(crc5 ^ words[5] ^ crc);
    852  1.3  christos #endif
    853  1.3  christos #endif
    854  1.3  christos #endif
    855  1.3  christos #endif
    856  1.3  christos #endif
    857  1.3  christos             words += N;
    858  1.3  christos         }
    859  1.3  christos         else {
    860  1.3  christos             /* Big endian. */
    861  1.3  christos 
    862  1.3  christos             z_word_t crc0, word0, comb;
    863  1.3  christos #if N > 1
    864  1.3  christos             z_word_t crc1, word1;
    865  1.3  christos #if N > 2
    866  1.3  christos             z_word_t crc2, word2;
    867  1.3  christos #if N > 3
    868  1.3  christos             z_word_t crc3, word3;
    869  1.3  christos #if N > 4
    870  1.3  christos             z_word_t crc4, word4;
    871  1.3  christos #if N > 5
    872  1.3  christos             z_word_t crc5, word5;
    873  1.3  christos #endif
    874  1.3  christos #endif
    875  1.3  christos #endif
    876  1.3  christos #endif
    877  1.3  christos #endif
    878  1.3  christos 
    879  1.3  christos             /* Initialize the CRC for each braid. */
    880  1.3  christos             crc0 = byte_swap(crc);
    881  1.3  christos #if N > 1
    882  1.3  christos             crc1 = 0;
    883  1.3  christos #if N > 2
    884  1.3  christos             crc2 = 0;
    885  1.3  christos #if N > 3
    886  1.3  christos             crc3 = 0;
    887  1.3  christos #if N > 4
    888  1.3  christos             crc4 = 0;
    889  1.3  christos #if N > 5
    890  1.3  christos             crc5 = 0;
    891  1.3  christos #endif
    892  1.3  christos #endif
    893  1.3  christos #endif
    894  1.3  christos #endif
    895  1.3  christos #endif
    896  1.1  christos 
    897  1.3  christos             /*
    898  1.3  christos               Process the first blks-1 blocks, computing the CRCs on each braid
    899  1.3  christos               independently.
    900  1.3  christos              */
    901  1.3  christos             while (--blks) {
    902  1.3  christos                 /* Load the word for each braid into registers. */
    903  1.3  christos                 word0 = crc0 ^ words[0];
    904  1.3  christos #if N > 1
    905  1.3  christos                 word1 = crc1 ^ words[1];
    906  1.3  christos #if N > 2
    907  1.3  christos                 word2 = crc2 ^ words[2];
    908  1.3  christos #if N > 3
    909  1.3  christos                 word3 = crc3 ^ words[3];
    910  1.3  christos #if N > 4
    911  1.3  christos                 word4 = crc4 ^ words[4];
    912  1.3  christos #if N > 5
    913  1.3  christos                 word5 = crc5 ^ words[5];
    914  1.3  christos #endif
    915  1.3  christos #endif
    916  1.3  christos #endif
    917  1.3  christos #endif
    918  1.3  christos #endif
    919  1.3  christos                 words += N;
    920  1.1  christos 
    921  1.3  christos                 /* Compute and update the CRC for each word. The loop should
    922  1.3  christos                    get unrolled. */
    923  1.3  christos                 crc0 = crc_braid_big_table[0][word0 & 0xff];
    924  1.3  christos #if N > 1
    925  1.3  christos                 crc1 = crc_braid_big_table[0][word1 & 0xff];
    926  1.3  christos #if N > 2
    927  1.3  christos                 crc2 = crc_braid_big_table[0][word2 & 0xff];
    928  1.3  christos #if N > 3
    929  1.3  christos                 crc3 = crc_braid_big_table[0][word3 & 0xff];
    930  1.3  christos #if N > 4
    931  1.3  christos                 crc4 = crc_braid_big_table[0][word4 & 0xff];
    932  1.3  christos #if N > 5
    933  1.3  christos                 crc5 = crc_braid_big_table[0][word5 & 0xff];
    934  1.3  christos #endif
    935  1.3  christos #endif
    936  1.3  christos #endif
    937  1.3  christos #endif
    938  1.3  christos #endif
    939  1.3  christos                 for (k = 1; k < W; k++) {
    940  1.3  christos                     crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
    941  1.3  christos #if N > 1
    942  1.3  christos                     crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
    943  1.3  christos #if N > 2
    944  1.3  christos                     crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
    945  1.3  christos #if N > 3
    946  1.3  christos                     crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
    947  1.3  christos #if N > 4
    948  1.3  christos                     crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
    949  1.3  christos #if N > 5
    950  1.3  christos                     crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
    951  1.3  christos #endif
    952  1.3  christos #endif
    953  1.3  christos #endif
    954  1.3  christos #endif
    955  1.3  christos #endif
    956  1.3  christos                 }
    957  1.3  christos             }
    958  1.1  christos 
    959  1.3  christos             /*
    960  1.3  christos               Process the last block, combining the CRCs of the N braids at the
    961  1.3  christos               same time.
    962  1.3  christos              */
    963  1.3  christos             comb = crc_word_big(crc0 ^ words[0]);
    964  1.3  christos #if N > 1
    965  1.3  christos             comb = crc_word_big(crc1 ^ words[1] ^ comb);
    966  1.3  christos #if N > 2
    967  1.3  christos             comb = crc_word_big(crc2 ^ words[2] ^ comb);
    968  1.3  christos #if N > 3
    969  1.3  christos             comb = crc_word_big(crc3 ^ words[3] ^ comb);
    970  1.3  christos #if N > 4
    971  1.3  christos             comb = crc_word_big(crc4 ^ words[4] ^ comb);
    972  1.3  christos #if N > 5
    973  1.3  christos             comb = crc_word_big(crc5 ^ words[5] ^ comb);
    974  1.3  christos #endif
    975  1.3  christos #endif
    976  1.3  christos #endif
    977  1.3  christos #endif
    978  1.3  christos #endif
    979  1.3  christos             words += N;
    980  1.3  christos             crc = byte_swap(comb);
    981  1.3  christos         }
    982  1.1  christos 
    983  1.3  christos         /*
    984  1.3  christos           Update the pointer to the remaining bytes to process.
    985  1.3  christos          */
    986  1.3  christos         buf = (unsigned char const *)words;
    987  1.1  christos     }
    988  1.1  christos 
    989  1.3  christos #endif /* W */
    990  1.3  christos 
    991  1.3  christos     /* Complete the computation of the CRC on any remaining bytes. */
    992  1.3  christos     while (len >= 8) {
    993  1.3  christos         len -= 8;
    994  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    995  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    996  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    997  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    998  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
    999  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
   1000  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
   1001  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
   1002  1.1  christos     }
   1003  1.3  christos     while (len) {
   1004  1.3  christos         len--;
   1005  1.3  christos         crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
   1006  1.1  christos     }
   1007  1.1  christos 
   1008  1.3  christos     /* Return the CRC, post-conditioned. */
   1009  1.3  christos     return crc ^ 0xffffffff;
   1010  1.1  christos }
   1011  1.1  christos 
   1012  1.3  christos #endif
   1013  1.1  christos 
   1014  1.1  christos /* ========================================================================= */
   1015  1.4  christos unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf,
   1016  1.4  christos                             uInt len) {
   1017  1.3  christos     return crc32_z(crc, buf, len);
   1018  1.1  christos }
   1019  1.1  christos 
   1020  1.1  christos /* ========================================================================= */
   1021  1.4  christos uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) {
   1022  1.3  christos #ifdef DYNAMIC_CRC_TABLE
   1023  1.3  christos     once(&made, make_crc_table);
   1024  1.3  christos #endif /* DYNAMIC_CRC_TABLE */
   1025  1.4  christos     return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff);
   1026  1.1  christos }
   1027  1.1  christos 
   1028  1.1  christos /* ========================================================================= */
   1029  1.4  christos uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) {
   1030  1.4  christos     return crc32_combine64(crc1, crc2, (z_off64_t)len2);
   1031  1.3  christos }
   1032  1.3  christos 
   1033  1.3  christos /* ========================================================================= */
   1034  1.4  christos uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) {
   1035  1.3  christos #ifdef DYNAMIC_CRC_TABLE
   1036  1.3  christos     once(&made, make_crc_table);
   1037  1.3  christos #endif /* DYNAMIC_CRC_TABLE */
   1038  1.3  christos     return x2nmodp(len2, 3);
   1039  1.1  christos }
   1040  1.1  christos 
   1041  1.1  christos /* ========================================================================= */
   1042  1.4  christos uLong ZEXPORT crc32_combine_gen(z_off_t len2) {
   1043  1.4  christos     return crc32_combine_gen64((z_off64_t)len2);
   1044  1.1  christos }
   1045  1.1  christos 
   1046  1.3  christos /* ========================================================================= */
   1047  1.4  christos uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) {
   1048  1.4  christos     return multmodp(op, crc1) ^ (crc2 & 0xffffffff);
   1049  1.1  christos }
   1050