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 | | /* |
218 | | Determine if ARM CRC-32 hardware instructions are to be used, and how. |
219 | | |
220 | | If the compiler targets a processor that has the instructions, then it |
221 | | defines __ARM_FEATURE_CRC32 and they are used unconditionally, with only the |
222 | | code to use the hardware instructions compiled. Otherwise, if the compiler |
223 | | can produce CRC-32 instructions and if there is a way to ask the operating |
224 | | system if the instructions are present, then both the hardware and software |
225 | | CRC-32 calculations are compiled, and which to use is decided at run time. |
226 | | Otherwise the CRC-32 is calculated in software. If the target is big-endian, |
227 | | then the calculation is in software regardless. |
228 | | |
229 | | Define ARMCRC32 to compile the code to use the hardware instructions. Define |
230 | | ARMCRC32_WORD as the number of bytes to be processed by the word CRC-32 |
231 | | instruction. Define ARMCRC32_RT to decide at run time whether or not to use |
232 | | the hardware instructions. |
233 | | |
234 | | Define ARMCRC32_ASK to ask the operating system if the feature is present, in |
235 | | which case define one of ARMCRC32_AUXV, ARMCRC32_ELFAUX, ARMCRC32_SYSCTL, |
236 | | ARMCRC32_NETBSD, or ARMCRC32_WIN to use, respectively, getauxval() on Linux, |
237 | | elf_aux_info() on FreeBSD and OpenBSD, sysctlbyname() on Apple systems, the |
238 | | ID registers from sysctlbyname() on NetBSD, or IsProcessorFeaturePresent() on |
239 | | Windows. |
240 | | |
241 | | The build can define Z_NO_ARMCRC32_RT to disallow the run-time check. |
242 | | */ |
243 | | #if (defined(__aarch64__) || defined(_M_ARM64)) && defined(Z_U8) |
244 | | # define ARMCRC32_WORD 8 /* eight bytes per instruction */ |
245 | | #elif defined(__arm__) && defined(Z_U4) |
246 | | # define ARMCRC32_WORD 4 /* four bytes per instruction */ |
247 | | #endif |
248 | | #if defined(ARMCRC32_WORD) && (defined(__ARMEB__) || \ |
249 | | defined(__AARCH64EB__) || \ |
250 | | defined(__ARM_BIG_ENDIAN)) |
251 | | # undef ARMCRC32_WORD |
252 | | #endif |
253 | | |
254 | | #ifdef ARMCRC32_WORD |
255 | | # ifndef Z_NO_ARMCRC32_RT |
256 | | # if defined(_MSC_VER) && !defined(__clang__) |
257 | | # if ARMCRC32_WORD == 8 |
258 | | # define ARMCRC32_CC |
259 | | # endif |
260 | | # elif defined(__clang__) |
261 | | # define ARMCRC32_CC |
262 | | # elif defined(__GNUC__) && __GNUC__ >= (ARMCRC32_WORD == 8 ? 6 : 8) |
263 | | # define ARMCRC32_CC |
264 | | # endif |
265 | | # if defined(__linux__) |
266 | | # if defined(__has_include) |
267 | | # if __has_include(<sys/auxv.h>) |
268 | | # define ARMCRC32_AUXV /* getauxval() */ |
269 | | # endif |
270 | | # elif defined(__GLIBC__) && (__GLIBC__ > 2 || \ |
271 | | (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
272 | | # define ARMCRC32_AUXV |
273 | | # endif |
274 | | # if defined(__ANDROID__) && defined(__ANDROID_API__) && \ |
275 | | __ANDROID_API__ < 18 |
276 | | # undef ARMCRC32_AUXV |
277 | | # endif |
278 | | # ifdef ARMCRC32_AUXV |
279 | | # define ARMCRC32_OS |
280 | | # endif |
281 | | # elif defined(__FreeBSD__) && __FreeBSD__ >= 12 |
282 | | # define ARMCRC32_ELFAUX /* elf_aux_info() */ |
283 | | # define ARMCRC32_OS |
284 | | # elif defined(__OpenBSD__) && defined(__has_include) |
285 | | # if __has_include(<sys/auxv.h>) /* OpenBSD 7.6 and later */ |
286 | | # define ARMCRC32_ELFAUX /* elf_aux_info() */ |
287 | | # define ARMCRC32_OS |
288 | | # endif |
289 | | # elif defined(__NetBSD__) && ARMCRC32_WORD == 8 |
290 | | # define ARMCRC32_NETBSD /* sysctlbyname() for ID registers */ |
291 | | # define ARMCRC32_OS |
292 | | # elif defined(__APPLE__) |
293 | | # define ARMCRC32_SYSCTL /* sysctlbyname() */ |
294 | | # define ARMCRC32_OS |
295 | | # elif defined(_WIN32) |
296 | | # define ARMCRC32_WIN /* IsProcessorFeaturePresent() */ |
297 | | # define ARMCRC32_OS |
298 | | # endif |
299 | | # if defined(ARMCRC32_CC) && defined(ARMCRC32_OS) |
300 | | # define ARMCRC32_CAN_ASK |
301 | | # endif |
302 | | # endif |
303 | | # ifdef __ARM_FEATURE_CRC32 |
304 | | # define ARMCRC32 /* just hardware instructions code */ |
305 | | # elif defined(ARMCRC32_CAN_ASK) |
306 | | # define ARMCRC32 /* hardware and software code, ask */ |
307 | | # define ARMCRC32_RT /* the operating system at run time */ |
308 | | # endif |
309 | | /* Define ARMPMULL to use the PMULL 64-bit carry-less multiply instruction, |
310 | | along with the CRC32 instruction, to combine the CRCs of interleaved |
311 | | strings. Define ARMPMULL_RT to check at run time if the instruction is |
312 | | available. The builder can define Z_NO_ARMPMULL to prevent the use of the |
313 | | instruction even if present. */ |
314 | | # if defined(ARMCRC32) && ARMCRC32_WORD == 8 && !defined(Z_NO_ARMPMULL) && \ |
315 | | !defined(Z_NO_ARMCRC32_BATCH) |
316 | | # if defined(__ARM_FEATURE_AES) || defined(__ARM_FEATURE_CRYPTO) |
317 | | # define ARMPMULL |
318 | | # elif defined(ARMCRC32_CAN_ASK) |
319 | | # define ARMPMULL |
320 | | # define ARMPMULL_RT |
321 | | # endif |
322 | | # endif |
323 | | # if defined(ARMCRC32_RT) || defined(ARMPMULL_RT) |
324 | | # define ARMCRC32_ASK /* ask the operating system */ |
325 | | # endif |
326 | | # if defined(ARMCRC32) && !defined(Z_NO_ARMCRC32_BATCH) && \ |
327 | | (!defined(ARMPMULL) || defined(ARMPMULL_RT) || \ |
328 | | defined(Z_TEST_ARMPMULL_ABSENT)) |
329 | | # define ARMCRC32_SOFT /* the software multiply is used */ |
330 | | # endif |
331 | | #endif |
332 | | |
333 | | #if defined(W) && (!defined(ARMCRC32) || defined(ARMCRC32_RT) || \ |
334 | | defined(DYNAMIC_CRC_TABLE)) |
335 | | /* |
336 | | Swap the bytes in a z_word_t to convert between little and big endian. Any |
337 | | self-respecting compiler will optimize this to a single machine byte-swap |
338 | | instruction, if one is available. This assumes that word_t is either 32 bits |
339 | | or 64 bits. |
340 | | */ |
341 | 0 | local z_word_t byte_swap(z_word_t word) { |
342 | | # if W == 8 |
343 | | return |
344 | | (word & 0xff00000000000000) >> 56 | |
345 | | (word & 0xff000000000000) >> 40 | |
346 | | (word & 0xff0000000000) >> 24 | |
347 | | (word & 0xff00000000) >> 8 | |
348 | | (word & 0xff000000) << 8 | |
349 | | (word & 0xff0000) << 24 | |
350 | | (word & 0xff00) << 40 | |
351 | | (word & 0xff) << 56; |
352 | | # else /* W == 4 */ |
353 | 0 | return |
354 | 0 | (word & 0xff000000) >> 24 | |
355 | 0 | (word & 0xff0000) >> 8 | |
356 | 0 | (word & 0xff00) << 8 | |
357 | 0 | (word & 0xff) << 24; |
358 | 0 | # endif |
359 | 0 | } |
360 | | #endif |
361 | | |
362 | | #if (defined(ARMCRC32) && !defined(Z_NO_ARMCRC32_BATCH)) || defined(MAKECRCH) |
363 | | /* |
364 | | batch[] is the count of bytes to process in each of the three interleaved |
365 | | streams. Stepping down through these counts provides the benefits of |
366 | | interleaving across whatever length is provided, while avoiding on-the-fly |
367 | | calculations of multipliers. Instead, constant multipliers in batch_t[] are |
368 | | used, which correspond to the fixed counts. The batch[] counts are each a bit |
369 | | less than a power of two, so as to spread out the use of new cache lines, |
370 | | with the amounts less (172 and 48) determined empirically on a Cortex-A72. |
371 | | The batch_t[] multipliers are calculated from these batch lengths. If these |
372 | | counts are changed, then MAKECRCH must be run to rebuild crc32.h. |
373 | | */ |
374 | | local const unsigned batch[] = {32592, 16208, 8016, 3920, 2000, 976, 464}; |
375 | | # define BATCHES (sizeof(batch) / sizeof(batch[0])) |
376 | | #endif |
377 | | |
378 | | #ifdef DYNAMIC_CRC_TABLE |
379 | | /* ========================================================================= |
380 | | * Table of powers of x for combining CRC-32s, and tables for combining the |
381 | | * CRCs of the ARM batches, filled in by make_crc_table() below. |
382 | | */ |
383 | | local z_crc_t FAR x2n_table[32]; |
384 | | # if defined(ARMCRC32_SOFT) || defined(MAKECRCH) |
385 | | local Z_U8 FAR batch_t[BATCHES][2][16]; |
386 | | # endif |
387 | | # if defined(ARMPMULL) || defined(MAKECRCH) |
388 | | local z_crc_t FAR batch_k[BATCHES][2]; |
389 | | # endif |
390 | | #else |
391 | | /* ========================================================================= |
392 | | * Tables for byte-wise and braided CRC-32 calculations, a table of powers of x |
393 | | * for combining CRC-32s, and tables for combining the CRCs of the ARM batches, |
394 | | * all made by make_crc_table(). |
395 | | */ |
396 | | # include "crc32.h" |
397 | | #endif |
398 | | |
399 | | /* CRC polynomial. */ |
400 | 0 | #define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */ |
401 | | |
402 | | /* |
403 | | Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial, |
404 | | reflected. For speed, this requires that a not be zero. |
405 | | */ |
406 | 0 | local uLong multmodp(uLong a, uLong b) { |
407 | 0 | uLong m, p; |
408 | |
|
409 | 0 | m = (uLong)1 << 31; |
410 | 0 | p = 0; |
411 | 0 | for (;;) { |
412 | 0 | if (a & m) { |
413 | 0 | p ^= b; |
414 | 0 | if ((a & (m - 1)) == 0) |
415 | 0 | break; |
416 | 0 | } |
417 | 0 | m >>= 1; |
418 | 0 | b = b & 1 ? (b >> 1) ^ POLY : b >> 1; |
419 | 0 | } |
420 | 0 | return p; |
421 | 0 | } |
422 | | |
423 | | /* |
424 | | Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been |
425 | | initialized. n must not be negative. |
426 | | */ |
427 | 0 | local uLong x2nmodp(z_off64_t n, unsigned k) { |
428 | 0 | uLong p; |
429 | |
|
430 | 0 | p = (uLong)1 << 31; /* x^0 == 1 */ |
431 | 0 | while (n) { |
432 | 0 | if (n & 1) |
433 | 0 | p = multmodp(x2n_table[k & 31], p); |
434 | 0 | n >>= 1; |
435 | 0 | k++; |
436 | 0 | } |
437 | 0 | return p; |
438 | 0 | } |
439 | | |
440 | | #ifdef DYNAMIC_CRC_TABLE |
441 | | /* ========================================================================= |
442 | | * Build the tables for byte-wise and braided CRC-32 calculations, and a table |
443 | | * of powers of x for combining CRC-32s. |
444 | | */ |
445 | | local z_crc_t FAR crc_table[256]; |
446 | | #ifdef W |
447 | | local z_word_t FAR crc_big_table[256]; |
448 | | local z_crc_t FAR BRAID_TABLE(N)[W][256]; |
449 | | local z_word_t FAR BRAID_BIG_TABLE(N)[W][256]; |
450 | | # if NLOW != N |
451 | | local z_crc_t FAR BRAID_TABLE(NLOW)[W][256]; |
452 | | local z_word_t FAR BRAID_BIG_TABLE(NLOW)[W][256]; |
453 | | # endif |
454 | | local void braid(z_crc_t [][256], z_word_t [][256], int, int); |
455 | | #endif |
456 | | #ifdef MAKECRCH |
457 | | local void write_table(FILE *, const z_crc_t FAR *, int); |
458 | | local void write_table32hi(FILE *, const z_word_t FAR *, int); |
459 | | local void write_table64(FILE *, const z_word_t FAR *, int); |
460 | | #endif /* MAKECRCH */ |
461 | | |
462 | | #if defined(ARMCRC32_SOFT) || defined(MAKECRCH) |
463 | | /* Set t[0..15] to the carry-less products of 0..15 and k. t is a table entry |
464 | | in batch_t[] that represents the multiplier k. It is used in place of k to |
465 | | speed up the multiplication. */ |
466 | | local void batch_products(Z_U8 FAR *t, z_crc_t k) { |
467 | | unsigned i, j; |
468 | | |
469 | | for (i = 0; i < 16; i++) { |
470 | | t[i] = 0; |
471 | | for (j = 0; j < 4; j++) |
472 | | if ((i >> j) & 1) |
473 | | t[i] ^= (Z_U8)k << j; |
474 | | } |
475 | | } |
476 | | #endif |
477 | | |
478 | | /* State for once(). */ |
479 | | local z_once_t made = Z_ONCE_INIT; |
480 | | |
481 | | /* |
482 | | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
483 | | 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. |
484 | | |
485 | | Polynomials over GF(2) are represented in binary, one bit per coefficient, |
486 | | with the lowest powers in the most significant bit. Then adding polynomials |
487 | | is just exclusive-or, and multiplying a polynomial by x is a right shift by |
488 | | one. If we call the above polynomial p, and represent a byte as the |
489 | | polynomial q, also with the lowest power in the most significant bit (so the |
490 | | byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p, |
491 | | where a mod b means the remainder after dividing a by b. |
492 | | |
493 | | This calculation is done using the shift-register method of multiplying and |
494 | | taking the remainder. The register is initialized to zero, and for each |
495 | | incoming bit, x^32 is added mod p to the register if the bit is a one (where |
496 | | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x |
497 | | (which is shifting right by one and adding x^32 mod p if the bit shifted out |
498 | | is a one). We start with the highest power (least significant bit) of q and |
499 | | repeat for all eight bits of q. |
500 | | |
501 | | The table is simply the CRC of all possible eight bit values. This is all the |
502 | | information needed to generate CRCs on data a byte at a time for all |
503 | | combinations of CRC register values and incoming bytes. |
504 | | */ |
505 | | |
506 | | local void make_crc_table(void) { |
507 | | unsigned i, j, n; |
508 | | z_crc_t p; |
509 | | |
510 | | /* initialize the CRC of bytes tables */ |
511 | | for (i = 0; i < 256; i++) { |
512 | | p = i; |
513 | | for (j = 0; j < 8; j++) |
514 | | p = p & 1 ? (p >> 1) ^ POLY : p >> 1; |
515 | | crc_table[i] = p; |
516 | | #ifdef W |
517 | | crc_big_table[i] = byte_swap(p); |
518 | | #endif |
519 | | } |
520 | | |
521 | | /* initialize the x^2^n mod p(x) table */ |
522 | | p = (z_crc_t)1 << 30; /* x^1 */ |
523 | | x2n_table[0] = p; |
524 | | for (n = 1; n < 32; n++) |
525 | | x2n_table[n] = p = (z_crc_t)multmodp(p, p); |
526 | | |
527 | | #if defined(ARMCRC32_SOFT) || defined(ARMPMULL) || defined(MAKECRCH) |
528 | | /* initialize the tables for combining the CRCs of the ARM batches */ |
529 | | for (n = 0; n < BATCHES; n++) { |
530 | | z_crc_t k1 = (z_crc_t)x2nmodp(((z_off64_t)batch[n] << 3) - 33, 0); |
531 | | z_crc_t k2 = (z_crc_t)x2nmodp(((z_off64_t)batch[n] << 4) - 33, 0); |
532 | | # if defined(ARMPMULL) || defined(MAKECRCH) |
533 | | batch_k[n][0] = k1; |
534 | | batch_k[n][1] = k2; |
535 | | # endif |
536 | | # if defined(ARMCRC32_SOFT) || defined(MAKECRCH) |
537 | | batch_products(batch_t[n][0], k1); |
538 | | batch_products(batch_t[n][1], k2); |
539 | | # endif |
540 | | } |
541 | | #endif |
542 | | |
543 | | #ifdef W |
544 | | /* initialize the braiding tables -- needs x2n_table[] */ |
545 | | braid(BRAID_TABLE(N), BRAID_BIG_TABLE(N), N, W); |
546 | | # if NLOW != N |
547 | | braid(BRAID_TABLE(NLOW), BRAID_BIG_TABLE(NLOW), NLOW, W); |
548 | | # endif |
549 | | #endif |
550 | | |
551 | | #ifdef MAKECRCH |
552 | | { |
553 | | /* |
554 | | The crc32.h header file contains tables for both 32-bit and 64-bit |
555 | | z_word_t's, and so requires a 64-bit type be available. In that case, |
556 | | z_word_t must be defined to be 64-bits. This code then also generates |
557 | | and writes out the tables for the case that z_word_t is 32 bits. |
558 | | */ |
559 | | #if !defined(W) || W != 8 |
560 | | # error Need a 64-bit integer type in order to generate crc32.h. |
561 | | #endif |
562 | | FILE *out; |
563 | | int k, n; |
564 | | z_crc_t ltl[8][256]; |
565 | | z_word_t big[8][256]; |
566 | | |
567 | | out = fopen("crc32.h", "w"); |
568 | | if (out == NULL) return; |
569 | | |
570 | | /* write out little-endian CRC table to crc32.h */ |
571 | | fprintf(out, |
572 | | "/* crc32.h -- tables for rapid CRC calculation\n" |
573 | | " * Generated automatically by crc32.c\n */\n" |
574 | | "\n" |
575 | | "local const z_crc_t FAR crc_table[] = {\n" |
576 | | " "); |
577 | | write_table(out, crc_table, 256); |
578 | | fprintf(out, |
579 | | "};\n"); |
580 | | |
581 | | /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */ |
582 | | fprintf(out, |
583 | | "\n" |
584 | | "#ifdef W\n" |
585 | | "\n" |
586 | | "#if W == 8\n" |
587 | | "\n" |
588 | | "local const z_word_t FAR crc_big_table[] = {\n" |
589 | | " "); |
590 | | write_table64(out, crc_big_table, 256); |
591 | | fprintf(out, |
592 | | "};\n"); |
593 | | |
594 | | /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */ |
595 | | fprintf(out, |
596 | | "\n" |
597 | | "#else /* W == 4 */\n" |
598 | | "\n" |
599 | | "local const z_word_t FAR crc_big_table[] = {\n" |
600 | | " "); |
601 | | write_table32hi(out, crc_big_table, 256); |
602 | | fprintf(out, |
603 | | "};\n" |
604 | | "\n" |
605 | | "#endif\n"); |
606 | | |
607 | | /* write out braid tables for each value of N, and of NLOW */ |
608 | | for (n = 1; n <= 6; n++) { |
609 | | fprintf(out, |
610 | | "\n" |
611 | | "#if N == %d || NLOW == %d\n", n, n); |
612 | | |
613 | | /* compute braid tables for this N and 64-bit word_t */ |
614 | | braid(ltl, big, n, 8); |
615 | | |
616 | | /* write out braid tables for 64-bit z_word_t to crc32.h */ |
617 | | fprintf(out, |
618 | | "\n" |
619 | | "#if W == 8\n" |
620 | | "\n" |
621 | | "local const z_crc_t FAR crc_braid%d_table[][256] = {\n", n); |
622 | | for (k = 0; k < 8; k++) { |
623 | | fprintf(out, " {"); |
624 | | write_table(out, ltl[k], 256); |
625 | | fprintf(out, "}%s", k < 7 ? ",\n" : ""); |
626 | | } |
627 | | fprintf(out, |
628 | | "};\n" |
629 | | "\n" |
630 | | "local const z_word_t FAR crc_braid%d_big_table[][256] = {\n", n); |
631 | | for (k = 0; k < 8; k++) { |
632 | | fprintf(out, " {"); |
633 | | write_table64(out, big[k], 256); |
634 | | fprintf(out, "}%s", k < 7 ? ",\n" : ""); |
635 | | } |
636 | | fprintf(out, |
637 | | "};\n"); |
638 | | |
639 | | /* compute braid tables for this N and 32-bit word_t */ |
640 | | braid(ltl, big, n, 4); |
641 | | |
642 | | /* write out braid tables for 32-bit z_word_t to crc32.h */ |
643 | | fprintf(out, |
644 | | "\n" |
645 | | "#else /* W == 4 */\n" |
646 | | "\n" |
647 | | "local const z_crc_t FAR crc_braid%d_table[][256] = {\n", n); |
648 | | for (k = 0; k < 4; k++) { |
649 | | fprintf(out, " {"); |
650 | | write_table(out, ltl[k], 256); |
651 | | fprintf(out, "}%s", k < 3 ? ",\n" : ""); |
652 | | } |
653 | | fprintf(out, |
654 | | "};\n" |
655 | | "\n" |
656 | | "local const z_word_t FAR crc_braid%d_big_table[][256] = {\n", n); |
657 | | for (k = 0; k < 4; k++) { |
658 | | fprintf(out, " {"); |
659 | | write_table32hi(out, big[k], 256); |
660 | | fprintf(out, "}%s", k < 3 ? ",\n" : ""); |
661 | | } |
662 | | fprintf(out, |
663 | | "};\n" |
664 | | "\n" |
665 | | "#endif\n" |
666 | | "\n" |
667 | | "#endif\n"); |
668 | | } |
669 | | fprintf(out, |
670 | | "\n" |
671 | | "#endif\n"); |
672 | | |
673 | | /* write out zeros operator table to crc32.h */ |
674 | | fprintf(out, |
675 | | "\n" |
676 | | "local const z_crc_t FAR x2n_table[] = {\n" |
677 | | " "); |
678 | | write_table(out, x2n_table, 32); |
679 | | fprintf(out, |
680 | | "};\n"); |
681 | | |
682 | | /* write out the tables for combining the CRCs of the ARM batches */ |
683 | | fprintf(out, |
684 | | "\n" |
685 | | "#ifdef ARMCRC32_SOFT\n" |
686 | | "\n" |
687 | | "local const Z_U8 FAR batch_t[][2][16] = {\n"); |
688 | | for (n = 0; n < (int)BATCHES; n++) { |
689 | | fprintf(out, " {{"); |
690 | | write_table64(out, batch_t[n][0], 16); |
691 | | fprintf(out, "},\n {"); |
692 | | write_table64(out, batch_t[n][1], 16); |
693 | | fprintf(out, "}}%s", n < (int)BATCHES - 1 ? ",\n" : ""); |
694 | | } |
695 | | fprintf(out, |
696 | | "};\n" |
697 | | "\n" |
698 | | "#endif\n"); |
699 | | fprintf(out, |
700 | | "\n" |
701 | | "#ifdef ARMPMULL\n" |
702 | | "\n" |
703 | | "local const z_crc_t FAR batch_k[][2] = {\n"); |
704 | | for (n = 0; n < (int)BATCHES; n++) |
705 | | fprintf(out, " {0x%08lx, 0x%08lx}%s", |
706 | | (unsigned long)batch_k[n][0], (unsigned long)batch_k[n][1], |
707 | | n < (int)BATCHES - 1 ? ",\n" : "\n"); |
708 | | fprintf(out, |
709 | | "};\n" |
710 | | "\n" |
711 | | "#endif\n"); |
712 | | fclose(out); |
713 | | } |
714 | | #endif /* MAKECRCH */ |
715 | | } |
716 | | |
717 | | #ifdef MAKECRCH |
718 | | |
719 | | /* |
720 | | Write the 32-bit values in table[0..k-1] to out, five per line in |
721 | | hexadecimal separated by commas. |
722 | | */ |
723 | | local void write_table(FILE *out, const z_crc_t FAR *table, int k) { |
724 | | int n; |
725 | | |
726 | | for (n = 0; n < k; n++) |
727 | | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", |
728 | | (unsigned long)(table[n]), |
729 | | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); |
730 | | } |
731 | | |
732 | | /* |
733 | | Write the high 32-bits of each value in table[0..k-1] to out, five per line |
734 | | in hexadecimal separated by commas. |
735 | | */ |
736 | | local void write_table32hi(FILE *out, const z_word_t FAR *table, int k) { |
737 | | int n; |
738 | | |
739 | | for (n = 0; n < k; n++) |
740 | | fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ", |
741 | | (unsigned long)(table[n] >> 32), |
742 | | n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", ")); |
743 | | } |
744 | | |
745 | | /* |
746 | | Write the 64-bit values in table[0..k-1] to out, three per line in |
747 | | hexadecimal separated by commas. This assumes that if there is a 64-bit |
748 | | type, then there is also a long long integer type, and it is at least 64 |
749 | | bits. If not, then the type cast and format string can be adjusted |
750 | | accordingly. |
751 | | */ |
752 | | local void write_table64(FILE *out, const z_word_t FAR *table, int k) { |
753 | | int n; |
754 | | |
755 | | for (n = 0; n < k; n++) |
756 | | fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ", |
757 | | (unsigned long long)(table[n]), |
758 | | n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", ")); |
759 | | } |
760 | | |
761 | | /* Actually do the deed. */ |
762 | | int main(void) { |
763 | | make_crc_table(); |
764 | | return 0; |
765 | | } |
766 | | |
767 | | #endif /* MAKECRCH */ |
768 | | |
769 | | #ifdef W |
770 | | /* |
771 | | Generate the little and big-endian braid tables for the given n and z_word_t |
772 | | size w. Each array must have room for w blocks of 256 elements. |
773 | | */ |
774 | | local void braid(z_crc_t ltl[][256], z_word_t big[][256], int n, int w) { |
775 | | int k; |
776 | | z_crc_t i, p, q; |
777 | | for (k = 0; k < w; k++) { |
778 | | p = (z_crc_t)x2nmodp((n * w + 3 - k) << 3, 0); |
779 | | ltl[k][0] = 0; |
780 | | big[w - 1 - k][0] = 0; |
781 | | for (i = 1; i < 256; i++) { |
782 | | ltl[k][i] = q = (z_crc_t)multmodp(i << 24, p); |
783 | | big[w - 1 - k][i] = byte_swap(q); |
784 | | } |
785 | | } |
786 | | } |
787 | | #endif |
788 | | |
789 | | #endif /* DYNAMIC_CRC_TABLE */ |
790 | | |
791 | | /* ========================================================================= |
792 | | * This function can be used by asm versions of crc32(), and to force the |
793 | | * generation of the CRC tables in a threaded application. |
794 | | */ |
795 | 0 | const z_crc_t FAR * ZEXPORT get_crc_table(void) { |
796 | | #ifdef DYNAMIC_CRC_TABLE |
797 | | z_once(&made, make_crc_table); |
798 | | #endif /* DYNAMIC_CRC_TABLE */ |
799 | 0 | return (const z_crc_t FAR *)crc_table; |
800 | 0 | } |
801 | | |
802 | | #ifdef ARMCRC32_ASK |
803 | | /* |
804 | | Ask the operating system whether the processor has the CRC32 instructions, as |
805 | | well as the 64-bit carry-less multiply, PMULL. The capability bits are fixed |
806 | | by the Linux ABI (FreeBSD uses the same), so they are defined here, in case |
807 | | the headers are too old to have them. |
808 | | */ |
809 | | #define ARM_HAVE_CRC32 1 |
810 | | #define ARM_HAVE_PMULL 2 |
811 | | #if defined(ARMCRC32_AUXV) || defined(ARMCRC32_ELFAUX) |
812 | | # include <sys/auxv.h> |
813 | | # ifndef AT_HWCAP |
814 | | # define AT_HWCAP 16 |
815 | | # endif |
816 | | # ifndef AT_HWCAP2 |
817 | | # define AT_HWCAP2 26 |
818 | | # endif |
819 | | # if ARMCRC32_WORD == 8 |
820 | | # define ARM_AT AT_HWCAP |
821 | | # define ARM_BIT_CRC32 7 /* HWCAP_CRC32 */ |
822 | | # define ARM_BIT_PMULL 4 /* HWCAP_PMULL */ |
823 | | # else |
824 | | # define ARM_AT AT_HWCAP2 |
825 | | # define ARM_BIT_CRC32 4 /* HWCAP2_CRC32 */ |
826 | | # define ARM_BIT_PMULL 1 /* HWCAP2_PMULL */ |
827 | | # endif |
828 | | #elif defined(ARMCRC32_SYSCTL) |
829 | | # include <sys/types.h> |
830 | | # include <sys/sysctl.h> |
831 | | #elif defined(ARMCRC32_NETBSD) |
832 | | # include <sys/types.h> |
833 | | # include <sys/sysctl.h> |
834 | | # include <aarch64/armreg.h> /* for struct aarch64_sysctl_cpu_id */ |
835 | | #elif defined(ARMCRC32_WIN) && !defined(_WINBASE_) |
836 | | __declspec(dllimport) int __stdcall IsProcessorFeaturePresent(unsigned long); |
837 | | #endif |
838 | | #ifndef PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE |
839 | | # define PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE 30 |
840 | | #endif |
841 | | #ifndef PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE |
842 | | # define PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE 31 |
843 | | #endif |
844 | | |
845 | | #ifdef ARMCRC32_SYSCTL |
846 | | local int arm_sysctl(const char *name) { |
847 | | int have = 0; |
848 | | size_t size = sizeof(have); |
849 | | return sysctlbyname(name, &have, &size, NULL, 0) == 0 && have; |
850 | | } |
851 | | #endif |
852 | | |
853 | | /* Ask for the capabilities identified by the bits set in want. Return those |
854 | | bits set if the respective capapility is present. */ |
855 | | local int arm_probe(int want) { |
856 | | int caps = 0; |
857 | | #if defined(ARMCRC32_AUXV) || defined(ARMCRC32_ELFAUX) |
858 | | unsigned long bits; |
859 | | # ifdef ARMCRC32_AUXV |
860 | | bits = getauxval(ARM_AT); |
861 | | # else |
862 | | bits = 0; |
863 | | if (elf_aux_info(ARM_AT, &bits, sizeof(bits)) != 0) |
864 | | bits = 0; |
865 | | # endif |
866 | | if ((bits >> ARM_BIT_CRC32) & 1) |
867 | | caps |= ARM_HAVE_CRC32; |
868 | | if ((bits >> ARM_BIT_PMULL) & 1) |
869 | | caps |= ARM_HAVE_PMULL; |
870 | | (void)want; |
871 | | #elif defined(ARMCRC32_SYSCTL) |
872 | | # ifdef ARMCRC32_RT |
873 | | if ((want & ARM_HAVE_CRC32) && arm_sysctl("hw.optional.armv8_crc32")) |
874 | | caps |= ARM_HAVE_CRC32; |
875 | | # endif |
876 | | # ifdef ARMPMULL_RT |
877 | | if ((want & ARM_HAVE_PMULL) && arm_sysctl("hw.optional.arm.FEAT_PMULL")) |
878 | | caps |= ARM_HAVE_PMULL; |
879 | | # endif |
880 | | #elif defined(ARMCRC32_NETBSD) |
881 | | /* ID_AA64ISAR0_EL1 of cpu0: CRC32 in bits 16..19 (1 if present), and AES |
882 | | in bits 4..7 (2 if PMULL is present) */ |
883 | | struct aarch64_sysctl_cpu_id id; |
884 | | size_t size = sizeof(id); |
885 | | if (sysctlbyname("machdep.cpu0.cpu_id", &id, &size, NULL, 0) == 0 && |
886 | | size == sizeof(id)) { |
887 | | if (((id.ac_aa64isar0 >> 16) & 15) >= 1) |
888 | | caps |= ARM_HAVE_CRC32; |
889 | | if (((id.ac_aa64isar0 >> 4) & 15) >= 2) |
890 | | caps |= ARM_HAVE_PMULL; |
891 | | } |
892 | | (void)want; |
893 | | #else |
894 | | if ((want & ARM_HAVE_CRC32) && |
895 | | IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE)) |
896 | | caps |= ARM_HAVE_CRC32; |
897 | | if ((want & ARM_HAVE_PMULL) && |
898 | | IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) |
899 | | caps |= ARM_HAVE_PMULL; |
900 | | #endif |
901 | | return caps; |
902 | | } |
903 | | |
904 | | /* Z_TEST_ARMCRC32_ABSENT and Z_TEST_ARMPMULL_ABSENT ask, but answer no, to |
905 | | test the code used without them, and to measure the cost of asking. */ |
906 | | local int arm_ask(int want) { |
907 | | volatile int caps = arm_probe(want); |
908 | | #ifdef Z_TEST_ARMCRC32_ABSENT |
909 | | caps &= ~ARM_HAVE_CRC32; |
910 | | #endif |
911 | | #ifdef Z_TEST_ARMPMULL_ABSENT |
912 | | caps &= ~ARM_HAVE_PMULL; |
913 | | #endif |
914 | | return caps; |
915 | | } |
916 | | |
917 | | /* |
918 | | If lock-free C11 atomics are available, arm_probe() is only called once and |
919 | | the answer is cached. If not, then arm_probe() is called for every crc32() |
920 | | call above a data-length threshold (Z_ARMCRC32_PROBE_LEN). |
921 | | */ |
922 | | #if !defined(Z_ARMCRC32_NOCACHE) && defined(__STDC__) && \ |
923 | | defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \ |
924 | | !defined(__STDC_NO_ATOMICS__) |
925 | | # include <stdatomic.h> |
926 | | # if ATOMIC_INT_LOCK_FREE == 2 |
927 | | # define ARMCRC32_CACHE |
928 | | # endif |
929 | | #endif |
930 | | |
931 | | #ifdef ARMCRC32_CACHE |
932 | | local atomic_int arm_known; |
933 | | |
934 | | /* Ask the question and cache the answer. */ |
935 | | local int arm_learn(void) { |
936 | | int caps = arm_ask(ARM_HAVE_CRC32 | ARM_HAVE_PMULL) | 4; |
937 | | atomic_store_explicit(&arm_known, caps, memory_order_relaxed); |
938 | | return caps; |
939 | | } |
940 | | |
941 | | /* If there are lock-free C11 atomics, then the cache is filled by asking for |
942 | | both of the capabilities, which is returned. Otherwise only the capabilities |
943 | | with set bits in want are asked for. The return value has bits set for the |
944 | | requested and available capabilities. The two bits are ARM_HAVE_CRC32 and |
945 | | ARM_HAVE_PMULL. */ |
946 | | local inline int arm_caps(int want) { |
947 | | int caps = atomic_load_explicit(&arm_known, memory_order_relaxed); |
948 | | (void)want; |
949 | | if (caps == 0) |
950 | | caps = arm_learn(); |
951 | | return caps & 3; |
952 | | } |
953 | | #else |
954 | | # define arm_caps(want) arm_ask(want) |
955 | | #endif |
956 | | #endif /* ARMCRC32_ASK */ |
957 | | |
958 | | /* ========================================================================= |
959 | | * Use the ARM CRC32 instructions, if available. This computes the CRC about |
960 | | * ten times faster than the braided calculation. |
961 | | */ |
962 | | #ifdef ARMCRC32 |
963 | | |
964 | | /* Define ARMCRC32_ATTR to attribute to the function with hardware CRC |
965 | | instructions, as needed to tell the compiler it can produce them. */ |
966 | | #ifndef ARMCRC32_RT |
967 | | # define ARMCRC32_ATTR |
968 | | #elif defined(Z_ARMCRC32_TARGET) |
969 | | # define ARMCRC32_ATTR __attribute__((target(Z_ARMCRC32_TARGET))) |
970 | | #elif defined(_MSC_VER) && !defined(__clang__) |
971 | | # define ARMCRC32_ATTR |
972 | | #elif defined(__clang__) |
973 | | # define ARMCRC32_ATTR __attribute__((target("crc"))) |
974 | | #elif ARMCRC32_WORD == 8 |
975 | | # define ARMCRC32_ATTR __attribute__((target("+crc"))) |
976 | | #elif defined(__ARM_PCS_VFP) |
977 | | /* gcc needs a floating-point unit for the hard-float calling convention */ |
978 | | # define ARMCRC32_ATTR __attribute__((target("arch=armv8-a+crc+simd"))) |
979 | | #else |
980 | | # define ARMCRC32_ATTR __attribute__((target("arch=armv8-a+crc"))) |
981 | | #endif |
982 | | #ifdef ARMPMULL |
983 | | # if !defined(ARMCRC32_RT) && !defined(ARMPMULL_RT) |
984 | | # define ARMPMULL_ATTR |
985 | | # elif defined(Z_ARMPMULL_TARGET) |
986 | | # define ARMPMULL_ATTR __attribute__((target(Z_ARMPMULL_TARGET))) |
987 | | # elif defined(_MSC_VER) && !defined(__clang__) |
988 | | # define ARMPMULL_ATTR |
989 | | # elif defined(__clang__) |
990 | | # define ARMPMULL_ATTR __attribute__((target("crc,crypto"))) |
991 | | # else |
992 | | # define ARMPMULL_ATTR __attribute__((target("+crc+crypto"))) |
993 | | # endif |
994 | | #endif |
995 | | |
996 | | /* Define CRC32W() to operate on a word and CRC32B() to operate on a byte. */ |
997 | | #if ARMCRC32_WORD == 8 |
998 | | typedef Z_U8 arm_word_t; |
999 | | # if defined(_MSC_VER) && !defined(__clang__) |
1000 | | # include <intrin.h> |
1001 | | # ifdef ARMPMULL |
1002 | | # include <arm64_neon.h> |
1003 | | # endif |
1004 | | # define CRC32B(c, b) (c = __crc32b((unsigned)(c), (unsigned char)(b))) |
1005 | | # define CRC32W(c, w) (c = __crc32d((unsigned)(c), w)) |
1006 | | # else |
1007 | | # define CRC32B(c, b) \ |
1008 | | __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(c) : "r"(b)) |
1009 | | # define CRC32W(c, w) \ |
1010 | | __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(c) : "r"(w)) |
1011 | | # endif |
1012 | | /* set c to p times x^32, modulo the CRC polynomial */ |
1013 | | # define CRC32_REDUCE(c, p) do { \ |
1014 | | c = 0; \ |
1015 | | CRC32W(c, p); \ |
1016 | | } while (0) |
1017 | | #else |
1018 | | typedef Z_U4 arm_word_t; |
1019 | | # define CRC32B(c, b) (c = __builtin_arm_crc32b((unsigned)(c), \ |
1020 | | (unsigned char)(b))) |
1021 | | # define CRC32W(c, w) (c = __builtin_arm_crc32w((unsigned)(c), w)) |
1022 | | /* set c to p times x^32, modulo the CRC polynomial */ |
1023 | | # define CRC32_REDUCE(c, p) do { \ |
1024 | | c = 0; \ |
1025 | | CRC32W(c, (Z_U4)(p)); \ |
1026 | | CRC32W(c, (Z_U4)((p) >> 32)); \ |
1027 | | } while (0) |
1028 | | #endif |
1029 | | |
1030 | | /* |
1031 | | The CRC is computed on three interleaved streams of words at once, to realize |
1032 | | a throughput of one CRC instruction per cycle, where each can have a latency |
1033 | | of up to three cycles. Do successively smaller batches so that any length |
1034 | | that is at least the smallest batch size will start with one of them. Below |
1035 | | that, do one word at a time in serial, and then one byte at a time for the |
1036 | | last few. The CRC from the first group is the CRC of all of the data through |
1037 | | that group. The other two CRCs are of those groups in isolation. Those two |
1038 | | are combined with the first CRC by multiplying them by the appropriate x^n |
1039 | | modulo the CRC polynomial, and then exclusive-oring the three together. |
1040 | | |
1041 | | If Z_NO_ARMCRC32_BATCH is defined, then the interleaving is skipped, and the |
1042 | | CRC is calculated a word at a time. Some ARM processors pipeline that to one |
1043 | | cycle per CRC instruction, so nothing is gained by the interleaving, and in |
1044 | | fact a little bit is lost due to the interleaving overhead. This is the case |
1045 | | for the Cortex-A76 and Cortex-A720 for example. The builder can measure this |
1046 | | on their processor and compiler and elect to define it if its faster. |
1047 | | */ |
1048 | | #ifndef Z_NO_ARMCRC32_BATCH |
1049 | | #ifdef ARMCRC32_SOFT |
1050 | | /* Return the carry-less product of c and and the value represented by the |
1051 | | table t from batch_t[]. The table speeds up the multiplication by doing |
1052 | | four bits of c at a time. */ |
1053 | | local Z_U8 clmul_soft(z_crc_t c, const Z_U8 *t) { |
1054 | | Z_U8 p; |
1055 | | int j; |
1056 | | |
1057 | | p = 0; |
1058 | | for (j = 0; j < 32; j += 4) |
1059 | | p ^= t[(c >> j) & 15] << j; |
1060 | | return p; |
1061 | | } |
1062 | | |
1063 | | /* Return the CRC through three sequential streams of batch[k] bytes, given |
1064 | | crc, the running CRC through the first stream, and crc1 and crc2, the CRCs |
1065 | | of the second and third streams each starting from zero. */ |
1066 | | local ARMCRC32_ATTR uLong batch_soft(uLong crc, uLong crc1, uLong crc2, |
1067 | | unsigned k) { |
1068 | | Z_U8 p; |
1069 | | uLong c; |
1070 | | |
1071 | | p = clmul_soft((z_crc_t)crc, batch_t[k][1]) ^ |
1072 | | clmul_soft((z_crc_t)crc1, batch_t[k][0]); |
1073 | | CRC32_REDUCE(c, p); |
1074 | | return c ^ crc2; |
1075 | | } |
1076 | | #endif |
1077 | | |
1078 | | #ifdef ARMPMULL |
1079 | | local ARMPMULL_ATTR uLong batch_pmull(uLong crc, uLong crc1, uLong crc2, |
1080 | | unsigned k) { |
1081 | | Z_U8 p; |
1082 | | uLong c; |
1083 | | #if defined(_MSC_VER) && !defined(__clang__) |
1084 | | __n64 a0, a1, b0, b1; |
1085 | | |
1086 | | a0.n64_u64[0] = crc; |
1087 | | b0.n64_u64[0] = batch_k[k][1]; |
1088 | | a1.n64_u64[0] = crc1; |
1089 | | b1.n64_u64[0] = batch_k[k][0]; |
1090 | | p = vmull_p64(a0, b0).n128_u64[0] ^ vmull_p64(a1, b1).n128_u64[0]; |
1091 | | #else |
1092 | | Z_U8 a, b; |
1093 | | |
1094 | | __asm__("pmull %0.1q, %1.1d, %2.1d" : "=w"(a) |
1095 | | : "w"((Z_U8)crc), "w"((Z_U8)batch_k[k][1])); |
1096 | | __asm__("pmull %0.1q, %1.1d, %2.1d" : "=w"(b) |
1097 | | : "w"((Z_U8)crc1), "w"((Z_U8)batch_k[k][0])); |
1098 | | p = a ^ b; |
1099 | | #endif |
1100 | | CRC32_REDUCE(c, p); |
1101 | | return c ^ crc2; |
1102 | | } |
1103 | | #endif |
1104 | | |
1105 | | #ifdef ARMPMULL |
1106 | | /* Return true to use the ARM carry-less multiply instruction. */ |
1107 | | local int arm_pmull(void) { |
1108 | | #if defined(Z_TEST_ARMPMULL_ABSENT) |
1109 | | return 0; |
1110 | | #elif !defined(ARMPMULL_RT) |
1111 | | return 1; |
1112 | | #else |
1113 | | return (arm_caps(ARM_HAVE_PMULL) & ARM_HAVE_PMULL) != 0; |
1114 | | #endif |
1115 | | } |
1116 | | #endif |
1117 | | #endif /* !Z_NO_ARMCRC32_BATCH */ |
1118 | | |
1119 | | /* Return the CRC-32 of len bytes at buf using ARM hardware instructions. */ |
1120 | | local ARMCRC32_ATTR uLong crc32_arm(uLong crc, const unsigned char FAR *buf, |
1121 | | z_size_t len) { |
1122 | | #ifdef ARMPMULL |
1123 | | int pm; |
1124 | | #endif |
1125 | | uLong val; |
1126 | | #ifndef Z_NO_ARMCRC32_BATCH |
1127 | | uLong crc1, crc2; |
1128 | | z_size_t n; |
1129 | | unsigned k; |
1130 | | #endif |
1131 | | const arm_word_t *word; |
1132 | | arm_word_t val0, val1, val2; |
1133 | | z_size_t i; |
1134 | | z_size_t num; |
1135 | | |
1136 | | /* Return initial CRC, if requested. */ |
1137 | | if (buf == Z_NULL) return 0; |
1138 | | |
1139 | | #ifdef DYNAMIC_CRC_TABLE |
1140 | | z_once(&made, make_crc_table); |
1141 | | #endif /* DYNAMIC_CRC_TABLE */ |
1142 | | |
1143 | | /* Pre-condition the CRC */ |
1144 | | crc = (~crc) & 0xffffffff; |
1145 | | |
1146 | | /* Compute the CRC up to a word boundary. */ |
1147 | | while (len && ((z_size_t)buf & (ARMCRC32_WORD - 1)) != 0) { |
1148 | | len--; |
1149 | | val = *buf++; |
1150 | | CRC32B(crc, val); |
1151 | | } |
1152 | | |
1153 | | /* Prepare to compute the CRC on full words word[0..num-1]. */ |
1154 | | word = (arm_word_t const *)buf; |
1155 | | num = len / ARMCRC32_WORD; |
1156 | | len &= ARMCRC32_WORD - 1; |
1157 | | |
1158 | | #ifndef Z_NO_ARMCRC32_BATCH |
1159 | | /* Do batches of three interleaved CRCs, from the largest size down. */ |
1160 | | if (num >= 3 * (batch[BATCHES - 1] / ARMCRC32_WORD)) { |
1161 | | #ifdef ARMPMULL |
1162 | | pm = arm_pmull(); |
1163 | | #endif |
1164 | | for (k = 0; k < BATCHES; k++) { |
1165 | | n = batch[k] / ARMCRC32_WORD; |
1166 | | while (num >= 3 * n) { |
1167 | | crc1 = 0; |
1168 | | crc2 = 0; |
1169 | | for (i = 0; i < n; i++) { |
1170 | | val0 = word[i]; |
1171 | | val1 = word[i + n]; |
1172 | | val2 = word[i + 2 * n]; |
1173 | | CRC32W(crc, val0); |
1174 | | CRC32W(crc1, val1); |
1175 | | CRC32W(crc2, val2); |
1176 | | } |
1177 | | word += 3 * n; |
1178 | | num -= 3 * n; |
1179 | | #ifdef ARMPMULL |
1180 | | if (pm) |
1181 | | crc = batch_pmull(crc, crc1, crc2, k); |
1182 | | # ifdef ARMCRC32_SOFT |
1183 | | else |
1184 | | crc = batch_soft(crc, crc1, crc2, k); |
1185 | | # endif |
1186 | | #else |
1187 | | crc = batch_soft(crc, crc1, crc2, k); |
1188 | | #endif |
1189 | | } |
1190 | | } |
1191 | | } |
1192 | | #endif |
1193 | | |
1194 | | /* Compute the CRC on any remaining words, four at a time, and then one at |
1195 | | a time. The four-at-a-time loop reduces the loop overhead and avoids |
1196 | | a code alignment issue observed on one processor that would randomly |
1197 | | halve the speed of the single-word loop. */ |
1198 | | for (i = 3; i < num; i += 4) { |
1199 | | val0 = word[i - 3]; |
1200 | | val1 = word[i - 2]; |
1201 | | val2 = word[i - 1]; |
1202 | | CRC32W(crc, val0); |
1203 | | CRC32W(crc, val1); |
1204 | | CRC32W(crc, val2); |
1205 | | val0 = word[i]; |
1206 | | CRC32W(crc, val0); |
1207 | | } |
1208 | | for (i -= 3; i < num; i++) { |
1209 | | val0 = word[i]; |
1210 | | CRC32W(crc, val0); |
1211 | | } |
1212 | | word += num; |
1213 | | |
1214 | | /* Complete the CRC on any remaining bytes. */ |
1215 | | buf = (const unsigned char FAR *)word; |
1216 | | while (len) { |
1217 | | len--; |
1218 | | val = *buf++; |
1219 | | CRC32B(crc, val); |
1220 | | } |
1221 | | |
1222 | | /* Return the CRC, post-conditioned. */ |
1223 | | return crc ^ 0xffffffff; |
1224 | | } |
1225 | | |
1226 | | #ifndef ARMCRC32_RT |
1227 | | /* ========================================================================= */ |
1228 | | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
1229 | | return crc32_arm(crc, buf, len); |
1230 | | } |
1231 | | #endif |
1232 | | |
1233 | | #endif /* ARMCRC32 */ |
1234 | | |
1235 | | #if !defined(ARMCRC32) || defined(ARMCRC32_RT) |
1236 | | |
1237 | | #ifdef W |
1238 | | |
1239 | | /* |
1240 | | Return the CRC of the W bytes in the word_t data, taking the |
1241 | | least-significant byte of the word as the first byte of data, without any pre |
1242 | | or post conditioning. This is used to combine the CRCs of each braid. |
1243 | | */ |
1244 | 0 | local z_crc_t crc_word(z_word_t data) { |
1245 | 0 | int k; |
1246 | 0 | for (k = 0; k < W; k++) |
1247 | 0 | data = (data >> 8) ^ crc_table[data & 0xff]; |
1248 | 0 | return (z_crc_t)data; |
1249 | 0 | } |
1250 | | |
1251 | 0 | local z_word_t crc_word_big(z_word_t data) { |
1252 | 0 | int k; |
1253 | 0 | for (k = 0; k < W; k++) |
1254 | 0 | data = (data << 8) ^ |
1255 | 0 | crc_big_table[(data >> ((W - 1) << 3)) & 0xff]; |
1256 | 0 | return data; |
1257 | 0 | } |
1258 | | |
1259 | | /* |
1260 | | The CRC of one braid's word w, using braid table t: the table entries for |
1261 | | its W bytes, combined in pairs. This is written out rather than looped over |
1262 | | the bytes, since gcc at -O2 does not unroll such a loop, which leaves the |
1263 | | shift and the table offset to be computed for each byte, and is up to twice |
1264 | | as slow. |
1265 | | */ |
1266 | 0 | #define CRCB(t, w, k) t[k][((w) >> ((k) << 3)) & 0xff] |
1267 | | #if W == 8 |
1268 | | # define CRCWORD(t, w) (((CRCB(t, w, 0) ^ CRCB(t, w, 1)) ^ \ |
1269 | | (CRCB(t, w, 2) ^ CRCB(t, w, 3))) ^ \ |
1270 | | ((CRCB(t, w, 4) ^ CRCB(t, w, 5)) ^ \ |
1271 | | (CRCB(t, w, 6) ^ CRCB(t, w, 7)))) |
1272 | | #else |
1273 | 0 | # define CRCWORD(t, w) ((CRCB(t, w, 0) ^ CRCB(t, w, 1)) ^ \ |
1274 | 0 | (CRCB(t, w, 2) ^ CRCB(t, w, 3))) |
1275 | | #endif |
1276 | | |
1277 | | #ifdef BRAID_CUT |
1278 | | /* |
1279 | | Return the CRC of buf[0..len-1] starting with crc, computed on NLOW braids. |
1280 | | crc is pre-conditioned, and the result is not post-conditioned. crc32_z() |
1281 | | uses this for lengths less than BRAID_CUT, and N braids for longer lengths. |
1282 | | The N braids are in crc32_z() itself, since compiled there, clang does not |
1283 | | spill a braid's CRC to the stack in the loop on x86-64 for N=5, as it did |
1284 | | when the N braids were here. A compiler can be expected to inline this, so |
1285 | | that short lengths do not incur a function call. |
1286 | | */ |
1287 | | local uLong crc32_braid(uLong crc, const unsigned char FAR *buf, |
1288 | 0 | z_size_t len) { |
1289 | | /* If provided enough bytes, do a braided CRC calculation. */ |
1290 | 0 | if (len >= NLOW * W + W - 1) { |
1291 | 0 | z_size_t blks; |
1292 | 0 | z_word_t const *words; |
1293 | 0 | unsigned endian; |
1294 | | |
1295 | | /* Compute the CRC up to a z_word_t boundary. */ |
1296 | 0 | while (len && ((z_size_t)buf & (W - 1)) != 0) { |
1297 | 0 | len--; |
1298 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1299 | 0 | } |
1300 | | |
1301 | | /* Compute the CRC on as many NLOW z_word_t blocks as are available. */ |
1302 | 0 | blks = len / (NLOW * W); |
1303 | 0 | len -= blks * NLOW * W; |
1304 | 0 | words = (z_word_t const *)buf; |
1305 | | |
1306 | | /* Do endian check at execution time instead of compile time, since ARM |
1307 | | processors can change the endianness at execution time. If the |
1308 | | compiler knows what the endianness will be, it can optimize out the |
1309 | | check and the unused branch. */ |
1310 | 0 | endian = 1; |
1311 | 0 | if (*(unsigned char *)&endian) { |
1312 | | /* Little endian. */ |
1313 | |
|
1314 | 0 | z_crc_t crc0; |
1315 | 0 | z_word_t word0; |
1316 | 0 | #if NLOW > 1 |
1317 | 0 | z_crc_t crc1; |
1318 | 0 | z_word_t word1; |
1319 | | #if NLOW > 2 |
1320 | | z_crc_t crc2; |
1321 | | z_word_t word2; |
1322 | | #if NLOW > 3 |
1323 | | z_crc_t crc3; |
1324 | | z_word_t word3; |
1325 | | #if NLOW > 4 |
1326 | | z_crc_t crc4; |
1327 | | z_word_t word4; |
1328 | | #if NLOW > 5 |
1329 | | z_crc_t crc5; |
1330 | | z_word_t word5; |
1331 | | #endif |
1332 | | #endif |
1333 | | #endif |
1334 | | #endif |
1335 | 0 | #endif |
1336 | | |
1337 | | /* Initialize the CRC for each braid. */ |
1338 | 0 | crc0 = (z_crc_t)crc; |
1339 | 0 | #if NLOW > 1 |
1340 | 0 | crc1 = 0; |
1341 | | #if NLOW > 2 |
1342 | | crc2 = 0; |
1343 | | #if NLOW > 3 |
1344 | | crc3 = 0; |
1345 | | #if NLOW > 4 |
1346 | | crc4 = 0; |
1347 | | #if NLOW > 5 |
1348 | | crc5 = 0; |
1349 | | #endif |
1350 | | #endif |
1351 | | #endif |
1352 | | #endif |
1353 | 0 | #endif |
1354 | | |
1355 | | /* |
1356 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1357 | | independently. |
1358 | | */ |
1359 | 0 | while (--blks) { |
1360 | | /* Load the word for each braid into registers. */ |
1361 | 0 | word0 = crc0 ^ words[0]; |
1362 | 0 | #if NLOW > 1 |
1363 | 0 | word1 = crc1 ^ words[1]; |
1364 | | #if NLOW > 2 |
1365 | | word2 = crc2 ^ words[2]; |
1366 | | #if NLOW > 3 |
1367 | | word3 = crc3 ^ words[3]; |
1368 | | #if NLOW > 4 |
1369 | | word4 = crc4 ^ words[4]; |
1370 | | #if NLOW > 5 |
1371 | | word5 = crc5 ^ words[5]; |
1372 | | #endif |
1373 | | #endif |
1374 | | #endif |
1375 | | #endif |
1376 | 0 | #endif |
1377 | 0 | words += NLOW; |
1378 | | |
1379 | | /* Compute and update the CRC for each word. */ |
1380 | 0 | crc0 = CRCWORD(BRAID_TABLE(NLOW), word0); |
1381 | 0 | #if NLOW > 1 |
1382 | 0 | crc1 = CRCWORD(BRAID_TABLE(NLOW), word1); |
1383 | | #if NLOW > 2 |
1384 | | crc2 = CRCWORD(BRAID_TABLE(NLOW), word2); |
1385 | | #if NLOW > 3 |
1386 | | crc3 = CRCWORD(BRAID_TABLE(NLOW), word3); |
1387 | | #if NLOW > 4 |
1388 | | crc4 = CRCWORD(BRAID_TABLE(NLOW), word4); |
1389 | | #if NLOW > 5 |
1390 | | crc5 = CRCWORD(BRAID_TABLE(NLOW), word5); |
1391 | | #endif |
1392 | | #endif |
1393 | | #endif |
1394 | | #endif |
1395 | 0 | #endif |
1396 | 0 | } |
1397 | | |
1398 | | /* |
1399 | | Process the last block, combining the CRCs of the NLOW braids at |
1400 | | the same time. |
1401 | | */ |
1402 | 0 | crc = crc_word(crc0 ^ words[0]); |
1403 | 0 | #if NLOW > 1 |
1404 | 0 | crc = crc_word(crc1 ^ words[1] ^ crc); |
1405 | | #if NLOW > 2 |
1406 | | crc = crc_word(crc2 ^ words[2] ^ crc); |
1407 | | #if NLOW > 3 |
1408 | | crc = crc_word(crc3 ^ words[3] ^ crc); |
1409 | | #if NLOW > 4 |
1410 | | crc = crc_word(crc4 ^ words[4] ^ crc); |
1411 | | #if NLOW > 5 |
1412 | | crc = crc_word(crc5 ^ words[5] ^ crc); |
1413 | | #endif |
1414 | | #endif |
1415 | | #endif |
1416 | | #endif |
1417 | 0 | #endif |
1418 | 0 | words += NLOW; |
1419 | 0 | } |
1420 | 0 | else { |
1421 | | /* Big endian. */ |
1422 | |
|
1423 | 0 | z_word_t crc0, word0, comb; |
1424 | 0 | #if NLOW > 1 |
1425 | 0 | z_word_t crc1, word1; |
1426 | | #if NLOW > 2 |
1427 | | z_word_t crc2, word2; |
1428 | | #if NLOW > 3 |
1429 | | z_word_t crc3, word3; |
1430 | | #if NLOW > 4 |
1431 | | z_word_t crc4, word4; |
1432 | | #if NLOW > 5 |
1433 | | z_word_t crc5, word5; |
1434 | | #endif |
1435 | | #endif |
1436 | | #endif |
1437 | | #endif |
1438 | 0 | #endif |
1439 | | |
1440 | | /* Initialize the CRC for each braid. */ |
1441 | 0 | crc0 = byte_swap(crc); |
1442 | 0 | #if NLOW > 1 |
1443 | 0 | crc1 = 0; |
1444 | | #if NLOW > 2 |
1445 | | crc2 = 0; |
1446 | | #if NLOW > 3 |
1447 | | crc3 = 0; |
1448 | | #if NLOW > 4 |
1449 | | crc4 = 0; |
1450 | | #if NLOW > 5 |
1451 | | crc5 = 0; |
1452 | | #endif |
1453 | | #endif |
1454 | | #endif |
1455 | | #endif |
1456 | 0 | #endif |
1457 | | |
1458 | | /* |
1459 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1460 | | independently. |
1461 | | */ |
1462 | 0 | while (--blks) { |
1463 | | /* Load the word for each braid into registers. */ |
1464 | 0 | word0 = crc0 ^ words[0]; |
1465 | 0 | #if NLOW > 1 |
1466 | 0 | word1 = crc1 ^ words[1]; |
1467 | | #if NLOW > 2 |
1468 | | word2 = crc2 ^ words[2]; |
1469 | | #if NLOW > 3 |
1470 | | word3 = crc3 ^ words[3]; |
1471 | | #if NLOW > 4 |
1472 | | word4 = crc4 ^ words[4]; |
1473 | | #if NLOW > 5 |
1474 | | word5 = crc5 ^ words[5]; |
1475 | | #endif |
1476 | | #endif |
1477 | | #endif |
1478 | | #endif |
1479 | 0 | #endif |
1480 | 0 | words += NLOW; |
1481 | | |
1482 | | /* Compute and update the CRC for each word. */ |
1483 | 0 | crc0 = CRCWORD(BRAID_BIG_TABLE(NLOW), word0); |
1484 | 0 | #if NLOW > 1 |
1485 | 0 | crc1 = CRCWORD(BRAID_BIG_TABLE(NLOW), word1); |
1486 | | #if NLOW > 2 |
1487 | | crc2 = CRCWORD(BRAID_BIG_TABLE(NLOW), word2); |
1488 | | #if NLOW > 3 |
1489 | | crc3 = CRCWORD(BRAID_BIG_TABLE(NLOW), word3); |
1490 | | #if NLOW > 4 |
1491 | | crc4 = CRCWORD(BRAID_BIG_TABLE(NLOW), word4); |
1492 | | #if NLOW > 5 |
1493 | | crc5 = CRCWORD(BRAID_BIG_TABLE(NLOW), word5); |
1494 | | #endif |
1495 | | #endif |
1496 | | #endif |
1497 | | #endif |
1498 | 0 | #endif |
1499 | 0 | } |
1500 | | |
1501 | | /* |
1502 | | Process the last block, combining the CRCs of the NLOW braids at |
1503 | | the same time. |
1504 | | */ |
1505 | 0 | comb = crc_word_big(crc0 ^ words[0]); |
1506 | 0 | #if NLOW > 1 |
1507 | 0 | comb = crc_word_big(crc1 ^ words[1] ^ comb); |
1508 | | #if NLOW > 2 |
1509 | | comb = crc_word_big(crc2 ^ words[2] ^ comb); |
1510 | | #if NLOW > 3 |
1511 | | comb = crc_word_big(crc3 ^ words[3] ^ comb); |
1512 | | #if NLOW > 4 |
1513 | | comb = crc_word_big(crc4 ^ words[4] ^ comb); |
1514 | | #if NLOW > 5 |
1515 | | comb = crc_word_big(crc5 ^ words[5] ^ comb); |
1516 | | #endif |
1517 | | #endif |
1518 | | #endif |
1519 | | #endif |
1520 | 0 | #endif |
1521 | 0 | words += NLOW; |
1522 | 0 | crc = byte_swap(comb); |
1523 | 0 | } |
1524 | | |
1525 | | /* |
1526 | | Update the pointer to the remaining bytes to process. |
1527 | | */ |
1528 | 0 | buf = (unsigned char const *)words; |
1529 | 0 | } |
1530 | | |
1531 | | /* Complete the computation of the CRC on any remaining bytes. */ |
1532 | 0 | while (len >= 8) { |
1533 | 0 | len -= 8; |
1534 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1535 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1536 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1537 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1538 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1539 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1540 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1541 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1542 | 0 | } |
1543 | 0 | while (len) { |
1544 | 0 | len--; |
1545 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1546 | 0 | } |
1547 | |
|
1548 | 0 | return crc; |
1549 | 0 | } |
1550 | | #endif |
1551 | | |
1552 | | #endif |
1553 | | |
1554 | | /* ========================================================================= */ |
1555 | | #ifdef ARMCRC32_RT |
1556 | | local uLong crc32_soft(uLong crc, const unsigned char FAR *buf, |
1557 | | z_size_t len) { |
1558 | | #else |
1559 | 0 | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
1560 | 0 | #endif |
1561 | | /* Return initial CRC, if requested. */ |
1562 | 0 | if (buf == Z_NULL) return 0; |
1563 | | |
1564 | | #ifdef DYNAMIC_CRC_TABLE |
1565 | | z_once(&made, make_crc_table); |
1566 | | #endif /* DYNAMIC_CRC_TABLE */ |
1567 | | |
1568 | | /* Pre-condition the CRC */ |
1569 | 0 | crc = (~crc) & 0xffffffff; |
1570 | |
|
1571 | 0 | #ifdef W |
1572 | | |
1573 | | /* If provided enough bytes, do a braided CRC calculation. */ |
1574 | 0 | if (len >= NMIN * W + W - 1) { |
1575 | 0 | z_size_t blks; |
1576 | 0 | z_word_t const *words; |
1577 | 0 | unsigned endian; |
1578 | |
|
1579 | 0 | #ifdef BRAID_CUT |
1580 | | /* For shorter lengths, use NLOW braids. */ |
1581 | 0 | if (len < BRAID_CUT || len < N * W + W - 1) |
1582 | 0 | return crc32_braid(crc, buf, len) ^ 0xffffffff; |
1583 | 0 | #endif |
1584 | | |
1585 | | /* Compute the CRC up to a z_word_t boundary. */ |
1586 | 0 | while (len && ((z_size_t)buf & (W - 1)) != 0) { |
1587 | 0 | len--; |
1588 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1589 | 0 | } |
1590 | | |
1591 | | /* Compute the CRC on as many N z_word_t blocks as are available. */ |
1592 | 0 | blks = len / (N * W); |
1593 | 0 | len -= blks * N * W; |
1594 | 0 | words = (z_word_t const *)buf; |
1595 | | |
1596 | | /* Do endian check at execution time instead of compile time, since ARM |
1597 | | processors can change the endianness at execution time. If the |
1598 | | compiler knows what the endianness will be, it can optimize out the |
1599 | | check and the unused branch. */ |
1600 | 0 | endian = 1; |
1601 | 0 | if (*(unsigned char *)&endian) { |
1602 | | /* Little endian. */ |
1603 | |
|
1604 | 0 | z_crc_t crc0; |
1605 | 0 | z_word_t word0; |
1606 | 0 | #if N > 1 |
1607 | 0 | z_crc_t crc1; |
1608 | 0 | z_word_t word1; |
1609 | 0 | #if N > 2 |
1610 | 0 | z_crc_t crc2; |
1611 | 0 | z_word_t word2; |
1612 | 0 | #if N > 3 |
1613 | 0 | z_crc_t crc3; |
1614 | 0 | z_word_t word3; |
1615 | 0 | #if N > 4 |
1616 | 0 | z_crc_t crc4; |
1617 | 0 | z_word_t word4; |
1618 | | #if N > 5 |
1619 | | z_crc_t crc5; |
1620 | | z_word_t word5; |
1621 | | #endif |
1622 | 0 | #endif |
1623 | 0 | #endif |
1624 | 0 | #endif |
1625 | 0 | #endif |
1626 | | |
1627 | | /* Initialize the CRC for each braid. */ |
1628 | 0 | crc0 = (z_crc_t)crc; |
1629 | 0 | #if N > 1 |
1630 | 0 | crc1 = 0; |
1631 | 0 | #if N > 2 |
1632 | 0 | crc2 = 0; |
1633 | 0 | #if N > 3 |
1634 | 0 | crc3 = 0; |
1635 | 0 | #if N > 4 |
1636 | 0 | crc4 = 0; |
1637 | | #if N > 5 |
1638 | | crc5 = 0; |
1639 | | #endif |
1640 | 0 | #endif |
1641 | 0 | #endif |
1642 | 0 | #endif |
1643 | 0 | #endif |
1644 | | |
1645 | | /* |
1646 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1647 | | independently. |
1648 | | */ |
1649 | 0 | while (--blks) { |
1650 | | /* Load the word for each braid into registers. */ |
1651 | 0 | word0 = crc0 ^ words[0]; |
1652 | 0 | #if N > 1 |
1653 | 0 | word1 = crc1 ^ words[1]; |
1654 | 0 | #if N > 2 |
1655 | 0 | word2 = crc2 ^ words[2]; |
1656 | 0 | #if N > 3 |
1657 | 0 | word3 = crc3 ^ words[3]; |
1658 | 0 | #if N > 4 |
1659 | 0 | word4 = crc4 ^ words[4]; |
1660 | | #if N > 5 |
1661 | | word5 = crc5 ^ words[5]; |
1662 | | #endif |
1663 | 0 | #endif |
1664 | 0 | #endif |
1665 | 0 | #endif |
1666 | 0 | #endif |
1667 | 0 | words += N; |
1668 | | |
1669 | | /* Compute and update the CRC for each word. */ |
1670 | 0 | crc0 = CRCWORD(BRAID_TABLE(N), word0); |
1671 | 0 | #if N > 1 |
1672 | 0 | crc1 = CRCWORD(BRAID_TABLE(N), word1); |
1673 | 0 | #if N > 2 |
1674 | 0 | crc2 = CRCWORD(BRAID_TABLE(N), word2); |
1675 | 0 | #if N > 3 |
1676 | 0 | crc3 = CRCWORD(BRAID_TABLE(N), word3); |
1677 | 0 | #if N > 4 |
1678 | 0 | crc4 = CRCWORD(BRAID_TABLE(N), word4); |
1679 | | #if N > 5 |
1680 | | crc5 = CRCWORD(BRAID_TABLE(N), word5); |
1681 | | #endif |
1682 | 0 | #endif |
1683 | 0 | #endif |
1684 | 0 | #endif |
1685 | 0 | #endif |
1686 | 0 | } |
1687 | | |
1688 | | /* |
1689 | | Process the last block, combining the CRCs of the N braids at the |
1690 | | same time. |
1691 | | */ |
1692 | 0 | crc = crc_word(crc0 ^ words[0]); |
1693 | 0 | #if N > 1 |
1694 | 0 | crc = crc_word(crc1 ^ words[1] ^ crc); |
1695 | 0 | #if N > 2 |
1696 | 0 | crc = crc_word(crc2 ^ words[2] ^ crc); |
1697 | 0 | #if N > 3 |
1698 | 0 | crc = crc_word(crc3 ^ words[3] ^ crc); |
1699 | 0 | #if N > 4 |
1700 | 0 | crc = crc_word(crc4 ^ words[4] ^ crc); |
1701 | | #if N > 5 |
1702 | | crc = crc_word(crc5 ^ words[5] ^ crc); |
1703 | | #endif |
1704 | 0 | #endif |
1705 | 0 | #endif |
1706 | 0 | #endif |
1707 | 0 | #endif |
1708 | 0 | words += N; |
1709 | 0 | } |
1710 | 0 | else { |
1711 | | /* Big endian. */ |
1712 | |
|
1713 | 0 | z_word_t crc0, word0, comb; |
1714 | 0 | #if N > 1 |
1715 | 0 | z_word_t crc1, word1; |
1716 | 0 | #if N > 2 |
1717 | 0 | z_word_t crc2, word2; |
1718 | 0 | #if N > 3 |
1719 | 0 | z_word_t crc3, word3; |
1720 | 0 | #if N > 4 |
1721 | 0 | z_word_t crc4, word4; |
1722 | | #if N > 5 |
1723 | | z_word_t crc5, word5; |
1724 | | #endif |
1725 | 0 | #endif |
1726 | 0 | #endif |
1727 | 0 | #endif |
1728 | 0 | #endif |
1729 | | |
1730 | | /* Initialize the CRC for each braid. */ |
1731 | 0 | crc0 = byte_swap(crc); |
1732 | 0 | #if N > 1 |
1733 | 0 | crc1 = 0; |
1734 | 0 | #if N > 2 |
1735 | 0 | crc2 = 0; |
1736 | 0 | #if N > 3 |
1737 | 0 | crc3 = 0; |
1738 | 0 | #if N > 4 |
1739 | 0 | crc4 = 0; |
1740 | | #if N > 5 |
1741 | | crc5 = 0; |
1742 | | #endif |
1743 | 0 | #endif |
1744 | 0 | #endif |
1745 | 0 | #endif |
1746 | 0 | #endif |
1747 | | |
1748 | | /* |
1749 | | Process the first blks-1 blocks, computing the CRCs on each braid |
1750 | | independently. |
1751 | | */ |
1752 | 0 | while (--blks) { |
1753 | | /* Load the word for each braid into registers. */ |
1754 | 0 | word0 = crc0 ^ words[0]; |
1755 | 0 | #if N > 1 |
1756 | 0 | word1 = crc1 ^ words[1]; |
1757 | 0 | #if N > 2 |
1758 | 0 | word2 = crc2 ^ words[2]; |
1759 | 0 | #if N > 3 |
1760 | 0 | word3 = crc3 ^ words[3]; |
1761 | 0 | #if N > 4 |
1762 | 0 | word4 = crc4 ^ words[4]; |
1763 | | #if N > 5 |
1764 | | word5 = crc5 ^ words[5]; |
1765 | | #endif |
1766 | 0 | #endif |
1767 | 0 | #endif |
1768 | 0 | #endif |
1769 | 0 | #endif |
1770 | 0 | words += N; |
1771 | | |
1772 | | /* Compute and update the CRC for each word. */ |
1773 | 0 | crc0 = CRCWORD(BRAID_BIG_TABLE(N), word0); |
1774 | 0 | #if N > 1 |
1775 | 0 | crc1 = CRCWORD(BRAID_BIG_TABLE(N), word1); |
1776 | 0 | #if N > 2 |
1777 | 0 | crc2 = CRCWORD(BRAID_BIG_TABLE(N), word2); |
1778 | 0 | #if N > 3 |
1779 | 0 | crc3 = CRCWORD(BRAID_BIG_TABLE(N), word3); |
1780 | 0 | #if N > 4 |
1781 | 0 | crc4 = CRCWORD(BRAID_BIG_TABLE(N), word4); |
1782 | | #if N > 5 |
1783 | | crc5 = CRCWORD(BRAID_BIG_TABLE(N), word5); |
1784 | | #endif |
1785 | 0 | #endif |
1786 | 0 | #endif |
1787 | 0 | #endif |
1788 | 0 | #endif |
1789 | 0 | } |
1790 | | |
1791 | | /* |
1792 | | Process the last block, combining the CRCs of the N braids at the |
1793 | | same time. |
1794 | | */ |
1795 | 0 | comb = crc_word_big(crc0 ^ words[0]); |
1796 | 0 | #if N > 1 |
1797 | 0 | comb = crc_word_big(crc1 ^ words[1] ^ comb); |
1798 | 0 | #if N > 2 |
1799 | 0 | comb = crc_word_big(crc2 ^ words[2] ^ comb); |
1800 | 0 | #if N > 3 |
1801 | 0 | comb = crc_word_big(crc3 ^ words[3] ^ comb); |
1802 | 0 | #if N > 4 |
1803 | 0 | comb = crc_word_big(crc4 ^ words[4] ^ comb); |
1804 | | #if N > 5 |
1805 | | comb = crc_word_big(crc5 ^ words[5] ^ comb); |
1806 | | #endif |
1807 | 0 | #endif |
1808 | 0 | #endif |
1809 | 0 | #endif |
1810 | 0 | #endif |
1811 | 0 | words += N; |
1812 | 0 | crc = byte_swap(comb); |
1813 | 0 | } |
1814 | | |
1815 | | /* |
1816 | | Update the pointer to the remaining bytes to process. |
1817 | | */ |
1818 | 0 | buf = (unsigned char const *)words; |
1819 | 0 | } |
1820 | | |
1821 | 0 | #endif /* W */ |
1822 | | |
1823 | | /* Complete the computation of the CRC on any remaining bytes. */ |
1824 | 0 | while (len >= 8) { |
1825 | 0 | len -= 8; |
1826 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1827 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1828 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1829 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1830 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1831 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1832 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1833 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1834 | 0 | } |
1835 | 0 | while (len) { |
1836 | 0 | len--; |
1837 | 0 | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1838 | 0 | } |
1839 | | |
1840 | | /* Return the CRC, post-conditioned. */ |
1841 | 0 | return crc ^ 0xffffffff; |
1842 | 0 | } |
1843 | | |
1844 | | #endif /* !ARMCRC32 || ARMCRC32_RT */ |
1845 | | |
1846 | | #ifdef ARMCRC32_RT |
1847 | | #ifndef Z_ARMCRC32_PROBE_LEN |
1848 | | # define Z_ARMCRC32_PROBE_LEN 8 |
1849 | | #endif |
1850 | | |
1851 | | /* ========================================================================= */ |
1852 | | uLong ZEXPORT crc32_z(uLong crc, const unsigned char FAR *buf, z_size_t len) { |
1853 | | int caps; |
1854 | | |
1855 | | #if Z_ARMCRC32_PROBE_LEN > 0 |
1856 | | /* For a few bytes, the byte table is faster than finding out whether the |
1857 | | instructions can be used. */ |
1858 | | if (len < Z_ARMCRC32_PROBE_LEN) { |
1859 | | if (buf == Z_NULL) return 0; |
1860 | | #ifdef DYNAMIC_CRC_TABLE |
1861 | | z_once(&made, make_crc_table); |
1862 | | #endif /* DYNAMIC_CRC_TABLE */ |
1863 | | crc = (~crc) & 0xffffffff; |
1864 | | while (len) { |
1865 | | len--; |
1866 | | crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; |
1867 | | } |
1868 | | return crc ^ 0xffffffff; |
1869 | | } |
1870 | | #endif |
1871 | | caps = arm_caps(ARM_HAVE_CRC32); |
1872 | | if (caps & ARM_HAVE_CRC32) |
1873 | | return crc32_arm(crc, buf, len); |
1874 | | return crc32_soft(crc, buf, len); |
1875 | | } |
1876 | | #endif /* ARMCRC32_RT */ |
1877 | | |
1878 | | /* ========================================================================= */ |
1879 | 0 | uLong ZEXPORT crc32(uLong crc, const unsigned char FAR *buf, uInt len) { |
1880 | | #ifdef HAVE_S390X_VX |
1881 | | return crc32_z_hook(crc, buf, len); |
1882 | | #endif |
1883 | 0 | return crc32_z(crc, buf, len); |
1884 | 0 | } |
1885 | | |
1886 | | /* ========================================================================= */ |
1887 | 0 | uLong ZEXPORT crc32_combine_gen64(z_off64_t len2) { |
1888 | 0 | if (len2 < 0) |
1889 | 0 | return 0; |
1890 | | #ifdef DYNAMIC_CRC_TABLE |
1891 | | z_once(&made, make_crc_table); |
1892 | | #endif /* DYNAMIC_CRC_TABLE */ |
1893 | 0 | return x2nmodp(len2, 3); |
1894 | 0 | } |
1895 | | |
1896 | | /* ========================================================================= */ |
1897 | 0 | uLong ZEXPORT crc32_combine_gen(z_off_t len2) { |
1898 | 0 | return crc32_combine_gen64((z_off64_t)len2); |
1899 | 0 | } |
1900 | | |
1901 | | /* ========================================================================= */ |
1902 | 0 | uLong ZEXPORT crc32_combine_op(uLong crc1, uLong crc2, uLong op) { |
1903 | 0 | if (op == 0) |
1904 | 0 | return 0; |
1905 | 0 | return multmodp(op, crc1 & 0xffffffff) ^ (crc2 & 0xffffffff); |
1906 | 0 | } |
1907 | | |
1908 | | /* ========================================================================= */ |
1909 | 0 | uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2) { |
1910 | 0 | return crc32_combine_op(crc1, crc2, crc32_combine_gen64(len2)); |
1911 | 0 | } |
1912 | | |
1913 | | /* ========================================================================= */ |
1914 | 0 | uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2) { |
1915 | 0 | return crc32_combine64(crc1, crc2, (z_off64_t)len2); |
1916 | 0 | } |