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

This commit is contained in:
Mark Adler 2012-02-11 00:26:38 -08:00
parent 1a4ba8cd91
commit 7d45cf5a1d
3 changed files with 19 additions and 13 deletions

10
crc32.c
View File

@ -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 */

View File

@ -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;

13
zutil.h
View File

@ -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 */