| OLD | NEW |
| 1 /* crc32.c -- compute the CRC-32 of a data stream | 1 /* crc32.c -- compute the CRC-32 of a data stream |
| 2 * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler | 2 * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler |
| 3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 * | 4 * |
| 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster | 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster |
| 6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing | 6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing |
| 7 * tables for updating the shift register in one step with three exclusive-ors | 7 * tables for updating the shift register in one step with three exclusive-ors |
| 8 * instead of four steps with four exclusive-ors. This results in about a | 8 * instead of four steps with four exclusive-ors. This results in about a |
| 9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. | 9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. |
| 10 */ | 10 */ |
| 11 | 11 |
| 12 /* @(#) $Id$ */ | 12 /* @(#) $Id$ */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 25 # include <stdio.h> | 25 # include <stdio.h> |
| 26 # ifndef DYNAMIC_CRC_TABLE | 26 # ifndef DYNAMIC_CRC_TABLE |
| 27 # define DYNAMIC_CRC_TABLE | 27 # define DYNAMIC_CRC_TABLE |
| 28 # endif /* !DYNAMIC_CRC_TABLE */ | 28 # endif /* !DYNAMIC_CRC_TABLE */ |
| 29 #endif /* MAKECRCH */ | 29 #endif /* MAKECRCH */ |
| 30 | 30 |
| 31 #include "deflate.h" | 31 #include "deflate.h" |
| 32 #include "x86.h" | 32 #include "x86.h" |
| 33 #include "zutil.h" /* for STDC and FAR definitions */ | 33 #include "zutil.h" /* for STDC and FAR definitions */ |
| 34 | 34 |
| 35 #define local static | |
| 36 | |
| 37 /* Definitions for doing the crc four data bytes at a time. */ | 35 /* Definitions for doing the crc four data bytes at a time. */ |
| 38 #if !defined(NOBYFOUR) && defined(Z_U4) | 36 #if !defined(NOBYFOUR) && defined(Z_U4) |
| 39 # define BYFOUR | 37 # define BYFOUR |
| 40 #endif | 38 #endif |
| 41 #ifdef BYFOUR | 39 #ifdef BYFOUR |
| 42 local unsigned long crc32_little OF((unsigned long, | 40 local unsigned long crc32_little OF((unsigned long, |
| 43 const unsigned char FAR *, unsigned)); | 41 const unsigned char FAR *, z_size_t)); |
| 44 local unsigned long crc32_big OF((unsigned long, | 42 local unsigned long crc32_big OF((unsigned long, |
| 45 const unsigned char FAR *, unsigned)); | 43 const unsigned char FAR *, z_size_t)); |
| 46 # define TBLS 8 | 44 # define TBLS 8 |
| 47 #else | 45 #else |
| 48 # define TBLS 1 | 46 # define TBLS 1 |
| 49 #endif /* BYFOUR */ | 47 #endif /* BYFOUR */ |
| 50 | 48 |
| 51 /* Local functions for crc concatenation */ | 49 /* Local functions for crc concatenation */ |
| 52 local unsigned long gf2_matrix_times OF((unsigned long *mat, | 50 local unsigned long gf2_matrix_times OF((unsigned long *mat, |
| 53 unsigned long vec)); | 51 unsigned long vec)); |
| 54 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); | 52 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); |
| 55 local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); | 53 local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 make_crc_table(); | 194 make_crc_table(); |
| 197 #endif /* DYNAMIC_CRC_TABLE */ | 195 #endif /* DYNAMIC_CRC_TABLE */ |
| 198 return (const z_crc_t FAR *)crc_table; | 196 return (const z_crc_t FAR *)crc_table; |
| 199 } | 197 } |
| 200 | 198 |
| 201 /* ========================================================================= */ | 199 /* ========================================================================= */ |
| 202 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) | 200 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) |
| 203 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 | 201 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 |
| 204 | 202 |
| 205 /* ========================================================================= */ | 203 /* ========================================================================= */ |
| 206 unsigned long ZEXPORT crc32(crc, buf, len) | 204 unsigned long ZEXPORT crc32_z(crc, buf, len) |
| 207 unsigned long crc; | 205 unsigned long crc; |
| 208 const unsigned char FAR *buf; | 206 const unsigned char FAR *buf; |
| 209 uInt len; | 207 z_size_t len; |
| 210 { | 208 { |
| 211 if (buf == Z_NULL) return 0UL; | 209 if (buf == Z_NULL) return 0UL; |
| 212 | 210 |
| 213 #ifdef DYNAMIC_CRC_TABLE | 211 #ifdef DYNAMIC_CRC_TABLE |
| 214 if (crc_table_empty) | 212 if (crc_table_empty) |
| 215 make_crc_table(); | 213 make_crc_table(); |
| 216 #endif /* DYNAMIC_CRC_TABLE */ | 214 #endif /* DYNAMIC_CRC_TABLE */ |
| 217 | 215 |
| 218 #ifdef BYFOUR | 216 #ifdef BYFOUR |
| 219 if (sizeof(void *) == sizeof(ptrdiff_t)) { | 217 if (sizeof(void *) == sizeof(ptrdiff_t)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 230 while (len >= 8) { | 228 while (len >= 8) { |
| 231 DO8; | 229 DO8; |
| 232 len -= 8; | 230 len -= 8; |
| 233 } | 231 } |
| 234 if (len) do { | 232 if (len) do { |
| 235 DO1; | 233 DO1; |
| 236 } while (--len); | 234 } while (--len); |
| 237 return crc ^ 0xffffffffUL; | 235 return crc ^ 0xffffffffUL; |
| 238 } | 236 } |
| 239 | 237 |
| 238 /* ========================================================================= */ |
| 239 unsigned long ZEXPORT crc32(crc, buf, len) |
| 240 unsigned long crc; |
| 241 const unsigned char FAR *buf; |
| 242 uInt len; |
| 243 { |
| 244 return crc32_z(crc, buf, len); |
| 245 } |
| 246 |
| 240 #ifdef BYFOUR | 247 #ifdef BYFOUR |
| 241 | 248 |
| 249 /* |
| 250 This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit |
| 251 integer pointer type. This violates the strict aliasing rule, where a |
| 252 compiler can assume, for optimization purposes, that two pointers to |
| 253 fundamentally different types won't ever point to the same memory. This can |
| 254 manifest as a problem only if one of the pointers is written to. This code |
| 255 only reads from those pointers. So long as this code remains isolated in |
| 256 this compilation unit, there won't be a problem. For this reason, this code |
| 257 should not be copied and pasted into a compilation unit in which other code |
| 258 writes to the buffer that is passed to these routines. |
| 259 */ |
| 260 |
| 242 /* ========================================================================= */ | 261 /* ========================================================================= */ |
| 243 #define DOLIT4 c ^= *buf4++; \ | 262 #define DOLIT4 c ^= *buf4++; \ |
| 244 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ | 263 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ |
| 245 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] | 264 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] |
| 246 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 | 265 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 |
| 247 | 266 |
| 248 /* ========================================================================= */ | 267 /* ========================================================================= */ |
| 249 local unsigned long crc32_little(crc, buf, len) | 268 local unsigned long crc32_little(crc, buf, len) |
| 250 unsigned long crc; | 269 unsigned long crc; |
| 251 const unsigned char FAR *buf; | 270 const unsigned char FAR *buf; |
| 252 unsigned len; | 271 z_size_t len; |
| 253 { | 272 { |
| 254 register z_crc_t c; | 273 register z_crc_t c; |
| 255 register const z_crc_t FAR *buf4; | 274 register const z_crc_t FAR *buf4; |
| 256 | 275 |
| 257 c = (z_crc_t)crc; | 276 c = (z_crc_t)crc; |
| 258 c = ~c; | 277 c = ~c; |
| 259 while (len && ((ptrdiff_t)buf & 3)) { | 278 while (len && ((ptrdiff_t)buf & 3)) { |
| 260 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | 279 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); |
| 261 len--; | 280 len--; |
| 262 } | 281 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 273 buf = (const unsigned char FAR *)buf4; | 292 buf = (const unsigned char FAR *)buf4; |
| 274 | 293 |
| 275 if (len) do { | 294 if (len) do { |
| 276 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | 295 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); |
| 277 } while (--len); | 296 } while (--len); |
| 278 c = ~c; | 297 c = ~c; |
| 279 return (unsigned long)c; | 298 return (unsigned long)c; |
| 280 } | 299 } |
| 281 | 300 |
| 282 /* ========================================================================= */ | 301 /* ========================================================================= */ |
| 283 #define DOBIG4 c ^= *++buf4; \ | 302 #define DOBIG4 c ^= *buf4++; \ |
| 284 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ | 303 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ |
| 285 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] | 304 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] |
| 286 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 | 305 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 |
| 287 | 306 |
| 288 /* ========================================================================= */ | 307 /* ========================================================================= */ |
| 289 local unsigned long crc32_big(crc, buf, len) | 308 local unsigned long crc32_big(crc, buf, len) |
| 290 unsigned long crc; | 309 unsigned long crc; |
| 291 const unsigned char FAR *buf; | 310 const unsigned char FAR *buf; |
| 292 unsigned len; | 311 z_size_t len; |
| 293 { | 312 { |
| 294 register z_crc_t c; | 313 register z_crc_t c; |
| 295 register const z_crc_t FAR *buf4; | 314 register const z_crc_t FAR *buf4; |
| 296 | 315 |
| 297 c = ZSWAP32((z_crc_t)crc); | 316 c = ZSWAP32((z_crc_t)crc); |
| 298 c = ~c; | 317 c = ~c; |
| 299 while (len && ((ptrdiff_t)buf & 3)) { | 318 while (len && ((ptrdiff_t)buf & 3)) { |
| 300 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | 319 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
| 301 len--; | 320 len--; |
| 302 } | 321 } |
| 303 | 322 |
| 304 buf4 = (const z_crc_t FAR *)(const void FAR *)buf; | 323 buf4 = (const z_crc_t FAR *)(const void FAR *)buf; |
| 305 buf4--; | |
| 306 while (len >= 32) { | 324 while (len >= 32) { |
| 307 DOBIG32; | 325 DOBIG32; |
| 308 len -= 32; | 326 len -= 32; |
| 309 } | 327 } |
| 310 while (len >= 4) { | 328 while (len >= 4) { |
| 311 DOBIG4; | 329 DOBIG4; |
| 312 len -= 4; | 330 len -= 4; |
| 313 } | 331 } |
| 314 buf4++; | |
| 315 buf = (const unsigned char FAR *)buf4; | 332 buf = (const unsigned char FAR *)buf4; |
| 316 | 333 |
| 317 if (len) do { | 334 if (len) do { |
| 318 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | 335 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
| 319 } while (--len); | 336 } while (--len); |
| 320 c = ~c; | 337 c = ~c; |
| 321 return (unsigned long)(ZSWAP32(c)); | 338 return (unsigned long)(ZSWAP32(c)); |
| 322 } | 339 } |
| 323 | 340 |
| 324 #endif /* BYFOUR */ | 341 #endif /* BYFOUR */ |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 | 460 |
| 444 ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size) | 461 ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size) |
| 445 { | 462 { |
| 446 if (x86_cpu_enable_simd) { | 463 if (x86_cpu_enable_simd) { |
| 447 crc_fold_copy(strm->state, dst, strm->next_in, size); | 464 crc_fold_copy(strm->state, dst, strm->next_in, size); |
| 448 return; | 465 return; |
| 449 } | 466 } |
| 450 zmemcpy(dst, strm->next_in, size); | 467 zmemcpy(dst, strm->next_in, size); |
| 451 strm->adler = crc32(strm->adler, dst, size); | 468 strm->adler = crc32(strm->adler, dst, size); |
| 452 } | 469 } |
| OLD | NEW |