00001 /* inffas8664.c is a hand tuned assembler version of inffast.c - fast decoding 00002 * version for AMD64 on Windows using Microsoft C compiler 00003 * 00004 * Copyright (C) 1995-2003 Mark Adler 00005 * For conditions of distribution and use, see copyright notice in zlib.h 00006 * 00007 * Copyright (C) 2003 Chris Anderson <christop@charm.net> 00008 * Please use the copyright conditions above. 00009 * 00010 * 2005 - Adaptation to Microsoft C Compiler for AMD64 by Gilles Vollant 00011 * 00012 * inffas8664.c call function inffas8664fnc in inffasx64.asm 00013 * inffasx64.asm is automatically convert from AMD64 portion of inffas86.c 00014 * 00015 * Dec-29-2003 -- I added AMD64 inflate asm support. This version is also 00016 * slightly quicker on x86 systems because, instead of using rep movsb to copy 00017 * data, it uses rep movsw, which moves data in 2-byte chunks instead of single 00018 * bytes. I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates 00019 * from http://fedora.linux.duke.edu/fc1_x86_64 00020 * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with 00021 * 1GB ram. The 64-bit version is about 4% faster than the 32-bit version, 00022 * when decompressing mozilla-source-1.3.tar.gz. 00023 * 00024 * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from 00025 * the gcc -S output of zlib-1.2.0/inffast.c. Zlib-1.2.0 is in beta release at 00026 * the moment. I have successfully compiled and tested this code with gcc2.96, 00027 * gcc3.2, icc5.0, msvc6.0. It is very close to the speed of inffast.S 00028 * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX 00029 * enabled. I will attempt to merge the MMX code into this version. Newer 00030 * versions of this and inffast.S can be found at 00031 * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/ 00032 * 00033 */ 00034 00035 #include <stdio.h> 00036 #include "zutil.h" 00037 #include "inftrees.h" 00038 #include "inflate.h" 00039 #include "inffast.h" 00040 00041 /* Mark Adler's comments from inffast.c: */ 00042 00043 /* 00044 Decode literal, length, and distance codes and write out the resulting 00045 literal and match bytes until either not enough input or output is 00046 available, an end-of-block is encountered, or a data error is encountered. 00047 When large enough input and output buffers are supplied to inflate(), for 00048 example, a 16K input buffer and a 64K output buffer, more than 95% of the 00049 inflate execution time is spent in this routine. 00050 00051 Entry assumptions: 00052 00053 state->mode == LEN 00054 strm->avail_in >= 6 00055 strm->avail_out >= 258 00056 start >= strm->avail_out 00057 state->bits < 8 00058 00059 On return, state->mode is one of: 00060 00061 LEN -- ran out of enough output space or enough available input 00062 TYPE -- reached end of block code, inflate() to interpret next block 00063 BAD -- error in block data 00064 00065 Notes: 00066 00067 - The maximum input bits used by a length/distance pair is 15 bits for the 00068 length code, 5 bits for the length extra, 15 bits for the distance code, 00069 and 13 bits for the distance extra. This totals 48 bits, or six bytes. 00070 Therefore if strm->avail_in >= 6, then there is enough input to avoid 00071 checking for available input while decoding. 00072 00073 - The maximum bytes that a single length/distance pair can output is 258 00074 bytes, which is the maximum length that can be coded. inflate_fast() 00075 requires strm->avail_out >= 258 for each loop to avoid checking for 00076 output space. 00077 */ 00078 00079 00080 00081 typedef struct inffast_ar { 00082 /* 64 32 x86 x86_64 */ 00083 /* ar offset register */ 00084 /* 0 0 */ void *esp; /* esp save */ 00085 /* 8 4 */ void *ebp; /* ebp save */ 00086 /* 16 8 */ unsigned char FAR *in; /* esi rsi local strm->next_in */ 00087 /* 24 12 */ unsigned char FAR *last; /* r9 while in < last */ 00088 /* 32 16 */ unsigned char FAR *out; /* edi rdi local strm->next_out */ 00089 /* 40 20 */ unsigned char FAR *beg; /* inflate()'s init next_out */ 00090 /* 48 24 */ unsigned char FAR *end; /* r10 while out < end */ 00091 /* 56 28 */ unsigned char FAR *window;/* size of window, wsize!=0 */ 00092 /* 64 32 */ code const FAR *lcode; /* ebp rbp local strm->lencode */ 00093 /* 72 36 */ code const FAR *dcode; /* r11 local strm->distcode */ 00094 /* 80 40 */ size_t /*unsigned long */hold; /* edx rdx local strm->hold */ 00095 /* 88 44 */ unsigned bits; /* ebx rbx local strm->bits */ 00096 /* 92 48 */ unsigned wsize; /* window size */ 00097 /* 96 52 */ unsigned write; /* window write index */ 00098 /*100 56 */ unsigned lmask; /* r12 mask for lcode */ 00099 /*104 60 */ unsigned dmask; /* r13 mask for dcode */ 00100 /*108 64 */ unsigned len; /* r14 match length */ 00101 /*112 68 */ unsigned dist; /* r15 match distance */ 00102 /*116 72 */ unsigned status; /* set when state chng*/ 00103 } type_ar; 00104 #ifdef ASMINF 00105 00106 void inflate_fast(strm, start) 00107 z_streamp strm; 00108 unsigned start; /* inflate()'s starting value for strm->avail_out */ 00109 { 00110 struct inflate_state FAR *state; 00111 type_ar ar; 00112 void inffas8664fnc(struct inffast_ar * par); 00113 00114 00115 00116 #if (defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )) || (defined(_MSC_VER) && defined(_M_AMD64)) 00117 #define PAD_AVAIL_IN 6 00118 #define PAD_AVAIL_OUT 258 00119 #else 00120 #define PAD_AVAIL_IN 5 00121 #define PAD_AVAIL_OUT 257 00122 #endif 00123 00124 /* copy state to local variables */ 00125 state = (struct inflate_state FAR *)strm->state; 00126 00127 ar.in = strm->next_in; 00128 ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN); 00129 ar.out = strm->next_out; 00130 ar.beg = ar.out - (start - strm->avail_out); 00131 ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT); 00132 ar.wsize = state->wsize; 00133 ar.write = state->wnext; 00134 ar.window = state->window; 00135 ar.hold = state->hold; 00136 ar.bits = state->bits; 00137 ar.lcode = state->lencode; 00138 ar.dcode = state->distcode; 00139 ar.lmask = (1U << state->lenbits) - 1; 00140 ar.dmask = (1U << state->distbits) - 1; 00141 00142 /* decode literals and length/distances until end-of-block or not enough 00143 input data or output space */ 00144 00145 /* align in on 1/2 hold size boundary */ 00146 while (((size_t)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) { 00147 ar.hold += (unsigned long)*ar.in++ << ar.bits; 00148 ar.bits += 8; 00149 } 00150 00151 inffas8664fnc(&ar); 00152 00153 if (ar.status > 1) { 00154 if (ar.status == 2) 00155 strm->msg = "invalid literal/length code"; 00156 else if (ar.status == 3) 00157 strm->msg = "invalid distance code"; 00158 else 00159 strm->msg = "invalid distance too far back"; 00160 state->mode = BAD; 00161 } 00162 else if ( ar.status == 1 ) { 00163 state->mode = TYPE; 00164 } 00165 00166 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ 00167 ar.len = ar.bits >> 3; 00168 ar.in -= ar.len; 00169 ar.bits -= ar.len << 3; 00170 ar.hold &= (1U << ar.bits) - 1; 00171 00172 /* update state and return */ 00173 strm->next_in = ar.in; 00174 strm->next_out = ar.out; 00175 strm->avail_in = (unsigned)(ar.in < ar.last ? 00176 PAD_AVAIL_IN + (ar.last - ar.in) : 00177 PAD_AVAIL_IN - (ar.in - ar.last)); 00178 strm->avail_out = (unsigned)(ar.out < ar.end ? 00179 PAD_AVAIL_OUT + (ar.end - ar.out) : 00180 PAD_AVAIL_OUT - (ar.out - ar.end)); 00181 state->hold = (unsigned long)ar.hold; 00182 state->bits = ar.bits; 00183 return; 00184 } 00185 00186 #endif