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