cannam@89: /* inffas8664.c is a hand tuned assembler version of inffast.c - fast decoding cannam@89: * version for AMD64 on Windows using Microsoft C compiler cannam@89: * cannam@89: * Copyright (C) 1995-2003 Mark Adler cannam@89: * For conditions of distribution and use, see copyright notice in zlib.h cannam@89: * cannam@89: * Copyright (C) 2003 Chris Anderson cannam@89: * Please use the copyright conditions above. cannam@89: * cannam@89: * 2005 - Adaptation to Microsoft C Compiler for AMD64 by Gilles Vollant cannam@89: * cannam@89: * inffas8664.c call function inffas8664fnc in inffasx64.asm cannam@89: * inffasx64.asm is automatically convert from AMD64 portion of inffas86.c cannam@89: * cannam@89: * Dec-29-2003 -- I added AMD64 inflate asm support. This version is also cannam@89: * slightly quicker on x86 systems because, instead of using rep movsb to copy cannam@89: * data, it uses rep movsw, which moves data in 2-byte chunks instead of single cannam@89: * bytes. I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates cannam@89: * from http://fedora.linux.duke.edu/fc1_x86_64 cannam@89: * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with cannam@89: * 1GB ram. The 64-bit version is about 4% faster than the 32-bit version, cannam@89: * when decompressing mozilla-source-1.3.tar.gz. cannam@89: * cannam@89: * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from cannam@89: * the gcc -S output of zlib-1.2.0/inffast.c. Zlib-1.2.0 is in beta release at cannam@89: * the moment. I have successfully compiled and tested this code with gcc2.96, cannam@89: * gcc3.2, icc5.0, msvc6.0. It is very close to the speed of inffast.S cannam@89: * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX cannam@89: * enabled. I will attempt to merge the MMX code into this version. Newer cannam@89: * versions of this and inffast.S can be found at cannam@89: * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/ cannam@89: * cannam@89: */ cannam@89: cannam@89: #include cannam@89: #include "zutil.h" cannam@89: #include "inftrees.h" cannam@89: #include "inflate.h" cannam@89: #include "inffast.h" cannam@89: cannam@89: /* Mark Adler's comments from inffast.c: */ cannam@89: cannam@89: /* cannam@89: Decode literal, length, and distance codes and write out the resulting cannam@89: literal and match bytes until either not enough input or output is cannam@89: available, an end-of-block is encountered, or a data error is encountered. cannam@89: When large enough input and output buffers are supplied to inflate(), for cannam@89: example, a 16K input buffer and a 64K output buffer, more than 95% of the cannam@89: inflate execution time is spent in this routine. cannam@89: cannam@89: Entry assumptions: cannam@89: cannam@89: state->mode == LEN cannam@89: strm->avail_in >= 6 cannam@89: strm->avail_out >= 258 cannam@89: start >= strm->avail_out cannam@89: state->bits < 8 cannam@89: cannam@89: On return, state->mode is one of: cannam@89: cannam@89: LEN -- ran out of enough output space or enough available input cannam@89: TYPE -- reached end of block code, inflate() to interpret next block cannam@89: BAD -- error in block data cannam@89: cannam@89: Notes: cannam@89: cannam@89: - The maximum input bits used by a length/distance pair is 15 bits for the cannam@89: length code, 5 bits for the length extra, 15 bits for the distance code, cannam@89: and 13 bits for the distance extra. This totals 48 bits, or six bytes. cannam@89: Therefore if strm->avail_in >= 6, then there is enough input to avoid cannam@89: checking for available input while decoding. cannam@89: cannam@89: - The maximum bytes that a single length/distance pair can output is 258 cannam@89: bytes, which is the maximum length that can be coded. inflate_fast() cannam@89: requires strm->avail_out >= 258 for each loop to avoid checking for cannam@89: output space. cannam@89: */ cannam@89: cannam@89: cannam@89: cannam@89: typedef struct inffast_ar { cannam@89: /* 64 32 x86 x86_64 */ cannam@89: /* ar offset register */ cannam@89: /* 0 0 */ void *esp; /* esp save */ cannam@89: /* 8 4 */ void *ebp; /* ebp save */ cannam@89: /* 16 8 */ unsigned char FAR *in; /* esi rsi local strm->next_in */ cannam@89: /* 24 12 */ unsigned char FAR *last; /* r9 while in < last */ cannam@89: /* 32 16 */ unsigned char FAR *out; /* edi rdi local strm->next_out */ cannam@89: /* 40 20 */ unsigned char FAR *beg; /* inflate()'s init next_out */ cannam@89: /* 48 24 */ unsigned char FAR *end; /* r10 while out < end */ cannam@89: /* 56 28 */ unsigned char FAR *window;/* size of window, wsize!=0 */ cannam@89: /* 64 32 */ code const FAR *lcode; /* ebp rbp local strm->lencode */ cannam@89: /* 72 36 */ code const FAR *dcode; /* r11 local strm->distcode */ cannam@89: /* 80 40 */ size_t /*unsigned long */hold; /* edx rdx local strm->hold */ cannam@89: /* 88 44 */ unsigned bits; /* ebx rbx local strm->bits */ cannam@89: /* 92 48 */ unsigned wsize; /* window size */ cannam@89: /* 96 52 */ unsigned write; /* window write index */ cannam@89: /*100 56 */ unsigned lmask; /* r12 mask for lcode */ cannam@89: /*104 60 */ unsigned dmask; /* r13 mask for dcode */ cannam@89: /*108 64 */ unsigned len; /* r14 match length */ cannam@89: /*112 68 */ unsigned dist; /* r15 match distance */ cannam@89: /*116 72 */ unsigned status; /* set when state chng*/ cannam@89: } type_ar; cannam@89: #ifdef ASMINF cannam@89: cannam@89: void inflate_fast(strm, start) cannam@89: z_streamp strm; cannam@89: unsigned start; /* inflate()'s starting value for strm->avail_out */ cannam@89: { cannam@89: struct inflate_state FAR *state; cannam@89: type_ar ar; cannam@89: void inffas8664fnc(struct inffast_ar * par); cannam@89: cannam@89: cannam@89: cannam@89: #if (defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )) || (defined(_MSC_VER) && defined(_M_AMD64)) cannam@89: #define PAD_AVAIL_IN 6 cannam@89: #define PAD_AVAIL_OUT 258 cannam@89: #else cannam@89: #define PAD_AVAIL_IN 5 cannam@89: #define PAD_AVAIL_OUT 257 cannam@89: #endif cannam@89: cannam@89: /* copy state to local variables */ cannam@89: state = (struct inflate_state FAR *)strm->state; cannam@89: cannam@89: ar.in = strm->next_in; cannam@89: ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN); cannam@89: ar.out = strm->next_out; cannam@89: ar.beg = ar.out - (start - strm->avail_out); cannam@89: ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT); cannam@89: ar.wsize = state->wsize; cannam@89: ar.write = state->wnext; cannam@89: ar.window = state->window; cannam@89: ar.hold = state->hold; cannam@89: ar.bits = state->bits; cannam@89: ar.lcode = state->lencode; cannam@89: ar.dcode = state->distcode; cannam@89: ar.lmask = (1U << state->lenbits) - 1; cannam@89: ar.dmask = (1U << state->distbits) - 1; cannam@89: cannam@89: /* decode literals and length/distances until end-of-block or not enough cannam@89: input data or output space */ cannam@89: cannam@89: /* align in on 1/2 hold size boundary */ cannam@89: while (((size_t)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) { cannam@89: ar.hold += (unsigned long)*ar.in++ << ar.bits; cannam@89: ar.bits += 8; cannam@89: } cannam@89: cannam@89: inffas8664fnc(&ar); cannam@89: cannam@89: if (ar.status > 1) { cannam@89: if (ar.status == 2) cannam@89: strm->msg = "invalid literal/length code"; cannam@89: else if (ar.status == 3) cannam@89: strm->msg = "invalid distance code"; cannam@89: else cannam@89: strm->msg = "invalid distance too far back"; cannam@89: state->mode = BAD; cannam@89: } cannam@89: else if ( ar.status == 1 ) { cannam@89: state->mode = TYPE; cannam@89: } cannam@89: cannam@89: /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ cannam@89: ar.len = ar.bits >> 3; cannam@89: ar.in -= ar.len; cannam@89: ar.bits -= ar.len << 3; cannam@89: ar.hold &= (1U << ar.bits) - 1; cannam@89: cannam@89: /* update state and return */ cannam@89: strm->next_in = ar.in; cannam@89: strm->next_out = ar.out; cannam@89: strm->avail_in = (unsigned)(ar.in < ar.last ? cannam@89: PAD_AVAIL_IN + (ar.last - ar.in) : cannam@89: PAD_AVAIL_IN - (ar.in - ar.last)); cannam@89: strm->avail_out = (unsigned)(ar.out < ar.end ? cannam@89: PAD_AVAIL_OUT + (ar.end - ar.out) : cannam@89: PAD_AVAIL_OUT - (ar.out - ar.end)); cannam@89: state->hold = (unsigned long)ar.hold; cannam@89: state->bits = ar.bits; cannam@89: return; cannam@89: } cannam@89: cannam@89: #endif