inffas86.c 39.7 KB
Newer Older
M
Mark Adler 已提交
1 2 3 4 5 6 7 8 9
/* inffas86.c is a hand tuned assembler version of
 *
 * inffast.c -- fast decoding
 * Copyright (C) 1995-2003 Mark Adler
 * For conditions of distribution and use, see copyright notice in zlib.h
 *
 * Copyright (C) 2003 Chris Anderson <christop@charm.net>
 * Please use the copyright conditions above.
 *
M
Mark Adler 已提交
10 11 12 13 14 15 16 17 18
 * Dec-29-2003 -- I added AMD64 inflate asm support.  This version is also
 * slightly quicker on x86 systems because, instead of using rep movsb to copy
 * data, it uses rep movsw, which moves data in 2-byte chunks instead of single
 * bytes.  I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates
 * from http://fedora.linux.duke.edu/fc1_x86_64
 * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with
 * 1GB ram.  The 64-bit version is about 4% faster than the 32-bit version,
 * when decompressing mozilla-source-1.3.tar.gz.
 *
M
Mark Adler 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from
 * the gcc -S output of zlib-1.2.0/inffast.c.  Zlib-1.2.0 is in beta release at
 * the moment.  I have successfully compiled and tested this code with gcc2.96,
 * gcc3.2, icc5.0, msvc6.0.  It is very close to the speed of inffast.S
 * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX
 * enabled.  I will attempt to merge the MMX code into this version.  Newer
 * versions of this and inffast.S can be found at
 * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/
 */

#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"

/* Mark Adler's comments from inffast.c: */

/*
   Decode literal, length, and distance codes and write out the resulting
   literal and match bytes until either not enough input or output is
   available, an end-of-block is encountered, or a data error is encountered.
   When large enough input and output buffers are supplied to inflate(), for
   example, a 16K input buffer and a 64K output buffer, more than 95% of the
   inflate execution time is spent in this routine.

   Entry assumptions:

        state->mode == LEN
        strm->avail_in >= 6
        strm->avail_out >= 258
        start >= strm->avail_out
        state->bits < 8

   On return, state->mode is one of:

        LEN -- ran out of enough output space or enough available input
        TYPE -- reached end of block code, inflate() to interpret next block
        BAD -- error in block data

   Notes:

    - The maximum input bits used by a length/distance pair is 15 bits for the
      length code, 5 bits for the length extra, 15 bits for the distance code,
      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
      Therefore if strm->avail_in >= 6, then there is enough input to avoid
      checking for available input while decoding.

    - The maximum bytes that a single length/distance pair can output is 258
      bytes, which is the maximum length that can be coded.  inflate_fast()
      requires strm->avail_out >= 258 for each loop to avoid checking for
      output space.
 */
void inflate_fast(strm, start)
z_streamp strm;
unsigned start;         /* inflate()'s starting value for strm->avail_out */
{
    struct inflate_state FAR *state;
    struct inffast_ar {
M
Mark Adler 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/* 64   32                               x86  x86_64 */
/* ar offset                              register */
/*  0    0 */ void *esp;                /* esp save */
/*  8    4 */ void *ebp;                /* ebp save */
/* 16    8 */ unsigned char FAR *in;    /* esi rsi  local strm->next_in */
/* 24   12 */ unsigned char FAR *last;  /*     r9   while in < last */
/* 32   16 */ unsigned char FAR *out;   /* edi rdi  local strm->next_out */
/* 40   20 */ unsigned char FAR *beg;   /*          inflate()'s init next_out */
/* 48   24 */ unsigned char FAR *end;   /*     r10  while out < end */
/* 56   28 */ unsigned char FAR *window;/*          size of window, wsize!=0 */
/* 64   32 */ code const FAR *lcode;    /* ebp rbp  local strm->lencode */
/* 72   36 */ code const FAR *dcode;    /*     r11  local strm->distcode */
/* 80   40 */ unsigned long hold;       /* edx rdx  local strm->hold */
/* 88   44 */ unsigned bits;            /* ebx rbx  local strm->bits */
/* 92   48 */ unsigned wsize;           /*          window size */
/* 96   52 */ unsigned write;           /*          window write index */
/*100   56 */ unsigned lmask;           /*     r12  mask for lcode */
/*104   60 */ unsigned dmask;           /*     r13  mask for dcode */
/*108   64 */ unsigned len;             /*     r14  match length */
/*112   68 */ unsigned dist;            /*     r15  match distance */
/*116   72 */ unsigned status;          /*          set when state chng*/
M
Mark Adler 已提交
98 99
    } ar;

M
Mark Adler 已提交
100 101 102 103 104 105 106 107
#if defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )
#define PAD_AVAIL_IN 6
#define PAD_AVAIL_OUT 258
#else
#define PAD_AVAIL_IN 5
#define PAD_AVAIL_OUT 257
#endif

M
Mark Adler 已提交
108 109 110
    /* copy state to local variables */
    state = (struct inflate_state FAR *)strm->state;
    ar.in = strm->next_in;
M
Mark Adler 已提交
111
    ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN);
M
Mark Adler 已提交
112 113
    ar.out = strm->next_out;
    ar.beg = ar.out - (start - strm->avail_out);
M
Mark Adler 已提交
114
    ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT);
M
Mark Adler 已提交
115
    ar.wsize = state->wsize;
M
Mark Adler 已提交
116
    ar.write = state->wnext;
M
Mark Adler 已提交
117 118 119 120 121 122 123 124 125 126 127
    ar.window = state->window;
    ar.hold = state->hold;
    ar.bits = state->bits;
    ar.lcode = state->lencode;
    ar.dcode = state->distcode;
    ar.lmask = (1U << state->lenbits) - 1;
    ar.dmask = (1U << state->distbits) - 1;

    /* decode literals and length/distances until end-of-block or not enough
       input data or output space */

M
Mark Adler 已提交
128 129
    /* align in on 1/2 hold size boundary */
    while (((unsigned long)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) {
M
Mark Adler 已提交
130 131 132 133
        ar.hold += (unsigned long)*ar.in++ << ar.bits;
        ar.bits += 8;
    }

M
Mark Adler 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
#if defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )
    __asm__ __volatile__ (
"        leaq    %0, %%rax\n"
"        movq    %%rbp, 8(%%rax)\n"       /* save regs rbp and rsp */
"        movq    %%rsp, (%%rax)\n"
"        movq    %%rax, %%rsp\n"          /* make rsp point to &ar */
"        movq    16(%%rsp), %%rsi\n"      /* rsi  = in */
"        movq    32(%%rsp), %%rdi\n"      /* rdi  = out */
"        movq    24(%%rsp), %%r9\n"       /* r9   = last */
"        movq    48(%%rsp), %%r10\n"      /* r10  = end */
"        movq    64(%%rsp), %%rbp\n"      /* rbp  = lcode */
"        movq    72(%%rsp), %%r11\n"      /* r11  = dcode */
"        movq    80(%%rsp), %%rdx\n"      /* rdx  = hold */
"        movl    88(%%rsp), %%ebx\n"      /* ebx  = bits */
"        movl    100(%%rsp), %%r12d\n"    /* r12d = lmask */
"        movl    104(%%rsp), %%r13d\n"    /* r13d = dmask */
                                          /* r14d = len */
                                          /* r15d = dist */
"        cld\n"
"        cmpq    %%rdi, %%r10\n"
"        je      .L_one_time\n"           /* if only one decode left */
"        cmpq    %%rsi, %%r9\n"
"        je      .L_one_time\n"
"        jmp     .L_do_loop\n"

".L_one_time:\n"
"        movq    %%r12, %%r8\n"           /* r8 = lmask */
"        cmpb    $32, %%bl\n"
"        ja      .L_get_length_code_one_time\n"

"        lodsl\n"                         /* eax = *(uint *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $32, %%bl\n"             /* bits += 32 */
"        shlq    %%cl, %%rax\n"
"        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */
"        jmp     .L_get_length_code_one_time\n"

".align 32,0x90\n"
".L_while_test:\n"
"        cmpq    %%rdi, %%r10\n"
"        jbe     .L_break_loop\n"
"        cmpq    %%rsi, %%r9\n"
"        jbe     .L_break_loop\n"

".L_do_loop:\n"
"        movq    %%r12, %%r8\n"           /* r8 = lmask */
"        cmpb    $32, %%bl\n"
"        ja      .L_get_length_code\n"    /* if (32 < bits) */

"        lodsl\n"                         /* eax = *(uint *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $32, %%bl\n"             /* bits += 32 */
"        shlq    %%cl, %%rax\n"
"        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */

".L_get_length_code:\n"
"        andq    %%rdx, %%r8\n"            /* r8 &= hold */
"        movl    (%%rbp,%%r8,4), %%eax\n"  /* eax = lcode[hold & lmask] */

"        movb    %%ah, %%cl\n"            /* cl = this.bits */
"        subb    %%ah, %%bl\n"            /* bits -= this.bits */
"        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */

"        testb   %%al, %%al\n"
"        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */

"        movq    %%r12, %%r8\n"            /* r8 = lmask */
"        shrl    $16, %%eax\n"            /* output this.val char */
"        stosb\n"

".L_get_length_code_one_time:\n"
"        andq    %%rdx, %%r8\n"            /* r8 &= hold */
"        movl    (%%rbp,%%r8,4), %%eax\n" /* eax = lcode[hold & lmask] */

".L_dolen:\n"
"        movb    %%ah, %%cl\n"            /* cl = this.bits */
"        subb    %%ah, %%bl\n"            /* bits -= this.bits */
"        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */

"        testb   %%al, %%al\n"
"        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */

"        shrl    $16, %%eax\n"            /* output this.val char */
"        stosb\n"
"        jmp     .L_while_test\n"

".align 32,0x90\n"
".L_test_for_length_base:\n"
"        movl    %%eax, %%r14d\n"         /* len = this */
"        shrl    $16, %%r14d\n"           /* len = this.val */
"        movb    %%al, %%cl\n"

"        testb   $16, %%al\n"
"        jz      .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
"        andb    $15, %%cl\n"             /* op &= 15 */
"        jz      .L_decode_distance\n"    /* if (!op) */

".L_add_bits_to_len:\n"
"        subb    %%cl, %%bl\n"
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        shrq    %%cl, %%rdx\n"
"        addl    %%eax, %%r14d\n"         /* len += hold & mask[op] */

".L_decode_distance:\n"
"        movq    %%r13, %%r8\n"           /* r8 = dmask */
"        cmpb    $32, %%bl\n"
"        ja      .L_get_distance_code\n"  /* if (32 < bits) */

"        lodsl\n"                         /* eax = *(uint *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $32, %%bl\n"             /* bits += 32 */
"        shlq    %%cl, %%rax\n"
"        orq     %%rax, %%rdx\n"          /* hold |= *((uint *)in)++ << bits */

".L_get_distance_code:\n"
"        andq    %%rdx, %%r8\n"           /* r8 &= hold */
"        movl    (%%r11,%%r8,4), %%eax\n" /* eax = dcode[hold & dmask] */

".L_dodist:\n"
"        movl    %%eax, %%r15d\n"         /* dist = this */
"        shrl    $16, %%r15d\n"           /* dist = this.val */
"        movb    %%ah, %%cl\n"
"        subb    %%ah, %%bl\n"            /* bits -= this.bits */
"        shrq    %%cl, %%rdx\n"           /* hold >>= this.bits */
"        movb    %%al, %%cl\n"            /* cl = this.op */

"        testb   $16, %%al\n"             /* if ((op & 16) == 0) */
"        jz      .L_test_for_second_level_dist\n"
"        andb    $15, %%cl\n"             /* op &= 15 */
"        jz      .L_check_dist_one\n"

".L_add_bits_to_dist:\n"
"        subb    %%cl, %%bl\n"
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"                 /* (1 << op) - 1 */
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        shrq    %%cl, %%rdx\n"
"        addl    %%eax, %%r15d\n"         /* dist += hold & ((1 << op) - 1) */

".L_check_window:\n"
"        movq    %%rsi, %%r8\n"           /* save in so from can use it's reg */
"        movq    %%rdi, %%rax\n"
"        subq    40(%%rsp), %%rax\n"      /* nbytes = out - beg */

"        cmpl    %%r15d, %%eax\n"
"        jb      .L_clip_window\n"        /* if (dist > nbytes) 4.2% */

"        movl    %%r14d, %%ecx\n"         /* ecx = len */
"        movq    %%rdi, %%rsi\n"
"        subq    %%r15, %%rsi\n"          /* from = out - dist */

"        sarl    %%ecx\n"
"        jnc     .L_copy_two\n"           /* if len % 2 == 0 */

"        rep     movsw\n"
"        movb    (%%rsi), %%al\n"
"        movb    %%al, (%%rdi)\n"
"        incq    %%rdi\n"

"        movq    %%r8, %%rsi\n"           /* move in back to %rsi, toss from */
"        jmp     .L_while_test\n"

".L_copy_two:\n"
"        rep     movsw\n"
"        movq    %%r8, %%rsi\n"           /* move in back to %rsi, toss from */
"        jmp     .L_while_test\n"

".align 32,0x90\n"
".L_check_dist_one:\n"
"        cmpl    $1, %%r15d\n"            /* if dist 1, is a memset */
"        jne     .L_check_window\n"
"        cmpq    %%rdi, 40(%%rsp)\n"      /* if out == beg, outside window */
"        je      .L_check_window\n"

"        movl    %%r14d, %%ecx\n"         /* ecx = len */
"        movb    -1(%%rdi), %%al\n"
"        movb    %%al, %%ah\n"

"        sarl    %%ecx\n"
"        jnc     .L_set_two\n"
"        movb    %%al, (%%rdi)\n"
"        incq    %%rdi\n"

".L_set_two:\n"
"        rep     stosw\n"
"        jmp     .L_while_test\n"

".align 32,0x90\n"
".L_test_for_second_level_length:\n"
"        testb   $64, %%al\n"
"        jnz     .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */

"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"         /* eax &= hold */
"        addl    %%r14d, %%eax\n"        /* eax += len */
"        movl    (%%rbp,%%rax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
"        jmp     .L_dolen\n"

".align 32,0x90\n"
".L_test_for_second_level_dist:\n"
"        testb   $64, %%al\n"
"        jnz     .L_invalid_distance_code\n" /* if ((op & 64) != 0) */

"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"         /* eax &= hold */
"        addl    %%r15d, %%eax\n"        /* eax += dist */
"        movl    (%%r11,%%rax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
"        jmp     .L_dodist\n"

".align 32,0x90\n"
".L_clip_window:\n"
"        movl    %%eax, %%ecx\n"         /* ecx = nbytes */
"        movl    92(%%rsp), %%eax\n"     /* eax = wsize, prepare for dist cmp */
"        negl    %%ecx\n"                /* nbytes = -nbytes */

"        cmpl    %%r15d, %%eax\n"
"        jb      .L_invalid_distance_too_far\n" /* if (dist > wsize) */

"        addl    %%r15d, %%ecx\n"         /* nbytes = dist - nbytes */
"        cmpl    $0, 96(%%rsp)\n"
"        jne     .L_wrap_around_window\n" /* if (write != 0) */

"        movq    56(%%rsp), %%rsi\n"     /* from  = window */
"        subl    %%ecx, %%eax\n"         /* eax  -= nbytes */
"        addq    %%rax, %%rsi\n"         /* from += wsize - nbytes */

"        movl    %%r14d, %%eax\n"        /* eax = len */
"        cmpl    %%ecx, %%r14d\n"
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */

"        subl    %%ecx, %%eax\n"         /* eax -= nbytes */
"        rep     movsb\n"
"        movq    %%rdi, %%rsi\n"
"        subq    %%r15, %%rsi\n"         /* from = &out[ -dist ] */
"        jmp     .L_do_copy\n"

".align 32,0x90\n"
".L_wrap_around_window:\n"
"        movl    96(%%rsp), %%eax\n"     /* eax = write */
"        cmpl    %%eax, %%ecx\n"
"        jbe     .L_contiguous_in_window\n" /* if (write >= nbytes) */

"        movl    92(%%rsp), %%esi\n"     /* from  = wsize */
"        addq    56(%%rsp), %%rsi\n"     /* from += window */
"        addq    %%rax, %%rsi\n"         /* from += write */
"        subq    %%rcx, %%rsi\n"         /* from -= nbytes */
"        subl    %%eax, %%ecx\n"         /* nbytes -= write */

"        movl    %%r14d, %%eax\n"        /* eax = len */
"        cmpl    %%ecx, %%eax\n"
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movq    56(%%rsp), %%rsi\n"     /* from = window */
"        movl    96(%%rsp), %%ecx\n"     /* nbytes = write */
"        cmpl    %%ecx, %%eax\n"
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movq    %%rdi, %%rsi\n"
"        subq    %%r15, %%rsi\n"         /* from = out - dist */
"        jmp     .L_do_copy\n"

".align 32,0x90\n"
".L_contiguous_in_window:\n"
"        movq    56(%%rsp), %%rsi\n"     /* rsi = window */
"        addq    %%rax, %%rsi\n"
"        subq    %%rcx, %%rsi\n"         /* from += write - nbytes */

"        movl    %%r14d, %%eax\n"        /* eax = len */
"        cmpl    %%ecx, %%eax\n"
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movq    %%rdi, %%rsi\n"
"        subq    %%r15, %%rsi\n"         /* from = out - dist */
"        jmp     .L_do_copy\n"           /* if (nbytes >= len) */

".align 32,0x90\n"
".L_do_copy:\n"
"        movl    %%eax, %%ecx\n"         /* ecx = len */
"        rep     movsb\n"

"        movq    %%r8, %%rsi\n"          /* move in back to %esi, toss from */
"        jmp     .L_while_test\n"

".L_test_for_end_of_block:\n"
"        testb   $32, %%al\n"
"        jz      .L_invalid_literal_length_code\n"
"        movl    $1, 116(%%rsp)\n"
"        jmp     .L_break_loop_with_status\n"

".L_invalid_literal_length_code:\n"
"        movl    $2, 116(%%rsp)\n"
"        jmp     .L_break_loop_with_status\n"

".L_invalid_distance_code:\n"
"        movl    $3, 116(%%rsp)\n"
"        jmp     .L_break_loop_with_status\n"

".L_invalid_distance_too_far:\n"
"        movl    $4, 116(%%rsp)\n"
"        jmp     .L_break_loop_with_status\n"

".L_break_loop:\n"
"        movl    $0, 116(%%rsp)\n"

".L_break_loop_with_status:\n"
/* put in, out, bits, and hold back into ar and pop esp */
"        movq    %%rsi, 16(%%rsp)\n"     /* in */
"        movq    %%rdi, 32(%%rsp)\n"     /* out */
"        movl    %%ebx, 88(%%rsp)\n"     /* bits */
"        movq    %%rdx, 80(%%rsp)\n"     /* hold */
"        movq    (%%rsp), %%rax\n"       /* restore rbp and rsp */
"        movq    8(%%rsp), %%rbp\n"
"        movq    %%rax, %%rsp\n"
          :
          : "m" (ar)
          : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
            "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
    );
#elif ( defined( __GNUC__ ) || defined( __ICC ) ) && defined( __i386 )
M
Mark Adler 已提交
471 472
    __asm__ __volatile__ (
"        leal    %0, %%eax\n"
M
Mark Adler 已提交
473 474
"        movl    %%esp, (%%eax)\n"        /* save esp, ebp */
"        movl    %%ebp, 4(%%eax)\n"
M
Mark Adler 已提交
475
"        movl    %%eax, %%esp\n"
M
Mark Adler 已提交
476 477 478 479 480
"        movl    8(%%esp), %%esi\n"       /* esi = in */
"        movl    16(%%esp), %%edi\n"      /* edi = out */
"        movl    40(%%esp), %%edx\n"      /* edx = hold */
"        movl    44(%%esp), %%ebx\n"      /* ebx = bits */
"        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
M
Mark Adler 已提交
481 482 483 484

"        cld\n"
"        jmp     .L_do_loop\n"

M
Mark Adler 已提交
485
".align 32,0x90\n"
M
Mark Adler 已提交
486
".L_while_test:\n"
M
Mark Adler 已提交
487
"        cmpl    %%edi, 24(%%esp)\n"      /* out < end */
M
Mark Adler 已提交
488
"        jbe     .L_break_loop\n"
M
Mark Adler 已提交
489
"        cmpl    %%esi, 12(%%esp)\n"      /* in < last */
M
Mark Adler 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503
"        jbe     .L_break_loop\n"

".L_do_loop:\n"
"        cmpb    $15, %%bl\n"
"        ja      .L_get_length_code\n"    /* if (15 < bits) */

"        xorl    %%eax, %%eax\n"
"        lodsw\n"                         /* al = *(ushort *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $16, %%bl\n"             /* bits += 16 */
"        shll    %%cl, %%eax\n"
"        orl     %%eax, %%edx\n"        /* hold |= *((ushort *)in)++ << bits */

".L_get_length_code:\n"
M
Mark Adler 已提交
504
"        movl    56(%%esp), %%eax\n"      /* eax = lmask */
M
Mark Adler 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        movl    (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[hold & lmask] */

".L_dolen:\n"
"        movb    %%ah, %%cl\n"            /* cl = this.bits */
"        subb    %%ah, %%bl\n"            /* bits -= this.bits */
"        shrl    %%cl, %%edx\n"           /* hold >>= this.bits */

"        testb   %%al, %%al\n"
"        jnz     .L_test_for_length_base\n" /* if (op != 0) 45.7% */

"        shrl    $16, %%eax\n"            /* output this.val char */
"        stosb\n"
"        jmp     .L_while_test\n"

M
Mark Adler 已提交
520
".align 32,0x90\n"
M
Mark Adler 已提交
521 522 523
".L_test_for_length_base:\n"
"        movl    %%eax, %%ecx\n"          /* len = this */
"        shrl    $16, %%ecx\n"            /* len = this.val */
M
Mark Adler 已提交
524
"        movl    %%ecx, 64(%%esp)\n"      /* save len */
M
Mark Adler 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
"        movb    %%al, %%cl\n"

"        testb   $16, %%al\n"
"        jz      .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
"        andb    $15, %%cl\n"             /* op &= 15 */
"        jz      .L_decode_distance\n"    /* if (!op) */
"        cmpb    %%cl, %%bl\n"
"        jae     .L_add_bits_to_len\n"    /* if (op <= bits) */

"        movb    %%cl, %%ch\n"            /* stash op in ch, freeing cl */
"        xorl    %%eax, %%eax\n"
"        lodsw\n"                         /* al = *(ushort *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $16, %%bl\n"             /* bits += 16 */
"        shll    %%cl, %%eax\n"
"        orl     %%eax, %%edx\n"         /* hold |= *((ushort *)in)++ << bits */
"        movb    %%ch, %%cl\n"            /* move op back to ecx */

".L_add_bits_to_len:\n"
M
Mark Adler 已提交
544 545 546
"        subb    %%cl, %%bl\n"
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
M
Mark Adler 已提交
547 548 549 550
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        shrl    %%cl, %%edx\n"
M
Mark Adler 已提交
551
"        addl    %%eax, 64(%%esp)\n"      /* len += hold & mask[op] */
M
Mark Adler 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564

".L_decode_distance:\n"
"        cmpb    $15, %%bl\n"
"        ja      .L_get_distance_code\n"  /* if (15 < bits) */

"        xorl    %%eax, %%eax\n"
"        lodsw\n"                         /* al = *(ushort *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $16, %%bl\n"             /* bits += 16 */
"        shll    %%cl, %%eax\n"
"        orl     %%eax, %%edx\n"         /* hold |= *((ushort *)in)++ << bits */

".L_get_distance_code:\n"
M
Mark Adler 已提交
565 566
"        movl    60(%%esp), %%eax\n"      /* eax = dmask */
"        movl    36(%%esp), %%ecx\n"      /* ecx = dcode */
M
Mark Adler 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        movl    (%%ecx,%%eax,4), %%eax\n"/* eax = dcode[hold & dmask] */

".L_dodist:\n"
"        movl    %%eax, %%ebp\n"          /* dist = this */
"        shrl    $16, %%ebp\n"            /* dist = this.val */
"        movb    %%ah, %%cl\n"
"        subb    %%ah, %%bl\n"            /* bits -= this.bits */
"        shrl    %%cl, %%edx\n"           /* hold >>= this.bits */
"        movb    %%al, %%cl\n"            /* cl = this.op */

"        testb   $16, %%al\n"             /* if ((op & 16) == 0) */
"        jz      .L_test_for_second_level_dist\n"
"        andb    $15, %%cl\n"             /* op &= 15 */
"        jz      .L_check_dist_one\n"
"        cmpb    %%cl, %%bl\n"
"        jae     .L_add_bits_to_dist\n"   /* if (op <= bits) 97.6% */

"        movb    %%cl, %%ch\n"            /* stash op in ch, freeing cl */
"        xorl    %%eax, %%eax\n"
"        lodsw\n"                         /* al = *(ushort *)in++ */
"        movb    %%bl, %%cl\n"            /* cl = bits, needs it for shifting */
"        addb    $16, %%bl\n"             /* bits += 16 */
"        shll    %%cl, %%eax\n"
"        orl     %%eax, %%edx\n"        /* hold |= *((ushort *)in)++ << bits */
"        movb    %%ch, %%cl\n"            /* move op back to ecx */

".L_add_bits_to_dist:\n"
M
Mark Adler 已提交
595 596 597
"        subb    %%cl, %%bl\n"
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
M
Mark Adler 已提交
598 599 600 601 602 603 604
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"                 /* (1 << op) - 1 */
"        andl    %%edx, %%eax\n"          /* eax &= hold */
"        shrl    %%cl, %%edx\n"
"        addl    %%eax, %%ebp\n"          /* dist += hold & ((1 << op) - 1) */

".L_check_window:\n"
M
Mark Adler 已提交
605
"        movl    %%esi, 8(%%esp)\n"       /* save in so from can use it's reg */
M
Mark Adler 已提交
606
"        movl    %%edi, %%eax\n"
M
Mark Adler 已提交
607
"        subl    20(%%esp), %%eax\n"      /* nbytes = out - beg */
M
Mark Adler 已提交
608 609 610 611

"        cmpl    %%ebp, %%eax\n"
"        jb      .L_clip_window\n"        /* if (dist > nbytes) 4.2% */

M
Mark Adler 已提交
612
"        movl    64(%%esp), %%ecx\n"      /* ecx = len */
M
Mark Adler 已提交
613 614 615
"        movl    %%edi, %%esi\n"
"        subl    %%ebp, %%esi\n"          /* from = out - dist */

M
Mark Adler 已提交
616 617 618 619
"        sarl    %%ecx\n"
"        jnc     .L_copy_two\n"           /* if len % 2 == 0 */

"        rep     movsw\n"
M
Mark Adler 已提交
620 621
"        movb    (%%esi), %%al\n"
"        movb    %%al, (%%edi)\n"
M
Mark Adler 已提交
622
"        incl    %%edi\n"
M
Mark Adler 已提交
623

M
Mark Adler 已提交
624 625
"        movl    8(%%esp), %%esi\n"       /* move in back to %esi, toss from */
"        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
M
Mark Adler 已提交
626 627
"        jmp     .L_while_test\n"

M
Mark Adler 已提交
628 629 630 631 632 633 634
".L_copy_two:\n"
"        rep     movsw\n"
"        movl    8(%%esp), %%esi\n"       /* move in back to %esi, toss from */
"        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
"        jmp     .L_while_test\n"

".align 32,0x90\n"
M
Mark Adler 已提交
635 636 637
".L_check_dist_one:\n"
"        cmpl    $1, %%ebp\n"            /* if dist 1, is a memset */
"        jne     .L_check_window\n"
M
Mark Adler 已提交
638 639 640 641 642 643 644 645 646 647 648
"        cmpl    %%edi, 20(%%esp)\n"
"        je      .L_check_window\n"      /* out == beg, if outside window */

"        movl    64(%%esp), %%ecx\n"      /* ecx = len */
"        movb    -1(%%edi), %%al\n"
"        movb    %%al, %%ah\n"

"        sarl    %%ecx\n"
"        jnc     .L_set_two\n"
"        movb    %%al, (%%edi)\n"
"        incl    %%edi\n"
M
Mark Adler 已提交
649

M
Mark Adler 已提交
650 651 652
".L_set_two:\n"
"        rep     stosw\n"
"        movl    32(%%esp), %%ebp\n"      /* ebp = lcode */
M
Mark Adler 已提交
653 654
"        jmp     .L_while_test\n"

M
Mark Adler 已提交
655
".align 32,0x90\n"
M
Mark Adler 已提交
656 657 658 659
".L_test_for_second_level_length:\n"
"        testb   $64, %%al\n"
"        jnz     .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */

M
Mark Adler 已提交
660 661
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
M
Mark Adler 已提交
662 663 664
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"         /* eax &= hold */
M
Mark Adler 已提交
665
"        addl    64(%%esp), %%eax\n"     /* eax += len */
M
Mark Adler 已提交
666 667 668
"        movl    (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
"        jmp     .L_dolen\n"

M
Mark Adler 已提交
669
".align 32,0x90\n"
M
Mark Adler 已提交
670 671 672 673
".L_test_for_second_level_dist:\n"
"        testb   $64, %%al\n"
"        jnz     .L_invalid_distance_code\n" /* if ((op & 64) != 0) */

M
Mark Adler 已提交
674 675
"        xorl    %%eax, %%eax\n"
"        incl    %%eax\n"
M
Mark Adler 已提交
676 677 678
"        shll    %%cl, %%eax\n"
"        decl    %%eax\n"
"        andl    %%edx, %%eax\n"         /* eax &= hold */
M
Mark Adler 已提交
679 680
"        addl    %%ebp, %%eax\n"         /* eax += dist */
"        movl    36(%%esp), %%ecx\n"     /* ecx = dcode */
M
Mark Adler 已提交
681 682 683
"        movl    (%%ecx,%%eax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
"        jmp     .L_dodist\n"

M
Mark Adler 已提交
684
".align 32,0x90\n"
M
Mark Adler 已提交
685 686
".L_clip_window:\n"
"        movl    %%eax, %%ecx\n"
M
Mark Adler 已提交
687
"        movl    48(%%esp), %%eax\n"     /* eax = wsize */
M
Mark Adler 已提交
688
"        negl    %%ecx\n"                /* nbytes = -nbytes */
M
Mark Adler 已提交
689
"        movl    28(%%esp), %%esi\n"     /* from = window */
M
Mark Adler 已提交
690 691 692 693 694

"        cmpl    %%ebp, %%eax\n"
"        jb      .L_invalid_distance_too_far\n" /* if (dist > wsize) */

"        addl    %%ebp, %%ecx\n"         /* nbytes = dist - nbytes */
M
Mark Adler 已提交
695
"        cmpl    $0, 52(%%esp)\n"
M
Mark Adler 已提交
696 697 698 699 700
"        jne     .L_wrap_around_window\n" /* if (write != 0) */

"        subl    %%ecx, %%eax\n"
"        addl    %%eax, %%esi\n"         /* from += wsize - nbytes */

M
Mark Adler 已提交
701
"        movl    64(%%esp), %%eax\n"     /* eax = len */
M
Mark Adler 已提交
702
"        cmpl    %%ecx, %%eax\n"
M
Mark Adler 已提交
703
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
M
Mark Adler 已提交
704 705 706 707 708

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movl    %%edi, %%esi\n"
"        subl    %%ebp, %%esi\n"         /* from = out - dist */
M
Mark Adler 已提交
709
"        jmp     .L_do_copy\n"
M
Mark Adler 已提交
710

M
Mark Adler 已提交
711
".align 32,0x90\n"
M
Mark Adler 已提交
712
".L_wrap_around_window:\n"
M
Mark Adler 已提交
713
"        movl    52(%%esp), %%eax\n"     /* eax = write */
M
Mark Adler 已提交
714 715 716
"        cmpl    %%eax, %%ecx\n"
"        jbe     .L_contiguous_in_window\n" /* if (write >= nbytes) */

M
Mark Adler 已提交
717 718 719
"        addl    48(%%esp), %%esi\n"     /* from += wsize */
"        addl    %%eax, %%esi\n"         /* from += write */
"        subl    %%ecx, %%esi\n"         /* from -= nbytes */
M
Mark Adler 已提交
720 721
"        subl    %%eax, %%ecx\n"         /* nbytes -= write */

M
Mark Adler 已提交
722
"        movl    64(%%esp), %%eax\n"     /* eax = len */
M
Mark Adler 已提交
723
"        cmpl    %%ecx, %%eax\n"
M
Mark Adler 已提交
724
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
M
Mark Adler 已提交
725 726 727

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
M
Mark Adler 已提交
728 729
"        movl    28(%%esp), %%esi\n"     /* from = window */
"        movl    52(%%esp), %%ecx\n"     /* nbytes = write */
M
Mark Adler 已提交
730
"        cmpl    %%ecx, %%eax\n"
M
Mark Adler 已提交
731
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
M
Mark Adler 已提交
732 733 734 735 736

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movl    %%edi, %%esi\n"
"        subl    %%ebp, %%esi\n"         /* from = out - dist */
M
Mark Adler 已提交
737
"        jmp     .L_do_copy\n"
M
Mark Adler 已提交
738

M
Mark Adler 已提交
739
".align 32,0x90\n"
M
Mark Adler 已提交
740 741 742 743
".L_contiguous_in_window:\n"
"        addl    %%eax, %%esi\n"
"        subl    %%ecx, %%esi\n"         /* from += write - nbytes */

M
Mark Adler 已提交
744
"        movl    64(%%esp), %%eax\n"     /* eax = len */
M
Mark Adler 已提交
745
"        cmpl    %%ecx, %%eax\n"
M
Mark Adler 已提交
746
"        jbe     .L_do_copy\n"           /* if (nbytes >= len) */
M
Mark Adler 已提交
747 748 749 750 751

"        subl    %%ecx, %%eax\n"         /* len -= nbytes */
"        rep     movsb\n"
"        movl    %%edi, %%esi\n"
"        subl    %%ebp, %%esi\n"         /* from = out - dist */
M
Mark Adler 已提交
752
"        jmp     .L_do_copy\n"           /* if (nbytes >= len) */
M
Mark Adler 已提交
753

M
Mark Adler 已提交
754 755
".align 32,0x90\n"
".L_do_copy:\n"
M
Mark Adler 已提交
756 757 758
"        movl    %%eax, %%ecx\n"
"        rep     movsb\n"

M
Mark Adler 已提交
759 760
"        movl    8(%%esp), %%esi\n"      /* move in back to %esi, toss from */
"        movl    32(%%esp), %%ebp\n"     /* ebp = lcode */
M
Mark Adler 已提交
761 762 763 764 765
"        jmp     .L_while_test\n"

".L_test_for_end_of_block:\n"
"        testb   $32, %%al\n"
"        jz      .L_invalid_literal_length_code\n"
M
Mark Adler 已提交
766
"        movl    $1, 72(%%esp)\n"
M
Mark Adler 已提交
767 768 769
"        jmp     .L_break_loop_with_status\n"

".L_invalid_literal_length_code:\n"
M
Mark Adler 已提交
770
"        movl    $2, 72(%%esp)\n"
M
Mark Adler 已提交
771 772 773
"        jmp     .L_break_loop_with_status\n"

".L_invalid_distance_code:\n"
M
Mark Adler 已提交
774
"        movl    $3, 72(%%esp)\n"
M
Mark Adler 已提交
775 776 777
"        jmp     .L_break_loop_with_status\n"

".L_invalid_distance_too_far:\n"
M
Mark Adler 已提交
778 779
"        movl    8(%%esp), %%esi\n"
"        movl    $4, 72(%%esp)\n"
M
Mark Adler 已提交
780 781 782
"        jmp     .L_break_loop_with_status\n"

".L_break_loop:\n"
M
Mark Adler 已提交
783
"        movl    $0, 72(%%esp)\n"
M
Mark Adler 已提交
784 785 786

".L_break_loop_with_status:\n"
/* put in, out, bits, and hold back into ar and pop esp */
M
Mark Adler 已提交
787 788 789 790 791
"        movl    %%esi, 8(%%esp)\n"      /* save in */
"        movl    %%edi, 16(%%esp)\n"     /* save out */
"        movl    %%ebx, 44(%%esp)\n"     /* save bits */
"        movl    %%edx, 40(%%esp)\n"     /* save hold */
"        movl    4(%%esp), %%ebp\n"      /* restore esp, ebp */
M
Mark Adler 已提交
792 793 794 795 796
"        movl    (%%esp), %%esp\n"
          :
          : "m" (ar)
          : "memory", "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
    );
M
Mark Adler 已提交
797
#elif defined( _MSC_VER ) && ! defined( _M_AMD64 )
M
Mark Adler 已提交
798 799
    __asm {
	lea	eax, ar
M
Mark Adler 已提交
800 801
	mov	[eax], esp         /* save esp, ebp */
	mov	[eax+4], ebp
M
Mark Adler 已提交
802
	mov	esp, eax
M
Mark Adler 已提交
803 804 805 806 807
	mov	esi, [esp+8]       /* esi = in */
	mov	edi, [esp+16]      /* edi = out */
	mov	edx, [esp+40]      /* edx = hold */
	mov	ebx, [esp+44]      /* ebx = bits */
	mov	ebp, [esp+32]      /* ebp = lcode */
M
Mark Adler 已提交
808 809 810 811

	cld
	jmp	L_do_loop

M
Mark Adler 已提交
812
ALIGN 4
M
Mark Adler 已提交
813
L_while_test:
M
Mark Adler 已提交
814
	cmp	[esp+24], edi
M
Mark Adler 已提交
815
	jbe	L_break_loop
M
Mark Adler 已提交
816
	cmp	[esp+12], esi
M
Mark Adler 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830
	jbe	L_break_loop

L_do_loop:
	cmp	bl, 15
	ja	L_get_length_code    /* if (15 < bits) */

	xor	eax, eax
	lodsw                         /* al = *(ushort *)in++ */
	mov	cl, bl            /* cl = bits, needs it for shifting */
	add	bl, 16             /* bits += 16 */
	shl	eax, cl
	or	edx, eax        /* hold |= *((ushort *)in)++ << bits */

L_get_length_code:
M
Mark Adler 已提交
831
	mov	eax, [esp+56]      /* eax = lmask */
M
Mark Adler 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	and	eax, edx          /* eax &= hold */
	mov	eax, [ebp+eax*4] /* eax = lcode[hold & lmask] */

L_dolen:
	mov	cl, ah            /* cl = this.bits */
	sub	bl, ah            /* bits -= this.bits */
	shr	edx, cl           /* hold >>= this.bits */

	test	al, al
	jnz	L_test_for_length_base /* if (op != 0) 45.7% */

	shr	eax, 16            /* output this.val char */
	stosb
	jmp	L_while_test

M
Mark Adler 已提交
847
ALIGN 4
M
Mark Adler 已提交
848 849 850
L_test_for_length_base:
	mov	ecx, eax          /* len = this */
	shr	ecx, 16            /* len = this.val */
M
Mark Adler 已提交
851
	mov	[esp+64], ecx      /* save len */
M
Mark Adler 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	mov	cl, al

	test	al, 16
	jz	L_test_for_second_level_length /* if ((op & 16) == 0) 8% */
	and	cl, 15             /* op &= 15 */
	jz	L_decode_distance    /* if (!op) */
	cmp	bl, cl
	jae	L_add_bits_to_len    /* if (op <= bits) */

	mov	ch, cl            /* stash op in ch, freeing cl */
	xor	eax, eax
	lodsw                         /* al = *(ushort *)in++ */
	mov	cl, bl            /* cl = bits, needs it for shifting */
	add	bl, 16             /* bits += 16 */
	shl	eax, cl
	or	edx, eax         /* hold |= *((ushort *)in)++ << bits */
	mov	cl, ch            /* move op back to ecx */

L_add_bits_to_len:
M
Mark Adler 已提交
871 872 873
	sub	bl, cl
	xor	eax, eax
	inc	eax
M
Mark Adler 已提交
874 875 876 877
	shl	eax, cl
	dec	eax
	and	eax, edx          /* eax &= hold */
	shr	edx, cl
M
Mark Adler 已提交
878
	add	[esp+64], eax      /* len += hold & mask[op] */
M
Mark Adler 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891

L_decode_distance:
	cmp	bl, 15
	ja	L_get_distance_code  /* if (15 < bits) */

	xor	eax, eax
	lodsw                         /* al = *(ushort *)in++ */
	mov	cl, bl            /* cl = bits, needs it for shifting */
	add	bl, 16             /* bits += 16 */
	shl	eax, cl
	or	edx, eax         /* hold |= *((ushort *)in)++ << bits */

L_get_distance_code:
M
Mark Adler 已提交
892 893
	mov	eax, [esp+60]      /* eax = dmask */
	mov	ecx, [esp+36]      /* ecx = dcode */
M
Mark Adler 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	and	eax, edx          /* eax &= hold */
	mov	eax, [ecx+eax*4]/* eax = dcode[hold & dmask] */

L_dodist:
	mov	ebp, eax          /* dist = this */
	shr	ebp, 16            /* dist = this.val */
	mov	cl, ah
	sub	bl, ah            /* bits -= this.bits */
	shr	edx, cl           /* hold >>= this.bits */
	mov	cl, al            /* cl = this.op */

	test	al, 16             /* if ((op & 16) == 0) */
	jz	L_test_for_second_level_dist
	and	cl, 15             /* op &= 15 */
	jz	L_check_dist_one
	cmp	bl, cl
	jae	L_add_bits_to_dist   /* if (op <= bits) 97.6% */

	mov	ch, cl            /* stash op in ch, freeing cl */
	xor	eax, eax
	lodsw                         /* al = *(ushort *)in++ */
	mov	cl, bl            /* cl = bits, needs it for shifting */
	add	bl, 16             /* bits += 16 */
	shl	eax, cl
	or	edx, eax        /* hold |= *((ushort *)in)++ << bits */
	mov	cl, ch            /* move op back to ecx */

L_add_bits_to_dist:
M
Mark Adler 已提交
922 923 924
	sub	bl, cl
	xor	eax, eax
	inc	eax
M
Mark Adler 已提交
925 926 927 928 929 930 931
	shl	eax, cl
	dec	eax                 /* (1 << op) - 1 */
	and	eax, edx          /* eax &= hold */
	shr	edx, cl
	add	ebp, eax          /* dist += hold & ((1 << op) - 1) */

L_check_window:
M
Mark Adler 已提交
932
	mov	[esp+8], esi       /* save in so from can use it's reg */
M
Mark Adler 已提交
933
	mov	eax, edi
M
Mark Adler 已提交
934
	sub	eax, [esp+20]      /* nbytes = out - beg */
M
Mark Adler 已提交
935 936 937 938

	cmp	eax, ebp
	jb	L_clip_window        /* if (dist > nbytes) 4.2% */

M
Mark Adler 已提交
939
	mov	ecx, [esp+64]      /* ecx = len */
M
Mark Adler 已提交
940 941 942
	mov	esi, edi
	sub	esi, ebp          /* from = out - dist */

M
Mark Adler 已提交
943 944 945 946
	sar	ecx, 1
	jnc	L_copy_two

	rep     movsw
M
Mark Adler 已提交
947 948
	mov	al, [esi]
	mov	[edi], al
M
Mark Adler 已提交
949 950 951 952 953
	inc	edi

	mov	esi, [esp+8]      /* move in back to %esi, toss from */
	mov	ebp, [esp+32]     /* ebp = lcode */
	jmp	L_while_test
M
Mark Adler 已提交
954

M
Mark Adler 已提交
955 956 957 958
L_copy_two:
	rep     movsw
	mov	esi, [esp+8]      /* move in back to %esi, toss from */
	mov	ebp, [esp+32]     /* ebp = lcode */
M
Mark Adler 已提交
959 960
	jmp	L_while_test

M
Mark Adler 已提交
961
ALIGN 4
M
Mark Adler 已提交
962 963 964
L_check_dist_one:
	cmp	ebp, 1            /* if dist 1, is a memset */
	jne	L_check_window
M
Mark Adler 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	cmp	[esp+20], edi
	je	L_check_window    /* out == beg, if outside window */

	mov	ecx, [esp+64]     /* ecx = len */
	mov	al, [edi-1]
	mov	ah, al

	sar	ecx, 1
	jnc	L_set_two
	mov	[edi], al         /* memset out with from[-1] */
	inc	edi

L_set_two:
	rep     stosw
	mov	ebp, [esp+32]     /* ebp = lcode */
M
Mark Adler 已提交
980 981
	jmp	L_while_test

M
Mark Adler 已提交
982
ALIGN 4
M
Mark Adler 已提交
983 984 985 986
L_test_for_second_level_length:
	test	al, 64
	jnz	L_test_for_end_of_block /* if ((op & 64) != 0) */

M
Mark Adler 已提交
987 988
	xor	eax, eax
	inc	eax
M
Mark Adler 已提交
989 990 991
	shl	eax, cl
	dec	eax
	and	eax, edx         /* eax &= hold */
M
Mark Adler 已提交
992
	add	eax, [esp+64]     /* eax += len */
M
Mark Adler 已提交
993 994 995
	mov	eax, [ebp+eax*4] /* eax = lcode[val+(hold&mask[op])]*/
	jmp	L_dolen

M
Mark Adler 已提交
996
ALIGN 4
M
Mark Adler 已提交
997 998 999 1000
L_test_for_second_level_dist:
	test	al, 64
	jnz	L_invalid_distance_code /* if ((op & 64) != 0) */

M
Mark Adler 已提交
1001 1002
	xor	eax, eax
	inc	eax
M
Mark Adler 已提交
1003 1004 1005
	shl	eax, cl
	dec	eax
	and	eax, edx         /* eax &= hold */
M
Mark Adler 已提交
1006 1007
	add	eax, ebp         /* eax += dist */
	mov	ecx, [esp+36]     /* ecx = dcode */
M
Mark Adler 已提交
1008 1009 1010
	mov	eax, [ecx+eax*4] /* eax = dcode[val+(hold&mask[op])]*/
	jmp	L_dodist

M
Mark Adler 已提交
1011
ALIGN 4
M
Mark Adler 已提交
1012 1013
L_clip_window:
	mov	ecx, eax
M
Mark Adler 已提交
1014
	mov	eax, [esp+48]     /* eax = wsize */
M
Mark Adler 已提交
1015
	neg	ecx                /* nbytes = -nbytes */
M
Mark Adler 已提交
1016
	mov	esi, [esp+28]     /* from = window */
M
Mark Adler 已提交
1017 1018 1019 1020 1021

	cmp	eax, ebp
	jb	L_invalid_distance_too_far /* if (dist > wsize) */

	add	ecx, ebp         /* nbytes = dist - nbytes */
M
Mark Adler 已提交
1022
	cmp	dword ptr [esp+52], 0
M
Mark Adler 已提交
1023 1024 1025 1026 1027
	jne	L_wrap_around_window /* if (write != 0) */

	sub	eax, ecx
	add	esi, eax         /* from += wsize - nbytes */

M
Mark Adler 已提交
1028
	mov	eax, [esp+64]    /* eax = len */
M
Mark Adler 已提交
1029
	cmp	eax, ecx
M
Mark Adler 已提交
1030
	jbe	L_do_copy          /* if (nbytes >= len) */
M
Mark Adler 已提交
1031 1032 1033 1034 1035

	sub	eax, ecx         /* len -= nbytes */
	rep     movsb
	mov	esi, edi
	sub	esi, ebp         /* from = out - dist */
M
Mark Adler 已提交
1036
	jmp	L_do_copy
M
Mark Adler 已提交
1037

M
Mark Adler 已提交
1038
ALIGN 4
M
Mark Adler 已提交
1039
L_wrap_around_window:
M
Mark Adler 已提交
1040
	mov	eax, [esp+52]    /* eax = write */
M
Mark Adler 已提交
1041 1042 1043
	cmp	ecx, eax
	jbe	L_contiguous_in_window /* if (write >= nbytes) */

M
Mark Adler 已提交
1044 1045 1046
	add	esi, [esp+48]    /* from += wsize */
	add	esi, eax         /* from += write */
	sub	esi, ecx         /* from -= nbytes */
M
Mark Adler 已提交
1047 1048
	sub	ecx, eax         /* nbytes -= write */

M
Mark Adler 已提交
1049
	mov	eax, [esp+64]    /* eax = len */
M
Mark Adler 已提交
1050
	cmp	eax, ecx
M
Mark Adler 已提交
1051
	jbe	L_do_copy          /* if (nbytes >= len) */
M
Mark Adler 已提交
1052 1053 1054

	sub	eax, ecx         /* len -= nbytes */
	rep     movsb
M
Mark Adler 已提交
1055 1056
	mov	esi, [esp+28]     /* from = window */
	mov	ecx, [esp+52]     /* nbytes = write */
M
Mark Adler 已提交
1057
	cmp	eax, ecx
M
Mark Adler 已提交
1058
	jbe	L_do_copy          /* if (nbytes >= len) */
M
Mark Adler 已提交
1059 1060 1061 1062 1063

	sub	eax, ecx         /* len -= nbytes */
	rep     movsb
	mov	esi, edi
	sub	esi, ebp         /* from = out - dist */
M
Mark Adler 已提交
1064
	jmp	L_do_copy
M
Mark Adler 已提交
1065

M
Mark Adler 已提交
1066
ALIGN 4
M
Mark Adler 已提交
1067 1068 1069 1070
L_contiguous_in_window:
	add	esi, eax
	sub	esi, ecx         /* from += write - nbytes */

M
Mark Adler 已提交
1071
	mov	eax, [esp+64]    /* eax = len */
M
Mark Adler 已提交
1072
	cmp	eax, ecx
M
Mark Adler 已提交
1073
	jbe	L_do_copy          /* if (nbytes >= len) */
M
Mark Adler 已提交
1074 1075 1076 1077 1078

	sub	eax, ecx         /* len -= nbytes */
	rep     movsb
	mov	esi, edi
	sub	esi, ebp         /* from = out - dist */
M
Mark Adler 已提交
1079
	jmp	L_do_copy
M
Mark Adler 已提交
1080

M
Mark Adler 已提交
1081 1082
ALIGN 4
L_do_copy:
M
Mark Adler 已提交
1083 1084 1085
	mov	ecx, eax
	rep     movsb

M
Mark Adler 已提交
1086 1087
	mov	esi, [esp+8]      /* move in back to %esi, toss from */
	mov	ebp, [esp+32]     /* ebp = lcode */
M
Mark Adler 已提交
1088 1089 1090 1091 1092
	jmp	L_while_test

L_test_for_end_of_block:
	test	al, 32
	jz	L_invalid_literal_length_code
M
Mark Adler 已提交
1093
	mov	dword ptr [esp+72], 1
M
Mark Adler 已提交
1094 1095 1096
	jmp	L_break_loop_with_status

L_invalid_literal_length_code:
M
Mark Adler 已提交
1097
	mov	dword ptr [esp+72], 2
M
Mark Adler 已提交
1098 1099 1100
	jmp	L_break_loop_with_status

L_invalid_distance_code:
M
Mark Adler 已提交
1101
	mov	dword ptr [esp+72], 3
M
Mark Adler 已提交
1102 1103 1104 1105
	jmp	L_break_loop_with_status

L_invalid_distance_too_far:
	mov	esi, [esp+4]
M
Mark Adler 已提交
1106
	mov	dword ptr [esp+72], 4
M
Mark Adler 已提交
1107 1108 1109
	jmp	L_break_loop_with_status

L_break_loop:
M
Mark Adler 已提交
1110
	mov	dword ptr [esp+72], 0
M
Mark Adler 已提交
1111 1112 1113

L_break_loop_with_status:
/* put in, out, bits, and hold back into ar and pop esp */
M
Mark Adler 已提交
1114 1115 1116 1117 1118
	mov	[esp+8], esi     /* save in */
	mov	[esp+16], edi    /* save out */
	mov	[esp+44], ebx    /* save bits */
	mov	[esp+40], edx    /* save hold */
	mov	ebp, [esp+4]     /* restore esp, ebp */
M
Mark Adler 已提交
1119 1120
	mov	esp, [esp]
    }
M
Mark Adler 已提交
1121 1122
#else
#error "x86 architecture not defined"
M
Mark Adler 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
#endif

    if (ar.status > 1) {
        if (ar.status == 2)
            strm->msg = "invalid literal/length code";
        else if (ar.status == 3)
            strm->msg = "invalid distance code";
        else
            strm->msg = "invalid distance too far back";
        state->mode = BAD;
    }
    else if ( ar.status == 1 ) {
        state->mode = TYPE;
    }

    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
    ar.len = ar.bits >> 3;
    ar.in -= ar.len;
    ar.bits -= ar.len << 3;
    ar.hold &= (1U << ar.bits) - 1;

    /* update state and return */
    strm->next_in = ar.in;
    strm->next_out = ar.out;
M
Mark Adler 已提交
1147 1148 1149 1150 1151 1152
    strm->avail_in = (unsigned)(ar.in < ar.last ?
                                PAD_AVAIL_IN + (ar.last - ar.in) :
                                PAD_AVAIL_IN - (ar.in - ar.last));
    strm->avail_out = (unsigned)(ar.out < ar.end ?
                                 PAD_AVAIL_OUT + (ar.end - ar.out) :
                                 PAD_AVAIL_OUT - (ar.out - ar.end));
M
Mark Adler 已提交
1153 1154 1155 1156 1157
    state->hold = ar.hold;
    state->bits = ar.bits;
    return;
}