1130803Smarcel/* inffas8664.c is a hand tuned assembler version of inffast.c - fast decoding 2130803Smarcel * version for AMD64 on Windows using Microsoft C compiler 3130803Smarcel * 4130803Smarcel * Copyright (C) 1995-2003 Mark Adler 5130803Smarcel * For conditions of distribution and use, see copyright notice in zlib.h 6130803Smarcel * 7130803Smarcel * Copyright (C) 2003 Chris Anderson <christop@charm.net> 8130803Smarcel * Please use the copyright conditions above. 9130803Smarcel * 10130803Smarcel * 2005 - Adaptation to Microsoft C Compiler for AMD64 by Gilles Vollant 11130803Smarcel * 12130803Smarcel * inffas8664.c call function inffas8664fnc in inffasx64.asm 13130803Smarcel * inffasx64.asm is automatically convert from AMD64 portion of inffas86.c 14130803Smarcel * 15130803Smarcel * Dec-29-2003 -- I added AMD64 inflate asm support. This version is also 16130803Smarcel * slightly quicker on x86 systems because, instead of using rep movsb to copy 17130803Smarcel * data, it uses rep movsw, which moves data in 2-byte chunks instead of single 18130803Smarcel * bytes. I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates 19130803Smarcel * from http://fedora.linux.duke.edu/fc1_x86_64 20130803Smarcel * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with 21130803Smarcel * 1GB ram. The 64-bit version is about 4% faster than the 32-bit version, 22130803Smarcel * when decompressing mozilla-source-1.3.tar.gz. 23130803Smarcel * 24130803Smarcel * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from 25130803Smarcel * the gcc -S output of zlib-1.2.0/inffast.c. Zlib-1.2.0 is in beta release at 26130803Smarcel * the moment. I have successfully compiled and tested this code with gcc2.96, 27130803Smarcel * gcc3.2, icc5.0, msvc6.0. It is very close to the speed of inffast.S 28130803Smarcel * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX 29130803Smarcel * enabled. I will attempt to merge the MMX code into this version. Newer 30130803Smarcel * versions of this and inffast.S can be found at 31130803Smarcel * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/ 32130803Smarcel * 33130803Smarcel */ 34130803Smarcel 35130803Smarcel#include <stdio.h> 36130803Smarcel#include "zutil.h" 37130803Smarcel#include "inftrees.h" 38130803Smarcel#include "inflate.h" 39130803Smarcel#include "inffast.h" 40130803Smarcel 41130803Smarcel/* Mark Adler's comments from inffast.c: */ 42130803Smarcel 43130803Smarcel/* 44130803Smarcel Decode literal, length, and distance codes and write out the resulting 45130803Smarcel literal and match bytes until either not enough input or output is 46130803Smarcel available, an end-of-block is encountered, or a data error is encountered. 47130803Smarcel When large enough input and output buffers are supplied to inflate(), for 48130803Smarcel example, a 16K input buffer and a 64K output buffer, more than 95% of the 49130803Smarcel inflate execution time is spent in this routine. 50130803Smarcel 51130803Smarcel Entry assumptions: 52130803Smarcel 53130803Smarcel state->mode == LEN 54130803Smarcel strm->avail_in >= 6 55130803Smarcel strm->avail_out >= 258 56130803Smarcel start >= strm->avail_out 57130803Smarcel state->bits < 8 58130803Smarcel 59130803Smarcel On return, state->mode is one of: 60130803Smarcel 61130803Smarcel LEN -- ran out of enough output space or enough available input 62130803Smarcel TYPE -- reached end of block code, inflate() to interpret next block 63130803Smarcel BAD -- error in block data 64130803Smarcel 65130803Smarcel Notes: 66130803Smarcel 67130803Smarcel - The maximum input bits used by a length/distance pair is 15 bits for the 68130803Smarcel length code, 5 bits for the length extra, 15 bits for the distance code, 69130803Smarcel and 13 bits for the distance extra. This totals 48 bits, or six bytes. 70130803Smarcel Therefore if strm->avail_in >= 6, then there is enough input to avoid 71130803Smarcel checking for available input while decoding. 72130803Smarcel 73130803Smarcel - The maximum bytes that a single length/distance pair can output is 258 74130803Smarcel bytes, which is the maximum length that can be coded. inflate_fast() 75130803Smarcel requires strm->avail_out >= 258 for each loop to avoid checking for 76130803Smarcel output space. 77130803Smarcel */ 78130803Smarcel 79130803Smarcel 80130803Smarcel 81130803Smarcel typedef struct inffast_ar { 82130803Smarcel/* 64 32 x86 x86_64 */ 83130803Smarcel/* ar offset register */ 84130803Smarcel/* 0 0 */ void *esp; /* esp save */ 85130803Smarcel/* 8 4 */ void *ebp; /* ebp save */ 86130803Smarcel/* 16 8 */ unsigned char FAR *in; /* esi rsi local strm->next_in */ 87130803Smarcel/* 24 12 */ unsigned char FAR *last; /* r9 while in < last */ 88130803Smarcel/* 32 16 */ unsigned char FAR *out; /* edi rdi local strm->next_out */ 89130803Smarcel/* 40 20 */ unsigned char FAR *beg; /* inflate()'s init next_out */ 90130803Smarcel/* 48 24 */ unsigned char FAR *end; /* r10 while out < end */ 91130803Smarcel/* 56 28 */ unsigned char FAR *window;/* size of window, wsize!=0 */ 92130803Smarcel/* 64 32 */ code const FAR *lcode; /* ebp rbp local strm->lencode */ 93130803Smarcel/* 72 36 */ code const FAR *dcode; /* r11 local strm->distcode */ 94130803Smarcel/* 80 40 */ size_t /*unsigned long */hold; /* edx rdx local strm->hold */ 95130803Smarcel/* 88 44 */ unsigned bits; /* ebx rbx local strm->bits */ 96130803Smarcel/* 92 48 */ unsigned wsize; /* window size */ 97130803Smarcel/* 96 52 */ unsigned write; /* window write index */ 98130803Smarcel/*100 56 */ unsigned lmask; /* r12 mask for lcode */ 99130803Smarcel/*104 60 */ unsigned dmask; /* r13 mask for dcode */ 100130803Smarcel/*108 64 */ unsigned len; /* r14 match length */ 101130803Smarcel/*112 68 */ unsigned dist; /* r15 match distance */ 102130803Smarcel/*116 72 */ unsigned status; /* set when state chng*/ 103130803Smarcel } type_ar; 104130803Smarcel#ifdef ASMINF 105130803Smarcel 106130803Smarcelvoid inflate_fast(strm, start) 107130803Smarcelz_streamp strm; 108130803Smarcelunsigned start; /* inflate()'s starting value for strm->avail_out */ 109130803Smarcel{ 110130803Smarcel struct inflate_state FAR *state; 111130803Smarcel type_ar ar; 112130803Smarcel void inffas8664fnc(struct inffast_ar * par); 113130803Smarcel 114130803Smarcel 115130803Smarcel 116130803Smarcel#if (defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )) || (defined(_MSC_VER) && defined(_M_AMD64)) 117130803Smarcel#define PAD_AVAIL_IN 6 118130803Smarcel#define PAD_AVAIL_OUT 258 119130803Smarcel#else 120130803Smarcel#define PAD_AVAIL_IN 5 121130803Smarcel#define PAD_AVAIL_OUT 257 122130803Smarcel#endif 123130803Smarcel 124130803Smarcel /* copy state to local variables */ 125130803Smarcel state = (struct inflate_state FAR *)strm->state; 126130803Smarcel 127130803Smarcel ar.in = strm->next_in; 128130803Smarcel ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN); 129130803Smarcel ar.out = strm->next_out; 130130803Smarcel ar.beg = ar.out - (start - strm->avail_out); 131130803Smarcel ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT); 132130803Smarcel ar.wsize = state->wsize; 133130803Smarcel ar.write = state->wnext; 134130803Smarcel ar.window = state->window; 135130803Smarcel ar.hold = state->hold; 136130803Smarcel ar.bits = state->bits; 137130803Smarcel ar.lcode = state->lencode; 138130803Smarcel ar.dcode = state->distcode; 139130803Smarcel ar.lmask = (1U << state->lenbits) - 1; 140130803Smarcel ar.dmask = (1U << state->distbits) - 1; 141130803Smarcel 142130803Smarcel /* decode literals and length/distances until end-of-block or not enough 143130803Smarcel input data or output space */ 144130803Smarcel 145130803Smarcel /* align in on 1/2 hold size boundary */ 146130803Smarcel while (((size_t)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) { 147130803Smarcel ar.hold += (unsigned long)*ar.in++ << ar.bits; 148130803Smarcel ar.bits += 8; 149130803Smarcel } 150130803Smarcel 151130803Smarcel inffas8664fnc(&ar); 152130803Smarcel 153130803Smarcel if (ar.status > 1) { 154130803Smarcel if (ar.status == 2) 155130803Smarcel strm->msg = "invalid literal/length code"; 156130803Smarcel else if (ar.status == 3) 157130803Smarcel strm->msg = "invalid distance code"; 158130803Smarcel else 159130803Smarcel strm->msg = "invalid distance too far back"; 160130803Smarcel state->mode = BAD; 161130803Smarcel } 162130803Smarcel else if ( ar.status == 1 ) { 163130803Smarcel state->mode = TYPE; 164130803Smarcel } 165130803Smarcel 166130803Smarcel /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ 167130803Smarcel ar.len = ar.bits >> 3; 168130803Smarcel ar.in -= ar.len; 169130803Smarcel ar.bits -= ar.len << 3; 170130803Smarcel ar.hold &= (1U << ar.bits) - 1; 171130803Smarcel 172130803Smarcel /* update state and return */ 173130803Smarcel strm->next_in = ar.in; 174130803Smarcel strm->next_out = ar.out; 175130803Smarcel strm->avail_in = (unsigned)(ar.in < ar.last ? 176130803Smarcel PAD_AVAIL_IN + (ar.last - ar.in) : 177130803Smarcel PAD_AVAIL_IN - (ar.in - ar.last)); 178130803Smarcel strm->avail_out = (unsigned)(ar.out < ar.end ? 179130803Smarcel PAD_AVAIL_OUT + (ar.end - ar.out) : 180130803Smarcel PAD_AVAIL_OUT - (ar.out - ar.end)); 181130803Smarcel state->hold = (unsigned long)ar.hold; 182130803Smarcel state->bits = ar.bits; 183130803Smarcel return; 184130803Smarcel} 185130803Smarcel 186130803Smarcel#endif 187130803Smarcel