OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / zlib / match686.S
1 /* match.s -- Pentium-Pro-optimized version of longest_match()
2  * Written for zlib 1.1.2
3  * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
4  *
5  * This is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License.
7  */
8
9 #ifndef NO_UNDERLINE
10 #define match_init      _ipcomp_match_init
11 #define longest_match   _ipcomp_longest_match
12 #else
13 #define match_init      ipcomp_match_init
14 #define longest_match   ipcomp_longest_match
15 #endif
16
17 #define MAX_MATCH       (258)
18 #define MIN_MATCH       (3)
19 #define MIN_LOOKAHEAD   (MAX_MATCH + MIN_MATCH + 1)
20 #define MAX_MATCH_8     ((MAX_MATCH + 7) & ~7)
21
22 /* stack frame offsets */
23
24 #define chainlenwmask           0       /* high word: current chain len */
25                                         /* low word: s->wmask           */
26 #define window                  4       /* local copy of s->window      */
27 #define windowbestlen           8       /* s->window + bestlen          */
28 #define scanstart               16      /* first two bytes of string    */
29 #define scanend                 12      /* last two bytes of string     */
30 #define scanalign               20      /* dword-misalignment of string */
31 #define nicematch               24      /* a good enough match size     */
32 #define bestlen                 28      /* size of best match so far    */
33 #define scan                    32      /* ptr to string wanting match  */
34
35 #define LocalVarsSize           (36)
36 /*      saved ebx               36 */
37 /*      saved edi               40 */
38 /*      saved esi               44 */
39 /*      saved ebp               48 */
40 /*      return address          52 */
41 #define deflatestate            56      /* the function arguments       */
42 #define curmatch                60
43
44 /* Offsets for fields in the deflate_state structure. These numbers
45  * are calculated from the definition of deflate_state, with the
46  * assumption that the compiler will dword-align the fields. (Thus,
47  * changing the definition of deflate_state could easily cause this
48  * program to crash horribly, without so much as a warning at
49  * compile time. Sigh.)
50  */
51 #define dsWSize                 36
52 #define dsWMask                 44
53 #define dsWindow                48
54 #define dsPrev                  56
55 #define dsMatchLen              88
56 #define dsPrevMatch             92
57 #define dsStrStart              100
58 #define dsMatchStart            104
59 #define dsLookahead             108
60 #define dsPrevLen               112
61 #define dsMaxChainLen           116
62 #define dsGoodMatch             132
63 #define dsNiceMatch             136
64
65
66 .file "match.S"
67
68 .globl  match_init, longest_match
69
70 .text
71
72 /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
73
74 longest_match:
75
76 /* Save registers that the compiler may be using, and adjust %esp to    */
77 /* make room for our stack frame.                                       */
78
79                 pushl   %ebp
80                 pushl   %edi
81                 pushl   %esi
82                 pushl   %ebx
83                 subl    $LocalVarsSize, %esp
84
85 /* Retrieve the function arguments. %ecx will hold cur_match            */
86 /* throughout the entire function. %edx will hold the pointer to the    */
87 /* deflate_state structure during the function's setup (before          */
88 /* entering the main loop).                                             */
89
90                 movl    deflatestate(%esp), %edx
91                 movl    curmatch(%esp), %ecx
92
93 /* uInt wmask = s->w_mask;                                              */
94 /* unsigned chain_length = s->max_chain_length;                         */
95 /* if (s->prev_length >= s->good_match) {                               */
96 /*     chain_length >>= 2;                                              */
97 /* }                                                                    */
98
99                 movl    dsPrevLen(%edx), %eax
100                 movl    dsGoodMatch(%edx), %ebx
101                 cmpl    %ebx, %eax
102                 movl    dsWMask(%edx), %eax
103                 movl    dsMaxChainLen(%edx), %ebx
104                 jl      LastMatchGood
105                 shrl    $2, %ebx
106 LastMatchGood:
107
108 /* chainlen is decremented once beforehand so that the function can     */
109 /* use the sign flag instead of the zero flag for the exit test.        */
110 /* It is then shifted into the high word, to make room for the wmask    */
111 /* value, which it will always accompany.                               */
112
113                 decl    %ebx
114                 shll    $16, %ebx
115                 orl     %eax, %ebx
116                 movl    %ebx, chainlenwmask(%esp)
117
118 /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;      */
119
120                 movl    dsNiceMatch(%edx), %eax
121                 movl    dsLookahead(%edx), %ebx
122                 cmpl    %eax, %ebx
123                 jl      LookaheadLess
124                 movl    %eax, %ebx
125 LookaheadLess:  movl    %ebx, nicematch(%esp)
126
127 /* register Bytef *scan = s->window + s->strstart;                      */
128
129                 movl    dsWindow(%edx), %esi
130                 movl    %esi, window(%esp)
131                 movl    dsStrStart(%edx), %ebp
132                 lea     (%esi,%ebp), %edi
133                 movl    %edi, scan(%esp)
134
135 /* Determine how many bytes the scan ptr is off from being              */
136 /* dword-aligned.                                                       */
137
138                 movl    %edi, %eax
139                 negl    %eax
140                 andl    $3, %eax
141                 movl    %eax, scanalign(%esp)
142
143 /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ?                       */
144 /*     s->strstart - (IPos)MAX_DIST(s) : NIL;                           */
145
146                 movl    dsWSize(%edx), %eax
147                 subl    $MIN_LOOKAHEAD, %eax
148                 subl    %eax, %ebp
149                 jg      LimitPositive
150                 xorl    %ebp, %ebp
151 LimitPositive:
152
153 /* int best_len = s->prev_length;                                       */
154
155                 movl    dsPrevLen(%edx), %eax
156                 movl    %eax, bestlen(%esp)
157
158 /* Store the sum of s->window + best_len in %esi locally, and in %esi.  */
159
160                 addl    %eax, %esi
161                 movl    %esi, windowbestlen(%esp)
162
163 /* register ush scan_start = *(ushf*)scan;                              */
164 /* register ush scan_end   = *(ushf*)(scan+best_len-1);                 */
165 /* Posf *prev = s->prev;                                                */
166
167                 movzwl  (%edi), %ebx
168                 movl    %ebx, scanstart(%esp)
169                 movzwl  -1(%edi,%eax), %ebx
170                 movl    %ebx, scanend(%esp)
171                 movl    dsPrev(%edx), %edi
172
173 /* Jump into the main loop.                                             */
174
175                 movl    chainlenwmask(%esp), %edx
176                 jmp     LoopEntry
177
178 .balign 16
179
180 /* do {
181  *     match = s->window + cur_match;
182  *     if (*(ushf*)(match+best_len-1) != scan_end ||
183  *         *(ushf*)match != scan_start) continue;
184  *     [...]
185  * } while ((cur_match = prev[cur_match & wmask]) > limit
186  *          && --chain_length != 0);
187  *
188  * Here is the inner loop of the function. The function will spend the
189  * majority of its time in this loop, and majority of that time will
190  * be spent in the first ten instructions.
191  *
192  * Within this loop:
193  * %ebx = scanend
194  * %ecx = curmatch
195  * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
196  * %esi = windowbestlen - i.e., (window + bestlen)
197  * %edi = prev
198  * %ebp = limit
199  */
200 LookupLoop:
201                 andl    %edx, %ecx
202                 movzwl  (%edi,%ecx,2), %ecx
203                 cmpl    %ebp, %ecx
204                 jbe     LeaveNow
205                 subl    $0x00010000, %edx
206                 js      LeaveNow
207 LoopEntry:      movzwl  -1(%esi,%ecx), %eax
208                 cmpl    %ebx, %eax
209                 jnz     LookupLoop
210                 movl    window(%esp), %eax
211                 movzwl  (%eax,%ecx), %eax
212                 cmpl    scanstart(%esp), %eax
213                 jnz     LookupLoop
214
215 /* Store the current value of chainlen.                                 */
216
217                 movl    %edx, chainlenwmask(%esp)
218
219 /* Point %edi to the string under scrutiny, and %esi to the string we   */
220 /* are hoping to match it up with. In actuality, %esi and %edi are      */
221 /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is      */
222 /* initialized to -(MAX_MATCH_8 - scanalign).                           */
223
224                 movl    window(%esp), %esi
225                 movl    scan(%esp), %edi
226                 addl    %ecx, %esi
227                 movl    scanalign(%esp), %eax
228                 movl    $(-MAX_MATCH_8), %edx
229                 lea     MAX_MATCH_8(%edi,%eax), %edi
230                 lea     MAX_MATCH_8(%esi,%eax), %esi
231
232 /* Test the strings for equality, 8 bytes at a time. At the end,
233  * adjust %edx so that it is offset to the exact byte that mismatched.
234  *
235  * We already know at this point that the first three bytes of the
236  * strings match each other, and they can be safely passed over before
237  * starting the compare loop. So what this code does is skip over 0-3
238  * bytes, as much as necessary in order to dword-align the %edi
239  * pointer. (%esi will still be misaligned three times out of four.)
240  *
241  * It should be confessed that this loop usually does not represent
242  * much of the total running time. Replacing it with a more
243  * straightforward "rep cmpsb" would not drastically degrade
244  * performance.
245  */
246 LoopCmps:
247                 movl    (%esi,%edx), %eax
248                 xorl    (%edi,%edx), %eax
249                 jnz     LeaveLoopCmps
250                 movl    4(%esi,%edx), %eax
251                 xorl    4(%edi,%edx), %eax
252                 jnz     LeaveLoopCmps4
253                 addl    $8, %edx
254                 jnz     LoopCmps
255                 jmp     LenMaximum
256 LeaveLoopCmps4: addl    $4, %edx
257 LeaveLoopCmps:  testl   $0x0000FFFF, %eax
258                 jnz     LenLower
259                 addl    $2, %edx
260                 shrl    $16, %eax
261 LenLower:       subb    $1, %al
262                 adcl    $0, %edx
263
264 /* Calculate the length of the match. If it is longer than MAX_MATCH,   */
265 /* then automatically accept it as the best possible match and leave.   */
266
267                 lea     (%edi,%edx), %eax
268                 movl    scan(%esp), %edi
269                 subl    %edi, %eax
270                 cmpl    $MAX_MATCH, %eax
271                 jge     LenMaximum
272
273 /* If the length of the match is not longer than the best match we      */
274 /* have so far, then forget it and return to the lookup loop.           */
275
276                 movl    deflatestate(%esp), %edx
277                 movl    bestlen(%esp), %ebx
278                 cmpl    %ebx, %eax
279                 jg      LongerMatch
280                 movl    windowbestlen(%esp), %esi
281                 movl    dsPrev(%edx), %edi
282                 movl    scanend(%esp), %ebx
283                 movl    chainlenwmask(%esp), %edx
284                 jmp     LookupLoop
285
286 /*         s->match_start = cur_match;                                  */
287 /*         best_len = len;                                              */
288 /*         if (len >= nice_match) break;                                */
289 /*         scan_end = *(ushf*)(scan+best_len-1);                        */
290
291 LongerMatch:    movl    nicematch(%esp), %ebx
292                 movl    %eax, bestlen(%esp)
293                 movl    %ecx, dsMatchStart(%edx)
294                 cmpl    %ebx, %eax
295                 jge     LeaveNow
296                 movl    window(%esp), %esi
297                 addl    %eax, %esi
298                 movl    %esi, windowbestlen(%esp)
299                 movzwl  -1(%edi,%eax), %ebx
300                 movl    dsPrev(%edx), %edi
301                 movl    %ebx, scanend(%esp)
302                 movl    chainlenwmask(%esp), %edx
303                 jmp     LookupLoop
304
305 /* Accept the current string, with the maximum possible length.         */
306
307 LenMaximum:     movl    deflatestate(%esp), %edx
308                 movl    $MAX_MATCH, bestlen(%esp)
309                 movl    %ecx, dsMatchStart(%edx)
310
311 /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len;           */
312 /* return s->lookahead;                                                 */
313
314 LeaveNow:
315                 movl    deflatestate(%esp), %edx
316                 movl    bestlen(%esp), %ebx
317                 movl    dsLookahead(%edx), %eax
318                 cmpl    %eax, %ebx
319                 jg      LookaheadRet
320                 movl    %ebx, %eax
321 LookaheadRet:
322
323 /* Restore the stack and return from whence we came.                    */
324
325                 addl    $LocalVarsSize, %esp
326                 popl    %ebx
327                 popl    %esi
328                 popl    %edi
329                 popl    %ebp
330 match_init:     ret