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