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