12af503bcStholo /* crc32.c -- compute the CRC-32 of a data stream
2703d4924Stb * Copyright (C) 1995-2022 Mark Adler
32af503bcStholo * For conditions of distribution and use, see copyright notice in zlib.h
485c48e79Shenning *
5703d4924Stb * This interleaved implementation of a CRC makes use of pipelined multiple
6703d4924Stb * arithmetic-logic units, commonly found in modern CPU cores. It is due to
7703d4924Stb * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
82af503bcStholo */
92af503bcStholo
10b967fc35Sdjm /*
11b967fc35Sdjm Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
12b967fc35Sdjm protection on the static variables used to control the first-use generation
13b967fc35Sdjm of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
14b967fc35Sdjm first call get_crc_table() to initialize the tables before allowing more than
15b967fc35Sdjm one thread to use crc32().
1636f395ceStb
17703d4924Stb MAKECRCH can be #defined to write out crc32.h. A main() routine is also
18703d4924Stb produced, so that this one source file can be compiled to an executable.
19b967fc35Sdjm */
20b967fc35Sdjm
2185c48e79Shenning #ifdef MAKECRCH
2285c48e79Shenning # include <stdio.h>
2385c48e79Shenning # ifndef DYNAMIC_CRC_TABLE
2485c48e79Shenning # define DYNAMIC_CRC_TABLE
2585c48e79Shenning # endif /* !DYNAMIC_CRC_TABLE */
2685c48e79Shenning #endif /* MAKECRCH */
272af503bcStholo
28703d4924Stb #include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */
292af503bcStholo
30703d4924Stb /*
31703d4924Stb A CRC of a message is computed on N braids of words in the message, where
32703d4924Stb each word consists of W bytes (4 or 8). If N is 3, for example, then three
33703d4924Stb running sparse CRCs are calculated respectively on each braid, at these
34703d4924Stb indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
35703d4924Stb This is done starting at a word boundary, and continues until as many blocks
36703d4924Stb of N * W bytes as are available have been processed. The results are combined
37703d4924Stb into a single CRC at the end. For this code, N must be in the range 1..6 and
38703d4924Stb W must be 4 or 8. The upper limit on N can be increased if desired by adding
39703d4924Stb more #if blocks, extending the patterns apparent in the code. In addition,
40703d4924Stb crc32.h would need to be regenerated, if the maximum N value is increased.
41703d4924Stb
42703d4924Stb N and W are chosen empirically by benchmarking the execution time on a given
43703d4924Stb processor. The choices for N and W below were based on testing on Intel Kaby
44703d4924Stb Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64
45703d4924Stb Octeon II processors. The Intel, AMD, and ARM processors were all fastest
46703d4924Stb with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4.
47703d4924Stb They were all tested with either gcc or clang, all using the -O3 optimization
48703d4924Stb level. Your mileage may vary.
49703d4924Stb */
50703d4924Stb
51703d4924Stb /* Define N */
52703d4924Stb #ifdef Z_TESTN
53703d4924Stb # define N Z_TESTN
5485c48e79Shenning #else
55703d4924Stb # define N 5
56703d4924Stb #endif
57703d4924Stb #if N < 1 || N > 6
58703d4924Stb # error N must be in 1..6
59703d4924Stb #endif
6085c48e79Shenning
61703d4924Stb /*
62703d4924Stb z_crc_t must be at least 32 bits. z_word_t must be at least as long as
63703d4924Stb z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and
64703d4924Stb that bytes are eight bits.
65703d4924Stb */
6636f395ceStb
67703d4924Stb /*
68703d4924Stb Define W and the associated z_word_t type. If W is not defined, then a
69703d4924Stb braided calculation is not used, and the associated tables and code are not
70703d4924Stb compiled.
71703d4924Stb */
72703d4924Stb #ifdef Z_TESTW
73703d4924Stb # if Z_TESTW-1 != -1
74703d4924Stb # define W Z_TESTW
75703d4924Stb # endif
76703d4924Stb #else
77703d4924Stb # ifdef MAKECRCH
78703d4924Stb # define W 8 /* required for MAKECRCH */
79703d4924Stb # else
80703d4924Stb # if defined(__x86_64__) || defined(__aarch64__)
81703d4924Stb # define W 8
82703d4924Stb # else
83703d4924Stb # define W 4
84703d4924Stb # endif
85703d4924Stb # endif
86703d4924Stb #endif
87703d4924Stb #ifdef W
88703d4924Stb # if W == 8 && defined(Z_U8)
89703d4924Stb typedef Z_U8 z_word_t;
90703d4924Stb # elif defined(Z_U4)
91703d4924Stb # undef W
92703d4924Stb # define W 4
93703d4924Stb typedef Z_U4 z_word_t;
94703d4924Stb # else
95703d4924Stb # undef W
96703d4924Stb # endif
97703d4924Stb #endif
98703d4924Stb
998bda5813Stb /* If available, use the ARM processor CRC32 instruction. */
1008bda5813Stb #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
1018bda5813Stb # define ARMCRC32
1028bda5813Stb #endif
1038bda5813Stb
104703d4924Stb #if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
105703d4924Stb /*
106703d4924Stb Swap the bytes in a z_word_t to convert between little and big endian. Any
107703d4924Stb self-respecting compiler will optimize this to a single machine byte-swap
108703d4924Stb instruction, if one is available. This assumes that word_t is either 32 bits
109703d4924Stb or 64 bits.
110703d4924Stb */
byte_swap(z_word_t word)111a04ea15dStb local z_word_t byte_swap(z_word_t word) {
112703d4924Stb # if W == 8
113703d4924Stb return
114703d4924Stb (word & 0xff00000000000000) >> 56 |
115703d4924Stb (word & 0xff000000000000) >> 40 |
116703d4924Stb (word & 0xff0000000000) >> 24 |
117703d4924Stb (word & 0xff00000000) >> 8 |
118703d4924Stb (word & 0xff000000) << 8 |
119703d4924Stb (word & 0xff0000) << 24 |
120703d4924Stb (word & 0xff00) << 40 |
121703d4924Stb (word & 0xff) << 56;
122703d4924Stb # else /* W == 4 */
123703d4924Stb return
124703d4924Stb (word & 0xff000000) >> 24 |
125703d4924Stb (word & 0xff0000) >> 8 |
126703d4924Stb (word & 0xff00) << 8 |
127703d4924Stb (word & 0xff) << 24;
128703d4924Stb # endif
129703d4924Stb }
130703d4924Stb #endif
131703d4924Stb
132a04ea15dStb #ifdef DYNAMIC_CRC_TABLE
133a04ea15dStb /* =========================================================================
134a04ea15dStb * Table of powers of x for combining CRC-32s, filled in by make_crc_table()
135a04ea15dStb * below.
136a04ea15dStb */
137a04ea15dStb local z_crc_t FAR x2n_table[32];
138a04ea15dStb #else
139a04ea15dStb /* =========================================================================
140a04ea15dStb * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
141a04ea15dStb * of x for combining CRC-32s, all made by make_crc_table().
142a04ea15dStb */
143a04ea15dStb # include "crc32.h"
144a04ea15dStb #endif
145a04ea15dStb
146703d4924Stb /* CRC polynomial. */
147703d4924Stb #define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */
148d76b9bfaSmillert
149a04ea15dStb /*
150a04ea15dStb Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
151a04ea15dStb reflected. For speed, this requires that a not be zero.
152a04ea15dStb */
multmodp(z_crc_t a,z_crc_t b)153a04ea15dStb local z_crc_t multmodp(z_crc_t a, z_crc_t b) {
154a04ea15dStb z_crc_t m, p;
1552af503bcStholo
156a04ea15dStb m = (z_crc_t)1 << 31;
157a04ea15dStb p = 0;
158a04ea15dStb for (;;) {
159a04ea15dStb if (a & m) {
160a04ea15dStb p ^= b;
161a04ea15dStb if ((a & (m - 1)) == 0)
162a04ea15dStb break;
163a04ea15dStb }
164a04ea15dStb m >>= 1;
165a04ea15dStb b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
166a04ea15dStb }
167a04ea15dStb return p;
168a04ea15dStb }
169a04ea15dStb
170a04ea15dStb /*
171a04ea15dStb Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
172a04ea15dStb initialized.
173a04ea15dStb */
x2nmodp(z_off64_t n,unsigned k)174a04ea15dStb local z_crc_t x2nmodp(z_off64_t n, unsigned k) {
175a04ea15dStb z_crc_t p;
176a04ea15dStb
177a04ea15dStb p = (z_crc_t)1 << 31; /* x^0 == 1 */
178a04ea15dStb while (n) {
179a04ea15dStb if (n & 1)
180a04ea15dStb p = multmodp(x2n_table[k & 31], p);
181a04ea15dStb n >>= 1;
182a04ea15dStb k++;
183a04ea15dStb }
184a04ea15dStb return p;
185a04ea15dStb }
186a04ea15dStb
187a04ea15dStb #ifdef DYNAMIC_CRC_TABLE
188a04ea15dStb /* =========================================================================
189a04ea15dStb * Build the tables for byte-wise and braided CRC-32 calculations, and a table
190a04ea15dStb * of powers of x for combining CRC-32s.
191a04ea15dStb */
192703d4924Stb local z_crc_t FAR crc_table[256];
193703d4924Stb #ifdef W
194703d4924Stb local z_word_t FAR crc_big_table[256];
195703d4924Stb local z_crc_t FAR crc_braid_table[W][256];
196703d4924Stb local z_word_t FAR crc_braid_big_table[W][256];
197a04ea15dStb local void braid(z_crc_t [][256], z_word_t [][256], int, int);
198703d4924Stb #endif
19985c48e79Shenning #ifdef MAKECRCH
200a04ea15dStb local void write_table(FILE *, const z_crc_t FAR *, int);
201a04ea15dStb local void write_table32hi(FILE *, const z_word_t FAR *, int);
202a04ea15dStb local void write_table64(FILE *, const z_word_t FAR *, int);
20385c48e79Shenning #endif /* MAKECRCH */
204703d4924Stb
205703d4924Stb /*
206703d4924Stb Define a once() function depending on the availability of atomics. If this is
207703d4924Stb compiled with DYNAMIC_CRC_TABLE defined, and if CRCs will be computed in
208703d4924Stb multiple threads, and if atomics are not available, then get_crc_table() must
209703d4924Stb be called to initialize the tables and must return before any threads are
210703d4924Stb allowed to compute or combine CRCs.
211703d4924Stb */
212703d4924Stb
213703d4924Stb /* Definition of once functionality. */
214703d4924Stb typedef struct once_s once_t;
215703d4924Stb
216703d4924Stb /* Check for the availability of atomics. */
217703d4924Stb #if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
218703d4924Stb !defined(__STDC_NO_ATOMICS__) && !defined(SMALL)
219703d4924Stb
220703d4924Stb #include <stdatomic.h>
221703d4924Stb
222703d4924Stb /* Structure for once(), which must be initialized with ONCE_INIT. */
223703d4924Stb struct once_s {
224703d4924Stb atomic_flag begun;
225703d4924Stb atomic_int done;
226703d4924Stb };
227703d4924Stb #define ONCE_INIT {ATOMIC_FLAG_INIT, 0}
228703d4924Stb
229703d4924Stb /*
230703d4924Stb Run the provided init() function exactly once, even if multiple threads
231703d4924Stb invoke once() at the same time. The state must be a once_t initialized with
232703d4924Stb ONCE_INIT.
233703d4924Stb */
once(once_t * state,void (* init)(void))234a04ea15dStb local void once(once_t *state, void (*init)(void)) {
235703d4924Stb if (!atomic_load(&state->done)) {
236703d4924Stb if (atomic_flag_test_and_set(&state->begun))
237703d4924Stb while (!atomic_load(&state->done))
238703d4924Stb ;
239703d4924Stb else {
240703d4924Stb init();
241703d4924Stb atomic_store(&state->done, 1);
242703d4924Stb }
243703d4924Stb }
244703d4924Stb }
245703d4924Stb
246703d4924Stb #else /* no atomics */
247703d4924Stb
248703d4924Stb /* Structure for once(), which must be initialized with ONCE_INIT. */
249703d4924Stb struct once_s {
250703d4924Stb volatile int begun;
251703d4924Stb volatile int done;
252703d4924Stb };
253703d4924Stb #define ONCE_INIT {0, 0}
254703d4924Stb
255703d4924Stb /* Test and set. Alas, not atomic, but tries to minimize the period of
256703d4924Stb vulnerability. */
test_and_set(int volatile * flag)257a04ea15dStb local int test_and_set(int volatile *flag) {
258703d4924Stb int was;
259703d4924Stb
260703d4924Stb was = *flag;
261703d4924Stb *flag = 1;
262703d4924Stb return was;
263703d4924Stb }
264703d4924Stb
265703d4924Stb /* Run the provided init() function once. This is not thread-safe. */
once(once_t * state,void (* init)(void))266a04ea15dStb local void once(once_t *state, void (*init)(void)) {
267703d4924Stb if (!state->done) {
268703d4924Stb if (test_and_set(&state->begun))
269703d4924Stb while (!state->done)
270703d4924Stb ;
271703d4924Stb else {
272703d4924Stb init();
273703d4924Stb state->done = 1;
274703d4924Stb }
275703d4924Stb }
276703d4924Stb }
277703d4924Stb
278703d4924Stb #endif
279703d4924Stb
280703d4924Stb /* State for once(). */
281703d4924Stb local once_t made = ONCE_INIT;
282703d4924Stb
2832af503bcStholo /*
28485c48e79Shenning Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
2852af503bcStholo 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.
2862af503bcStholo
2872af503bcStholo Polynomials over GF(2) are represented in binary, one bit per coefficient,
2882af503bcStholo with the lowest powers in the most significant bit. Then adding polynomials
2892af503bcStholo is just exclusive-or, and multiplying a polynomial by x is a right shift by
2902af503bcStholo one. If we call the above polynomial p, and represent a byte as the
2912af503bcStholo polynomial q, also with the lowest power in the most significant bit (so the
292703d4924Stb byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p,
2932af503bcStholo where a mod b means the remainder after dividing a by b.
2942af503bcStholo
2952af503bcStholo This calculation is done using the shift-register method of multiplying and
2962af503bcStholo taking the remainder. The register is initialized to zero, and for each
2972af503bcStholo incoming bit, x^32 is added mod p to the register if the bit is a one (where
298703d4924Stb x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x
299703d4924Stb (which is shifting right by one and adding x^32 mod p if the bit shifted out
300703d4924Stb is a one). We start with the highest power (least significant bit) of q and
301703d4924Stb repeat for all eight bits of q.
3022af503bcStholo
303703d4924Stb The table is simply the CRC of all possible eight bit values. This is all the
304703d4924Stb information needed to generate CRCs on data a byte at a time for all
305703d4924Stb combinations of CRC register values and incoming bytes.
3062af503bcStholo */
307703d4924Stb
make_crc_table(void)308a04ea15dStb local void make_crc_table(void) {
309703d4924Stb unsigned i, j, n;
310703d4924Stb z_crc_t p;
3112af503bcStholo
312703d4924Stb /* initialize the CRC of bytes tables */
313703d4924Stb for (i = 0; i < 256; i++) {
314703d4924Stb p = i;
315703d4924Stb for (j = 0; j < 8; j++)
316703d4924Stb p = p & 1 ? (p >> 1) ^ POLY : p >> 1;
317703d4924Stb crc_table[i] = p;
318703d4924Stb #ifdef W
319703d4924Stb crc_big_table[i] = byte_swap(p);
320703d4924Stb #endif
3212af503bcStholo }
32285c48e79Shenning
323703d4924Stb /* initialize the x^2^n mod p(x) table */
324703d4924Stb p = (z_crc_t)1 << 30; /* x^1 */
325703d4924Stb x2n_table[0] = p;
326703d4924Stb for (n = 1; n < 32; n++)
327703d4924Stb x2n_table[n] = p = multmodp(p, p);
32885c48e79Shenning
329703d4924Stb #ifdef W
330703d4924Stb /* initialize the braiding tables -- needs x2n_table[] */
331703d4924Stb braid(crc_braid_table, crc_braid_big_table, N, W);
332703d4924Stb #endif
33385c48e79Shenning
33485c48e79Shenning #ifdef MAKECRCH
33585c48e79Shenning {
336703d4924Stb /*
337703d4924Stb The crc32.h header file contains tables for both 32-bit and 64-bit
338703d4924Stb z_word_t's, and so requires a 64-bit type be available. In that case,
339703d4924Stb z_word_t must be defined to be 64-bits. This code then also generates
340703d4924Stb and writes out the tables for the case that z_word_t is 32 bits.
341703d4924Stb */
342703d4924Stb #if !defined(W) || W != 8
343703d4924Stb # error Need a 64-bit integer type in order to generate crc32.h.
344703d4924Stb #endif
34585c48e79Shenning FILE *out;
346703d4924Stb int k, n;
347703d4924Stb z_crc_t ltl[8][256];
348703d4924Stb z_word_t big[8][256];
34985c48e79Shenning
35085c48e79Shenning out = fopen("crc32.h", "w");
35185c48e79Shenning if (out == NULL) return;
352703d4924Stb
353703d4924Stb /* write out little-endian CRC table to crc32.h */
354703d4924Stb fprintf(out,
355703d4924Stb "/* crc32.h -- tables for rapid CRC calculation\n"
356703d4924Stb " * Generated automatically by crc32.c\n */\n"
357703d4924Stb "\n"
358703d4924Stb "local const z_crc_t FAR crc_table[] = {\n"
359703d4924Stb " ");
360703d4924Stb write_table(out, crc_table, 256);
361703d4924Stb fprintf(out,
362703d4924Stb "};\n");
363703d4924Stb
364703d4924Stb /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */
365703d4924Stb fprintf(out,
366703d4924Stb "\n"
367703d4924Stb "#ifdef W\n"
368703d4924Stb "\n"
369703d4924Stb "#if W == 8\n"
370703d4924Stb "\n"
371703d4924Stb "local const z_word_t FAR crc_big_table[] = {\n"
372703d4924Stb " ");
373703d4924Stb write_table64(out, crc_big_table, 256);
374703d4924Stb fprintf(out,
375703d4924Stb "};\n");
376703d4924Stb
377703d4924Stb /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */
378703d4924Stb fprintf(out,
379703d4924Stb "\n"
380703d4924Stb "#else /* W == 4 */\n"
381703d4924Stb "\n"
382703d4924Stb "local const z_word_t FAR crc_big_table[] = {\n"
383703d4924Stb " ");
384703d4924Stb write_table32hi(out, crc_big_table, 256);
385703d4924Stb fprintf(out,
386703d4924Stb "};\n"
387703d4924Stb "\n"
388703d4924Stb "#endif\n");
389703d4924Stb
390703d4924Stb /* write out braid tables for each value of N */
391703d4924Stb for (n = 1; n <= 6; n++) {
392703d4924Stb fprintf(out,
393703d4924Stb "\n"
394703d4924Stb "#if N == %d\n", n);
395703d4924Stb
396703d4924Stb /* compute braid tables for this N and 64-bit word_t */
397703d4924Stb braid(ltl, big, n, 8);
398703d4924Stb
399703d4924Stb /* write out braid tables for 64-bit z_word_t to crc32.h */
400703d4924Stb fprintf(out,
401703d4924Stb "\n"
402703d4924Stb "#if W == 8\n"
403703d4924Stb "\n"
404703d4924Stb "local const z_crc_t FAR crc_braid_table[][256] = {\n");
405703d4924Stb for (k = 0; k < 8; k++) {
406703d4924Stb fprintf(out, " {");
407703d4924Stb write_table(out, ltl[k], 256);
408703d4924Stb fprintf(out, "}%s", k < 7 ? ",\n" : "");
4092af503bcStholo }
410703d4924Stb fprintf(out,
411703d4924Stb "};\n"
412703d4924Stb "\n"
413703d4924Stb "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
414703d4924Stb for (k = 0; k < 8; k++) {
415703d4924Stb fprintf(out, " {");
416703d4924Stb write_table64(out, big[k], 256);
417703d4924Stb fprintf(out, "}%s", k < 7 ? ",\n" : "");
418703d4924Stb }
419703d4924Stb fprintf(out,
420703d4924Stb "};\n");
421703d4924Stb
422703d4924Stb /* compute braid tables for this N and 32-bit word_t */
423703d4924Stb braid(ltl, big, n, 4);
424703d4924Stb
425703d4924Stb /* write out braid tables for 32-bit z_word_t to crc32.h */
426703d4924Stb fprintf(out,
427703d4924Stb "\n"
428703d4924Stb "#else /* W == 4 */\n"
429703d4924Stb "\n"
430703d4924Stb "local const z_crc_t FAR crc_braid_table[][256] = {\n");
431703d4924Stb for (k = 0; k < 4; k++) {
432703d4924Stb fprintf(out, " {");
433703d4924Stb write_table(out, ltl[k], 256);
434703d4924Stb fprintf(out, "}%s", k < 3 ? ",\n" : "");
435703d4924Stb }
436703d4924Stb fprintf(out,
437703d4924Stb "};\n"
438703d4924Stb "\n"
439703d4924Stb "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
440703d4924Stb for (k = 0; k < 4; k++) {
441703d4924Stb fprintf(out, " {");
442703d4924Stb write_table32hi(out, big[k], 256);
443703d4924Stb fprintf(out, "}%s", k < 3 ? ",\n" : "");
444703d4924Stb }
445703d4924Stb fprintf(out,
446703d4924Stb "};\n"
447703d4924Stb "\n"
448703d4924Stb "#endif\n"
449703d4924Stb "\n"
450703d4924Stb "#endif\n");
451703d4924Stb }
452703d4924Stb fprintf(out,
453703d4924Stb "\n"
454703d4924Stb "#endif\n");
455703d4924Stb
456703d4924Stb /* write out zeros operator table to crc32.h */
457703d4924Stb fprintf(out,
458703d4924Stb "\n"
459703d4924Stb "local const z_crc_t FAR x2n_table[] = {\n"
460703d4924Stb " ");
461703d4924Stb write_table(out, x2n_table, 32);
462703d4924Stb fprintf(out,
463703d4924Stb "};\n");
46485c48e79Shenning fclose(out);
46585c48e79Shenning }
46685c48e79Shenning #endif /* MAKECRCH */
46785c48e79Shenning }
46885c48e79Shenning
46985c48e79Shenning #ifdef MAKECRCH
470703d4924Stb
471703d4924Stb /*
472703d4924Stb Write the 32-bit values in table[0..k-1] to out, five per line in
473703d4924Stb hexadecimal separated by commas.
474703d4924Stb */
write_table(FILE * out,const z_crc_t FAR * table,int k)475a04ea15dStb local void write_table(FILE *out, const z_crc_t FAR *table, int k) {
47685c48e79Shenning int n;
47785c48e79Shenning
478703d4924Stb for (n = 0; n < k; n++)
479703d4924Stb fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
48036f395ceStb (unsigned long)(table[n]),
481703d4924Stb n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
48285c48e79Shenning }
483703d4924Stb
484703d4924Stb /*
485703d4924Stb Write the high 32-bits of each value in table[0..k-1] to out, five per line
486703d4924Stb in hexadecimal separated by commas.
487703d4924Stb */
write_table32hi(FILE * out,const z_word_t FAR * table,int k)488a04ea15dStb local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) {
489703d4924Stb int n;
490703d4924Stb
491703d4924Stb for (n = 0; n < k; n++)
492703d4924Stb fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
493703d4924Stb (unsigned long)(table[n] >> 32),
494703d4924Stb n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
495703d4924Stb }
496703d4924Stb
497703d4924Stb /*
498703d4924Stb Write the 64-bit values in table[0..k-1] to out, three per line in
499703d4924Stb hexadecimal separated by commas. This assumes that if there is a 64-bit
500703d4924Stb type, then there is also a long long integer type, and it is at least 64
501703d4924Stb bits. If not, then the type cast and format string can be adjusted
502703d4924Stb accordingly.
503703d4924Stb */
write_table64(FILE * out,const z_word_t FAR * table,int k)504a04ea15dStb local void write_table64(FILE *out, const z_word_t FAR *table, int k) {
505703d4924Stb int n;
506703d4924Stb
507703d4924Stb for (n = 0; n < k; n++)
508703d4924Stb fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ",
509703d4924Stb (unsigned long long)(table[n]),
510703d4924Stb n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", "));
511703d4924Stb }
512703d4924Stb
513703d4924Stb /* Actually do the deed. */
main(void)514a04ea15dStb int main(void) {
515703d4924Stb make_crc_table();
516703d4924Stb return 0;
517703d4924Stb }
518703d4924Stb
51985c48e79Shenning #endif /* MAKECRCH */
52085c48e79Shenning
521703d4924Stb #ifdef W
522703d4924Stb /*
523703d4924Stb Generate the little and big-endian braid tables for the given n and z_word_t
524703d4924Stb size w. Each array must have room for w blocks of 256 elements.
525703d4924Stb */
braid(z_crc_t ltl[][256],z_word_t big[][256],int n,int w)526a04ea15dStb local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) {
527703d4924Stb int k;
528703d4924Stb z_crc_t i, p, q;
529703d4924Stb for (k = 0; k < w; k++) {
530703d4924Stb p = x2nmodp((n * w + 3 - k) << 3, 0);
531703d4924Stb ltl[k][0] = 0;
532703d4924Stb big[w - 1 - k][0] = 0;
533703d4924Stb for (i = 1; i < 256; i++) {
534703d4924Stb ltl[k][i] = q = multmodp(i << 24, p);
535703d4924Stb big[w - 1 - k][i] = byte_swap(q);
536703d4924Stb }
537703d4924Stb }
538703d4924Stb }
539703d4924Stb #endif
540703d4924Stb
54185c48e79Shenning #endif /* DYNAMIC_CRC_TABLE */
5422af503bcStholo
5432af503bcStholo /* =========================================================================
544703d4924Stb * This function can be used by asm versions of crc32(), and to force the
545703d4924Stb * generation of the CRC tables in a threaded application.
5462af503bcStholo */
get_crc_table(void)547a04ea15dStb const z_crc_t FAR * ZEXPORT get_crc_table(void) {
5482af503bcStholo #ifdef DYNAMIC_CRC_TABLE
549703d4924Stb once(&made, make_crc_table);
55085c48e79Shenning #endif /* DYNAMIC_CRC_TABLE */
55136f395ceStb return (const z_crc_t FAR *)crc_table;
5522af503bcStholo }
5532af503bcStholo
554703d4924Stb /* =========================================================================
555703d4924Stb * Use ARM machine instructions if available. This will compute the CRC about
556703d4924Stb * ten times faster than the braided calculation. This code does not check for
557703d4924Stb * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will
558703d4924Stb * only be defined if the compilation specifies an ARM processor architecture
559703d4924Stb * that has the instructions. For example, compiling with -march=armv8.1-a or
560703d4924Stb * -march=armv8-a+crc, or -march=native if the compile machine has the crc32
561703d4924Stb * instructions.
562703d4924Stb */
563703d4924Stb #ifdef ARMCRC32
564703d4924Stb
565703d4924Stb /*
566703d4924Stb Constants empirically determined to maximize speed. These values are from
567703d4924Stb measurements on a Cortex-A57. Your mileage may vary.
568703d4924Stb */
569703d4924Stb #define Z_BATCH 3990 /* number of words in a batch */
570703d4924Stb #define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */
571703d4924Stb #define Z_BATCH_MIN 800 /* fewest words in a final batch */
572703d4924Stb
crc32_z(unsigned long crc,const unsigned char FAR * buf,z_size_t len)573a04ea15dStb unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
574a04ea15dStb z_size_t len) {
575703d4924Stb z_crc_t val;
576703d4924Stb z_word_t crc1, crc2;
577703d4924Stb const z_word_t *word;
578703d4924Stb z_word_t val0, val1, val2;
579703d4924Stb z_size_t last, last2, i;
580703d4924Stb z_size_t num;
581703d4924Stb
582703d4924Stb /* Return initial CRC, if requested. */
583703d4924Stb if (buf == Z_NULL) return 0;
584703d4924Stb
585703d4924Stb #ifdef DYNAMIC_CRC_TABLE
586703d4924Stb once(&made, make_crc_table);
587703d4924Stb #endif /* DYNAMIC_CRC_TABLE */
588703d4924Stb
589703d4924Stb /* Pre-condition the CRC */
590c5288b00Stb crc = (~crc) & 0xffffffff;
591703d4924Stb
592703d4924Stb /* Compute the CRC up to a word boundary. */
593703d4924Stb while (len && ((z_size_t)buf & 7) != 0) {
594703d4924Stb len--;
595703d4924Stb val = *buf++;
596703d4924Stb __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
597703d4924Stb }
598703d4924Stb
599703d4924Stb /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */
600703d4924Stb word = (z_word_t const *)buf;
601703d4924Stb num = len >> 3;
602703d4924Stb len &= 7;
603703d4924Stb
604703d4924Stb /* Do three interleaved CRCs to realize the throughput of one crc32x
6058bda5813Stb instruction per cycle. Each CRC is calculated on Z_BATCH words. The
6068bda5813Stb three CRCs are combined into a single CRC after each set of batches. */
607703d4924Stb while (num >= 3 * Z_BATCH) {
608703d4924Stb crc1 = 0;
609703d4924Stb crc2 = 0;
610703d4924Stb for (i = 0; i < Z_BATCH; i++) {
611703d4924Stb val0 = word[i];
612703d4924Stb val1 = word[i + Z_BATCH];
613703d4924Stb val2 = word[i + 2 * Z_BATCH];
614703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
615703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
616703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
617703d4924Stb }
618703d4924Stb word += 3 * Z_BATCH;
619703d4924Stb num -= 3 * Z_BATCH;
620703d4924Stb crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
621703d4924Stb crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
622703d4924Stb }
623703d4924Stb
624703d4924Stb /* Do one last smaller batch with the remaining words, if there are enough
625703d4924Stb to pay for the combination of CRCs. */
626703d4924Stb last = num / 3;
627703d4924Stb if (last >= Z_BATCH_MIN) {
628703d4924Stb last2 = last << 1;
629703d4924Stb crc1 = 0;
630703d4924Stb crc2 = 0;
631703d4924Stb for (i = 0; i < last; i++) {
632703d4924Stb val0 = word[i];
633703d4924Stb val1 = word[i + last];
634703d4924Stb val2 = word[i + last2];
635703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
636703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
637703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
638703d4924Stb }
639703d4924Stb word += 3 * last;
640703d4924Stb num -= 3 * last;
641703d4924Stb val = x2nmodp(last, 6);
642703d4924Stb crc = multmodp(val, crc) ^ crc1;
643703d4924Stb crc = multmodp(val, crc) ^ crc2;
644703d4924Stb }
645703d4924Stb
646703d4924Stb /* Compute the CRC on any remaining words. */
647703d4924Stb for (i = 0; i < num; i++) {
648703d4924Stb val0 = word[i];
649703d4924Stb __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
650703d4924Stb }
651703d4924Stb word += num;
652703d4924Stb
653703d4924Stb /* Complete the CRC on any remaining bytes. */
654703d4924Stb buf = (const unsigned char FAR *)word;
655703d4924Stb while (len) {
656703d4924Stb len--;
657703d4924Stb val = *buf++;
658703d4924Stb __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
659703d4924Stb }
660703d4924Stb
661703d4924Stb /* Return the CRC, post-conditioned. */
662703d4924Stb return crc ^ 0xffffffff;
663703d4924Stb }
664703d4924Stb
665703d4924Stb #else
666703d4924Stb
667703d4924Stb #ifdef W
668703d4924Stb
669703d4924Stb /*
670703d4924Stb Return the CRC of the W bytes in the word_t data, taking the
671703d4924Stb least-significant byte of the word as the first byte of data, without any pre
672703d4924Stb or post conditioning. This is used to combine the CRCs of each braid.
673703d4924Stb */
crc_word(z_word_t data)674a04ea15dStb local z_crc_t crc_word(z_word_t data) {
675703d4924Stb int k;
676703d4924Stb for (k = 0; k < W; k++)
677703d4924Stb data = (data >> 8) ^ crc_table[data & 0xff];
678703d4924Stb return (z_crc_t)data;
679703d4924Stb }
680703d4924Stb
crc_word_big(z_word_t data)681a04ea15dStb local z_word_t crc_word_big(z_word_t data) {
682703d4924Stb int k;
683703d4924Stb for (k = 0; k < W; k++)
684703d4924Stb data = (data << 8) ^
685703d4924Stb crc_big_table[(data >> ((W - 1) << 3)) & 0xff];
686703d4924Stb return data;
687703d4924Stb }
688703d4924Stb
689703d4924Stb #endif
6902af503bcStholo
6912af503bcStholo /* ========================================================================= */
crc32_z(unsigned long crc,const unsigned char FAR * buf,z_size_t len)692a04ea15dStb unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
693a04ea15dStb z_size_t len) {
694703d4924Stb /* Return initial CRC, if requested. */
695703d4924Stb if (buf == Z_NULL) return 0;
69685c48e79Shenning
6972af503bcStholo #ifdef DYNAMIC_CRC_TABLE
698703d4924Stb once(&made, make_crc_table);
69985c48e79Shenning #endif /* DYNAMIC_CRC_TABLE */
70085c48e79Shenning
701703d4924Stb /* Pre-condition the CRC */
702c5288b00Stb crc = (~crc) & 0xffffffff;
70385c48e79Shenning
704703d4924Stb #ifdef W
705703d4924Stb
706703d4924Stb /* If provided enough bytes, do a braided CRC calculation. */
707703d4924Stb if (len >= N * W + W - 1) {
708703d4924Stb z_size_t blks;
709703d4924Stb z_word_t const *words;
710703d4924Stb unsigned endian;
711703d4924Stb int k;
712703d4924Stb
713703d4924Stb /* Compute the CRC up to a z_word_t boundary. */
714703d4924Stb while (len && ((z_size_t)buf & (W - 1)) != 0) {
715703d4924Stb len--;
716703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
717703d4924Stb }
718703d4924Stb
719703d4924Stb /* Compute the CRC on as many N z_word_t blocks as are available. */
720703d4924Stb blks = len / (N * W);
721703d4924Stb len -= blks * N * W;
722703d4924Stb words = (z_word_t const *)buf;
723703d4924Stb
724703d4924Stb /* Do endian check at execution time instead of compile time, since ARM
725*70ce8dcaStb processors can change the endianness at execution time. If the
726*70ce8dcaStb compiler knows what the endianness will be, it can optimize out the
727703d4924Stb check and the unused branch. */
72885c48e79Shenning endian = 1;
729703d4924Stb if (*(unsigned char *)&endian) {
730703d4924Stb /* Little endian. */
731703d4924Stb
732703d4924Stb z_crc_t crc0;
733703d4924Stb z_word_t word0;
734703d4924Stb #if N > 1
735703d4924Stb z_crc_t crc1;
736703d4924Stb z_word_t word1;
737703d4924Stb #if N > 2
738703d4924Stb z_crc_t crc2;
739703d4924Stb z_word_t word2;
740703d4924Stb #if N > 3
741703d4924Stb z_crc_t crc3;
742703d4924Stb z_word_t word3;
743703d4924Stb #if N > 4
744703d4924Stb z_crc_t crc4;
745703d4924Stb z_word_t word4;
746703d4924Stb #if N > 5
747703d4924Stb z_crc_t crc5;
748703d4924Stb z_word_t word5;
749703d4924Stb #endif
750703d4924Stb #endif
751703d4924Stb #endif
752703d4924Stb #endif
753703d4924Stb #endif
754703d4924Stb
755703d4924Stb /* Initialize the CRC for each braid. */
756703d4924Stb crc0 = crc;
757703d4924Stb #if N > 1
758703d4924Stb crc1 = 0;
759703d4924Stb #if N > 2
760703d4924Stb crc2 = 0;
761703d4924Stb #if N > 3
762703d4924Stb crc3 = 0;
763703d4924Stb #if N > 4
764703d4924Stb crc4 = 0;
765703d4924Stb #if N > 5
766703d4924Stb crc5 = 0;
767703d4924Stb #endif
768703d4924Stb #endif
769703d4924Stb #endif
770703d4924Stb #endif
771703d4924Stb #endif
772703d4924Stb
773703d4924Stb /*
774703d4924Stb Process the first blks-1 blocks, computing the CRCs on each braid
775703d4924Stb independently.
776703d4924Stb */
777703d4924Stb while (--blks) {
778703d4924Stb /* Load the word for each braid into registers. */
779703d4924Stb word0 = crc0 ^ words[0];
780703d4924Stb #if N > 1
781703d4924Stb word1 = crc1 ^ words[1];
782703d4924Stb #if N > 2
783703d4924Stb word2 = crc2 ^ words[2];
784703d4924Stb #if N > 3
785703d4924Stb word3 = crc3 ^ words[3];
786703d4924Stb #if N > 4
787703d4924Stb word4 = crc4 ^ words[4];
788703d4924Stb #if N > 5
789703d4924Stb word5 = crc5 ^ words[5];
790703d4924Stb #endif
791703d4924Stb #endif
792703d4924Stb #endif
793703d4924Stb #endif
794703d4924Stb #endif
795703d4924Stb words += N;
796703d4924Stb
797703d4924Stb /* Compute and update the CRC for each word. The loop should
798703d4924Stb get unrolled. */
799703d4924Stb crc0 = crc_braid_table[0][word0 & 0xff];
800703d4924Stb #if N > 1
801703d4924Stb crc1 = crc_braid_table[0][word1 & 0xff];
802703d4924Stb #if N > 2
803703d4924Stb crc2 = crc_braid_table[0][word2 & 0xff];
804703d4924Stb #if N > 3
805703d4924Stb crc3 = crc_braid_table[0][word3 & 0xff];
806703d4924Stb #if N > 4
807703d4924Stb crc4 = crc_braid_table[0][word4 & 0xff];
808703d4924Stb #if N > 5
809703d4924Stb crc5 = crc_braid_table[0][word5 & 0xff];
810703d4924Stb #endif
811703d4924Stb #endif
812703d4924Stb #endif
813703d4924Stb #endif
814703d4924Stb #endif
815703d4924Stb for (k = 1; k < W; k++) {
816703d4924Stb crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
817703d4924Stb #if N > 1
818703d4924Stb crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
819703d4924Stb #if N > 2
820703d4924Stb crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
821703d4924Stb #if N > 3
822703d4924Stb crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
823703d4924Stb #if N > 4
824703d4924Stb crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
825703d4924Stb #if N > 5
826703d4924Stb crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
827703d4924Stb #endif
828703d4924Stb #endif
829703d4924Stb #endif
830703d4924Stb #endif
831703d4924Stb #endif
83285c48e79Shenning }
833703d4924Stb }
834703d4924Stb
835703d4924Stb /*
836703d4924Stb Process the last block, combining the CRCs of the N braids at the
837703d4924Stb same time.
838703d4924Stb */
839703d4924Stb crc = crc_word(crc0 ^ words[0]);
840703d4924Stb #if N > 1
841703d4924Stb crc = crc_word(crc1 ^ words[1] ^ crc);
842703d4924Stb #if N > 2
843703d4924Stb crc = crc_word(crc2 ^ words[2] ^ crc);
844703d4924Stb #if N > 3
845703d4924Stb crc = crc_word(crc3 ^ words[3] ^ crc);
846703d4924Stb #if N > 4
847703d4924Stb crc = crc_word(crc4 ^ words[4] ^ crc);
848703d4924Stb #if N > 5
849703d4924Stb crc = crc_word(crc5 ^ words[5] ^ crc);
850703d4924Stb #endif
851703d4924Stb #endif
852703d4924Stb #endif
853703d4924Stb #endif
854703d4924Stb #endif
855703d4924Stb words += N;
856703d4924Stb }
857703d4924Stb else {
858703d4924Stb /* Big endian. */
859703d4924Stb
860703d4924Stb z_word_t crc0, word0, comb;
861703d4924Stb #if N > 1
862703d4924Stb z_word_t crc1, word1;
863703d4924Stb #if N > 2
864703d4924Stb z_word_t crc2, word2;
865703d4924Stb #if N > 3
866703d4924Stb z_word_t crc3, word3;
867703d4924Stb #if N > 4
868703d4924Stb z_word_t crc4, word4;
869703d4924Stb #if N > 5
870703d4924Stb z_word_t crc5, word5;
871703d4924Stb #endif
872703d4924Stb #endif
873703d4924Stb #endif
874703d4924Stb #endif
875703d4924Stb #endif
876703d4924Stb
877703d4924Stb /* Initialize the CRC for each braid. */
878703d4924Stb crc0 = byte_swap(crc);
879703d4924Stb #if N > 1
880703d4924Stb crc1 = 0;
881703d4924Stb #if N > 2
882703d4924Stb crc2 = 0;
883703d4924Stb #if N > 3
884703d4924Stb crc3 = 0;
885703d4924Stb #if N > 4
886703d4924Stb crc4 = 0;
887703d4924Stb #if N > 5
888703d4924Stb crc5 = 0;
889703d4924Stb #endif
890703d4924Stb #endif
891703d4924Stb #endif
892703d4924Stb #endif
893703d4924Stb #endif
894703d4924Stb
895703d4924Stb /*
896703d4924Stb Process the first blks-1 blocks, computing the CRCs on each braid
897703d4924Stb independently.
898703d4924Stb */
899703d4924Stb while (--blks) {
900703d4924Stb /* Load the word for each braid into registers. */
901703d4924Stb word0 = crc0 ^ words[0];
902703d4924Stb #if N > 1
903703d4924Stb word1 = crc1 ^ words[1];
904703d4924Stb #if N > 2
905703d4924Stb word2 = crc2 ^ words[2];
906703d4924Stb #if N > 3
907703d4924Stb word3 = crc3 ^ words[3];
908703d4924Stb #if N > 4
909703d4924Stb word4 = crc4 ^ words[4];
910703d4924Stb #if N > 5
911703d4924Stb word5 = crc5 ^ words[5];
912703d4924Stb #endif
913703d4924Stb #endif
914703d4924Stb #endif
915703d4924Stb #endif
916703d4924Stb #endif
917703d4924Stb words += N;
918703d4924Stb
919703d4924Stb /* Compute and update the CRC for each word. The loop should
920703d4924Stb get unrolled. */
921703d4924Stb crc0 = crc_braid_big_table[0][word0 & 0xff];
922703d4924Stb #if N > 1
923703d4924Stb crc1 = crc_braid_big_table[0][word1 & 0xff];
924703d4924Stb #if N > 2
925703d4924Stb crc2 = crc_braid_big_table[0][word2 & 0xff];
926703d4924Stb #if N > 3
927703d4924Stb crc3 = crc_braid_big_table[0][word3 & 0xff];
928703d4924Stb #if N > 4
929703d4924Stb crc4 = crc_braid_big_table[0][word4 & 0xff];
930703d4924Stb #if N > 5
931703d4924Stb crc5 = crc_braid_big_table[0][word5 & 0xff];
932703d4924Stb #endif
933703d4924Stb #endif
934703d4924Stb #endif
935703d4924Stb #endif
936703d4924Stb #endif
937703d4924Stb for (k = 1; k < W; k++) {
938703d4924Stb crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
939703d4924Stb #if N > 1
940703d4924Stb crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
941703d4924Stb #if N > 2
942703d4924Stb crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
943703d4924Stb #if N > 3
944703d4924Stb crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
945703d4924Stb #if N > 4
946703d4924Stb crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
947703d4924Stb #if N > 5
948703d4924Stb crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
949703d4924Stb #endif
950703d4924Stb #endif
951703d4924Stb #endif
952703d4924Stb #endif
953703d4924Stb #endif
954703d4924Stb }
955703d4924Stb }
956703d4924Stb
957703d4924Stb /*
958703d4924Stb Process the last block, combining the CRCs of the N braids at the
959703d4924Stb same time.
960703d4924Stb */
961703d4924Stb comb = crc_word_big(crc0 ^ words[0]);
962703d4924Stb #if N > 1
963703d4924Stb comb = crc_word_big(crc1 ^ words[1] ^ comb);
964703d4924Stb #if N > 2
965703d4924Stb comb = crc_word_big(crc2 ^ words[2] ^ comb);
966703d4924Stb #if N > 3
967703d4924Stb comb = crc_word_big(crc3 ^ words[3] ^ comb);
968703d4924Stb #if N > 4
969703d4924Stb comb = crc_word_big(crc4 ^ words[4] ^ comb);
970703d4924Stb #if N > 5
971703d4924Stb comb = crc_word_big(crc5 ^ words[5] ^ comb);
972703d4924Stb #endif
973703d4924Stb #endif
974703d4924Stb #endif
975703d4924Stb #endif
976703d4924Stb #endif
977703d4924Stb words += N;
978703d4924Stb crc = byte_swap(comb);
979703d4924Stb }
980703d4924Stb
981703d4924Stb /*
982703d4924Stb Update the pointer to the remaining bytes to process.
983703d4924Stb */
984703d4924Stb buf = (unsigned char const *)words;
985703d4924Stb }
986703d4924Stb
987703d4924Stb #endif /* W */
988703d4924Stb
989703d4924Stb /* Complete the computation of the CRC on any remaining bytes. */
99085c48e79Shenning while (len >= 8) {
9912af503bcStholo len -= 8;
992703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
993703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
994703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
995703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
996703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
997703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
998703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
999703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
10002af503bcStholo }
1001703d4924Stb while (len) {
1002703d4924Stb len--;
1003703d4924Stb crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
10042af503bcStholo }
100585c48e79Shenning
1006703d4924Stb /* Return the CRC, post-conditioned. */
1007703d4924Stb return crc ^ 0xffffffff;
1008703d4924Stb }
1009703d4924Stb
1010703d4924Stb #endif
1011703d4924Stb
101236f395ceStb /* ========================================================================= */
crc32(unsigned long crc,const unsigned char FAR * buf,uInt len)1013a04ea15dStb unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf,
1014a04ea15dStb uInt len) {
101536f395ceStb return crc32_z(crc, buf, len);
101636f395ceStb }
101736f395ceStb
101885c48e79Shenning /* ========================================================================= */
crc32_combine64(uLong crc1,uLong crc2,z_off64_t len2)1019a04ea15dStb uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) {
1020703d4924Stb #ifdef DYNAMIC_CRC_TABLE
1021703d4924Stb once(&made, make_crc_table);
1022703d4924Stb #endif /* DYNAMIC_CRC_TABLE */
1023c5288b00Stb return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff);
1024d76b9bfaSmillert }
102536f395ceStb
102636f395ceStb /* ========================================================================= */
crc32_combine(uLong crc1,uLong crc2,z_off_t len2)1027a04ea15dStb uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) {
10288bda5813Stb return crc32_combine64(crc1, crc2, (z_off64_t)len2);
102936f395ceStb }
103036f395ceStb
1031703d4924Stb /* ========================================================================= */
crc32_combine_gen64(z_off64_t len2)1032a04ea15dStb uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) {
1033703d4924Stb #ifdef DYNAMIC_CRC_TABLE
1034703d4924Stb once(&made, make_crc_table);
1035703d4924Stb #endif /* DYNAMIC_CRC_TABLE */
1036703d4924Stb return x2nmodp(len2, 3);
1037703d4924Stb }
1038703d4924Stb
1039703d4924Stb /* ========================================================================= */
crc32_combine_gen(z_off_t len2)1040a04ea15dStb uLong ZEXPORT crc32_combine_gen(z_off_t len2) {
10418bda5813Stb return crc32_combine_gen64((z_off64_t)len2);
1042703d4924Stb }
1043703d4924Stb
1044703d4924Stb /* ========================================================================= */
crc32_combine_op(uLong crc1,uLong crc2,uLong op)1045a04ea15dStb uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) {
1046c5288b00Stb return multmodp(op, crc1) ^ (crc2 & 0xffffffff);
104736f395ceStb }
1048