OSDN Git Service

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