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>
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License.
10 #define match_init _ipcomp_match_init
11 #define longest_match _ipcomp_longest_match
13 #define match_init ipcomp_match_init
14 #define longest_match ipcomp_longest_match
17 #define MAX_MATCH (258)
19 #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
20 #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
22 /* stack frame offsets */
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 */
35 #define LocalVarsSize (36)
40 /* return address 52 */
41 #define deflatestate 56 /* the function arguments */
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.)
56 #define dsPrevMatch 92
57 #define dsStrStart 100
58 #define dsMatchStart 104
59 #define dsLookahead 108
61 #define dsMaxChainLen 116
62 #define dsGoodMatch 132
63 #define dsNiceMatch 136
68 .globl match_init, longest_match
72 /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
76 /* Save registers that the compiler may be using, and adjust %esp to */
77 /* make room for our stack frame. */
83 subl $LocalVarsSize, %esp
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). */
90 movl deflatestate(%esp), %edx
91 movl curmatch(%esp), %ecx
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; */
99 movl dsPrevLen(%edx), %eax
100 movl dsGoodMatch(%edx), %ebx
102 movl dsWMask(%edx), %eax
103 movl dsMaxChainLen(%edx), %ebx
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. */
116 movl %ebx, chainlenwmask(%esp)
118 /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
120 movl dsNiceMatch(%edx), %eax
121 movl dsLookahead(%edx), %ebx
125 LookaheadLess: movl %ebx, nicematch(%esp)
127 /* register Bytef *scan = s->window + s->strstart; */
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)
135 /* Determine how many bytes the scan ptr is off from being */
141 movl %eax, scanalign(%esp)
143 /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
144 /* s->strstart - (IPos)MAX_DIST(s) : NIL; */
146 movl dsWSize(%edx), %eax
147 subl $MIN_LOOKAHEAD, %eax
153 /* int best_len = s->prev_length; */
155 movl dsPrevLen(%edx), %eax
156 movl %eax, bestlen(%esp)
158 /* Store the sum of s->window + best_len in %esi locally, and in %esi. */
161 movl %esi, windowbestlen(%esp)
163 /* register ush scan_start = *(ushf*)scan; */
164 /* register ush scan_end = *(ushf*)(scan+best_len-1); */
165 /* Posf *prev = s->prev; */
168 movl %ebx, scanstart(%esp)
169 movzwl -1(%edi,%eax), %ebx
170 movl %ebx, scanend(%esp)
171 movl dsPrev(%edx), %edi
173 /* Jump into the main loop. */
175 movl chainlenwmask(%esp), %edx
181 * match = s->window + cur_match;
182 * if (*(ushf*)(match+best_len-1) != scan_end ||
183 * *(ushf*)match != scan_start) continue;
185 * } while ((cur_match = prev[cur_match & wmask]) > limit
186 * && --chain_length != 0);
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.
195 * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
196 * %esi = windowbestlen - i.e., (window + bestlen)
202 movzwl (%edi,%ecx,2), %ecx
205 subl $0x00010000, %edx
207 LoopEntry: movzwl -1(%esi,%ecx), %eax
210 movl window(%esp), %eax
211 movzwl (%eax,%ecx), %eax
212 cmpl scanstart(%esp), %eax
215 /* Store the current value of chainlen. */
217 movl %edx, chainlenwmask(%esp)
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). */
224 movl window(%esp), %esi
225 movl scan(%esp), %edi
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
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.
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.)
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
247 movl (%esi,%edx), %eax
248 xorl (%edi,%edx), %eax
250 movl 4(%esi,%edx), %eax
251 xorl 4(%edi,%edx), %eax
256 LeaveLoopCmps4: addl $4, %edx
257 LeaveLoopCmps: testl $0x0000FFFF, %eax
261 LenLower: subb $1, %al
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. */
267 lea (%edi,%edx), %eax
268 movl scan(%esp), %edi
270 cmpl $MAX_MATCH, %eax
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. */
276 movl deflatestate(%esp), %edx
277 movl bestlen(%esp), %ebx
280 movl windowbestlen(%esp), %esi
281 movl dsPrev(%edx), %edi
282 movl scanend(%esp), %ebx
283 movl chainlenwmask(%esp), %edx
286 /* s->match_start = cur_match; */
287 /* best_len = len; */
288 /* if (len >= nice_match) break; */
289 /* scan_end = *(ushf*)(scan+best_len-1); */
291 LongerMatch: movl nicematch(%esp), %ebx
292 movl %eax, bestlen(%esp)
293 movl %ecx, dsMatchStart(%edx)
296 movl window(%esp), %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
305 /* Accept the current string, with the maximum possible length. */
307 LenMaximum: movl deflatestate(%esp), %edx
308 movl $MAX_MATCH, bestlen(%esp)
309 movl %ecx, dsMatchStart(%edx)
311 /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
312 /* return s->lookahead; */
315 movl deflatestate(%esp), %edx
316 movl bestlen(%esp), %ebx
317 movl dsLookahead(%edx), %eax
323 /* Restore the stack and return from whence we came. */
325 addl $LocalVarsSize, %esp