xref: /minix3/common/dist/zlib/contrib/inflate86/inffas86.c (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: inffas86.c,v 1.1.1.1 2006/01/14 20:10:53 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /* inffas86.c is a hand tuned assembler version of
4*44bedb31SLionel Sambuc  *
5*44bedb31SLionel Sambuc  * inffast.c -- fast decoding
6*44bedb31SLionel Sambuc  * Copyright (C) 1995-2003 Mark Adler
7*44bedb31SLionel Sambuc  * For conditions of distribution and use, see copyright notice in zlib.h
8*44bedb31SLionel Sambuc  *
9*44bedb31SLionel Sambuc  * Copyright (C) 2003 Chris Anderson <christop@charm.net>
10*44bedb31SLionel Sambuc  * Please use the copyright conditions above.
11*44bedb31SLionel Sambuc  *
12*44bedb31SLionel Sambuc  * Dec-29-2003 -- I added AMD64 inflate asm support.  This version is also
13*44bedb31SLionel Sambuc  * slightly quicker on x86 systems because, instead of using rep movsb to copy
14*44bedb31SLionel Sambuc  * data, it uses rep movsw, which moves data in 2-byte chunks instead of single
15*44bedb31SLionel Sambuc  * bytes.  I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates
16*44bedb31SLionel Sambuc  * from http://fedora.linux.duke.edu/fc1_x86_64
17*44bedb31SLionel Sambuc  * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with
18*44bedb31SLionel Sambuc  * 1GB ram.  The 64-bit version is about 4% faster than the 32-bit version,
19*44bedb31SLionel Sambuc  * when decompressing mozilla-source-1.3.tar.gz.
20*44bedb31SLionel Sambuc  *
21*44bedb31SLionel Sambuc  * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from
22*44bedb31SLionel Sambuc  * the gcc -S output of zlib-1.2.0/inffast.c.  Zlib-1.2.0 is in beta release at
23*44bedb31SLionel Sambuc  * the moment.  I have successfully compiled and tested this code with gcc2.96,
24*44bedb31SLionel Sambuc  * gcc3.2, icc5.0, msvc6.0.  It is very close to the speed of inffast.S
25*44bedb31SLionel Sambuc  * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX
26*44bedb31SLionel Sambuc  * enabled.  I will attempt to merge the MMX code into this version.  Newer
27*44bedb31SLionel Sambuc  * versions of this and inffast.S can be found at
28*44bedb31SLionel Sambuc  * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/
29*44bedb31SLionel Sambuc  */
30*44bedb31SLionel Sambuc 
31*44bedb31SLionel Sambuc #include "zutil.h"
32*44bedb31SLionel Sambuc #include "inftrees.h"
33*44bedb31SLionel Sambuc #include "inflate.h"
34*44bedb31SLionel Sambuc #include "inffast.h"
35*44bedb31SLionel Sambuc 
36*44bedb31SLionel Sambuc /* Mark Adler's comments from inffast.c: */
37*44bedb31SLionel Sambuc 
38*44bedb31SLionel Sambuc /*
39*44bedb31SLionel Sambuc    Decode literal, length, and distance codes and write out the resulting
40*44bedb31SLionel Sambuc    literal and match bytes until either not enough input or output is
41*44bedb31SLionel Sambuc    available, an end-of-block is encountered, or a data error is encountered.
42*44bedb31SLionel Sambuc    When large enough input and output buffers are supplied to inflate(), for
43*44bedb31SLionel Sambuc    example, a 16K input buffer and a 64K output buffer, more than 95% of the
44*44bedb31SLionel Sambuc    inflate execution time is spent in this routine.
45*44bedb31SLionel Sambuc 
46*44bedb31SLionel Sambuc    Entry assumptions:
47*44bedb31SLionel Sambuc 
48*44bedb31SLionel Sambuc         state->mode == LEN
49*44bedb31SLionel Sambuc         strm->avail_in >= 6
50*44bedb31SLionel Sambuc         strm->avail_out >= 258
51*44bedb31SLionel Sambuc         start >= strm->avail_out
52*44bedb31SLionel Sambuc         state->bits < 8
53*44bedb31SLionel Sambuc 
54*44bedb31SLionel Sambuc    On return, state->mode is one of:
55*44bedb31SLionel Sambuc 
56*44bedb31SLionel Sambuc         LEN -- ran out of enough output space or enough available input
57*44bedb31SLionel Sambuc         TYPE -- reached end of block code, inflate() to interpret next block
58*44bedb31SLionel Sambuc         BAD -- error in block data
59*44bedb31SLionel Sambuc 
60*44bedb31SLionel Sambuc    Notes:
61*44bedb31SLionel Sambuc 
62*44bedb31SLionel Sambuc     - The maximum input bits used by a length/distance pair is 15 bits for the
63*44bedb31SLionel Sambuc       length code, 5 bits for the length extra, 15 bits for the distance code,
64*44bedb31SLionel Sambuc       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
65*44bedb31SLionel Sambuc       Therefore if strm->avail_in >= 6, then there is enough input to avoid
66*44bedb31SLionel Sambuc       checking for available input while decoding.
67*44bedb31SLionel Sambuc 
68*44bedb31SLionel Sambuc     - The maximum bytes that a single length/distance pair can output is 258
69*44bedb31SLionel Sambuc       bytes, which is the maximum length that can be coded.  inflate_fast()
70*44bedb31SLionel Sambuc       requires strm->avail_out >= 258 for each loop to avoid checking for
71*44bedb31SLionel Sambuc       output space.
72*44bedb31SLionel Sambuc  */
inflate_fast(strm,start)73*44bedb31SLionel Sambuc void inflate_fast(strm, start)
74*44bedb31SLionel Sambuc z_streamp strm;
75*44bedb31SLionel Sambuc unsigned start;         /* inflate()'s starting value for strm->avail_out */
76*44bedb31SLionel Sambuc {
77*44bedb31SLionel Sambuc     struct inflate_state FAR *state;
78*44bedb31SLionel Sambuc     struct inffast_ar {
79*44bedb31SLionel Sambuc /* 64   32                               x86  x86_64 */
80*44bedb31SLionel Sambuc /* ar offset                              register */
81*44bedb31SLionel Sambuc /*  0    0 */ void *esp;                /* esp save */
82*44bedb31SLionel Sambuc /*  8    4 */ void *ebp;                /* ebp save */
83*44bedb31SLionel Sambuc /* 16    8 */ unsigned char FAR *in;    /* esi rsi  local strm->next_in */
84*44bedb31SLionel Sambuc /* 24   12 */ unsigned char FAR *last;  /*     r9   while in < last */
85*44bedb31SLionel Sambuc /* 32   16 */ unsigned char FAR *out;   /* edi rdi  local strm->next_out */
86*44bedb31SLionel Sambuc /* 40   20 */ unsigned char FAR *beg;   /*          inflate()'s init next_out */
87*44bedb31SLionel Sambuc /* 48   24 */ unsigned char FAR *end;   /*     r10  while out < end */
88*44bedb31SLionel Sambuc /* 56   28 */ unsigned char FAR *window;/*          size of window, wsize!=0 */
89*44bedb31SLionel Sambuc /* 64   32 */ code const FAR *lcode;    /* ebp rbp  local strm->lencode */
90*44bedb31SLionel Sambuc /* 72   36 */ code const FAR *dcode;    /*     r11  local strm->distcode */
91*44bedb31SLionel Sambuc /* 80   40 */ unsigned long hold;       /* edx rdx  local strm->hold */
92*44bedb31SLionel Sambuc /* 88   44 */ unsigned bits;            /* ebx rbx  local strm->bits */
93*44bedb31SLionel Sambuc /* 92   48 */ unsigned wsize;           /*          window size */
94*44bedb31SLionel Sambuc /* 96   52 */ unsigned write;           /*          window write index */
95*44bedb31SLionel Sambuc /*100   56 */ unsigned lmask;           /*     r12  mask for lcode */
96*44bedb31SLionel Sambuc /*104   60 */ unsigned dmask;           /*     r13  mask for dcode */
97*44bedb31SLionel Sambuc /*108   64 */ unsigned len;             /*     r14  match length */
98*44bedb31SLionel Sambuc /*112   68 */ unsigned dist;            /*     r15  match distance */
99*44bedb31SLionel Sambuc /*116   72 */ unsigned status;          /*          set when state chng*/
100*44bedb31SLionel Sambuc     } ar;
101*44bedb31SLionel Sambuc 
102*44bedb31SLionel Sambuc #if defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )
103*44bedb31SLionel Sambuc #define PAD_AVAIL_IN 6
104*44bedb31SLionel Sambuc #define PAD_AVAIL_OUT 258
105*44bedb31SLionel Sambuc #else
106*44bedb31SLionel Sambuc #define PAD_AVAIL_IN 5
107*44bedb31SLionel Sambuc #define PAD_AVAIL_OUT 257
108*44bedb31SLionel Sambuc #endif
109*44bedb31SLionel Sambuc 
110*44bedb31SLionel Sambuc     /* copy state to local variables */
111*44bedb31SLionel Sambuc     state = (struct inflate_state FAR *)strm->state;
112*44bedb31SLionel Sambuc     ar.in = strm->next_in;
113*44bedb31SLionel Sambuc     ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN);
114*44bedb31SLionel Sambuc     ar.out = strm->next_out;
115*44bedb31SLionel Sambuc     ar.beg = ar.out - (start - strm->avail_out);
116*44bedb31SLionel Sambuc     ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT);
117*44bedb31SLionel Sambuc     ar.wsize = state->wsize;
118*44bedb31SLionel Sambuc     ar.write = state->write;
119*44bedb31SLionel Sambuc     ar.window = state->window;
120*44bedb31SLionel Sambuc     ar.hold = state->hold;
121*44bedb31SLionel Sambuc     ar.bits = state->bits;
122*44bedb31SLionel Sambuc     ar.lcode = state->lencode;
123*44bedb31SLionel Sambuc     ar.dcode = state->distcode;
124*44bedb31SLionel Sambuc     ar.lmask = (1U << state->lenbits) - 1;
125*44bedb31SLionel Sambuc     ar.dmask = (1U << state->distbits) - 1;
126*44bedb31SLionel Sambuc 
127*44bedb31SLionel Sambuc     /* decode literals and length/distances until end-of-block or not enough
128*44bedb31SLionel Sambuc        input data or output space */
129*44bedb31SLionel Sambuc 
130*44bedb31SLionel Sambuc     /* align in on 1/2 hold size boundary */
131*44bedb31SLionel Sambuc     while (((unsigned long)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) {
132*44bedb31SLionel Sambuc         ar.hold += (unsigned long)*ar.in++ << ar.bits;
133*44bedb31SLionel Sambuc         ar.bits += 8;
134*44bedb31SLionel Sambuc     }
135*44bedb31SLionel Sambuc 
136*44bedb31SLionel Sambuc #if defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )
137*44bedb31SLionel Sambuc     __asm__ __volatile__ (
138*44bedb31SLionel Sambuc "        leaq    %0, %%rax\n"
139*44bedb31SLionel Sambuc "        movq    %%rbp, 8(%%rax)\n"       /* save regs rbp and rsp */
140*44bedb31SLionel Sambuc "        movq    %%rsp, (%%rax)\n"
141*44bedb31SLionel Sambuc "        movq    %%rax, %%rsp\n"          /* make rsp point to &ar */
142*44bedb31SLionel Sambuc "        movq    16(%%rsp), %%rsi\n"      /* rsi  = in */
143*44bedb31SLionel Sambuc "        movq    32(%%rsp), %%rdi\n"      /* rdi  = out */
144*44bedb31SLionel Sambuc "        movq    24(%%rsp), %%r9\n"       /* r9   = last */
145*44bedb31SLionel Sambuc "        movq    48(%%rsp), %%r10\n"      /* r10  = end */
146*44bedb31SLionel Sambuc "        movq    64(%%rsp), %%rbp\n"      /* rbp  = lcode */
147*44bedb31SLionel Sambuc "        movq    72(%%rsp), %%r11\n"      /* r11  = dcode */
148*44bedb31SLionel Sambuc "        movq    80(%%rsp), %%rdx\n"      /* rdx  = hold */
149*44bedb31SLionel Sambuc "        movl    88(%%rsp), %%ebx\n"      /* ebx  = bits */
150*44bedb31SLionel Sambuc "        movl    100(%%rsp), %%r12d\n"    /* r12d = lmask */
151*44bedb31SLionel Sambuc "        movl    104(%%rsp), %%r13d\n"    /* r13d = dmask */
152*44bedb31SLionel Sambuc                                           /* r14d = len */
153*44bedb31SLionel Sambuc                                           /* r15d = dist */
154*44bedb31SLionel Sambuc "        cld\n"
155*44bedb31SLionel Sambuc "        cmpq    %%rdi, %%r10\n"
156*44bedb31SLionel Sambuc "        je      .L_one_time\n"           /* if only one decode left */
157*44bedb31SLionel Sambuc "        cmpq    %%rsi, %%r9\n"
158*44bedb31SLionel Sambuc "        je      .L_one_time\n"
159*44bedb31SLionel Sambuc "        jmp     .L_do_loop\n"
160*44bedb31SLionel Sambuc 
161*44bedb31SLionel Sambuc ".L_one_time:\n"
162*44bedb31SLionel Sambuc "        movq    %%r12, %%r8\n"           /* r8 = lmask */
163*44bedb31SLionel Sambuc "        cmpb    $32, %%bl\n"
164*44bedb31SLionel Sambuc "        ja      .L_get_length_code_one_time\n"
165*44bedb31SLionel Sambuc 
166*44bedb31SLionel Sambuc "        lodsl\n"                         /* eax = *(uint *)in++ */
167*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
168*44bedb31SLionel Sambuc "        addb    $32, %%bl\n"             /* bits += 32 */
169*44bedb31SLionel Sambuc "        shlq    %%cl, %%rax\n"
170*44bedb31SLionel Sambuc "        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */
171*44bedb31SLionel Sambuc "        jmp     .L_get_length_code_one_time\n"
172*44bedb31SLionel Sambuc 
173*44bedb31SLionel Sambuc ".align 32,0x90\n"
174*44bedb31SLionel Sambuc ".L_while_test:\n"
175*44bedb31SLionel Sambuc "        cmpq    %%rdi, %%r10\n"
176*44bedb31SLionel Sambuc "        jbe     .L_break_loop\n"
177*44bedb31SLionel Sambuc "        cmpq    %%rsi, %%r9\n"
178*44bedb31SLionel Sambuc "        jbe     .L_break_loop\n"
179*44bedb31SLionel Sambuc 
180*44bedb31SLionel Sambuc ".L_do_loop:\n"
181*44bedb31SLionel Sambuc "        movq    %%r12, %%r8\n"           /* r8 = lmask */
182*44bedb31SLionel Sambuc "        cmpb    $32, %%bl\n"
183*44bedb31SLionel Sambuc "        ja      .L_get_length_code\n"    /* if (32 < bits) */
184*44bedb31SLionel Sambuc 
185*44bedb31SLionel Sambuc "        lodsl\n"                         /* eax = *(uint *)in++ */
186*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
187*44bedb31SLionel Sambuc "        addb    $32, %%bl\n"             /* bits += 32 */
188*44bedb31SLionel Sambuc "        shlq    %%cl, %%rax\n"
189*44bedb31SLionel Sambuc "        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */
190*44bedb31SLionel Sambuc 
191*44bedb31SLionel Sambuc ".L_get_length_code:\n"
192*44bedb31SLionel Sambuc "        andq    %%rdx, %%r8\n"            /* r8 &= hold */
193*44bedb31SLionel Sambuc "        movl    (%%rbp,%%r8,4), %%eax\n"  /* eax = lcode[hold & lmask] */
194*44bedb31SLionel Sambuc 
195*44bedb31SLionel Sambuc "        movb    %%ah, %%cl\n"            /* cl = this.bits */
196*44bedb31SLionel Sambuc "        subb    %%ah, %%bl\n"            /* bits -= this.bits */
197*44bedb31SLionel Sambuc "        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */
198*44bedb31SLionel Sambuc 
199*44bedb31SLionel Sambuc "        testb   %%al, %%al\n"
200*44bedb31SLionel Sambuc "        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */
201*44bedb31SLionel Sambuc 
202*44bedb31SLionel Sambuc "        movq    %%r12, %%r8\n"            /* r8 = lmask */
203*44bedb31SLionel Sambuc "        shrl    $16, %%eax\n"            /* output this.val char */
204*44bedb31SLionel Sambuc "        stosb\n"
205*44bedb31SLionel Sambuc 
206*44bedb31SLionel Sambuc ".L_get_length_code_one_time:\n"
207*44bedb31SLionel Sambuc "        andq    %%rdx, %%r8\n"            /* r8 &= hold */
208*44bedb31SLionel Sambuc "        movl    (%%rbp,%%r8,4), %%eax\n" /* eax = lcode[hold & lmask] */
209*44bedb31SLionel Sambuc 
210*44bedb31SLionel Sambuc ".L_dolen:\n"
211*44bedb31SLionel Sambuc "        movb    %%ah, %%cl\n"            /* cl = this.bits */
212*44bedb31SLionel Sambuc "        subb    %%ah, %%bl\n"            /* bits -= this.bits */
213*44bedb31SLionel Sambuc "        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */
214*44bedb31SLionel Sambuc 
215*44bedb31SLionel Sambuc "        testb   %%al, %%al\n"
216*44bedb31SLionel Sambuc "        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */
217*44bedb31SLionel Sambuc 
218*44bedb31SLionel Sambuc "        shrl    $16, %%eax\n"            /* output this.val char */
219*44bedb31SLionel Sambuc "        stosb\n"
220*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
221*44bedb31SLionel Sambuc 
222*44bedb31SLionel Sambuc ".align 32,0x90\n"
223*44bedb31SLionel Sambuc ".L_test_for_length_base:\n"
224*44bedb31SLionel Sambuc "        movl    %%eax, %%r14d\n"         /* len = this */
225*44bedb31SLionel Sambuc "        shrl    $16, %%r14d\n"           /* len = this.val */
226*44bedb31SLionel Sambuc "        movb    %%al, %%cl\n"
227*44bedb31SLionel Sambuc 
228*44bedb31SLionel Sambuc "        testb   $16, %%al\n"
229*44bedb31SLionel Sambuc "        jz      .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
230*44bedb31SLionel Sambuc "        andb    $15, %%cl\n"             /* op &= 15 */
231*44bedb31SLionel Sambuc "        jz      .L_decode_distance\n"    /* if (!op) */
232*44bedb31SLionel Sambuc 
233*44bedb31SLionel Sambuc ".L_add_bits_to_len:\n"
234*44bedb31SLionel Sambuc "        subb    %%cl, %%bl\n"
235*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
236*44bedb31SLionel Sambuc "        incl    %%eax\n"
237*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
238*44bedb31SLionel Sambuc "        decl    %%eax\n"
239*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
240*44bedb31SLionel Sambuc "        shrq    %%cl, %%rdx\n"
241*44bedb31SLionel Sambuc "        addl    %%eax, %%r14d\n"         /* len += hold & mask[op] */
242*44bedb31SLionel Sambuc 
243*44bedb31SLionel Sambuc ".L_decode_distance:\n"
244*44bedb31SLionel Sambuc "        movq    %%r13, %%r8\n"           /* r8 = dmask */
245*44bedb31SLionel Sambuc "        cmpb    $32, %%bl\n"
246*44bedb31SLionel Sambuc "        ja      .L_get_distance_code\n"  /* if (32 < bits) */
247*44bedb31SLionel Sambuc 
248*44bedb31SLionel Sambuc "        lodsl\n"                         /* eax = *(uint *)in++ */
249*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
250*44bedb31SLionel Sambuc "        addb    $32, %%bl\n"             /* bits += 32 */
251*44bedb31SLionel Sambuc "        shlq    %%cl, %%rax\n"
252*44bedb31SLionel Sambuc "        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */
253*44bedb31SLionel Sambuc 
254*44bedb31SLionel Sambuc ".L_get_distance_code:\n"
255*44bedb31SLionel Sambuc "        andq    %%rdx, %%r8\n"           /* r8 &= hold */
256*44bedb31SLionel Sambuc "        movl    (%%r11,%%r8,4), %%eax\n" /* eax = dcode[hold & dmask] */
257*44bedb31SLionel Sambuc 
258*44bedb31SLionel Sambuc ".L_dodist:\n"
259*44bedb31SLionel Sambuc "        movl    %%eax, %%r15d\n"         /* dist = this */
260*44bedb31SLionel Sambuc "        shrl    $16, %%r15d\n"           /* dist = this.val */
261*44bedb31SLionel Sambuc "        movb    %%ah, %%cl\n"
262*44bedb31SLionel Sambuc "        subb    %%ah, %%bl\n"            /* bits -= this.bits */
263*44bedb31SLionel Sambuc "        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */
264*44bedb31SLionel Sambuc "        movb    %%al, %%cl\n"            /* cl = this.op */
265*44bedb31SLionel Sambuc 
266*44bedb31SLionel Sambuc "        testb   $16, %%al\n"             /* if ((op & 16) == 0) */
267*44bedb31SLionel Sambuc "        jz      .L_test_for_second_level_dist\n"
268*44bedb31SLionel Sambuc "        andb    $15, %%cl\n"             /* op &= 15 */
269*44bedb31SLionel Sambuc "        jz      .L_check_dist_one\n"
270*44bedb31SLionel Sambuc 
271*44bedb31SLionel Sambuc ".L_add_bits_to_dist:\n"
272*44bedb31SLionel Sambuc "        subb    %%cl, %%bl\n"
273*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
274*44bedb31SLionel Sambuc "        incl    %%eax\n"
275*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
276*44bedb31SLionel Sambuc "        decl    %%eax\n"                 /* (1 << op) - 1 */
277*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
278*44bedb31SLionel Sambuc "        shrq    %%cl, %%rdx\n"
279*44bedb31SLionel Sambuc "        addl    %%eax, %%r15d\n"         /* dist += hold & ((1 << op) - 1) */
280*44bedb31SLionel Sambuc 
281*44bedb31SLionel Sambuc ".L_check_window:\n"
282*44bedb31SLionel Sambuc "        movq    %%rsi, %%r8\n"           /* save in so from can use it's reg */
283*44bedb31SLionel Sambuc "        movq    %%rdi, %%rax\n"
284*44bedb31SLionel Sambuc "        subq    40(%%rsp), %%rax\n"      /* nbytes = out - beg */
285*44bedb31SLionel Sambuc 
286*44bedb31SLionel Sambuc "        cmpl    %%r15d, %%eax\n"
287*44bedb31SLionel Sambuc "        jb      .L_clip_window\n"        /* if (dist > nbytes) 4.2% */
288*44bedb31SLionel Sambuc 
289*44bedb31SLionel Sambuc "        movl    %%r14d, %%ecx\n"         /* ecx = len */
290*44bedb31SLionel Sambuc "        movq    %%rdi, %%rsi\n"
291*44bedb31SLionel Sambuc "        subq    %%r15, %%rsi\n"          /* from = out - dist */
292*44bedb31SLionel Sambuc 
293*44bedb31SLionel Sambuc "        sarl    %%ecx\n"
294*44bedb31SLionel Sambuc "        jnc     .L_copy_two\n"           /* if len % 2 == 0 */
295*44bedb31SLionel Sambuc 
296*44bedb31SLionel Sambuc "        rep     movsw\n"
297*44bedb31SLionel Sambuc "        movb    (%%rsi), %%al\n"
298*44bedb31SLionel Sambuc "        movb    %%al, (%%rdi)\n"
299*44bedb31SLionel Sambuc "        incq    %%rdi\n"
300*44bedb31SLionel Sambuc 
301*44bedb31SLionel Sambuc "        movq    %%r8, %%rsi\n"           /* move in back to %rsi, toss from */
302*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
303*44bedb31SLionel Sambuc 
304*44bedb31SLionel Sambuc ".L_copy_two:\n"
305*44bedb31SLionel Sambuc "        rep     movsw\n"
306*44bedb31SLionel Sambuc "        movq    %%r8, %%rsi\n"           /* move in back to %rsi, toss from */
307*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
308*44bedb31SLionel Sambuc 
309*44bedb31SLionel Sambuc ".align 32,0x90\n"
310*44bedb31SLionel Sambuc ".L_check_dist_one:\n"
311*44bedb31SLionel Sambuc "        cmpl    $1, %%r15d\n"            /* if dist 1, is a memset */
312*44bedb31SLionel Sambuc "        jne     .L_check_window\n"
313*44bedb31SLionel Sambuc "        cmpq    %%rdi, 40(%%rsp)\n"      /* if out == beg, outside window */
314*44bedb31SLionel Sambuc "        je      .L_check_window\n"
315*44bedb31SLionel Sambuc 
316*44bedb31SLionel Sambuc "        movl    %%r14d, %%ecx\n"         /* ecx = len */
317*44bedb31SLionel Sambuc "        movb    -1(%%rdi), %%al\n"
318*44bedb31SLionel Sambuc "        movb    %%al, %%ah\n"
319*44bedb31SLionel Sambuc 
320*44bedb31SLionel Sambuc "        sarl    %%ecx\n"
321*44bedb31SLionel Sambuc "        jnc     .L_set_two\n"
322*44bedb31SLionel Sambuc "        movb    %%al, (%%rdi)\n"
323*44bedb31SLionel Sambuc "        incq    %%rdi\n"
324*44bedb31SLionel Sambuc 
325*44bedb31SLionel Sambuc ".L_set_two:\n"
326*44bedb31SLionel Sambuc "        rep     stosw\n"
327*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
328*44bedb31SLionel Sambuc 
329*44bedb31SLionel Sambuc ".align 32,0x90\n"
330*44bedb31SLionel Sambuc ".L_test_for_second_level_length:\n"
331*44bedb31SLionel Sambuc "        testb   $64, %%al\n"
332*44bedb31SLionel Sambuc "        jnz     .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */
333*44bedb31SLionel Sambuc 
334*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
335*44bedb31SLionel Sambuc "        incl    %%eax\n"
336*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
337*44bedb31SLionel Sambuc "        decl    %%eax\n"
338*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"         /* eax &= hold */
339*44bedb31SLionel Sambuc "        addl    %%r14d, %%eax\n"        /* eax += len */
340*44bedb31SLionel Sambuc "        movl    (%%rbp,%%rax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
341*44bedb31SLionel Sambuc "        jmp     .L_dolen\n"
342*44bedb31SLionel Sambuc 
343*44bedb31SLionel Sambuc ".align 32,0x90\n"
344*44bedb31SLionel Sambuc ".L_test_for_second_level_dist:\n"
345*44bedb31SLionel Sambuc "        testb   $64, %%al\n"
346*44bedb31SLionel Sambuc "        jnz     .L_invalid_distance_code\n" /* if ((op & 64) != 0) */
347*44bedb31SLionel Sambuc 
348*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
349*44bedb31SLionel Sambuc "        incl    %%eax\n"
350*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
351*44bedb31SLionel Sambuc "        decl    %%eax\n"
352*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"         /* eax &= hold */
353*44bedb31SLionel Sambuc "        addl    %%r15d, %%eax\n"        /* eax += dist */
354*44bedb31SLionel Sambuc "        movl    (%%r11,%%rax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
355*44bedb31SLionel Sambuc "        jmp     .L_dodist\n"
356*44bedb31SLionel Sambuc 
357*44bedb31SLionel Sambuc ".align 32,0x90\n"
358*44bedb31SLionel Sambuc ".L_clip_window:\n"
359*44bedb31SLionel Sambuc "        movl    %%eax, %%ecx\n"         /* ecx = nbytes */
360*44bedb31SLionel Sambuc "        movl    92(%%rsp), %%eax\n"     /* eax = wsize, prepare for dist cmp */
361*44bedb31SLionel Sambuc "        negl    %%ecx\n"                /* nbytes = -nbytes */
362*44bedb31SLionel Sambuc 
363*44bedb31SLionel Sambuc "        cmpl    %%r15d, %%eax\n"
364*44bedb31SLionel Sambuc "        jb      .L_invalid_distance_too_far\n" /* if (dist > wsize) */
365*44bedb31SLionel Sambuc 
366*44bedb31SLionel Sambuc "        addl    %%r15d, %%ecx\n"         /* nbytes = dist - nbytes */
367*44bedb31SLionel Sambuc "        cmpl    $0, 96(%%rsp)\n"
368*44bedb31SLionel Sambuc "        jne     .L_wrap_around_window\n" /* if (write != 0) */
369*44bedb31SLionel Sambuc 
370*44bedb31SLionel Sambuc "        movq    56(%%rsp), %%rsi\n"     /* from  = window */
371*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* eax  -= nbytes */
372*44bedb31SLionel Sambuc "        addq    %%rax, %%rsi\n"         /* from += wsize - nbytes */
373*44bedb31SLionel Sambuc 
374*44bedb31SLionel Sambuc "        movl    %%r14d, %%eax\n"        /* eax = len */
375*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%r14d\n"
376*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
377*44bedb31SLionel Sambuc 
378*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* eax -= nbytes */
379*44bedb31SLionel Sambuc "        rep     movsb\n"
380*44bedb31SLionel Sambuc "        movq    %%rdi, %%rsi\n"
381*44bedb31SLionel Sambuc "        subq    %%r15, %%rsi\n"         /* from = &out[ -dist ] */
382*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"
383*44bedb31SLionel Sambuc 
384*44bedb31SLionel Sambuc ".align 32,0x90\n"
385*44bedb31SLionel Sambuc ".L_wrap_around_window:\n"
386*44bedb31SLionel Sambuc "        movl    96(%%rsp), %%eax\n"     /* eax = write */
387*44bedb31SLionel Sambuc "        cmpl    %%eax, %%ecx\n"
388*44bedb31SLionel Sambuc "        jbe     .L_contiguous_in_window\n" /* if (write >= nbytes) */
389*44bedb31SLionel Sambuc 
390*44bedb31SLionel Sambuc "        movl    92(%%rsp), %%esi\n"     /* from  = wsize */
391*44bedb31SLionel Sambuc "        addq    56(%%rsp), %%rsi\n"     /* from += window */
392*44bedb31SLionel Sambuc "        addq    %%rax, %%rsi\n"         /* from += write */
393*44bedb31SLionel Sambuc "        subq    %%rcx, %%rsi\n"         /* from -= nbytes */
394*44bedb31SLionel Sambuc "        subl    %%eax, %%ecx\n"         /* nbytes -= write */
395*44bedb31SLionel Sambuc 
396*44bedb31SLionel Sambuc "        movl    %%r14d, %%eax\n"        /* eax = len */
397*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
398*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
399*44bedb31SLionel Sambuc 
400*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
401*44bedb31SLionel Sambuc "        rep     movsb\n"
402*44bedb31SLionel Sambuc "        movq    56(%%rsp), %%rsi\n"     /* from = window */
403*44bedb31SLionel Sambuc "        movl    96(%%rsp), %%ecx\n"     /* nbytes = write */
404*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
405*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
406*44bedb31SLionel Sambuc 
407*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
408*44bedb31SLionel Sambuc "        rep     movsb\n"
409*44bedb31SLionel Sambuc "        movq    %%rdi, %%rsi\n"
410*44bedb31SLionel Sambuc "        subq    %%r15, %%rsi\n"         /* from = out - dist */
411*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"
412*44bedb31SLionel Sambuc 
413*44bedb31SLionel Sambuc ".align 32,0x90\n"
414*44bedb31SLionel Sambuc ".L_contiguous_in_window:\n"
415*44bedb31SLionel Sambuc "        movq    56(%%rsp), %%rsi\n"     /* rsi = window */
416*44bedb31SLionel Sambuc "        addq    %%rax, %%rsi\n"
417*44bedb31SLionel Sambuc "        subq    %%rcx, %%rsi\n"         /* from += write - nbytes */
418*44bedb31SLionel Sambuc 
419*44bedb31SLionel Sambuc "        movl    %%r14d, %%eax\n"        /* eax = len */
420*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
421*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
422*44bedb31SLionel Sambuc 
423*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
424*44bedb31SLionel Sambuc "        rep     movsb\n"
425*44bedb31SLionel Sambuc "        movq    %%rdi, %%rsi\n"
426*44bedb31SLionel Sambuc "        subq    %%r15, %%rsi\n"         /* from = out - dist */
427*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"           /* if (nbytes >= len) */
428*44bedb31SLionel Sambuc 
429*44bedb31SLionel Sambuc ".align 32,0x90\n"
430*44bedb31SLionel Sambuc ".L_do_copy:\n"
431*44bedb31SLionel Sambuc "        movl    %%eax, %%ecx\n"         /* ecx = len */
432*44bedb31SLionel Sambuc "        rep     movsb\n"
433*44bedb31SLionel Sambuc 
434*44bedb31SLionel Sambuc "        movq    %%r8, %%rsi\n"          /* move in back to %esi, toss from */
435*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
436*44bedb31SLionel Sambuc 
437*44bedb31SLionel Sambuc ".L_test_for_end_of_block:\n"
438*44bedb31SLionel Sambuc "        testb   $32, %%al\n"
439*44bedb31SLionel Sambuc "        jz      .L_invalid_literal_length_code\n"
440*44bedb31SLionel Sambuc "        movl    $1, 116(%%rsp)\n"
441*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
442*44bedb31SLionel Sambuc 
443*44bedb31SLionel Sambuc ".L_invalid_literal_length_code:\n"
444*44bedb31SLionel Sambuc "        movl    $2, 116(%%rsp)\n"
445*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
446*44bedb31SLionel Sambuc 
447*44bedb31SLionel Sambuc ".L_invalid_distance_code:\n"
448*44bedb31SLionel Sambuc "        movl    $3, 116(%%rsp)\n"
449*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
450*44bedb31SLionel Sambuc 
451*44bedb31SLionel Sambuc ".L_invalid_distance_too_far:\n"
452*44bedb31SLionel Sambuc "        movl    $4, 116(%%rsp)\n"
453*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
454*44bedb31SLionel Sambuc 
455*44bedb31SLionel Sambuc ".L_break_loop:\n"
456*44bedb31SLionel Sambuc "        movl    $0, 116(%%rsp)\n"
457*44bedb31SLionel Sambuc 
458*44bedb31SLionel Sambuc ".L_break_loop_with_status:\n"
459*44bedb31SLionel Sambuc /* put in, out, bits, and hold back into ar and pop esp */
460*44bedb31SLionel Sambuc "        movq    %%rsi, 16(%%rsp)\n"     /* in */
461*44bedb31SLionel Sambuc "        movq    %%rdi, 32(%%rsp)\n"     /* out */
462*44bedb31SLionel Sambuc "        movl    %%ebx, 88(%%rsp)\n"     /* bits */
463*44bedb31SLionel Sambuc "        movq    %%rdx, 80(%%rsp)\n"     /* hold */
464*44bedb31SLionel Sambuc "        movq    (%%rsp), %%rax\n"       /* restore rbp and rsp */
465*44bedb31SLionel Sambuc "        movq    8(%%rsp), %%rbp\n"
466*44bedb31SLionel Sambuc "        movq    %%rax, %%rsp\n"
467*44bedb31SLionel Sambuc           :
468*44bedb31SLionel Sambuc           : "m" (ar)
469*44bedb31SLionel Sambuc           : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
470*44bedb31SLionel Sambuc             "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
471*44bedb31SLionel Sambuc     );
472*44bedb31SLionel Sambuc #elif ( defined( __GNUC__ ) || defined( __ICC ) ) && defined( __i386 )
473*44bedb31SLionel Sambuc     __asm__ __volatile__ (
474*44bedb31SLionel Sambuc "        leal    %0, %%eax\n"
475*44bedb31SLionel Sambuc "        movl    %%esp, (%%eax)\n"        /* save esp, ebp */
476*44bedb31SLionel Sambuc "        movl    %%ebp, 4(%%eax)\n"
477*44bedb31SLionel Sambuc "        movl    %%eax, %%esp\n"
478*44bedb31SLionel Sambuc "        movl    8(%%esp), %%esi\n"       /* esi = in */
479*44bedb31SLionel Sambuc "        movl    16(%%esp), %%edi\n"      /* edi = out */
480*44bedb31SLionel Sambuc "        movl    40(%%esp), %%edx\n"      /* edx = hold */
481*44bedb31SLionel Sambuc "        movl    44(%%esp), %%ebx\n"      /* ebx = bits */
482*44bedb31SLionel Sambuc "        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
483*44bedb31SLionel Sambuc 
484*44bedb31SLionel Sambuc "        cld\n"
485*44bedb31SLionel Sambuc "        jmp     .L_do_loop\n"
486*44bedb31SLionel Sambuc 
487*44bedb31SLionel Sambuc ".align 32,0x90\n"
488*44bedb31SLionel Sambuc ".L_while_test:\n"
489*44bedb31SLionel Sambuc "        cmpl    %%edi, 24(%%esp)\n"      /* out < end */
490*44bedb31SLionel Sambuc "        jbe     .L_break_loop\n"
491*44bedb31SLionel Sambuc "        cmpl    %%esi, 12(%%esp)\n"      /* in < last */
492*44bedb31SLionel Sambuc "        jbe     .L_break_loop\n"
493*44bedb31SLionel Sambuc 
494*44bedb31SLionel Sambuc ".L_do_loop:\n"
495*44bedb31SLionel Sambuc "        cmpb    $15, %%bl\n"
496*44bedb31SLionel Sambuc "        ja      .L_get_length_code\n"    /* if (15 < bits) */
497*44bedb31SLionel Sambuc 
498*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
499*44bedb31SLionel Sambuc "        lodsw\n"                         /* al = *(ushort *)in++ */
500*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
501*44bedb31SLionel Sambuc "        addb    $16, %%bl\n"             /* bits += 16 */
502*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
503*44bedb31SLionel Sambuc "        orl     %%eax, %%edx\n"        /* hold |= *((ushort *)in)++ << bits */
504*44bedb31SLionel Sambuc 
505*44bedb31SLionel Sambuc ".L_get_length_code:\n"
506*44bedb31SLionel Sambuc "        movl    56(%%esp), %%eax\n"      /* eax = lmask */
507*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
508*44bedb31SLionel Sambuc "        movl    (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[hold & lmask] */
509*44bedb31SLionel Sambuc 
510*44bedb31SLionel Sambuc ".L_dolen:\n"
511*44bedb31SLionel Sambuc "        movb    %%ah, %%cl\n"            /* cl = this.bits */
512*44bedb31SLionel Sambuc "        subb    %%ah, %%bl\n"            /* bits -= this.bits */
513*44bedb31SLionel Sambuc "        shrl    %%cl, %%edx\n"           /* hold >>= this.bits */
514*44bedb31SLionel Sambuc 
515*44bedb31SLionel Sambuc "        testb   %%al, %%al\n"
516*44bedb31SLionel Sambuc "        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */
517*44bedb31SLionel Sambuc 
518*44bedb31SLionel Sambuc "        shrl    $16, %%eax\n"            /* output this.val char */
519*44bedb31SLionel Sambuc "        stosb\n"
520*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
521*44bedb31SLionel Sambuc 
522*44bedb31SLionel Sambuc ".align 32,0x90\n"
523*44bedb31SLionel Sambuc ".L_test_for_length_base:\n"
524*44bedb31SLionel Sambuc "        movl    %%eax, %%ecx\n"          /* len = this */
525*44bedb31SLionel Sambuc "        shrl    $16, %%ecx\n"            /* len = this.val */
526*44bedb31SLionel Sambuc "        movl    %%ecx, 64(%%esp)\n"      /* save len */
527*44bedb31SLionel Sambuc "        movb    %%al, %%cl\n"
528*44bedb31SLionel Sambuc 
529*44bedb31SLionel Sambuc "        testb   $16, %%al\n"
530*44bedb31SLionel Sambuc "        jz      .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
531*44bedb31SLionel Sambuc "        andb    $15, %%cl\n"             /* op &= 15 */
532*44bedb31SLionel Sambuc "        jz      .L_decode_distance\n"    /* if (!op) */
533*44bedb31SLionel Sambuc "        cmpb    %%cl, %%bl\n"
534*44bedb31SLionel Sambuc "        jae     .L_add_bits_to_len\n"    /* if (op <= bits) */
535*44bedb31SLionel Sambuc 
536*44bedb31SLionel Sambuc "        movb    %%cl, %%ch\n"            /* stash op in ch, freeing cl */
537*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
538*44bedb31SLionel Sambuc "        lodsw\n"                         /* al = *(ushort *)in++ */
539*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
540*44bedb31SLionel Sambuc "        addb    $16, %%bl\n"             /* bits += 16 */
541*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
542*44bedb31SLionel Sambuc "        orl     %%eax, %%edx\n"         /* hold |= *((ushort *)in)++ << bits */
543*44bedb31SLionel Sambuc "        movb    %%ch, %%cl\n"            /* move op back to ecx */
544*44bedb31SLionel Sambuc 
545*44bedb31SLionel Sambuc ".L_add_bits_to_len:\n"
546*44bedb31SLionel Sambuc "        subb    %%cl, %%bl\n"
547*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
548*44bedb31SLionel Sambuc "        incl    %%eax\n"
549*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
550*44bedb31SLionel Sambuc "        decl    %%eax\n"
551*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
552*44bedb31SLionel Sambuc "        shrl    %%cl, %%edx\n"
553*44bedb31SLionel Sambuc "        addl    %%eax, 64(%%esp)\n"      /* len += hold & mask[op] */
554*44bedb31SLionel Sambuc 
555*44bedb31SLionel Sambuc ".L_decode_distance:\n"
556*44bedb31SLionel Sambuc "        cmpb    $15, %%bl\n"
557*44bedb31SLionel Sambuc "        ja      .L_get_distance_code\n"  /* if (15 < bits) */
558*44bedb31SLionel Sambuc 
559*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
560*44bedb31SLionel Sambuc "        lodsw\n"                         /* al = *(ushort *)in++ */
561*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
562*44bedb31SLionel Sambuc "        addb    $16, %%bl\n"             /* bits += 16 */
563*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
564*44bedb31SLionel Sambuc "        orl     %%eax, %%edx\n"         /* hold |= *((ushort *)in)++ << bits */
565*44bedb31SLionel Sambuc 
566*44bedb31SLionel Sambuc ".L_get_distance_code:\n"
567*44bedb31SLionel Sambuc "        movl    60(%%esp), %%eax\n"      /* eax = dmask */
568*44bedb31SLionel Sambuc "        movl    36(%%esp), %%ecx\n"      /* ecx = dcode */
569*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
570*44bedb31SLionel Sambuc "        movl    (%%ecx,%%eax,4), %%eax\n"/* eax = dcode[hold & dmask] */
571*44bedb31SLionel Sambuc 
572*44bedb31SLionel Sambuc ".L_dodist:\n"
573*44bedb31SLionel Sambuc "        movl    %%eax, %%ebp\n"          /* dist = this */
574*44bedb31SLionel Sambuc "        shrl    $16, %%ebp\n"            /* dist = this.val */
575*44bedb31SLionel Sambuc "        movb    %%ah, %%cl\n"
576*44bedb31SLionel Sambuc "        subb    %%ah, %%bl\n"            /* bits -= this.bits */
577*44bedb31SLionel Sambuc "        shrl    %%cl, %%edx\n"           /* hold >>= this.bits */
578*44bedb31SLionel Sambuc "        movb    %%al, %%cl\n"            /* cl = this.op */
579*44bedb31SLionel Sambuc 
580*44bedb31SLionel Sambuc "        testb   $16, %%al\n"             /* if ((op & 16) == 0) */
581*44bedb31SLionel Sambuc "        jz      .L_test_for_second_level_dist\n"
582*44bedb31SLionel Sambuc "        andb    $15, %%cl\n"             /* op &= 15 */
583*44bedb31SLionel Sambuc "        jz      .L_check_dist_one\n"
584*44bedb31SLionel Sambuc "        cmpb    %%cl, %%bl\n"
585*44bedb31SLionel Sambuc "        jae     .L_add_bits_to_dist\n"   /* if (op <= bits) 97.6% */
586*44bedb31SLionel Sambuc 
587*44bedb31SLionel Sambuc "        movb    %%cl, %%ch\n"            /* stash op in ch, freeing cl */
588*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
589*44bedb31SLionel Sambuc "        lodsw\n"                         /* al = *(ushort *)in++ */
590*44bedb31SLionel Sambuc "        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
591*44bedb31SLionel Sambuc "        addb    $16, %%bl\n"             /* bits += 16 */
592*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
593*44bedb31SLionel Sambuc "        orl     %%eax, %%edx\n"        /* hold |= *((ushort *)in)++ << bits */
594*44bedb31SLionel Sambuc "        movb    %%ch, %%cl\n"            /* move op back to ecx */
595*44bedb31SLionel Sambuc 
596*44bedb31SLionel Sambuc ".L_add_bits_to_dist:\n"
597*44bedb31SLionel Sambuc "        subb    %%cl, %%bl\n"
598*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
599*44bedb31SLionel Sambuc "        incl    %%eax\n"
600*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
601*44bedb31SLionel Sambuc "        decl    %%eax\n"                 /* (1 << op) - 1 */
602*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"          /* eax &= hold */
603*44bedb31SLionel Sambuc "        shrl    %%cl, %%edx\n"
604*44bedb31SLionel Sambuc "        addl    %%eax, %%ebp\n"          /* dist += hold & ((1 << op) - 1) */
605*44bedb31SLionel Sambuc 
606*44bedb31SLionel Sambuc ".L_check_window:\n"
607*44bedb31SLionel Sambuc "        movl    %%esi, 8(%%esp)\n"       /* save in so from can use it's reg */
608*44bedb31SLionel Sambuc "        movl    %%edi, %%eax\n"
609*44bedb31SLionel Sambuc "        subl    20(%%esp), %%eax\n"      /* nbytes = out - beg */
610*44bedb31SLionel Sambuc 
611*44bedb31SLionel Sambuc "        cmpl    %%ebp, %%eax\n"
612*44bedb31SLionel Sambuc "        jb      .L_clip_window\n"        /* if (dist > nbytes) 4.2% */
613*44bedb31SLionel Sambuc 
614*44bedb31SLionel Sambuc "        movl    64(%%esp), %%ecx\n"      /* ecx = len */
615*44bedb31SLionel Sambuc "        movl    %%edi, %%esi\n"
616*44bedb31SLionel Sambuc "        subl    %%ebp, %%esi\n"          /* from = out - dist */
617*44bedb31SLionel Sambuc 
618*44bedb31SLionel Sambuc "        sarl    %%ecx\n"
619*44bedb31SLionel Sambuc "        jnc     .L_copy_two\n"           /* if len % 2 == 0 */
620*44bedb31SLionel Sambuc 
621*44bedb31SLionel Sambuc "        rep     movsw\n"
622*44bedb31SLionel Sambuc "        movb    (%%esi), %%al\n"
623*44bedb31SLionel Sambuc "        movb    %%al, (%%edi)\n"
624*44bedb31SLionel Sambuc "        incl    %%edi\n"
625*44bedb31SLionel Sambuc 
626*44bedb31SLionel Sambuc "        movl    8(%%esp), %%esi\n"       /* move in back to %esi, toss from */
627*44bedb31SLionel Sambuc "        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
628*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
629*44bedb31SLionel Sambuc 
630*44bedb31SLionel Sambuc ".L_copy_two:\n"
631*44bedb31SLionel Sambuc "        rep     movsw\n"
632*44bedb31SLionel Sambuc "        movl    8(%%esp), %%esi\n"       /* move in back to %esi, toss from */
633*44bedb31SLionel Sambuc "        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
634*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
635*44bedb31SLionel Sambuc 
636*44bedb31SLionel Sambuc ".align 32,0x90\n"
637*44bedb31SLionel Sambuc ".L_check_dist_one:\n"
638*44bedb31SLionel Sambuc "        cmpl    $1, %%ebp\n"            /* if dist 1, is a memset */
639*44bedb31SLionel Sambuc "        jne     .L_check_window\n"
640*44bedb31SLionel Sambuc "        cmpl    %%edi, 20(%%esp)\n"
641*44bedb31SLionel Sambuc "        je      .L_check_window\n"      /* out == beg, if outside window */
642*44bedb31SLionel Sambuc 
643*44bedb31SLionel Sambuc "        movl    64(%%esp), %%ecx\n"      /* ecx = len */
644*44bedb31SLionel Sambuc "        movb    -1(%%edi), %%al\n"
645*44bedb31SLionel Sambuc "        movb    %%al, %%ah\n"
646*44bedb31SLionel Sambuc 
647*44bedb31SLionel Sambuc "        sarl    %%ecx\n"
648*44bedb31SLionel Sambuc "        jnc     .L_set_two\n"
649*44bedb31SLionel Sambuc "        movb    %%al, (%%edi)\n"
650*44bedb31SLionel Sambuc "        incl    %%edi\n"
651*44bedb31SLionel Sambuc 
652*44bedb31SLionel Sambuc ".L_set_two:\n"
653*44bedb31SLionel Sambuc "        rep     stosw\n"
654*44bedb31SLionel Sambuc "        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
655*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
656*44bedb31SLionel Sambuc 
657*44bedb31SLionel Sambuc ".align 32,0x90\n"
658*44bedb31SLionel Sambuc ".L_test_for_second_level_length:\n"
659*44bedb31SLionel Sambuc "        testb   $64, %%al\n"
660*44bedb31SLionel Sambuc "        jnz     .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */
661*44bedb31SLionel Sambuc 
662*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
663*44bedb31SLionel Sambuc "        incl    %%eax\n"
664*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
665*44bedb31SLionel Sambuc "        decl    %%eax\n"
666*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"         /* eax &= hold */
667*44bedb31SLionel Sambuc "        addl    64(%%esp), %%eax\n"     /* eax += len */
668*44bedb31SLionel Sambuc "        movl    (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
669*44bedb31SLionel Sambuc "        jmp     .L_dolen\n"
670*44bedb31SLionel Sambuc 
671*44bedb31SLionel Sambuc ".align 32,0x90\n"
672*44bedb31SLionel Sambuc ".L_test_for_second_level_dist:\n"
673*44bedb31SLionel Sambuc "        testb   $64, %%al\n"
674*44bedb31SLionel Sambuc "        jnz     .L_invalid_distance_code\n" /* if ((op & 64) != 0) */
675*44bedb31SLionel Sambuc 
676*44bedb31SLionel Sambuc "        xorl    %%eax, %%eax\n"
677*44bedb31SLionel Sambuc "        incl    %%eax\n"
678*44bedb31SLionel Sambuc "        shll    %%cl, %%eax\n"
679*44bedb31SLionel Sambuc "        decl    %%eax\n"
680*44bedb31SLionel Sambuc "        andl    %%edx, %%eax\n"         /* eax &= hold */
681*44bedb31SLionel Sambuc "        addl    %%ebp, %%eax\n"         /* eax += dist */
682*44bedb31SLionel Sambuc "        movl    36(%%esp), %%ecx\n"     /* ecx = dcode */
683*44bedb31SLionel Sambuc "        movl    (%%ecx,%%eax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
684*44bedb31SLionel Sambuc "        jmp     .L_dodist\n"
685*44bedb31SLionel Sambuc 
686*44bedb31SLionel Sambuc ".align 32,0x90\n"
687*44bedb31SLionel Sambuc ".L_clip_window:\n"
688*44bedb31SLionel Sambuc "        movl    %%eax, %%ecx\n"
689*44bedb31SLionel Sambuc "        movl    48(%%esp), %%eax\n"     /* eax = wsize */
690*44bedb31SLionel Sambuc "        negl    %%ecx\n"                /* nbytes = -nbytes */
691*44bedb31SLionel Sambuc "        movl    28(%%esp), %%esi\n"     /* from = window */
692*44bedb31SLionel Sambuc 
693*44bedb31SLionel Sambuc "        cmpl    %%ebp, %%eax\n"
694*44bedb31SLionel Sambuc "        jb      .L_invalid_distance_too_far\n" /* if (dist > wsize) */
695*44bedb31SLionel Sambuc 
696*44bedb31SLionel Sambuc "        addl    %%ebp, %%ecx\n"         /* nbytes = dist - nbytes */
697*44bedb31SLionel Sambuc "        cmpl    $0, 52(%%esp)\n"
698*44bedb31SLionel Sambuc "        jne     .L_wrap_around_window\n" /* if (write != 0) */
699*44bedb31SLionel Sambuc 
700*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"
701*44bedb31SLionel Sambuc "        addl    %%eax, %%esi\n"         /* from += wsize - nbytes */
702*44bedb31SLionel Sambuc 
703*44bedb31SLionel Sambuc "        movl    64(%%esp), %%eax\n"     /* eax = len */
704*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
705*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
706*44bedb31SLionel Sambuc 
707*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
708*44bedb31SLionel Sambuc "        rep     movsb\n"
709*44bedb31SLionel Sambuc "        movl    %%edi, %%esi\n"
710*44bedb31SLionel Sambuc "        subl    %%ebp, %%esi\n"         /* from = out - dist */
711*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"
712*44bedb31SLionel Sambuc 
713*44bedb31SLionel Sambuc ".align 32,0x90\n"
714*44bedb31SLionel Sambuc ".L_wrap_around_window:\n"
715*44bedb31SLionel Sambuc "        movl    52(%%esp), %%eax\n"     /* eax = write */
716*44bedb31SLionel Sambuc "        cmpl    %%eax, %%ecx\n"
717*44bedb31SLionel Sambuc "        jbe     .L_contiguous_in_window\n" /* if (write >= nbytes) */
718*44bedb31SLionel Sambuc 
719*44bedb31SLionel Sambuc "        addl    48(%%esp), %%esi\n"     /* from += wsize */
720*44bedb31SLionel Sambuc "        addl    %%eax, %%esi\n"         /* from += write */
721*44bedb31SLionel Sambuc "        subl    %%ecx, %%esi\n"         /* from -= nbytes */
722*44bedb31SLionel Sambuc "        subl    %%eax, %%ecx\n"         /* nbytes -= write */
723*44bedb31SLionel Sambuc 
724*44bedb31SLionel Sambuc "        movl    64(%%esp), %%eax\n"     /* eax = len */
725*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
726*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
727*44bedb31SLionel Sambuc 
728*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
729*44bedb31SLionel Sambuc "        rep     movsb\n"
730*44bedb31SLionel Sambuc "        movl    28(%%esp), %%esi\n"     /* from = window */
731*44bedb31SLionel Sambuc "        movl    52(%%esp), %%ecx\n"     /* nbytes = write */
732*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
733*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
734*44bedb31SLionel Sambuc 
735*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
736*44bedb31SLionel Sambuc "        rep     movsb\n"
737*44bedb31SLionel Sambuc "        movl    %%edi, %%esi\n"
738*44bedb31SLionel Sambuc "        subl    %%ebp, %%esi\n"         /* from = out - dist */
739*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"
740*44bedb31SLionel Sambuc 
741*44bedb31SLionel Sambuc ".align 32,0x90\n"
742*44bedb31SLionel Sambuc ".L_contiguous_in_window:\n"
743*44bedb31SLionel Sambuc "        addl    %%eax, %%esi\n"
744*44bedb31SLionel Sambuc "        subl    %%ecx, %%esi\n"         /* from += write - nbytes */
745*44bedb31SLionel Sambuc 
746*44bedb31SLionel Sambuc "        movl    64(%%esp), %%eax\n"     /* eax = len */
747*44bedb31SLionel Sambuc "        cmpl    %%ecx, %%eax\n"
748*44bedb31SLionel Sambuc "        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
749*44bedb31SLionel Sambuc 
750*44bedb31SLionel Sambuc "        subl    %%ecx, %%eax\n"         /* len -= nbytes */
751*44bedb31SLionel Sambuc "        rep     movsb\n"
752*44bedb31SLionel Sambuc "        movl    %%edi, %%esi\n"
753*44bedb31SLionel Sambuc "        subl    %%ebp, %%esi\n"         /* from = out - dist */
754*44bedb31SLionel Sambuc "        jmp     .L_do_copy\n"           /* if (nbytes >= len) */
755*44bedb31SLionel Sambuc 
756*44bedb31SLionel Sambuc ".align 32,0x90\n"
757*44bedb31SLionel Sambuc ".L_do_copy:\n"
758*44bedb31SLionel Sambuc "        movl    %%eax, %%ecx\n"
759*44bedb31SLionel Sambuc "        rep     movsb\n"
760*44bedb31SLionel Sambuc 
761*44bedb31SLionel Sambuc "        movl    8(%%esp), %%esi\n"      /* move in back to %esi, toss from */
762*44bedb31SLionel Sambuc "        movl    32(%%esp), %%ebp\n"     /* ebp = lcode */
763*44bedb31SLionel Sambuc "        jmp     .L_while_test\n"
764*44bedb31SLionel Sambuc 
765*44bedb31SLionel Sambuc ".L_test_for_end_of_block:\n"
766*44bedb31SLionel Sambuc "        testb   $32, %%al\n"
767*44bedb31SLionel Sambuc "        jz      .L_invalid_literal_length_code\n"
768*44bedb31SLionel Sambuc "        movl    $1, 72(%%esp)\n"
769*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
770*44bedb31SLionel Sambuc 
771*44bedb31SLionel Sambuc ".L_invalid_literal_length_code:\n"
772*44bedb31SLionel Sambuc "        movl    $2, 72(%%esp)\n"
773*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
774*44bedb31SLionel Sambuc 
775*44bedb31SLionel Sambuc ".L_invalid_distance_code:\n"
776*44bedb31SLionel Sambuc "        movl    $3, 72(%%esp)\n"
777*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
778*44bedb31SLionel Sambuc 
779*44bedb31SLionel Sambuc ".L_invalid_distance_too_far:\n"
780*44bedb31SLionel Sambuc "        movl    8(%%esp), %%esi\n"
781*44bedb31SLionel Sambuc "        movl    $4, 72(%%esp)\n"
782*44bedb31SLionel Sambuc "        jmp     .L_break_loop_with_status\n"
783*44bedb31SLionel Sambuc 
784*44bedb31SLionel Sambuc ".L_break_loop:\n"
785*44bedb31SLionel Sambuc "        movl    $0, 72(%%esp)\n"
786*44bedb31SLionel Sambuc 
787*44bedb31SLionel Sambuc ".L_break_loop_with_status:\n"
788*44bedb31SLionel Sambuc /* put in, out, bits, and hold back into ar and pop esp */
789*44bedb31SLionel Sambuc "        movl    %%esi, 8(%%esp)\n"      /* save in */
790*44bedb31SLionel Sambuc "        movl    %%edi, 16(%%esp)\n"     /* save out */
791*44bedb31SLionel Sambuc "        movl    %%ebx, 44(%%esp)\n"     /* save bits */
792*44bedb31SLionel Sambuc "        movl    %%edx, 40(%%esp)\n"     /* save hold */
793*44bedb31SLionel Sambuc "        movl    4(%%esp), %%ebp\n"      /* restore esp, ebp */
794*44bedb31SLionel Sambuc "        movl    (%%esp), %%esp\n"
795*44bedb31SLionel Sambuc           :
796*44bedb31SLionel Sambuc           : "m" (ar)
797*44bedb31SLionel Sambuc           : "memory", "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
798*44bedb31SLionel Sambuc     );
799*44bedb31SLionel Sambuc #elif defined( _MSC_VER ) && ! defined( _M_AMD64 )
800*44bedb31SLionel Sambuc     __asm {
801*44bedb31SLionel Sambuc 	lea	eax, ar
802*44bedb31SLionel Sambuc 	mov	[eax], esp         /* save esp, ebp */
803*44bedb31SLionel Sambuc 	mov	[eax+4], ebp
804*44bedb31SLionel Sambuc 	mov	esp, eax
805*44bedb31SLionel Sambuc 	mov	esi, [esp+8]       /* esi = in */
806*44bedb31SLionel Sambuc 	mov	edi, [esp+16]      /* edi = out */
807*44bedb31SLionel Sambuc 	mov	edx, [esp+40]      /* edx = hold */
808*44bedb31SLionel Sambuc 	mov	ebx, [esp+44]      /* ebx = bits */
809*44bedb31SLionel Sambuc 	mov	ebp, [esp+32]      /* ebp = lcode */
810*44bedb31SLionel Sambuc 
811*44bedb31SLionel Sambuc 	cld
812*44bedb31SLionel Sambuc 	jmp	L_do_loop
813*44bedb31SLionel Sambuc 
814*44bedb31SLionel Sambuc ALIGN 4
815*44bedb31SLionel Sambuc L_while_test:
816*44bedb31SLionel Sambuc 	cmp	[esp+24], edi
817*44bedb31SLionel Sambuc 	jbe	L_break_loop
818*44bedb31SLionel Sambuc 	cmp	[esp+12], esi
819*44bedb31SLionel Sambuc 	jbe	L_break_loop
820*44bedb31SLionel Sambuc 
821*44bedb31SLionel Sambuc L_do_loop:
822*44bedb31SLionel Sambuc 	cmp	bl, 15
823*44bedb31SLionel Sambuc 	ja	L_get_length_code    /* if (15 < bits) */
824*44bedb31SLionel Sambuc 
825*44bedb31SLionel Sambuc 	xor	eax, eax
826*44bedb31SLionel Sambuc 	lodsw                         /* al = *(ushort *)in++ */
827*44bedb31SLionel Sambuc 	mov	cl, bl            /* cl = bits, needs it for shifting */
828*44bedb31SLionel Sambuc 	add	bl, 16             /* bits += 16 */
829*44bedb31SLionel Sambuc 	shl	eax, cl
830*44bedb31SLionel Sambuc 	or	edx, eax        /* hold |= *((ushort *)in)++ << bits */
831*44bedb31SLionel Sambuc 
832*44bedb31SLionel Sambuc L_get_length_code:
833*44bedb31SLionel Sambuc 	mov	eax, [esp+56]      /* eax = lmask */
834*44bedb31SLionel Sambuc 	and	eax, edx          /* eax &= hold */
835*44bedb31SLionel Sambuc 	mov	eax, [ebp+eax*4] /* eax = lcode[hold & lmask] */
836*44bedb31SLionel Sambuc 
837*44bedb31SLionel Sambuc L_dolen:
838*44bedb31SLionel Sambuc 	mov	cl, ah            /* cl = this.bits */
839*44bedb31SLionel Sambuc 	sub	bl, ah            /* bits -= this.bits */
840*44bedb31SLionel Sambuc 	shr	edx, cl           /* hold >>= this.bits */
841*44bedb31SLionel Sambuc 
842*44bedb31SLionel Sambuc 	test	al, al
843*44bedb31SLionel Sambuc 	jnz	L_test_for_length_base /* if (op != 0) 45.7% */
844*44bedb31SLionel Sambuc 
845*44bedb31SLionel Sambuc 	shr	eax, 16            /* output this.val char */
846*44bedb31SLionel Sambuc 	stosb
847*44bedb31SLionel Sambuc 	jmp	L_while_test
848*44bedb31SLionel Sambuc 
849*44bedb31SLionel Sambuc ALIGN 4
850*44bedb31SLionel Sambuc L_test_for_length_base:
851*44bedb31SLionel Sambuc 	mov	ecx, eax          /* len = this */
852*44bedb31SLionel Sambuc 	shr	ecx, 16            /* len = this.val */
853*44bedb31SLionel Sambuc 	mov	[esp+64], ecx      /* save len */
854*44bedb31SLionel Sambuc 	mov	cl, al
855*44bedb31SLionel Sambuc 
856*44bedb31SLionel Sambuc 	test	al, 16
857*44bedb31SLionel Sambuc 	jz	L_test_for_second_level_length /* if ((op & 16) == 0) 8% */
858*44bedb31SLionel Sambuc 	and	cl, 15             /* op &= 15 */
859*44bedb31SLionel Sambuc 	jz	L_decode_distance    /* if (!op) */
860*44bedb31SLionel Sambuc 	cmp	bl, cl
861*44bedb31SLionel Sambuc 	jae	L_add_bits_to_len    /* if (op <= bits) */
862*44bedb31SLionel Sambuc 
863*44bedb31SLionel Sambuc 	mov	ch, cl            /* stash op in ch, freeing cl */
864*44bedb31SLionel Sambuc 	xor	eax, eax
865*44bedb31SLionel Sambuc 	lodsw                         /* al = *(ushort *)in++ */
866*44bedb31SLionel Sambuc 	mov	cl, bl            /* cl = bits, needs it for shifting */
867*44bedb31SLionel Sambuc 	add	bl, 16             /* bits += 16 */
868*44bedb31SLionel Sambuc 	shl	eax, cl
869*44bedb31SLionel Sambuc 	or	edx, eax         /* hold |= *((ushort *)in)++ << bits */
870*44bedb31SLionel Sambuc 	mov	cl, ch            /* move op back to ecx */
871*44bedb31SLionel Sambuc 
872*44bedb31SLionel Sambuc L_add_bits_to_len:
873*44bedb31SLionel Sambuc 	sub	bl, cl
874*44bedb31SLionel Sambuc 	xor	eax, eax
875*44bedb31SLionel Sambuc 	inc	eax
876*44bedb31SLionel Sambuc 	shl	eax, cl
877*44bedb31SLionel Sambuc 	dec	eax
878*44bedb31SLionel Sambuc 	and	eax, edx          /* eax &= hold */
879*44bedb31SLionel Sambuc 	shr	edx, cl
880*44bedb31SLionel Sambuc 	add	[esp+64], eax      /* len += hold & mask[op] */
881*44bedb31SLionel Sambuc 
882*44bedb31SLionel Sambuc L_decode_distance:
883*44bedb31SLionel Sambuc 	cmp	bl, 15
884*44bedb31SLionel Sambuc 	ja	L_get_distance_code  /* if (15 < bits) */
885*44bedb31SLionel Sambuc 
886*44bedb31SLionel Sambuc 	xor	eax, eax
887*44bedb31SLionel Sambuc 	lodsw                         /* al = *(ushort *)in++ */
888*44bedb31SLionel Sambuc 	mov	cl, bl            /* cl = bits, needs it for shifting */
889*44bedb31SLionel Sambuc 	add	bl, 16             /* bits += 16 */
890*44bedb31SLionel Sambuc 	shl	eax, cl
891*44bedb31SLionel Sambuc 	or	edx, eax         /* hold |= *((ushort *)in)++ << bits */
892*44bedb31SLionel Sambuc 
893*44bedb31SLionel Sambuc L_get_distance_code:
894*44bedb31SLionel Sambuc 	mov	eax, [esp+60]      /* eax = dmask */
895*44bedb31SLionel Sambuc 	mov	ecx, [esp+36]      /* ecx = dcode */
896*44bedb31SLionel Sambuc 	and	eax, edx          /* eax &= hold */
897*44bedb31SLionel Sambuc 	mov	eax, [ecx+eax*4]/* eax = dcode[hold & dmask] */
898*44bedb31SLionel Sambuc 
899*44bedb31SLionel Sambuc L_dodist:
900*44bedb31SLionel Sambuc 	mov	ebp, eax          /* dist = this */
901*44bedb31SLionel Sambuc 	shr	ebp, 16            /* dist = this.val */
902*44bedb31SLionel Sambuc 	mov	cl, ah
903*44bedb31SLionel Sambuc 	sub	bl, ah            /* bits -= this.bits */
904*44bedb31SLionel Sambuc 	shr	edx, cl           /* hold >>= this.bits */
905*44bedb31SLionel Sambuc 	mov	cl, al            /* cl = this.op */
906*44bedb31SLionel Sambuc 
907*44bedb31SLionel Sambuc 	test	al, 16             /* if ((op & 16) == 0) */
908*44bedb31SLionel Sambuc 	jz	L_test_for_second_level_dist
909*44bedb31SLionel Sambuc 	and	cl, 15             /* op &= 15 */
910*44bedb31SLionel Sambuc 	jz	L_check_dist_one
911*44bedb31SLionel Sambuc 	cmp	bl, cl
912*44bedb31SLionel Sambuc 	jae	L_add_bits_to_dist   /* if (op <= bits) 97.6% */
913*44bedb31SLionel Sambuc 
914*44bedb31SLionel Sambuc 	mov	ch, cl            /* stash op in ch, freeing cl */
915*44bedb31SLionel Sambuc 	xor	eax, eax
916*44bedb31SLionel Sambuc 	lodsw                         /* al = *(ushort *)in++ */
917*44bedb31SLionel Sambuc 	mov	cl, bl            /* cl = bits, needs it for shifting */
918*44bedb31SLionel Sambuc 	add	bl, 16             /* bits += 16 */
919*44bedb31SLionel Sambuc 	shl	eax, cl
920*44bedb31SLionel Sambuc 	or	edx, eax        /* hold |= *((ushort *)in)++ << bits */
921*44bedb31SLionel Sambuc 	mov	cl, ch            /* move op back to ecx */
922*44bedb31SLionel Sambuc 
923*44bedb31SLionel Sambuc L_add_bits_to_dist:
924*44bedb31SLionel Sambuc 	sub	bl, cl
925*44bedb31SLionel Sambuc 	xor	eax, eax
926*44bedb31SLionel Sambuc 	inc	eax
927*44bedb31SLionel Sambuc 	shl	eax, cl
928*44bedb31SLionel Sambuc 	dec	eax                 /* (1 << op) - 1 */
929*44bedb31SLionel Sambuc 	and	eax, edx          /* eax &= hold */
930*44bedb31SLionel Sambuc 	shr	edx, cl
931*44bedb31SLionel Sambuc 	add	ebp, eax          /* dist += hold & ((1 << op) - 1) */
932*44bedb31SLionel Sambuc 
933*44bedb31SLionel Sambuc L_check_window:
934*44bedb31SLionel Sambuc 	mov	[esp+8], esi       /* save in so from can use it's reg */
935*44bedb31SLionel Sambuc 	mov	eax, edi
936*44bedb31SLionel Sambuc 	sub	eax, [esp+20]      /* nbytes = out - beg */
937*44bedb31SLionel Sambuc 
938*44bedb31SLionel Sambuc 	cmp	eax, ebp
939*44bedb31SLionel Sambuc 	jb	L_clip_window        /* if (dist > nbytes) 4.2% */
940*44bedb31SLionel Sambuc 
941*44bedb31SLionel Sambuc 	mov	ecx, [esp+64]      /* ecx = len */
942*44bedb31SLionel Sambuc 	mov	esi, edi
943*44bedb31SLionel Sambuc 	sub	esi, ebp          /* from = out - dist */
944*44bedb31SLionel Sambuc 
945*44bedb31SLionel Sambuc 	sar	ecx, 1
946*44bedb31SLionel Sambuc 	jnc	L_copy_two
947*44bedb31SLionel Sambuc 
948*44bedb31SLionel Sambuc 	rep     movsw
949*44bedb31SLionel Sambuc 	mov	al, [esi]
950*44bedb31SLionel Sambuc 	mov	[edi], al
951*44bedb31SLionel Sambuc 	inc	edi
952*44bedb31SLionel Sambuc 
953*44bedb31SLionel Sambuc 	mov	esi, [esp+8]      /* move in back to %esi, toss from */
954*44bedb31SLionel Sambuc 	mov	ebp, [esp+32]     /* ebp = lcode */
955*44bedb31SLionel Sambuc 	jmp	L_while_test
956*44bedb31SLionel Sambuc 
957*44bedb31SLionel Sambuc L_copy_two:
958*44bedb31SLionel Sambuc 	rep     movsw
959*44bedb31SLionel Sambuc 	mov	esi, [esp+8]      /* move in back to %esi, toss from */
960*44bedb31SLionel Sambuc 	mov	ebp, [esp+32]     /* ebp = lcode */
961*44bedb31SLionel Sambuc 	jmp	L_while_test
962*44bedb31SLionel Sambuc 
963*44bedb31SLionel Sambuc ALIGN 4
964*44bedb31SLionel Sambuc L_check_dist_one:
965*44bedb31SLionel Sambuc 	cmp	ebp, 1            /* if dist 1, is a memset */
966*44bedb31SLionel Sambuc 	jne	L_check_window
967*44bedb31SLionel Sambuc 	cmp	[esp+20], edi
968*44bedb31SLionel Sambuc 	je	L_check_window    /* out == beg, if outside window */
969*44bedb31SLionel Sambuc 
970*44bedb31SLionel Sambuc 	mov	ecx, [esp+64]     /* ecx = len */
971*44bedb31SLionel Sambuc 	mov	al, [edi-1]
972*44bedb31SLionel Sambuc 	mov	ah, al
973*44bedb31SLionel Sambuc 
974*44bedb31SLionel Sambuc 	sar	ecx, 1
975*44bedb31SLionel Sambuc 	jnc	L_set_two
976*44bedb31SLionel Sambuc 	mov	[edi], al         /* memset out with from[-1] */
977*44bedb31SLionel Sambuc 	inc	edi
978*44bedb31SLionel Sambuc 
979*44bedb31SLionel Sambuc L_set_two:
980*44bedb31SLionel Sambuc 	rep     stosw
981*44bedb31SLionel Sambuc 	mov	ebp, [esp+32]     /* ebp = lcode */
982*44bedb31SLionel Sambuc 	jmp	L_while_test
983*44bedb31SLionel Sambuc 
984*44bedb31SLionel Sambuc ALIGN 4
985*44bedb31SLionel Sambuc L_test_for_second_level_length:
986*44bedb31SLionel Sambuc 	test	al, 64
987*44bedb31SLionel Sambuc 	jnz	L_test_for_end_of_block /* if ((op & 64) != 0) */
988*44bedb31SLionel Sambuc 
989*44bedb31SLionel Sambuc 	xor	eax, eax
990*44bedb31SLionel Sambuc 	inc	eax
991*44bedb31SLionel Sambuc 	shl	eax, cl
992*44bedb31SLionel Sambuc 	dec	eax
993*44bedb31SLionel Sambuc 	and	eax, edx         /* eax &= hold */
994*44bedb31SLionel Sambuc 	add	eax, [esp+64]     /* eax += len */
995*44bedb31SLionel Sambuc 	mov	eax, [ebp+eax*4] /* eax = lcode[val+(hold&mask[op])]*/
996*44bedb31SLionel Sambuc 	jmp	L_dolen
997*44bedb31SLionel Sambuc 
998*44bedb31SLionel Sambuc ALIGN 4
999*44bedb31SLionel Sambuc L_test_for_second_level_dist:
1000*44bedb31SLionel Sambuc 	test	al, 64
1001*44bedb31SLionel Sambuc 	jnz	L_invalid_distance_code /* if ((op & 64) != 0) */
1002*44bedb31SLionel Sambuc 
1003*44bedb31SLionel Sambuc 	xor	eax, eax
1004*44bedb31SLionel Sambuc 	inc	eax
1005*44bedb31SLionel Sambuc 	shl	eax, cl
1006*44bedb31SLionel Sambuc 	dec	eax
1007*44bedb31SLionel Sambuc 	and	eax, edx         /* eax &= hold */
1008*44bedb31SLionel Sambuc 	add	eax, ebp         /* eax += dist */
1009*44bedb31SLionel Sambuc 	mov	ecx, [esp+36]     /* ecx = dcode */
1010*44bedb31SLionel Sambuc 	mov	eax, [ecx+eax*4] /* eax = dcode[val+(hold&mask[op])]*/
1011*44bedb31SLionel Sambuc 	jmp	L_dodist
1012*44bedb31SLionel Sambuc 
1013*44bedb31SLionel Sambuc ALIGN 4
1014*44bedb31SLionel Sambuc L_clip_window:
1015*44bedb31SLionel Sambuc 	mov	ecx, eax
1016*44bedb31SLionel Sambuc 	mov	eax, [esp+48]     /* eax = wsize */
1017*44bedb31SLionel Sambuc 	neg	ecx                /* nbytes = -nbytes */
1018*44bedb31SLionel Sambuc 	mov	esi, [esp+28]     /* from = window */
1019*44bedb31SLionel Sambuc 
1020*44bedb31SLionel Sambuc 	cmp	eax, ebp
1021*44bedb31SLionel Sambuc 	jb	L_invalid_distance_too_far /* if (dist > wsize) */
1022*44bedb31SLionel Sambuc 
1023*44bedb31SLionel Sambuc 	add	ecx, ebp         /* nbytes = dist - nbytes */
1024*44bedb31SLionel Sambuc 	cmp	dword ptr [esp+52], 0
1025*44bedb31SLionel Sambuc 	jne	L_wrap_around_window /* if (write != 0) */
1026*44bedb31SLionel Sambuc 
1027*44bedb31SLionel Sambuc 	sub	eax, ecx
1028*44bedb31SLionel Sambuc 	add	esi, eax         /* from += wsize - nbytes */
1029*44bedb31SLionel Sambuc 
1030*44bedb31SLionel Sambuc 	mov	eax, [esp+64]    /* eax = len */
1031*44bedb31SLionel Sambuc 	cmp	eax, ecx
1032*44bedb31SLionel Sambuc 	jbe	L_do_copy          /* if (nbytes >= len) */
1033*44bedb31SLionel Sambuc 
1034*44bedb31SLionel Sambuc 	sub	eax, ecx         /* len -= nbytes */
1035*44bedb31SLionel Sambuc 	rep     movsb
1036*44bedb31SLionel Sambuc 	mov	esi, edi
1037*44bedb31SLionel Sambuc 	sub	esi, ebp         /* from = out - dist */
1038*44bedb31SLionel Sambuc 	jmp	L_do_copy
1039*44bedb31SLionel Sambuc 
1040*44bedb31SLionel Sambuc ALIGN 4
1041*44bedb31SLionel Sambuc L_wrap_around_window:
1042*44bedb31SLionel Sambuc 	mov	eax, [esp+52]    /* eax = write */
1043*44bedb31SLionel Sambuc 	cmp	ecx, eax
1044*44bedb31SLionel Sambuc 	jbe	L_contiguous_in_window /* if (write >= nbytes) */
1045*44bedb31SLionel Sambuc 
1046*44bedb31SLionel Sambuc 	add	esi, [esp+48]    /* from += wsize */
1047*44bedb31SLionel Sambuc 	add	esi, eax         /* from += write */
1048*44bedb31SLionel Sambuc 	sub	esi, ecx         /* from -= nbytes */
1049*44bedb31SLionel Sambuc 	sub	ecx, eax         /* nbytes -= write */
1050*44bedb31SLionel Sambuc 
1051*44bedb31SLionel Sambuc 	mov	eax, [esp+64]    /* eax = len */
1052*44bedb31SLionel Sambuc 	cmp	eax, ecx
1053*44bedb31SLionel Sambuc 	jbe	L_do_copy          /* if (nbytes >= len) */
1054*44bedb31SLionel Sambuc 
1055*44bedb31SLionel Sambuc 	sub	eax, ecx         /* len -= nbytes */
1056*44bedb31SLionel Sambuc 	rep     movsb
1057*44bedb31SLionel Sambuc 	mov	esi, [esp+28]     /* from = window */
1058*44bedb31SLionel Sambuc 	mov	ecx, [esp+52]     /* nbytes = write */
1059*44bedb31SLionel Sambuc 	cmp	eax, ecx
1060*44bedb31SLionel Sambuc 	jbe	L_do_copy          /* if (nbytes >= len) */
1061*44bedb31SLionel Sambuc 
1062*44bedb31SLionel Sambuc 	sub	eax, ecx         /* len -= nbytes */
1063*44bedb31SLionel Sambuc 	rep     movsb
1064*44bedb31SLionel Sambuc 	mov	esi, edi
1065*44bedb31SLionel Sambuc 	sub	esi, ebp         /* from = out - dist */
1066*44bedb31SLionel Sambuc 	jmp	L_do_copy
1067*44bedb31SLionel Sambuc 
1068*44bedb31SLionel Sambuc ALIGN 4
1069*44bedb31SLionel Sambuc L_contiguous_in_window:
1070*44bedb31SLionel Sambuc 	add	esi, eax
1071*44bedb31SLionel Sambuc 	sub	esi, ecx         /* from += write - nbytes */
1072*44bedb31SLionel Sambuc 
1073*44bedb31SLionel Sambuc 	mov	eax, [esp+64]    /* eax = len */
1074*44bedb31SLionel Sambuc 	cmp	eax, ecx
1075*44bedb31SLionel Sambuc 	jbe	L_do_copy          /* if (nbytes >= len) */
1076*44bedb31SLionel Sambuc 
1077*44bedb31SLionel Sambuc 	sub	eax, ecx         /* len -= nbytes */
1078*44bedb31SLionel Sambuc 	rep     movsb
1079*44bedb31SLionel Sambuc 	mov	esi, edi
1080*44bedb31SLionel Sambuc 	sub	esi, ebp         /* from = out - dist */
1081*44bedb31SLionel Sambuc 	jmp	L_do_copy
1082*44bedb31SLionel Sambuc 
1083*44bedb31SLionel Sambuc ALIGN 4
1084*44bedb31SLionel Sambuc L_do_copy:
1085*44bedb31SLionel Sambuc 	mov	ecx, eax
1086*44bedb31SLionel Sambuc 	rep     movsb
1087*44bedb31SLionel Sambuc 
1088*44bedb31SLionel Sambuc 	mov	esi, [esp+8]      /* move in back to %esi, toss from */
1089*44bedb31SLionel Sambuc 	mov	ebp, [esp+32]     /* ebp = lcode */
1090*44bedb31SLionel Sambuc 	jmp	L_while_test
1091*44bedb31SLionel Sambuc 
1092*44bedb31SLionel Sambuc L_test_for_end_of_block:
1093*44bedb31SLionel Sambuc 	test	al, 32
1094*44bedb31SLionel Sambuc 	jz	L_invalid_literal_length_code
1095*44bedb31SLionel Sambuc 	mov	dword ptr [esp+72], 1
1096*44bedb31SLionel Sambuc 	jmp	L_break_loop_with_status
1097*44bedb31SLionel Sambuc 
1098*44bedb31SLionel Sambuc L_invalid_literal_length_code:
1099*44bedb31SLionel Sambuc 	mov	dword ptr [esp+72], 2
1100*44bedb31SLionel Sambuc 	jmp	L_break_loop_with_status
1101*44bedb31SLionel Sambuc 
1102*44bedb31SLionel Sambuc L_invalid_distance_code:
1103*44bedb31SLionel Sambuc 	mov	dword ptr [esp+72], 3
1104*44bedb31SLionel Sambuc 	jmp	L_break_loop_with_status
1105*44bedb31SLionel Sambuc 
1106*44bedb31SLionel Sambuc L_invalid_distance_too_far:
1107*44bedb31SLionel Sambuc 	mov	esi, [esp+4]
1108*44bedb31SLionel Sambuc 	mov	dword ptr [esp+72], 4
1109*44bedb31SLionel Sambuc 	jmp	L_break_loop_with_status
1110*44bedb31SLionel Sambuc 
1111*44bedb31SLionel Sambuc L_break_loop:
1112*44bedb31SLionel Sambuc 	mov	dword ptr [esp+72], 0
1113*44bedb31SLionel Sambuc 
1114*44bedb31SLionel Sambuc L_break_loop_with_status:
1115*44bedb31SLionel Sambuc /* put in, out, bits, and hold back into ar and pop esp */
1116*44bedb31SLionel Sambuc 	mov	[esp+8], esi     /* save in */
1117*44bedb31SLionel Sambuc 	mov	[esp+16], edi    /* save out */
1118*44bedb31SLionel Sambuc 	mov	[esp+44], ebx    /* save bits */
1119*44bedb31SLionel Sambuc 	mov	[esp+40], edx    /* save hold */
1120*44bedb31SLionel Sambuc 	mov	ebp, [esp+4]     /* restore esp, ebp */
1121*44bedb31SLionel Sambuc 	mov	esp, [esp]
1122*44bedb31SLionel Sambuc     }
1123*44bedb31SLionel Sambuc #else
1124*44bedb31SLionel Sambuc #error "x86 architecture not defined"
1125*44bedb31SLionel Sambuc #endif
1126*44bedb31SLionel Sambuc 
1127*44bedb31SLionel Sambuc     if (ar.status > 1) {
1128*44bedb31SLionel Sambuc         if (ar.status == 2)
1129*44bedb31SLionel Sambuc             strm->msg = "invalid literal/length code";
1130*44bedb31SLionel Sambuc         else if (ar.status == 3)
1131*44bedb31SLionel Sambuc             strm->msg = "invalid distance code";
1132*44bedb31SLionel Sambuc         else
1133*44bedb31SLionel Sambuc             strm->msg = "invalid distance too far back";
1134*44bedb31SLionel Sambuc         state->mode = BAD;
1135*44bedb31SLionel Sambuc     }
1136*44bedb31SLionel Sambuc     else if ( ar.status == 1 ) {
1137*44bedb31SLionel Sambuc         state->mode = TYPE;
1138*44bedb31SLionel Sambuc     }
1139*44bedb31SLionel Sambuc 
1140*44bedb31SLionel Sambuc     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
1141*44bedb31SLionel Sambuc     ar.len = ar.bits >> 3;
1142*44bedb31SLionel Sambuc     ar.in -= ar.len;
1143*44bedb31SLionel Sambuc     ar.bits -= ar.len << 3;
1144*44bedb31SLionel Sambuc     ar.hold &= (1U << ar.bits) - 1;
1145*44bedb31SLionel Sambuc 
1146*44bedb31SLionel Sambuc     /* update state and return */
1147*44bedb31SLionel Sambuc     strm->next_in = ar.in;
1148*44bedb31SLionel Sambuc     strm->next_out = ar.out;
1149*44bedb31SLionel Sambuc     strm->avail_in = (unsigned)(ar.in < ar.last ?
1150*44bedb31SLionel Sambuc                                 PAD_AVAIL_IN + (ar.last - ar.in) :
1151*44bedb31SLionel Sambuc                                 PAD_AVAIL_IN - (ar.in - ar.last));
1152*44bedb31SLionel Sambuc     strm->avail_out = (unsigned)(ar.out < ar.end ?
1153*44bedb31SLionel Sambuc                                  PAD_AVAIL_OUT + (ar.end - ar.out) :
1154*44bedb31SLionel Sambuc                                  PAD_AVAIL_OUT - (ar.out - ar.end));
1155*44bedb31SLionel Sambuc     state->hold = ar.hold;
1156*44bedb31SLionel Sambuc     state->bits = ar.bits;
1157*44bedb31SLionel Sambuc     return;
1158*44bedb31SLionel Sambuc }
1159*44bedb31SLionel Sambuc 
1160