From: Nathan Moinvaziri Date: Sun, 9 Jan 2022 15:35:10 +0000 (-0800) Subject: Merge crc32_little and crc32_big with preprocessor macros for each endian. Removed... X-Git-Tag: 2.1.0-beta1~445 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=346ec9c42cdd2a4b369e1fc068038c66b5c6dc7f;p=thirdparty%2Fzlib-ng.git Merge crc32_little and crc32_big with preprocessor macros for each endian. Removed crc32_generic since it is not being used. --- diff --git a/arch/s390/crc32-vx.c b/arch/s390/crc32-vx.c index 18b8bb223..3465a5732 100644 --- a/arch/s390/crc32-vx.c +++ b/arch/s390/crc32-vx.c @@ -202,12 +202,12 @@ uint32_t Z_INTERNAL s390_crc32_vx(uint32_t crc, const unsigned char *buf, uint64 uint64_t prealign, aligned, remaining; if (len < VX_MIN_LEN + VX_ALIGN_MASK) - return crc32_big(crc, buf, len); + return crc32_byfour(crc, buf, len); if ((uintptr_t)buf & VX_ALIGN_MASK) { prealign = VX_ALIGNMENT - ((uintptr_t)buf & VX_ALIGN_MASK); len -= prealign; - crc = crc32_big(crc, buf, prealign); + crc = crc32_byfour(crc, buf, prealign); buf += prealign; } aligned = len & ~VX_ALIGN_MASK; @@ -216,7 +216,7 @@ uint32_t Z_INTERNAL s390_crc32_vx(uint32_t crc, const unsigned char *buf, uint64 crc = crc32_le_vgfm_16(crc ^ 0xffffffff, buf, (size_t)aligned) ^ 0xffffffff; if (remaining) - crc = crc32_big(crc, buf + aligned, remaining); + crc = crc32_byfour(crc, buf + aligned, remaining); return crc; } diff --git a/crc32.c b/crc32.c index c9a81d37b..b962fb6b5 100644 --- a/crc32.c +++ b/crc32.c @@ -16,9 +16,7 @@ #include "functable.h" #include "crc32_tbl.h" -/* ========================================================================= - * This function can be used by asm versions of crc32() - */ +/* ========================================================================= */ const uint32_t * Z_EXPORT PREFIX(get_crc_table)(void) { return (const uint32_t *)crc_table; } @@ -35,34 +33,8 @@ uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t return functable.crc32(crc, buf, len); } -#endif -/* ========================================================================= */ -#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) -#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 -#define DO4 DO1; DO1; DO1; DO1 - -/* ========================================================================= */ -Z_INTERNAL uint32_t crc32_generic(uint32_t crc, const unsigned char *buf, uint64_t len) { - crc = crc ^ 0xffffffff; - -#ifdef UNROLL_MORE - while (len >= 8) { - DO8; - len -= 8; - } -#else - while (len >= 4) { - DO4; - len -= 4; - } #endif - if (len) do { - DO1; - } while (--len); - return crc ^ 0xffffffff; -} - #ifdef ZLIB_COMPAT unsigned long Z_EXPORT PREFIX(crc32)(unsigned long crc, const unsigned char *buf, unsigned int len) { return (unsigned long)PREFIX(crc32_z)((uint32_t)crc, buf, len); @@ -73,6 +45,8 @@ uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t } #endif +/* ========================================================================= */ + /* This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit integer pointer type. This violates the strict aliasing rule, where a @@ -87,62 +61,33 @@ uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t /* ========================================================================= */ #if BYTE_ORDER == LITTLE_ENDIAN -#define DOLIT4 c ^= *buf4++; \ - c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ - crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] -#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 - -/* ========================================================================= */ -Z_INTERNAL uint32_t crc32_little(uint32_t crc, const unsigned char *buf, uint64_t len) { - Z_REGISTER uint32_t c; - Z_REGISTER const uint32_t *buf4; - - c = crc; - c = ~c; - while (len && ((ptrdiff_t)buf & 3)) { - c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); - len--; - } - - buf4 = (const uint32_t *)(const void *)buf; - -#ifdef UNROLL_MORE - while (len >= 32) { - DOLIT32; - len -= 32; - } +#define DOSWAP(crc) (crc) +#define DO1 \ + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8) +#define DO4 c ^= *buf4++; \ + c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ + crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] +#elif BYTE_ORDER == BIG_ENDIAN +#define DOSWAP(crc) ZSWAP32(crc) +#define DO1 \ + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8) +#define DO4 c ^= *buf4++; \ + c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ + crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] +#else +# error "No endian defined" #endif - - while (len >= 4) { - DOLIT4; - len -= 4; - } - buf = (const unsigned char *)buf4; - - if (len) do { - c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); - } while (--len); - c = ~c; - return c; -} -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ - -/* ========================================================================= */ -#if BYTE_ORDER == BIG_ENDIAN -#define DOBIG4 c ^= *buf4++; \ - c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ - crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] -#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 +#define DO32 DO4; DO4; DO4; DO4; DO4; DO4; DO4; DO4 /* ========================================================================= */ -Z_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, uint64_t len) { +Z_INTERNAL uint32_t crc32_byfour(uint32_t crc, const unsigned char *buf, uint64_t len) { Z_REGISTER uint32_t c; Z_REGISTER const uint32_t *buf4; - c = ZSWAP32(crc); + c = DOSWAP(crc); c = ~c; while (len && ((ptrdiff_t)buf & 3)) { - c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + DO1; len--; } @@ -150,21 +95,20 @@ Z_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, uint64_t l #ifdef UNROLL_MORE while (len >= 32) { - DOBIG32; + DO32; len -= 32; } #endif while (len >= 4) { - DOBIG4; + DO4; len -= 4; } buf = (const unsigned char *)buf4; if (len) do { - c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + DO1; } while (--len); c = ~c; - return ZSWAP32(c); + return DOSWAP(c); } -#endif /* BYTE_ORDER == BIG_ENDIAN */ diff --git a/crc32_p.h b/crc32_p.h index fbaf8db88..371ddf137 100644 --- a/crc32_p.h +++ b/crc32_p.h @@ -19,10 +19,6 @@ static inline uint32_t gf2_matrix_times(const uint32_t *mat, uint32_t vec) { } -#if BYTE_ORDER == LITTLE_ENDIAN -extern uint32_t crc32_little(uint32_t, const unsigned char *, uint64_t); -#elif BYTE_ORDER == BIG_ENDIAN -extern uint32_t crc32_big(uint32_t, const unsigned char *, uint64_t); -#endif +extern uint32_t crc32_byfour(uint32_t, const unsigned char *, uint64_t); #endif /* CRC32_P_H_ */ diff --git a/functable.c b/functable.c index ccb0b69a3..1c113c8db 100644 --- a/functable.c +++ b/functable.c @@ -528,13 +528,7 @@ Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t Assert(sizeof(uint64_t) >= sizeof(size_t), "crc32_z takes size_t but internally we have a uint64_t len"); -#if BYTE_ORDER == LITTLE_ENDIAN - functable.crc32 = &crc32_little; -#elif BYTE_ORDER == BIG_ENDIAN - functable.crc32 = &crc32_big; -#else - functable.crc32 = &crc32_generic; -#endif + functable.crc32 = &crc32_byfour; cpu_check_features(); #ifdef ARM_ACLE_CRC_HASH if (arm_cpu_has_crc32)