Line | Count | Source |
1 | | /* crc32.c -- compute the CRC-32 of a data stream |
2 | | * Copyright (C) 1995-2026 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 | | #ifdef HAVE_S390X_VX |
36 | | # include "contrib/crc32vx/crc32_vx_hooks.h" |
37 | | #endif |
38 | | |
39 | | /* |
40 | | A CRC of a message is computed on N braids of words in the message, where |
41 | | each word consists of W bytes (4 or 8). If N is 3, for example, then three |
42 | | running sparse CRCs are calculated respectively on each braid, at these |
43 | | indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ... |
44 | | This is done starting at a word boundary, and continues until as many blocks |
45 | | of N * W bytes as are available have been processed. The results are combined |
46 | | into a single CRC at the end. For this code, N must be in the range 1..6 and |
47 | | W must be 4 or 8. The upper limit on N can be increased if desired by adding |
48 | | more #if blocks, extending the patterns apparent in the code. In addition, |
49 | | crc32.h would need to be regenerated, if the maximum N value is increased. |
50 | | |
51 | | N and W are chosen empirically by benchmarking the execution time on a given |
52 | | processor. The choices below were based on testing on Intel Haswell, AMD Zen |
53 | | 4, Apple M1, ARM Cortex-A76, another ARM64 server, IBM POWER10, UltraSPARC |
54 | | T5, LoongArch64, and ARM Cortex-A7 (32-bit) processors, with gcc and clang, |
55 | | and MSVC and clang-cl on Windows, at -O2 and -O3. W=4 was faster than W=8 on |
56 | | all of them for lengths up to 1K, and as fast or nearly so on most of them |
57 | | for long lengths. With 64-bit registers (see Z_REG64 in zutil.h), N=5 was the |
58 | | fastest, or within 10% of the fastest, for lengths of 1K and more. For |
59 | | lengths less than about 320 bytes, N=2 was faster, due to the cost of |
60 | | combining the N braids at the end. So both are used, divided at BRAID_CUT. |
61 | | With 32-bit registers on the Cortex-A7, more than two braids ran out of |
62 | | registers, and N=2 was the fastest at all lengths. Your mileage may vary. |
63 | | */ |
64 | | |
65 | | /* |
66 | | These can be defined when compiling, to change the calculation: |
67 | | |
68 | | Z_REG64 Use the choices for 64-bit registers below. zutil.h |
69 | | defines Z_REG64 when the architecture has 64-bit general |
70 | | registers, unless Z_REG32 is defined. It requires an |
71 | | eight-byte unsigned integer type, Z_U8. |
72 | | Z_REG32 Use the choices for 32-bit registers below, even if the |
73 | | architecture has 64-bit registers. Z_REG64 and Z_REG32 |
74 | | cannot both be defined. |
75 | | Z_TESTW=w Use words of w bytes, 4 or 8, for the braids, in place of |
76 | | DEF_W. 8 requires Z_U8, and 4 is used without it. If w is |
77 | | 0 or empty (-DZ_TESTW=), then the braided calculation is |
78 | | not compiled, and the CRC is computed a byte at a time. |
79 | | Any other value is taken as 4. |
80 | | Z_TESTN=n Use n braids, 1..6, in place of DEF_N. If Z_TESTCUT is not |
81 | | defined, then n braids are used at all lengths. |
82 | | Z_TESTCUT=len Use Z_TESTNLOW braids for lengths less than len, and N |
83 | | braids for lengths of len or more, in place of DEF_CUT and |
84 | | DEF_NLOW. Z_TESTCUT and Z_TESTNLOW must be defined |
85 | | together. |
86 | | Z_TESTNLOW=n The number of braids, 1..6, for lengths less than |
87 | | Z_TESTCUT. It may be more or less than N. |
88 | | DYNAMIC_CRC_TABLE |
89 | | Compute the tables at run time, instead of using those in |
90 | | crc32.h. Only the braid tables for N and NLOW are made. |
91 | | MAKECRCH Compile a program that writes crc32.h, with the braid |
92 | | tables for N from 1 to 6, for both word sizes, and for |
93 | | both little and big-endian. It requires Z_U8. |
94 | | Z_U4, Z_U8 The four and eight-byte unsigned integer types, if they are |
95 | | not found by zconf.h and zutil.h. Without Z_U4, the CRC is |
96 | | computed a byte at a time. Without Z_U8, Z_REG64 is not |
97 | | defined, and W is 4. |
98 | | |
99 | | These are defined by the compiler or the build, and also change the |
100 | | calculation: |
101 | | |
102 | | __ARM_FEATURE_CRC32 |
103 | | On 64-bit ARM, use the CRC32 instructions instead of the |
104 | | braided calculation. The compiler defines this when the |
105 | | target architecture has them, e.g. with |
106 | | -march=armv8-a+crc, -march=armv8.1-a, or -march=native on |
107 | | a machine that has them. |
108 | | HAVE_S390X_VX On s390x, crc32() uses the vector instruction code in |
109 | | contrib/crc32vx. |
110 | | */ |
111 | | |
112 | | /* |
113 | | The default choices, used unless overridden by the defines above: |
114 | | |
115 | | DEF_W The size in bytes of the words that the braids are computed |
116 | | on, 4 or 8. |
117 | | DEF_N The number of braids, 1..6, for lengths of DEF_CUT or more, or |
118 | | for all lengths if there is no cutoff. |
119 | | DEF_CUT The length below which DEF_NLOW braids are used instead of |
120 | | DEF_N. If it is empty or zero, then there is no cutoff, and |
121 | | DEF_N braids are used at all lengths. |
122 | | DEF_NLOW The number of braids, 1..6, for lengths less than DEF_CUT. It |
123 | | must be set if DEF_CUT is set. If it is equal to DEF_N, then |
124 | | there is no cutoff. |
125 | | */ |
126 | | #ifdef Z_REG64 |
127 | | # define DEF_W 4 |
128 | 0 | # define DEF_N 5 |
129 | 0 | # define DEF_CUT 320 |
130 | 0 | # define DEF_NLOW 2 |
131 | | #else |
132 | | # define DEF_W 4 |
133 | | # define DEF_N 2 |
134 | | # define DEF_CUT |
135 | | # define DEF_NLOW |
136 | | #endif |
137 | | |
138 | | /* |
139 | | Define N, and if there is a cutoff, BRAID_CUT and NLOW, from the defaults, |
140 | | or from Z_TESTN, Z_TESTCUT, and Z_TESTNLOW. If there is no cutoff, then NLOW |
141 | | is N. |
142 | | */ |
143 | | #ifdef Z_TESTN |
144 | | # define N Z_TESTN |
145 | | #else |
146 | 0 | # define N DEF_N |
147 | | #endif |
148 | | #if defined(Z_TESTCUT) != defined(Z_TESTNLOW) |
149 | | # error Z_TESTCUT and Z_TESTNLOW must be set together |
150 | | #endif |
151 | | #ifdef Z_TESTCUT |
152 | | # define BRAID_CUT Z_TESTCUT |
153 | | # define NLOW Z_TESTNLOW |
154 | | #elif !defined(Z_TESTN) && DEF_CUT-1 != -1 |
155 | | # if DEF_NLOW-1 == -1 |
156 | | # error DEF_NLOW must be set when DEF_CUT is |
157 | | # define NLOW N |
158 | | # elif DEF_NLOW != DEF_N |
159 | 0 | # define BRAID_CUT DEF_CUT |
160 | 0 | # define NLOW DEF_NLOW |
161 | | # else |
162 | | # define NLOW N |
163 | | # endif |
164 | | #else |
165 | | # define NLOW N |
166 | | #endif |
167 | | #if N < 1 || N > 6 || NLOW < 1 || NLOW > 6 |
168 | | # error N and NLOW must be in 1..6 |
169 | | #endif |
170 | | #if NLOW < N |
171 | 0 | # define NMIN NLOW /* the fewest braids used */ |
172 | | #else |
173 | | # define NMIN N |
174 | | #endif |
175 | | |
176 | | /* The names of the braid tables for n braids, in crc32.h or made by |
177 | | make_crc_table(). */ |
178 | | #define BRAID_TABLE_(n) crc_braid##n##_table |
179 | | #define BRAID_TABLE(n) BRAID_TABLE_(n) |
180 | | #define BRAID_BIG_TABLE_(n) crc_braid##n##_big_table |
181 | | #define BRAID_BIG_TABLE(n) BRAID_BIG_TABLE_(n) |
182 | | |
183 | | /* |
184 | | z_crc_t must be at least 32 bits. z_word_t must be at least as long as |
185 | | z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and |
186 | | that bytes are eight bits. |
187 | | */ |
188 | | |
189 | | /* |
190 | | Define W and the associated z_word_t type. If W is not defined, then a |
191 | | braided calculation is not used, and the associated tables and code are not |
192 | | compiled. |
193 | | */ |
194 | | #ifdef Z_TESTW |
195 | | # if Z_TESTW-1 != -1 |
196 | | # define W Z_TESTW |
197 | | # endif |
198 | | #else |
199 | | # ifdef MAKECRCH |
200 | | # define W 8 /* required for MAKECRCH */ |
201 | | # else |
202 | | # define W DEF_W |
203 | | # endif |
204 | | #endif |
205 | | #ifdef W |
206 | | # if W == 8 && defined(Z_U8) |
207 | | typedef Z_U8 z_word_t; |
208 | | # elif defined(Z_U4) |
209 | | # undef W |
210 | 0 | # define W 4 |
211 | | typedef Z_U4 z_word_t; |
212 | | # else |
213 | | # undef W |
214 | | # endif |
215 | | #endif |
216 | | |
217 | | /* If available, use the ARM processor CRC32 instruction. This works on 64-bit |
218 | | words, and so does not depend on W, the word size for the braids. */ |
219 | | #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && defined(Z_U8) |
220 | | # define ARMCRC32 |
221 | | #endif |
222 | | |
223 | | #if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE)) |
224 | | /* |
225 | | Swap the bytes in a z_word_t to convert between little and big endian. Any |
226 | | self-respecting compiler will optimize this to a single machine byte-swap |
227 | | instruction, if one is available. This assumes that word_t is either 32 bits |
228 | | or 64 bits. |
229 | | */ |
230 | 0 | local z_word_t byte_swap(z_word_t word) { |
231 | | # if W == 8 |
232 | | return |
233 | | (word & 0xff00000000000000) >> 56 | |
234 | | (word & 0xff000000000000) >> 40 | |
235 | | (word & 0xff0000000000) >> 24 | |
236 | | (word & 0xff00000000) >> 8 | |
237 | | (word & 0xff000000) << 8 | |
238 | | (word & 0xff0000) << 24 | |
239 | | (word & 0xff00) << 40 | |
240 | | (word & 0xff) << 56; |
241 | | # else /* W == 4 */ |
242 | 0 | return |
243 | 0 | (word & 0xff000000) >> 24 | |
244 | 0 | (word & 0xff0000) >> 8 | |
245 | 0 | (word & 0xff00) << 8 | |
246 | 0 | (word & 0xff) << 24; |
247 | 0 | # endif |
248 | 0 | } |
249 | | #endif |
250 | | |
251 | | #ifdef DYNAMIC_CRC_TABLE |
252 | | /* ========================================================================= |
253 | | * Table of powers of x for combining CRC-32s, filled in by make_crc_table() |
254 | | * below. |
255 | | */ |
256 | | local z_crc_t FAR x2n_table[32]; |
257 | | #else |
258 | | /* ========================================================================= |
259 | | * Tables for byte-wise and braided CRC-32 calculations, and a table of powers |
260 | | * of x for combining CRC-32s, all made by make_crc_table(). |
261 | | */ |
262 | | # include "crc32.h" |
263 | | #endif |
264 | | |
265 | | /* CRC polynomial. */ |
266 | 0 | #define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */ |
267 | | |
268 | | /* |
269 | | Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, |
270 | | reflected. For speed, this requires that a not be zero. |
271 | | */ |
272 | 0 | local uLong multmodp(uLong a, uLong b) { |
273 | 0 | uLong m, p; |
274 | |
|
275 | 0 | m = (uLong)1 << 31; |
276 | 0 | p = 0; |
277 | 0 | for (;;) { |
278 | 0 | if (a & m) { |
279 | 0 | p ^= b; |
280 | 0 | if ((a & (m - 1)) == 0) |
281 | 0 | break; |
282 | 0 | } |
283 | 0 | m >>= 1; |
284 | 0 | b = b & 1 ? (b >> 1) ^ POLY : b >> 1; |
285 | 0 | } |
286 | 0 | return p; |
287 | 0 | } |
288 | | |
289 | | /* |
290 | | Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been |
291 | | initialized. n must not be negative. |
292 | | */ |
293 | 0 | local uLong x2nmodp(z_off64_t n, unsigned k) { |
294 | 0 | uLong p; |
295 | |
|
296 | 0 | p = (uLong)1 << 31; /* x^0 == 1 */ |
297 | 0 | while (n) { |
298 | 0 | if (n & 1) |
299 | 0 | p = multmodp(x2n_table[k & 31], p); |
300 | 0 | n >>= 1; |
301 | 0 | k++; |
302 | 0 | } |
303 | 0 | return p; |
304 | 0 | } |
305 | | |
306 | | #ifdef DYNAMIC_CRC_TABLE |
307 | | /* ========================================================================= |
308 | | * Build the tables for byte-wise and braided CRC-32 calculations, and a table |
309 | | * of powers of x for combining CRC-32s. |
310 | | */ |
311 | | local z_crc_t FAR crc_table[256]; |
312 | | #ifdef W |
313 | | local z_word_t FAR crc_big_table[256]; |
314 | | local z_crc_t FAR BRAID_TABLE(N)[W][256]; |
315 | | local z_word_t FAR BRAID_BIG_TABLE(N)[W][256]; |
316 | | # if NLOW != N |
317 | | local z_crc_t FAR BRAID_TABLE(NLOW)[W][256]; |
318 | | local z_word_t FAR BRAID_BIG_TABLE(NLOW)[W][256]; |
319 | | # endif |
320 | | local void braid(z_crc_t [][256], z_word_t [][256], int, int); |
321 | | #endif |
322 | | #ifdef MAKECRCH |
323 | | local void write_table(FILE *, const z_crc_t FAR *, int); |
324 | | local void write_table32hi(FILE *, const z_word_t FAR *, int); |
325 | | local void write_table64(FILE *, const z_word_t FAR *, int); |
326 | | #endif /* MAKECRCH */ |
327 | | |
328 | | /* State for once(). */ |
329 | | local z_once_t made = Z_ONCE_INIT; |
330 | | |
331 | | /* |
332 | | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
333 | | 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. |
334 | | |
335 | | Polynomials over GF(2) are represented in binary, one bit per coefficient, |
336 | | with the lowest powers in the most significant bit. Then adding polynomials |
337 | | is just exclusive-or, and multiplying a polynomial by x is a right shift by |
338 | | one. If we call the above polynomial p, and represent a byte as the |
339 | | polynomial q, also with the lowest power in the most significant bit (so the |
340 | | byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p, |
341 | | where a mod b means the remainder after dividing a by b. |
342 | | |
343 | | This calculation is done using the shift-register method of multiplying and |
344 | | taking the remainder. The register is initialized to zero, and for each |
345 | | incoming bit, x^32 is added mod p to the register if the bit is a one (where |
346 | | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x |
347 | | (which is shifting right by one and adding x^32 mod p if the bit shifted out |
348 | | is a one). We start with the highest power (least significant bit) of q and |
349 | | repeat for all eight bits of q. |
350 | | |
351 | | The table is simply the CRC of all possible eight bit values. This is all the |
352 | | information needed to generate CRCs on data a byte at a time for all |
353 | | combinations of CRC register values and incoming bytes. |
354 | | */ |
355 | | |
356 | | local void make_crc_table(void) { |
357 | | unsigned i, j, n; |
358 | | z_crc_t p; |
359 | | |
360 | | /* initialize the CRC of bytes tables */ |
361 | | for (i = 0; i < 256; i++) { |
362 | | p = i; |
363 | | for (j = 0; j < 8; j++) |
364 | | p = p & 1 ? (p >> 1) ^ POLY : p >> 1; |
365 | | crc_table[i] = p; |
366 | | #ifdef W |
367 | | crc_big_table[i] = byte_swap(p); |
368 | | #endif |
369 | | } |
370 | | |
371 | | /* initialize the x^2^n mod p(x) table */ |
372 | | p = (z_crc_t)1 << 30; /* x^1 */ |
373 | | x2n_table[0] = p; |
374 | | for (n = 1; n < 32; n++) |
375 | | x2n_table[n] = p = (z_crc_t)multmodp(p, p); |
376 | | |
377 | | #ifdef W |
378 | | /* initialize the braiding tables -- needs x2n_table[] */ |
379 | | braid(BRAID_TABLE(N), BRAID_BIG_TABLE(N), N, W); |
380 | | # if NLOW != N |
381 | | braid(BRAID_TABLE(NLOW), BRAID_BIG_TABLE(NLOW), NLOW, W); |
382 | | # endif |
383 | | #endif |
384 | | |
385 | | #ifdef MAKECRCH |
386 | | { |
387 | | /* |
388 | | The crc32.h header file contains tables for both 32-bit and 64-bit |
389 | | z_word_t's, and so requires a 64-bit type be available. In that case, |
390 | | z_word_t must be defined to be 64-bits. This code then also generates |
391 | | and writes out the tables for the case that z_word_t is 32 bits. |
392 | | */ |
393 | | #if !defined(W) || W != 8 |
394 | | # error Need a 64-bit integer type in order to generate crc32.h. |
395 | | #endif |
396 | | FILE *out; |
397 | | int k, n; |
398 | | z_crc_t ltl[8][256]; |
399 | | z_word_t big[8][256]; |
400 | | |
401 | | out = fopen("crc32.h", "w"); |
402 | | if (out == NULL) return; |
403 | | |
404 | | /* write out little-endian CRC table to crc32.h */ |
405 | | fprintf(out, |
406 | | "/* crc32.h -- tables for rapid CRC calculation\n" |
407 | | " * Generated automatically by crc32.c\n */\n" |
408 | | "\n" |
409 | | "local const z_crc_t FAR crc_table[] = {\n" |
410 | | " "); |
411 | | write_table(out, crc_table, 256); |
412 | | fprintf(out, |
413 | | "};\n"); |
414 | | |
415 | | /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */ |
416 | | fprintf(out, |
417 | | "\n" |
418 | | "#ifdef W\n" |
419 | | "\n" |
420 | | "#if W == 8\n" |
421 | | "\n" |
422 | | "local const z_word_t FAR crc_big_table[] = {\n" |
423 | | " "); |
424 | | write_table64(out, crc_big_table, 256); |
425 | | fprintf(out, |
426 | | "};\n"); |
427 | | |
428 | | /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */ |
429 | | fprintf(out, |
430 | | "\n" |
431 | | "#else /* W == 4 */\n" |
432 | | "\n" |
433 | | "local const z_word_t FAR crc_big_table[] = {\n" |
434 | | " "); |
435 | | write_table32hi(out, crc_big_table, 256); |
436 | | fprintf(out, |
437 | | "};\n" |
438 | | "\n" |
439 | | "#endif\n"); |
440 | | |
441 | | /* write out braid tables for each value of N, and of NLOW */ |
442 | | for (n = 1; n <= 6; n++) { |
443 | | fprintf(out, |
444 | | "\n" |
445 | | "#if N == %d || NLOW == %d\n", n, n); |
446 | | |
447 | | /* compute braid tables for this N and 64-bit word_t */ |
448 | | braid(ltl, big, n, 8); |
449 | | |
450 | | /* write out braid tables for 64-bit z_word_t to crc32.h */ |
451 | | fprintf(out, |
452 | | "\n" |
453 | | "#if W == 8\n" |
454 | | "\n" |
455 | | "local const z_crc_t FAR crc_braid%d_table[][256] = {\n", n); |
456 | | for (k = 0; k < 8; k++) { |
457 | | fprintf(out, " {"); |
458 | | write_table(out, ltl[k], 256); |
459 | | fprintf(out, "}%s", k < 7 ? ",\n" : ""); |
460 | | } |
461 | | fprintf(out, |
462 | | "};\n" |
463 | | "\n" |
464 | | "local const z_word_t FAR crc_braid%d_big_table[][256] = {\n", n); |
465 | | for (k = 0; k < 8; k++) { |
466 | | fprintf(out, " {"); |
467 | | write_table64(out, big[k], 256); |
468 | | fprintf(out, "}%s", k < 7 ? ",\n" : ""); |
469 | | } |
470 | | fprintf(out, |
471 | | "};\n"); |
472 | | |
473 | | /* compute braid tables for this N and 32-bit word_t */ |
474 | | braid(ltl, big, n, 4); |
475 | | |
476 | | /* write out braid tables for 32-bit z_word_t to crc32.h */ |
477 | | fprintf(out, |
478 | | "\n" |
479 | | "#else /* W == 4 */\n" |
480 | | "\n" |
481 | | "local const z_crc_t FAR crc_braid%d_table[][256] = {\n", n); |
482 | | for (k = 0; k < 4; k++) { |
483 | | fprintf(out, " {"); |
484 | | write_table(out, ltl[k], 256); |
485 | | fprintf(out, "}%s", k < 3 ? ",\n" : ""); |
486 | | } |
487 | | fprintf(out, |
488 | | "};\n" |
489 | | "\n" |
490 | | "local const z_word_t FAR crc_braid%d_big_table[][256] = {\n", n); |
491 | | for (k = 0; k < 4; k++) { |
492 | | fprintf(out, " {"); |
493 | | write_table32hi(out, big[k], 256); |
494 | | fprintf(out, "}%s", k < 3 ? ",\n" : ""); |
495 | | } |
496 | | fprintf(out, |
497 | | "};\n" |
498 | | "\n" |
499 | | "#endif\n" |
500 | | "\n" |
501 | | "#endif\n"); |
502 | | } |
503 | | fprintf(out, |
504 | | "\n" |
505 | | "#endif\n"); |
506 | | |
507 | | /* write out zeros operator table to crc32.h */ |
508 | | fprintf(out, |
509 | | "\n" |
510 | | "local const z_crc_t FAR x2n_table[] = {\n" |
511 | | " "); |
512 | | write_table(out, x2n_table, 32); |
513 | | fprintf(out, |
514 | | "};\n"); |
515 | | fclose(out); |
516 | | } |
517 | | #endif /* MAKECRCH */ |
518 | | } |
519 | | |
520 | | #ifdef MAKECRCH |
521 | | |
522 | | /* |
523 | | Write the 32-bit values in table[0..k-1] to out, five per line in |
524 | | hexadecimal separated by commas. |
525 | | */ |
526 | | local void write_table(FILE *out, const z_crc_t FAR *table, int k) { |
527 | | int n; |
528 | | |
529 | | for (n = 0; n < k; n++) |
530 | | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", |
531 | | (unsigned long)(table[n]), |
532 | | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); |
533 | | } |
534 | | |
535 | | /* |
536 | | Write the high 32-bits of each value in table[0..k-1] to out, five per line |
537 | | in hexadecimal separated by commas. |
538 | | */ |
539 | | local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) { |
540 | | int n; |
541 | | |
542 | | for (n = 0; n < k; n++) |
543 | | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", |
544 | | (unsigned long)(table[n] >> 32), |
545 | | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); |
546 | | } |
547 | | |
548 | | /* |
549 | | Write the 64-bit values in table[0..k-1] to out, three per line in |
550 | | hexadecimal separated by commas. This assumes that if there is a 64-bit |
551 | | type, then there is also a long long integer type, and it is at least 64 |
552 | | bits. If not, then the type cast and format string can be adjusted |
553 | | accordingly. |
554 | | */ |
555 | | local void write_table64(FILE *out, const z_word_t FAR *table, int k) { |
556 | | int n; |
557 | | |
558 | | for (n = 0; n < k; n++) |
559 | | fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ", |
560 | | (unsigned long long)(table[n]), |
561 | | n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", ")); |
562 | | } |
563 | | |
564 | | /* Actually do the deed. */ |
565 | | int main(void) { |
566 | | make_crc_table(); |
567 | | return 0; |
568 | | } |
569 | | |
570 | | #endif /* MAKECRCH */ |
571 | | |
572 | | #ifdef W |
573 | | /* |
574 | | Generate the little and big-endian braid tables for the given n and z_word_t |
575 | | size w. Each array must have room for w blocks of 256 elements. |
576 | | */ |
577 | | local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) { |
578 | | int k; |
579 | | z_crc_t i, p, q; |
580 | | for (k = 0; k < w; k++) { |
581 | | p = (z_crc_t)x2nmodp((n * w + 3 - k) << 3, 0); |
582 | | ltl[k][0] = 0; |
583 | | big[w - 1 - k][0] = 0; |
584 | | for (i = 1; i < 256; i++) { |
585 | | ltl[k][i] = q = (z_crc_t)multmodp(i << 24, p); |
586 | | big[w - 1 - k][i] = byte_swap(q); |
587 | | } |
588 | | } |
589 | | } |
590 | | #endif |
591 | | |
592 | | #endif /* DYNAMIC_CRC_TABLE */ |
593 | | |
594 | | /* ========================================================================= |
595 | | * This function can be used by asm versions of crc32(), and to force the |
596 | | * generation of the CRC tables in a threaded application. |
597 | | */ |
598 | 0 | const z_crc_t FAR * ZEXPORT get_crc_table(void) { |
599 | | #ifdef DYNAMIC_CRC_TABLE |
600 | | z_once(&made, make_crc_table); |
601 | | #endif /* DYNAMIC_CRC_TABLE */ |
602 | 0 | return (const z_crc_t FAR *)crc_table; |
603 | 0 | } |
604 | | |
605 | | /* ========================================================================= |
606 | | * Use ARM machine instructions if available. This will compute the CRC about |
607 | | * ten times faster than the braided calculation. This code does not check for |
608 | | * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will |
609 | | * only be defined if the compilation specifies an ARM processor architecture |
610 | | * that has the instructions. For example, compiling with -march=armv8.1-a or |
611 | | * -march=armv8-a+crc, or -march=native if the compile machine has the crc32 |
612 | | * instructions. |
613 | | */ |
614 | | #ifdef ARMCRC32 |
615 | | |
616 | | /* |
617 | | Constants empirically determined to maximize speed. These values are from |
618 | | measurements on a Cortex-A57. Your mileage may vary. |
619 | | */ |
620 | | #define Z_BATCH 3990 /* number of words in a batch */ |
621 | | #define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */ |
622 | | #define Z_BATCH_MIN 800 /* fewest words in a final batch */ |
623 | | |
624 | | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
625 | | uLong val; |
626 | | Z_U8 crc1, crc2; |
627 | | const Z_U8 *word; |
628 | | Z_U8 val0, val1, val2; |
629 | | z_size_t last, last2, i; |
630 | | z_size_t num; |
631 | | |
632 | | /* Return initial CRC, if requested. */ |
633 | | if (buf == Z_NULL) return 0; |
634 | | |
635 | | #ifdef DYNAMIC_CRC_TABLE |
636 | | z_once(&made, make_crc_table); |
637 | | #endif /* DYNAMIC_CRC_TABLE */ |
638 | | |
639 | | /* Pre-condition the CRC */ |
640 | | crc = (~crc) & 0xffffffff; |
641 | | |
642 | | /* Compute the CRC up to a word boundary. */ |
643 | | while (len && ((z_size_t)buf & 7) != 0) { |
644 | | len--; |
645 | | val = *buf++; |
646 | | __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val)); |
647 | | } |
648 | | |
649 | | /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */ |
650 | | word = (Z_U8 const *)buf; |
651 | | num = len >> 3; |
652 | | len &= 7; |
653 | | |
654 | | /* Do three interleaved CRCs to realize the throughput of one crc32x |
655 | | instruction per cycle. Each CRC is calculated on Z_BATCH words. The |
656 | | three CRCs are combined into a single CRC after each set of batches. */ |
657 | | while (num >= 3 * Z_BATCH) { |
658 | | crc1 = 0; |
659 | | crc2 = 0; |
660 | | for (i = 0; i < Z_BATCH; i++) { |
661 | | val0 = word[i]; |
662 | | val1 = word[i + Z_BATCH]; |
663 | | val2 = word[i + 2 * Z_BATCH]; |
664 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); |
665 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1)); |
666 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2)); |
667 | | } |
668 | | word += 3 * Z_BATCH; |
669 | | num -= 3 * Z_BATCH; |
670 | | crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1; |
671 | | crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2; |
672 | | } |
673 | | |
674 | | /* Do one last smaller batch with the remaining words, if there are enough |
675 | | to pay for the combination of CRCs. */ |
676 | | last = num / 3; |
677 | | if (last >= Z_BATCH_MIN) { |
678 | | last2 = last << 1; |
679 | | crc1 = 0; |
680 | | crc2 = 0; |
681 | | for (i = 0; i < last; i++) { |
682 | | val0 = word[i]; |
683 | | val1 = word[i + last]; |
684 | | val2 = word[i + last2]; |
685 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); |
686 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1)); |
687 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2)); |
688 | | } |
689 | | word += 3 * last; |
690 | | num -= 3 * last; |
691 | | val = x2nmodp((int)last, 6); |
692 | | crc = multmodp(val, crc) ^ crc1; |
693 | | crc = multmodp(val, crc) ^ crc2; |
694 | | } |
695 | | |
696 | | /* Compute the CRC on any remaining words. */ |
697 | | for (i = 0; i < num; i++) { |
698 | | val0 = word[i]; |
699 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0)); |
700 | | } |
701 | | word += num; |
702 | | |
703 | | /* Complete the CRC on any remaining bytes. */ |
704 | | buf = (const unsigned char FAR *)word; |
705 | | while (len) { |
706 | | len--; |
707 | | val = *buf++; |
708 | | __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val)); |
709 | | } |
710 | | |
711 | | /* Return the CRC, post-conditioned. */ |
712 | | return crc ^ 0xffffffff; |
713 | | } |
714 | | |
715 | | #else |
716 | | |
717 | | #ifdef W |
718 | | |
719 | | /* |
720 | | Return the CRC of the W bytes in the word_t data, taking the |
721 | | least-significant byte of the word as the first byte of data, without any pre |
722 | | or post conditioning. This is used to combine the CRCs of each braid. |
723 | | */ |
724 | 0 | local z_crc_t crc_word(z_word_t data) { |
725 | 0 | int k; |
726 | 0 | for (k = 0; k < W; k++) |
727 | 0 | data = (data >> 8) ^ crc_table[data & 0xff]; |
728 | 0 | return (z_crc_t)data; |
729 | 0 | } |
730 | | |
731 | 0 | local z_word_t crc_word_big(z_word_t data) { |
732 | 0 | int k; |
733 | 0 | for (k = 0; k < W; k++) |
734 | 0 | data = (data << 8) ^ |
735 | 0 | crc_big_table[(data >> ((W - 1) << 3)) & 0xff]; |
736 | 0 | return data; |
737 | 0 | } |
738 | | |
739 | | /* |
740 | | The CRC of one braid's word w, using braid table t: the table entries for |
741 | | its W bytes, combined in pairs. This is written out rather than looped over |
742 | | the bytes, since gcc at -O2 does not unroll such a loop, which leaves the |
743 | | shift and the table offset to be computed for each byte, and is up to twice |
744 | | as slow. |
745 | | */ |
746 | 0 | #define CRCB(t, w, k) t[k][((w) >> ((k) << 3)) & 0xff] |
747 | | #if W == 8 |
748 | | # define CRCWORD(t, w) (((CRCB(t, w, 0) ^ CRCB(t, w, 1)) ^ \ |
749 | | (CRCB(t, w, 2) ^ CRCB(t, w, 3))) ^ \ |
750 | | ((CRCB(t, w, 4) ^ CRCB(t, w, 5)) ^ \ |
751 | | (CRCB(t, w, 6) ^ CRCB(t, w, 7)))) |
752 | | #else |
753 | 0 | # define CRCWORD(t, w) ((CRCB(t, w, 0) ^ CRCB(t, w, 1)) ^ \ |
754 | 0 | (CRCB(t, w, 2) ^ CRCB(t, w, 3))) |
755 | | #endif |
756 | | |
757 | | #ifdef BRAID_CUT |
758 | | /* |
759 | | Return the CRC of buf[0..len-1] starting with crc, computed on NLOW braids. |
760 | | crc is pre-conditioned, and the result is not post-conditioned. crc32_z() |
761 | | uses this for lengths less than BRAID_CUT, and N braids for longer lengths. |
762 | | The N braids are in crc32_z() itself, since compiled there, clang does not |
763 | | spill a braid's CRC to the stack in the loop on x86-64 for N=5, as it did |
764 | | when the N braids were here. A compiler can be expected to inline this, so |
765 | | that short lengths do not incur a function call. |
766 | | */ |
767 | | local uLong crc32_braid(uLong crc, const unsigned char FAR *buf, |
768 | 0 | z_size_t len) { |
769 | | /* If provided enough bytes, do a braided CRC calculation. */ |
770 | 0 | if (len >= NLOW * W + W - 1) { |
771 | 0 | z_size_t blks; |
772 | 0 | z_word_t const *words; |
773 | 0 | unsigned endian; |
774 | | |
775 | | /* Compute the CRC up to a z_word_t boundary. */ |
776 | 0 | while (len && ((z_size_t)buf & (W - 1)) != 0) { |
777 | 0 | len--; |
778 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
779 | 0 | } |
780 | | |
781 | | /* Compute the CRC on as many NLOW z_word_t blocks as are available. */ |
782 | 0 | blks = len / (NLOW * W); |
783 | 0 | len -= blks * NLOW * W; |
784 | 0 | words = (z_word_t const *)buf; |
785 | | |
786 | | /* Do endian check at execution time instead of compile time, since ARM |
787 | | processors can change the endianness at execution time. If the |
788 | | compiler knows what the endianness will be, it can optimize out the |
789 | | check and the unused branch. */ |
790 | 0 | endian = 1; |
791 | 0 | if (*(unsigned char *)&endian) { |
792 | | /* Little endian. */ |
793 | |
|
794 | 0 | z_crc_t crc0; |
795 | 0 | z_word_t word0; |
796 | 0 | #if NLOW > 1 |
797 | 0 | z_crc_t crc1; |
798 | 0 | z_word_t word1; |
799 | | #if NLOW > 2 |
800 | | z_crc_t crc2; |
801 | | z_word_t word2; |
802 | | #if NLOW > 3 |
803 | | z_crc_t crc3; |
804 | | z_word_t word3; |
805 | | #if NLOW > 4 |
806 | | z_crc_t crc4; |
807 | | z_word_t word4; |
808 | | #if NLOW > 5 |
809 | | z_crc_t crc5; |
810 | | z_word_t word5; |
811 | | #endif |
812 | | #endif |
813 | | #endif |
814 | | #endif |
815 | 0 | #endif |
816 | | |
817 | | /* Initialize the CRC for each braid. */ |
818 | 0 | crc0 = (z_crc_t)crc; |
819 | 0 | #if NLOW > 1 |
820 | 0 | crc1 = 0; |
821 | | #if NLOW > 2 |
822 | | crc2 = 0; |
823 | | #if NLOW > 3 |
824 | | crc3 = 0; |
825 | | #if NLOW > 4 |
826 | | crc4 = 0; |
827 | | #if NLOW > 5 |
828 | | crc5 = 0; |
829 | | #endif |
830 | | #endif |
831 | | #endif |
832 | | #endif |
833 | 0 | #endif |
834 | | |
835 | | /* |
836 | | Process the first blks-1 blocks, computing the CRCs on each braid |
837 | | independently. |
838 | | */ |
839 | 0 | while (--blks) { |
840 | | /* Load the word for each braid into registers. */ |
841 | 0 | word0 = crc0 ^ words[0]; |
842 | 0 | #if NLOW > 1 |
843 | 0 | word1 = crc1 ^ words[1]; |
844 | | #if NLOW > 2 |
845 | | word2 = crc2 ^ words[2]; |
846 | | #if NLOW > 3 |
847 | | word3 = crc3 ^ words[3]; |
848 | | #if NLOW > 4 |
849 | | word4 = crc4 ^ words[4]; |
850 | | #if NLOW > 5 |
851 | | word5 = crc5 ^ words[5]; |
852 | | #endif |
853 | | #endif |
854 | | #endif |
855 | | #endif |
856 | 0 | #endif |
857 | 0 | words += NLOW; |
858 | | |
859 | | /* Compute and update the CRC for each word. */ |
860 | 0 | crc0 = CRCWORD(BRAID_TABLE(NLOW), word0); |
861 | 0 | #if NLOW > 1 |
862 | 0 | crc1 = CRCWORD(BRAID_TABLE(NLOW), word1); |
863 | | #if NLOW > 2 |
864 | | crc2 = CRCWORD(BRAID_TABLE(NLOW), word2); |
865 | | #if NLOW > 3 |
866 | | crc3 = CRCWORD(BRAID_TABLE(NLOW), word3); |
867 | | #if NLOW > 4 |
868 | | crc4 = CRCWORD(BRAID_TABLE(NLOW), word4); |
869 | | #if NLOW > 5 |
870 | | crc5 = CRCWORD(BRAID_TABLE(NLOW), word5); |
871 | | #endif |
872 | | #endif |
873 | | #endif |
874 | | #endif |
875 | 0 | #endif |
876 | 0 | } |
877 | | |
878 | | /* |
879 | | Process the last block, combining the CRCs of the NLOW braids at |
880 | | the same time. |
881 | | */ |
882 | 0 | crc = crc_word(crc0 ^ words[0]); |
883 | 0 | #if NLOW > 1 |
884 | 0 | crc = crc_word(crc1 ^ words[1] ^ crc); |
885 | | #if NLOW > 2 |
886 | | crc = crc_word(crc2 ^ words[2] ^ crc); |
887 | | #if NLOW > 3 |
888 | | crc = crc_word(crc3 ^ words[3] ^ crc); |
889 | | #if NLOW > 4 |
890 | | crc = crc_word(crc4 ^ words[4] ^ crc); |
891 | | #if NLOW > 5 |
892 | | crc = crc_word(crc5 ^ words[5] ^ crc); |
893 | | #endif |
894 | | #endif |
895 | | #endif |
896 | | #endif |
897 | 0 | #endif |
898 | 0 | words += NLOW; |
899 | 0 | } |
900 | 0 | else { |
901 | | /* Big endian. */ |
902 | |
|
903 | 0 | z_word_t crc0, word0, comb; |
904 | 0 | #if NLOW > 1 |
905 | 0 | z_word_t crc1, word1; |
906 | | #if NLOW > 2 |
907 | | z_word_t crc2, word2; |
908 | | #if NLOW > 3 |
909 | | z_word_t crc3, word3; |
910 | | #if NLOW > 4 |
911 | | z_word_t crc4, word4; |
912 | | #if NLOW > 5 |
913 | | z_word_t crc5, word5; |
914 | | #endif |
915 | | #endif |
916 | | #endif |
917 | | #endif |
918 | 0 | #endif |
919 | | |
920 | | /* Initialize the CRC for each braid. */ |
921 | 0 | crc0 = byte_swap(crc); |
922 | 0 | #if NLOW > 1 |
923 | 0 | crc1 = 0; |
924 | | #if NLOW > 2 |
925 | | crc2 = 0; |
926 | | #if NLOW > 3 |
927 | | crc3 = 0; |
928 | | #if NLOW > 4 |
929 | | crc4 = 0; |
930 | | #if NLOW > 5 |
931 | | crc5 = 0; |
932 | | #endif |
933 | | #endif |
934 | | #endif |
935 | | #endif |
936 | 0 | #endif |
937 | | |
938 | | /* |
939 | | Process the first blks-1 blocks, computing the CRCs on each braid |
940 | | independently. |
941 | | */ |
942 | 0 | while (--blks) { |
943 | | /* Load the word for each braid into registers. */ |
944 | 0 | word0 = crc0 ^ words[0]; |
945 | 0 | #if NLOW > 1 |
946 | 0 | word1 = crc1 ^ words[1]; |
947 | | #if NLOW > 2 |
948 | | word2 = crc2 ^ words[2]; |
949 | | #if NLOW > 3 |
950 | | word3 = crc3 ^ words[3]; |
951 | | #if NLOW > 4 |
952 | | word4 = crc4 ^ words[4]; |
953 | | #if NLOW > 5 |
954 | | word5 = crc5 ^ words[5]; |
955 | | #endif |
956 | | #endif |
957 | | #endif |
958 | | #endif |
959 | 0 | #endif |
960 | 0 | words += NLOW; |
961 | | |
962 | | /* Compute and update the CRC for each word. */ |
963 | 0 | crc0 = CRCWORD(BRAID_BIG_TABLE(NLOW), word0); |
964 | 0 | #if NLOW > 1 |
965 | 0 | crc1 = CRCWORD(BRAID_BIG_TABLE(NLOW), word1); |
966 | | #if NLOW > 2 |
967 | | crc2 = CRCWORD(BRAID_BIG_TABLE(NLOW), word2); |
968 | | #if NLOW > 3 |
969 | | crc3 = CRCWORD(BRAID_BIG_TABLE(NLOW), word3); |
970 | | #if NLOW > 4 |
971 | | crc4 = CRCWORD(BRAID_BIG_TABLE(NLOW), word4); |
972 | | #if NLOW > 5 |
973 | | crc5 = CRCWORD(BRAID_BIG_TABLE(NLOW), word5); |
974 | | #endif |
975 | | #endif |
976 | | #endif |
977 | | #endif |
978 | 0 | #endif |
979 | 0 | } |
980 | | |
981 | | /* |
982 | | Process the last block, combining the CRCs of the NLOW braids at |
983 | | the same time. |
984 | | */ |
985 | 0 | comb = crc_word_big(crc0 ^ words[0]); |
986 | 0 | #if NLOW > 1 |
987 | 0 | comb = crc_word_big(crc1 ^ words[1] ^ comb); |
988 | | #if NLOW > 2 |
989 | | comb = crc_word_big(crc2 ^ words[2] ^ comb); |
990 | | #if NLOW > 3 |
991 | | comb = crc_word_big(crc3 ^ words[3] ^ comb); |
992 | | #if NLOW > 4 |
993 | | comb = crc_word_big(crc4 ^ words[4] ^ comb); |
994 | | #if NLOW > 5 |
995 | | comb = crc_word_big(crc5 ^ words[5] ^ comb); |
996 | | #endif |
997 | | #endif |
998 | | #endif |
999 | | #endif |
1000 | 0 | #endif |
1001 | 0 | words += NLOW; |
1002 | 0 | crc = byte_swap(comb); |
1003 | 0 | } |
1004 | | |
1005 | | /* |
1006 | | Update the pointer to the remaining bytes to process. |
1007 | | */ |
1008 | 0 | buf = (unsigned char const *)words; |
1009 | 0 | } |
1010 | | |
1011 | | /* Complete the computation of the CRC on any remaining bytes. */ |
1012 | 0 | while (len >= 8) { |
1013 | 0 | len -= 8; |
1014 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1015 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1016 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1017 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1018 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1019 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1020 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1021 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1022 | 0 | } |
1023 | 0 | while (len) { |
1024 | 0 | len--; |
1025 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1026 | 0 | } |
1027 | |
|
1028 | 0 | return crc; |
1029 | 0 | } |
1030 | | #endif |
1031 | | |
1032 | | #endif |
1033 | | |
1034 | | /* ========================================================================= */ |
1035 | 0 | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
1036 | | /* Return initial CRC, if requested. */ |
1037 | 0 | if (buf == Z_NULL) return 0; |
1038 | | |
1039 | | #ifdef DYNAMIC_CRC_TABLE |
1040 | | z_once(&made, make_crc_table); |
1041 | | #endif /* DYNAMIC_CRC_TABLE */ |
1042 | | |
1043 | | /* Pre-condition the CRC */ |
1044 | 0 | crc = (~crc) & 0xffffffff; |
1045 | |
|
1046 | 0 | #ifdef W |
1047 | | |
1048 | | /* If provided enough bytes, do a braided CRC calculation. */ |
1049 | 0 | if (len >= NMIN * W + W - 1) { |
1050 | 0 | z_size_t blks; |
1051 | 0 | z_word_t const *words; |
1052 | 0 | unsigned endian; |
1053 | |
|
1054 | 0 | #ifdef BRAID_CUT |
1055 | | /* For shorter lengths, use NLOW braids. */ |
1056 | 0 | if (len < BRAID_CUT || len < N * W + W - 1) |
1057 | 0 | return crc32_braid(crc, buf, len) ^ 0xffffffff; |
1058 | 0 | #endif |
1059 | | |
1060 | | /* Compute the CRC up to a z_word_t boundary. */ |
1061 | 0 | while (len && ((z_size_t)buf & (W - 1)) != 0) { |
1062 | 0 | len--; |
1063 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1064 | 0 | } |
1065 | | |
1066 | | /* Compute the CRC on as many N z_word_t blocks as are available. */ |
1067 | 0 | blks = len / (N * W); |
1068 | 0 | len -= blks * N * W; |
1069 | 0 | words = (z_word_t const *)buf; |
1070 | | |
1071 | | /* Do endian check at execution time instead of compile time, since ARM |
1072 | | processors can change the endianness at execution time. If the |
1073 | | compiler knows what the endianness will be, it can optimize out the |
1074 | | check and the unused branch. */ |
1075 | 0 | endian = 1; |
1076 | 0 | if (*(unsigned char *)&endian) { |
1077 | | /* Little endian. */ |
1078 | |
|
1079 | 0 | z_crc_t crc0; |
1080 | 0 | z_word_t word0; |
1081 | 0 | #if N > 1 |
1082 | 0 | z_crc_t crc1; |
1083 | 0 | z_word_t word1; |
1084 | 0 | #if N > 2 |
1085 | 0 | z_crc_t crc2; |
1086 | 0 | z_word_t word2; |
1087 | 0 | #if N > 3 |
1088 | 0 | z_crc_t crc3; |
1089 | 0 | z_word_t word3; |
1090 | 0 | #if N > 4 |
1091 | 0 | z_crc_t crc4; |
1092 | 0 | z_word_t word4; |
1093 | | #if N > 5 |
1094 | | z_crc_t crc5; |
1095 | | z_word_t word5; |
1096 | | #endif |
1097 | 0 | #endif |
1098 | 0 | #endif |
1099 | 0 | #endif |
1100 | 0 | #endif |
1101 | | |
1102 | | /* Initialize the CRC for each braid. */ |
1103 | 0 | crc0 = (z_crc_t)crc; |
1104 | 0 | #if N > 1 |
1105 | 0 | crc1 = 0; |
1106 | 0 | #if N > 2 |
1107 | 0 | crc2 = 0; |
1108 | 0 | #if N > 3 |
1109 | 0 | crc3 = 0; |
1110 | 0 | #if N > 4 |
1111 | 0 | crc4 = 0; |
1112 | | #if N > 5 |
1113 | | crc5 = 0; |
1114 | | #endif |
1115 | 0 | #endif |
1116 | 0 | #endif |
1117 | 0 | #endif |
1118 | 0 | #endif |
1119 | | |
1120 | | /* |
1121 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1122 | | independently. |
1123 | | */ |
1124 | 0 | while (--blks) { |
1125 | | /* Load the word for each braid into registers. */ |
1126 | 0 | word0 = crc0 ^ words[0]; |
1127 | 0 | #if N > 1 |
1128 | 0 | word1 = crc1 ^ words[1]; |
1129 | 0 | #if N > 2 |
1130 | 0 | word2 = crc2 ^ words[2]; |
1131 | 0 | #if N > 3 |
1132 | 0 | word3 = crc3 ^ words[3]; |
1133 | 0 | #if N > 4 |
1134 | 0 | word4 = crc4 ^ words[4]; |
1135 | | #if N > 5 |
1136 | | word5 = crc5 ^ words[5]; |
1137 | | #endif |
1138 | 0 | #endif |
1139 | 0 | #endif |
1140 | 0 | #endif |
1141 | 0 | #endif |
1142 | 0 | words += N; |
1143 | | |
1144 | | /* Compute and update the CRC for each word. */ |
1145 | 0 | crc0 = CRCWORD(BRAID_TABLE(N), word0); |
1146 | 0 | #if N > 1 |
1147 | 0 | crc1 = CRCWORD(BRAID_TABLE(N), word1); |
1148 | 0 | #if N > 2 |
1149 | 0 | crc2 = CRCWORD(BRAID_TABLE(N), word2); |
1150 | 0 | #if N > 3 |
1151 | 0 | crc3 = CRCWORD(BRAID_TABLE(N), word3); |
1152 | 0 | #if N > 4 |
1153 | 0 | crc4 = CRCWORD(BRAID_TABLE(N), word4); |
1154 | | #if N > 5 |
1155 | | crc5 = CRCWORD(BRAID_TABLE(N), word5); |
1156 | | #endif |
1157 | 0 | #endif |
1158 | 0 | #endif |
1159 | 0 | #endif |
1160 | 0 | #endif |
1161 | 0 | } |
1162 | | |
1163 | | /* |
1164 | | Process the last block, combining the CRCs of the N braids at the |
1165 | | same time. |
1166 | | */ |
1167 | 0 | crc = crc_word(crc0 ^ words[0]); |
1168 | 0 | #if N > 1 |
1169 | 0 | crc = crc_word(crc1 ^ words[1] ^ crc); |
1170 | 0 | #if N > 2 |
1171 | 0 | crc = crc_word(crc2 ^ words[2] ^ crc); |
1172 | 0 | #if N > 3 |
1173 | 0 | crc = crc_word(crc3 ^ words[3] ^ crc); |
1174 | 0 | #if N > 4 |
1175 | 0 | crc = crc_word(crc4 ^ words[4] ^ crc); |
1176 | | #if N > 5 |
1177 | | crc = crc_word(crc5 ^ words[5] ^ crc); |
1178 | | #endif |
1179 | 0 | #endif |
1180 | 0 | #endif |
1181 | 0 | #endif |
1182 | 0 | #endif |
1183 | 0 | words += N; |
1184 | 0 | } |
1185 | 0 | else { |
1186 | | /* Big endian. */ |
1187 | |
|
1188 | 0 | z_word_t crc0, word0, comb; |
1189 | 0 | #if N > 1 |
1190 | 0 | z_word_t crc1, word1; |
1191 | 0 | #if N > 2 |
1192 | 0 | z_word_t crc2, word2; |
1193 | 0 | #if N > 3 |
1194 | 0 | z_word_t crc3, word3; |
1195 | 0 | #if N > 4 |
1196 | 0 | z_word_t crc4, word4; |
1197 | | #if N > 5 |
1198 | | z_word_t crc5, word5; |
1199 | | #endif |
1200 | 0 | #endif |
1201 | 0 | #endif |
1202 | 0 | #endif |
1203 | 0 | #endif |
1204 | | |
1205 | | /* Initialize the CRC for each braid. */ |
1206 | 0 | crc0 = byte_swap(crc); |
1207 | 0 | #if N > 1 |
1208 | 0 | crc1 = 0; |
1209 | 0 | #if N > 2 |
1210 | 0 | crc2 = 0; |
1211 | 0 | #if N > 3 |
1212 | 0 | crc3 = 0; |
1213 | 0 | #if N > 4 |
1214 | 0 | crc4 = 0; |
1215 | | #if N > 5 |
1216 | | crc5 = 0; |
1217 | | #endif |
1218 | 0 | #endif |
1219 | 0 | #endif |
1220 | 0 | #endif |
1221 | 0 | #endif |
1222 | | |
1223 | | /* |
1224 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1225 | | independently. |
1226 | | */ |
1227 | 0 | while (--blks) { |
1228 | | /* Load the word for each braid into registers. */ |
1229 | 0 | word0 = crc0 ^ words[0]; |
1230 | 0 | #if N > 1 |
1231 | 0 | word1 = crc1 ^ words[1]; |
1232 | 0 | #if N > 2 |
1233 | 0 | word2 = crc2 ^ words[2]; |
1234 | 0 | #if N > 3 |
1235 | 0 | word3 = crc3 ^ words[3]; |
1236 | 0 | #if N > 4 |
1237 | 0 | word4 = crc4 ^ words[4]; |
1238 | | #if N > 5 |
1239 | | word5 = crc5 ^ words[5]; |
1240 | | #endif |
1241 | 0 | #endif |
1242 | 0 | #endif |
1243 | 0 | #endif |
1244 | 0 | #endif |
1245 | 0 | words += N; |
1246 | | |
1247 | | /* Compute and update the CRC for each word. */ |
1248 | 0 | crc0 = CRCWORD(BRAID_BIG_TABLE(N), word0); |
1249 | 0 | #if N > 1 |
1250 | 0 | crc1 = CRCWORD(BRAID_BIG_TABLE(N), word1); |
1251 | 0 | #if N > 2 |
1252 | 0 | crc2 = CRCWORD(BRAID_BIG_TABLE(N), word2); |
1253 | 0 | #if N > 3 |
1254 | 0 | crc3 = CRCWORD(BRAID_BIG_TABLE(N), word3); |
1255 | 0 | #if N > 4 |
1256 | 0 | crc4 = CRCWORD(BRAID_BIG_TABLE(N), word4); |
1257 | | #if N > 5 |
1258 | | crc5 = CRCWORD(BRAID_BIG_TABLE(N), word5); |
1259 | | #endif |
1260 | 0 | #endif |
1261 | 0 | #endif |
1262 | 0 | #endif |
1263 | 0 | #endif |
1264 | 0 | } |
1265 | | |
1266 | | /* |
1267 | | Process the last block, combining the CRCs of the N braids at the |
1268 | | same time. |
1269 | | */ |
1270 | 0 | comb = crc_word_big(crc0 ^ words[0]); |
1271 | 0 | #if N > 1 |
1272 | 0 | comb = crc_word_big(crc1 ^ words[1] ^ comb); |
1273 | 0 | #if N > 2 |
1274 | 0 | comb = crc_word_big(crc2 ^ words[2] ^ comb); |
1275 | 0 | #if N > 3 |
1276 | 0 | comb = crc_word_big(crc3 ^ words[3] ^ comb); |
1277 | 0 | #if N > 4 |
1278 | 0 | comb = crc_word_big(crc4 ^ words[4] ^ comb); |
1279 | | #if N > 5 |
1280 | | comb = crc_word_big(crc5 ^ words[5] ^ comb); |
1281 | | #endif |
1282 | 0 | #endif |
1283 | 0 | #endif |
1284 | 0 | #endif |
1285 | 0 | #endif |
1286 | 0 | words += N; |
1287 | 0 | crc = byte_swap(comb); |
1288 | 0 | } |
1289 | | |
1290 | | /* |
1291 | | Update the pointer to the remaining bytes to process. |
1292 | | */ |
1293 | 0 | buf = (unsigned char const *)words; |
1294 | 0 | } |
1295 | | |
1296 | 0 | #endif /* W */ |
1297 | | |
1298 | | /* Complete the computation of the CRC on any remaining bytes. */ |
1299 | 0 | while (len >= 8) { |
1300 | 0 | len -= 8; |
1301 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1302 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1303 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1304 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1305 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1306 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1307 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1308 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1309 | 0 | } |
1310 | 0 | while (len) { |
1311 | 0 | len--; |
1312 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1313 | 0 | } |
1314 | | |
1315 | | /* Return the CRC, post-conditioned. */ |
1316 | 0 | return crc ^ 0xffffffff; |
1317 | 0 | } |
1318 | | |
1319 | | #endif |
1320 | | |
1321 | | /* ========================================================================= */ |
1322 | 0 | uLong ZEXPORT crc32(uLong crc, const unsigned char FAR *buf, uInt len) { |
1323 | | #ifdef HAVE_S390X_VX |
1324 | | return crc32_z_hook(crc, buf, len); |
1325 | | #endif |
1326 | 0 | return crc32_z(crc, buf, len); |
1327 | 0 | } |
1328 | | |
1329 | | /* ========================================================================= */ |
1330 | 0 | uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) { |
1331 | 0 | if (len2 < 0) |
1332 | 0 | return 0; |
1333 | | #ifdef DYNAMIC_CRC_TABLE |
1334 | | z_once(&made, make_crc_table); |
1335 | | #endif /* DYNAMIC_CRC_TABLE */ |
1336 | 0 | return x2nmodp(len2, 3); |
1337 | 0 | } |
1338 | | |
1339 | | /* ========================================================================= */ |
1340 | 0 | uLong ZEXPORT crc32_combine_gen(z_off_t len2) { |
1341 | 0 | return crc32_combine_gen64((z_off64_t)len2); |
1342 | 0 | } |
1343 | | |
1344 | | /* ========================================================================= */ |
1345 | 0 | uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) { |
1346 | 0 | if (op == 0) |
1347 | 0 | return 0; |
1348 | 0 | return multmodp(op, crc1 & 0xffffffff) ^ (crc2 & 0xffffffff); |
1349 | 0 | } |
1350 | | |
1351 | | /* ========================================================================= */ |
1352 | 0 | uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) { |
1353 | 0 | return crc32_combine_op(crc1, crc2, crc32_combine_gen64(len2)); |
1354 | 0 | } |
1355 | | |
1356 | | /* ========================================================================= */ |
1357 | 0 | uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) { |
1358 | 0 | return crc32_combine64(crc1, crc2, (z_off64_t)len2); |
1359 | 0 | } |