From 7d45cf5a1dbe9d34f9fb18e2f485efda83019493 Mon Sep 17 00:00:00 2001 From: Mark Adler Date: Sat, 11 Feb 2012 00:26:38 -0800 Subject: [PATCH] Use optimized byte swap operations for Microsoft and GNU [Snyder]. --- crc32.c | 10 ++++------ inflate.c | 9 ++------- zutil.h | 13 +++++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/crc32.c b/crc32.c index ddf0025c..95225ef5 100644 --- 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 */ diff --git a/inflate.c b/inflate.c index 0885c1d2..70cd7d91 100644 --- 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 dff1112f..2f4d14d2 100644 --- 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 +# pragma intrinsic(_byteswap_ulong) +# define ZSWAP32(q) _byteswap_ulong(q) +#elif defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) +# include +# 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 */