inffas8664.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* inffas8664.c is a hand tuned assembler version of inffast.c - fast decoding
  2.  * version for AMD64 on Windows using Microsoft C compiler
  3.  *
  4.  * Copyright (C) 1995-2003 Mark Adler
  5.  * For conditions of distribution and use, see copyright notice in zlib.h
  6.  *
  7.  * Copyright (C) 2003 Chris Anderson <christop@charm.net>
  8.  * Please use the copyright conditions above.
  9.  *
  10.  * 2005 - Adaptation to Microsoft C Compiler for AMD64 by Gilles Vollant
  11.  *
  12.  * inffas8664.c call function inffas8664fnc in inffasx64.asm
  13.  *  inffasx64.asm is automatically convert from AMD64 portion of inffas86.c
  14.  *
  15.  * Dec-29-2003 -- I added AMD64 inflate asm support.  This version is also
  16.  * slightly quicker on x86 systems because, instead of using rep movsb to copy
  17.  * data, it uses rep movsw, which moves data in 2-byte chunks instead of single
  18.  * bytes.  I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates
  19.  * from http://fedora.linux.duke.edu/fc1_x86_64
  20.  * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with
  21.  * 1GB ram.  The 64-bit version is about 4% faster than the 32-bit version,
  22.  * when decompressing mozilla-source-1.3.tar.gz.
  23.  *
  24.  * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from
  25.  * the gcc -S output of zlib-1.2.0/inffast.c.  Zlib-1.2.0 is in beta release at
  26.  * the moment.  I have successfully compiled and tested this code with gcc2.96,
  27.  * gcc3.2, icc5.0, msvc6.0.  It is very close to the speed of inffast.S
  28.  * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX
  29.  * enabled.  I will attempt to merge the MMX code into this version.  Newer
  30.  * versions of this and inffast.S can be found at
  31.  * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/
  32.  *
  33.  */
  34. #include <stdio.h>
  35. #include "zutil.h"
  36. #include "inftrees.h"
  37. #include "inflate.h"
  38. #include "inffast.h"
  39. /* Mark Adler's comments from inffast.c: */
  40. /*
  41.    Decode literal, length, and distance codes and write out the resulting
  42.    literal and match bytes until either not enough input or output is
  43.    available, an end-of-block is encountered, or a data error is encountered.
  44.    When large enough input and output buffers are supplied to inflate(), for
  45.    example, a 16K input buffer and a 64K output buffer, more than 95% of the
  46.    inflate execution time is spent in this routine.
  47.    Entry assumptions:
  48.         state->mode == LEN
  49.         strm->avail_in >= 6
  50.         strm->avail_out >= 258
  51.         start >= strm->avail_out
  52.         state->bits < 8
  53.    On return, state->mode is one of:
  54.         LEN -- ran out of enough output space or enough available input
  55.         TYPE -- reached end of block code, inflate() to interpret next block
  56.         BAD -- error in block data
  57.    Notes:
  58.     - The maximum input bits used by a length/distance pair is 15 bits for the
  59.       length code, 5 bits for the length extra, 15 bits for the distance code,
  60.       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
  61.       Therefore if strm->avail_in >= 6, then there is enough input to avoid
  62.       checking for available input while decoding.
  63.     - The maximum bytes that a single length/distance pair can output is 258
  64.       bytes, which is the maximum length that can be coded.  inflate_fast()
  65.       requires strm->avail_out >= 258 for each loop to avoid checking for
  66.       output space.
  67.  */
  68.     typedef struct inffast_ar {
  69. /* 64   32                               x86  x86_64 */
  70. /* ar offset                              register */
  71. /*  0    0 */ void *esp;                /* esp save */
  72. /*  8    4 */ void *ebp;                /* ebp save */
  73. /* 16    8 */ unsigned char FAR *in;    /* esi rsi  local strm->next_in */
  74. /* 24   12 */ unsigned char FAR *last;  /*     r9   while in < last */
  75. /* 32   16 */ unsigned char FAR *out;   /* edi rdi  local strm->next_out */
  76. /* 40   20 */ unsigned char FAR *beg;   /*          inflate()'s init next_out */
  77. /* 48   24 */ unsigned char FAR *end;   /*     r10  while out < end */
  78. /* 56   28 */ unsigned char FAR *window;/*          size of window, wsize!=0 */
  79. /* 64   32 */ code const FAR *lcode;    /* ebp rbp  local strm->lencode */
  80. /* 72   36 */ code const FAR *dcode;    /*     r11  local strm->distcode */
  81. /* 80   40 */ size_t /*unsigned long */hold;       /* edx rdx  local strm->hold */
  82. /* 88   44 */ unsigned bits;            /* ebx rbx  local strm->bits */
  83. /* 92   48 */ unsigned wsize;           /*          window size */
  84. /* 96   52 */ unsigned write;           /*          window write index */
  85. /*100   56 */ unsigned lmask;           /*     r12  mask for lcode */
  86. /*104   60 */ unsigned dmask;           /*     r13  mask for dcode */
  87. /*108   64 */ unsigned len;             /*     r14  match length */
  88. /*112   68 */ unsigned dist;            /*     r15  match distance */
  89. /*116   72 */ unsigned status;          /*          set when state chng*/
  90.     } type_ar;
  91. #ifdef ASMINF
  92. void inflate_fast(strm, start)
  93. z_streamp strm;
  94. unsigned start;         /* inflate()'s starting value for strm->avail_out */
  95. {
  96.     struct inflate_state FAR *state;
  97.     type_ar ar;
  98.     void inffas8664fnc(struct inffast_ar * par);
  99.     
  100. #if (defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )) || (defined(_MSC_VER) && defined(_M_AMD64))
  101. #define PAD_AVAIL_IN 6
  102. #define PAD_AVAIL_OUT 258    
  103. #else
  104. #define PAD_AVAIL_IN 5
  105. #define PAD_AVAIL_OUT 257
  106. #endif
  107.     /* copy state to local variables */
  108.     state = (struct inflate_state FAR *)strm->state;
  109.     ar.in = strm->next_in;
  110.     ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN);
  111.     ar.out = strm->next_out;
  112.     ar.beg = ar.out - (start - strm->avail_out);
  113.     ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT);
  114.     ar.wsize = state->wsize;
  115.     ar.write = state->write;
  116.     ar.window = state->window;
  117.     ar.hold = state->hold;
  118.     ar.bits = state->bits;
  119.     ar.lcode = state->lencode;
  120.     ar.dcode = state->distcode;
  121.     ar.lmask = (1U << state->lenbits) - 1;
  122.     ar.dmask = (1U << state->distbits) - 1;
  123.     /* decode literals and length/distances until end-of-block or not enough
  124.        input data or output space */
  125.     /* align in on 1/2 hold size boundary */
  126.     while (((size_t)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) {
  127.         ar.hold += (unsigned long)*ar.in++ << ar.bits;
  128.         ar.bits += 8;
  129.     }
  130.     inffas8664fnc(&ar);
  131.     if (ar.status > 1) {
  132.         if (ar.status == 2)
  133.             strm->msg = "invalid literal/length code";
  134.         else if (ar.status == 3)
  135.             strm->msg = "invalid distance code";
  136.         else
  137.             strm->msg = "invalid distance too far back";
  138.         state->mode = BAD;
  139.     }
  140.     else if ( ar.status == 1 ) {
  141.         state->mode = TYPE;
  142.     }
  143.     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  144.     ar.len = ar.bits >> 3;
  145.     ar.in -= ar.len;
  146.     ar.bits -= ar.len << 3;
  147.     ar.hold &= (1U << ar.bits) - 1;
  148.     /* update state and return */
  149.     strm->next_in = ar.in;
  150.     strm->next_out = ar.out;
  151.     strm->avail_in = (unsigned)(ar.in < ar.last ?
  152.                                 PAD_AVAIL_IN + (ar.last - ar.in) :
  153.                                 PAD_AVAIL_IN - (ar.in - ar.last));
  154.     strm->avail_out = (unsigned)(ar.out < ar.end ?
  155.                                  PAD_AVAIL_OUT + (ar.end - ar.out) :
  156.                                  PAD_AVAIL_OUT - (ar.out - ar.end));
  157.     state->hold = (unsigned long)ar.hold;
  158.     state->bits = ar.bits;
  159.     return;
  160. }
  161. #endif