提交 7d45cf5a 编写于 作者: M Mark Adler

Use optimized byte swap operations for Microsoft and GNU [Snyder].

上级 1a4ba8cd
...@@ -59,8 +59,6 @@ ...@@ -59,8 +59,6 @@
/* Definitions for doing the crc four data bytes at a time. */ /* Definitions for doing the crc four data bytes at a time. */
#ifdef BYFOUR #ifdef BYFOUR
typedef u4 crc_table_t; 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, local unsigned long crc32_little OF((unsigned long,
const unsigned char FAR *, unsigned)); const unsigned char FAR *, unsigned));
local unsigned long crc32_big OF((unsigned long, local unsigned long crc32_big OF((unsigned long,
...@@ -145,11 +143,11 @@ local void make_crc_table() ...@@ -145,11 +143,11 @@ local void make_crc_table()
and then the byte reversal of those as well as the first table */ and then the byte reversal of those as well as the first table */
for (n = 0; n < 256; n++) { for (n = 0; n < 256; n++) {
c = crc_table[0][n]; c = crc_table[0][n];
crc_table[4][n] = REV(c); crc_table[4][n] = ZSWAP32(c);
for (k = 1; k < 4; k++) { for (k = 1; k < 4; k++) {
c = crc_table[0][c & 0xff] ^ (c >> 8); c = crc_table[0][c & 0xff] ^ (c >> 8);
crc_table[k][n] = c; crc_table[k][n] = c;
crc_table[k + 4][n] = REV(c); crc_table[k + 4][n] = ZSWAP32(c);
} }
} }
#endif /* BYFOUR */ #endif /* BYFOUR */
...@@ -317,7 +315,7 @@ local unsigned long crc32_big(crc, buf, len) ...@@ -317,7 +315,7 @@ local unsigned long crc32_big(crc, buf, len)
register u4 c; register u4 c;
register const u4 FAR *buf4; register const u4 FAR *buf4;
c = REV((u4)crc); c = ZSWAP32((u4)crc);
c = ~c; c = ~c;
while (len && ((ptrdiff_t)buf & 3)) { while (len && ((ptrdiff_t)buf & 3)) {
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
...@@ -341,7 +339,7 @@ local unsigned long crc32_big(crc, buf, len) ...@@ -341,7 +339,7 @@ local unsigned long crc32_big(crc, buf, len)
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
} while (--len); } while (--len);
c = ~c; c = ~c;
return (unsigned long)(REV(c)); return (unsigned long)(ZSWAP32(c));
} }
#endif /* BYFOUR */ #endif /* BYFOUR */
......
...@@ -519,11 +519,6 @@ unsigned out; ...@@ -519,11 +519,6 @@ unsigned out;
bits -= bits & 7; \ bits -= bits & 7; \
} while (0) } 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 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 much output data as possible before returning. The state machine is
...@@ -817,7 +812,7 @@ int flush; ...@@ -817,7 +812,7 @@ int flush;
#endif #endif
case DICTID: case DICTID:
NEEDBITS(32); NEEDBITS(32);
strm->adler = state->check = REVERSE(hold); strm->adler = state->check = ZSWAP32(hold);
INITBITS(); INITBITS();
state->mode = DICT; state->mode = DICT;
case DICT: case DICT:
...@@ -1189,7 +1184,7 @@ int flush; ...@@ -1189,7 +1184,7 @@ int flush;
#ifdef GUNZIP #ifdef GUNZIP
state->flags ? hold : state->flags ? hold :
#endif #endif
REVERSE(hold)) != state->check) { ZSWAP32(hold)) != state->check) {
strm->msg = (char *)"incorrect data check"; strm->msg = (char *)"incorrect data check";
state->mode = BAD; state->mode = BAD;
break; break;
......
...@@ -245,4 +245,17 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ ...@@ -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 ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} #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 */ #endif /* ZUTIL_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册