]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Merge crc32_little and crc32_big with preprocessor macros for each endian. Removed...
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 9 Jan 2022 15:35:10 +0000 (07:35 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sat, 15 Jan 2022 11:29:57 +0000 (12:29 +0100)
arch/s390/crc32-vx.c
crc32.c
crc32_p.h
functable.c

index 18b8bb223555213b653adb3044a0d37832006b22..3465a573233f938461ae7768c2da82f1699fb986 100644 (file)
@@ -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 c9a81d37b07ebe54ed2a2da78058a64ec411783a..b962fb6b50720772f9cc2fb128a1d26386d72348 100644 (file)
--- 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 */
index fbaf8db88c3b3b10b140dbd5fc7c84b8ad7307f1..371ddf1374ba0f82c564d8103c40ba8253984b76 100644 (file)
--- 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_ */
index ccb0b69a302a5277e82151569187b6d7d221648f..1c113c8db880cb2c9491deb668a8e12f1c01d2c2 100644 (file)
@@ -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)