aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2012-02-11 00:26:38 -0800
committerMark Adler <madler@alumni.caltech.edu>2012-02-11 00:26:38 -0800
commit7d45cf5a1dbe9d34f9fb18e2f485efda83019493 (patch)
tree290e01d92e398389e83c943ca26e4c9201d3a5bb
parent1a4ba8cd912466fe538f62d61fbcc25eead6d31a (diff)
downloadzlib-7d45cf5a1dbe9d34f9fb18e2f485efda83019493.tar.gz
zlib-7d45cf5a1dbe9d34f9fb18e2f485efda83019493.tar.bz2
zlib-7d45cf5a1dbe9d34f9fb18e2f485efda83019493.zip
Use optimized byte swap operations for Microsoft and GNU [Snyder].
-rw-r--r--crc32.c10
-rw-r--r--inflate.c9
-rw-r--r--zutil.h13
3 files changed, 19 insertions, 13 deletions
diff --git a/crc32.c b/crc32.c
index ddf0025..95225ef 100644
--- a/crc32.c
+++ b/crc32.c
@@ -59,8 +59,6 @@
59/* Definitions for doing the crc four data bytes at a time. */ 59/* Definitions for doing the crc four data bytes at a time. */
60#ifdef BYFOUR 60#ifdef BYFOUR
61 typedef u4 crc_table_t; 61 typedef u4 crc_table_t;
62# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
63 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
64 local unsigned long crc32_little OF((unsigned long, 62 local unsigned long crc32_little OF((unsigned long,
65 const unsigned char FAR *, unsigned)); 63 const unsigned char FAR *, unsigned));
66 local unsigned long crc32_big OF((unsigned long, 64 local unsigned long crc32_big OF((unsigned long,
@@ -145,11 +143,11 @@ local void make_crc_table()
145 and then the byte reversal of those as well as the first table */ 143 and then the byte reversal of those as well as the first table */
146 for (n = 0; n < 256; n++) { 144 for (n = 0; n < 256; n++) {
147 c = crc_table[0][n]; 145 c = crc_table[0][n];
148 crc_table[4][n] = REV(c); 146 crc_table[4][n] = ZSWAP32(c);
149 for (k = 1; k < 4; k++) { 147 for (k = 1; k < 4; k++) {
150 c = crc_table[0][c & 0xff] ^ (c >> 8); 148 c = crc_table[0][c & 0xff] ^ (c >> 8);
151 crc_table[k][n] = c; 149 crc_table[k][n] = c;
152 crc_table[k + 4][n] = REV(c); 150 crc_table[k + 4][n] = ZSWAP32(c);
153 } 151 }
154 } 152 }
155#endif /* BYFOUR */ 153#endif /* BYFOUR */
@@ -317,7 +315,7 @@ local unsigned long crc32_big(crc, buf, len)
317 register u4 c; 315 register u4 c;
318 register const u4 FAR *buf4; 316 register const u4 FAR *buf4;
319 317
320 c = REV((u4)crc); 318 c = ZSWAP32((u4)crc);
321 c = ~c; 319 c = ~c;
322 while (len && ((ptrdiff_t)buf & 3)) { 320 while (len && ((ptrdiff_t)buf & 3)) {
323 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 321 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
@@ -341,7 +339,7 @@ local unsigned long crc32_big(crc, buf, len)
341 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 339 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
342 } while (--len); 340 } while (--len);
343 c = ~c; 341 c = ~c;
344 return (unsigned long)(REV(c)); 342 return (unsigned long)(ZSWAP32(c));
345} 343}
346 344
347#endif /* BYFOUR */ 345#endif /* BYFOUR */
diff --git a/inflate.c b/inflate.c
index 0885c1d..70cd7d9 100644
--- a/inflate.c
+++ b/inflate.c
@@ -519,11 +519,6 @@ unsigned out;
519 bits -= bits & 7; \ 519 bits -= bits & 7; \
520 } while (0) 520 } while (0)
521 521
522/* Reverse the bytes in a 32-bit value */
523#define REVERSE(q) \
524 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
525 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
526
527/* 522/*
528 inflate() uses a state machine to process as much input data and generate as 523 inflate() uses a state machine to process as much input data and generate as
529 much output data as possible before returning. The state machine is 524 much output data as possible before returning. The state machine is
@@ -817,7 +812,7 @@ int flush;
817#endif 812#endif
818 case DICTID: 813 case DICTID:
819 NEEDBITS(32); 814 NEEDBITS(32);
820 strm->adler = state->check = REVERSE(hold); 815 strm->adler = state->check = ZSWAP32(hold);
821 INITBITS(); 816 INITBITS();
822 state->mode = DICT; 817 state->mode = DICT;
823 case DICT: 818 case DICT:
@@ -1189,7 +1184,7 @@ int flush;
1189#ifdef GUNZIP 1184#ifdef GUNZIP
1190 state->flags ? hold : 1185 state->flags ? hold :
1191#endif 1186#endif
1192 REVERSE(hold)) != state->check) { 1187 ZSWAP32(hold)) != state->check) {
1193 strm->msg = (char *)"incorrect data check"; 1188 strm->msg = (char *)"incorrect data check";
1194 state->mode = BAD; 1189 state->mode = BAD;
1195 break; 1190 break;
diff --git a/zutil.h b/zutil.h
index dff1112..2f4d14d 100644
--- a/zutil.h
+++ b/zutil.h
@@ -245,4 +245,17 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
245#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 245#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
246#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 246#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
247 247
248/* Reverse the bytes in a 64-bit or 32-bit or 16-bit value */
249#if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_X64))
250# include <stdlib.h>
251# pragma intrinsic(_byteswap_ulong)
252# define ZSWAP32(q) _byteswap_ulong(q)
253#elif defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
254# include <byteswap.h>
255# define ZSWAP32(q) __builtin_bswap32(q)
256#else
257# define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
258 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
259#endif
260
248#endif /* ZUTIL_H */ 261#endif /* ZUTIL_H */