]> git.lizzy.rs Git - zlib.git/commitdiff
Use optimized byte swap operations for Microsoft and GNU [Snyder].
authorMark Adler <madler@alumni.caltech.edu>
Sat, 11 Feb 2012 08:26:38 +0000 (00:26 -0800)
committerMark Adler <madler@alumni.caltech.edu>
Sat, 11 Feb 2012 08:26:38 +0000 (00:26 -0800)
crc32.c
inflate.c
zutil.h

diff --git a/crc32.c b/crc32.c
index ddf0025c054c2ac8b7ebe0e56d99bf6923e34938..95225ef574ea11fa8a91739a373e81a18ff5bb6c 100644 (file)
--- a/crc32.c
+++ b/crc32.c
@@ -59,8 +59,6 @@
 /* Definitions for doing the crc four data bytes at a time. */
 #ifdef BYFOUR
    typedef u4 crc_table_t;
-#  define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
-                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
    local unsigned long crc32_little OF((unsigned long,
                         const unsigned char FAR *, unsigned));
    local unsigned long crc32_big OF((unsigned long,
@@ -145,11 +143,11 @@ local void make_crc_table()
            and then the byte reversal of those as well as the first table */
         for (n = 0; n < 256; n++) {
             c = crc_table[0][n];
-            crc_table[4][n] = REV(c);
+            crc_table[4][n] = ZSWAP32(c);
             for (k = 1; k < 4; k++) {
                 c = crc_table[0][c & 0xff] ^ (c >> 8);
                 crc_table[k][n] = c;
-                crc_table[k + 4][n] = REV(c);
+                crc_table[k + 4][n] = ZSWAP32(c);
             }
         }
 #endif /* BYFOUR */
@@ -317,7 +315,7 @@ local unsigned long crc32_big(crc, buf, len)
     register u4 c;
     register const u4 FAR *buf4;
 
-    c = REV((u4)crc);
+    c = ZSWAP32((u4)crc);
     c = ~c;
     while (len && ((ptrdiff_t)buf & 3)) {
         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
@@ -341,7 +339,7 @@ local unsigned long crc32_big(crc, buf, len)
         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
     } while (--len);
     c = ~c;
-    return (unsigned long)(REV(c));
+    return (unsigned long)(ZSWAP32(c));
 }
 
 #endif /* BYFOUR */
index 0885c1d268704d7680ec862596e25f98ce8ec0a0..70cd7d91baa1f906474d1d0ac35873fe152c5b5a 100644 (file)
--- a/inflate.c
+++ b/inflate.c
@@ -519,11 +519,6 @@ unsigned out;
         bits -= bits & 7; \
     } while (0)
 
-/* Reverse the bytes in a 32-bit value */
-#define REVERSE(q) \
-    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
-     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
-
 /*
    inflate() uses a state machine to process as much input data and generate as
    much output data as possible before returning.  The state machine is
@@ -817,7 +812,7 @@ int flush;
 #endif
         case DICTID:
             NEEDBITS(32);
-            strm->adler = state->check = REVERSE(hold);
+            strm->adler = state->check = ZSWAP32(hold);
             INITBITS();
             state->mode = DICT;
         case DICT:
@@ -1189,7 +1184,7 @@ int flush;
 #ifdef GUNZIP
                      state->flags ? hold :
 #endif
-                     REVERSE(hold)) != state->check) {
+                     ZSWAP32(hold)) != state->check) {
                     strm->msg = (char *)"incorrect data check";
                     state->mode = BAD;
                     break;
diff --git a/zutil.h b/zutil.h
index dff1112febd2ce4cd4fed3ed5db8ee1fef6aaa13..2f4d14d2b45daa2782c8f3da7c7d00a2c891be36 100644 (file)
--- a/zutil.h
+++ b/zutil.h
@@ -245,4 +245,17 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
 
+/* Reverse the bytes in a 64-bit or 32-bit or 16-bit value */
+#if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_X64))
+#  include <stdlib.h>
+#  pragma intrinsic(_byteswap_ulong)
+#  define ZSWAP32(q) _byteswap_ulong(q)
+#elif defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
+#  include <byteswap.h>
+#  define ZSWAP32(q) __builtin_bswap32(q)
+#else
+#  define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
+                             (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+#endif
+
 #endif /* ZUTIL_H */