1 /* match.s -- Pentium-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 _match_init
11 #define longest_match _longest_match
14 #define MAX_MATCH (258)
16 #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
17 #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
19 /* stack frame offsets */
21 #define wmask 0 /* local copy of s->wmask */
22 #define window 4 /* local copy of s->window */
23 #define windowbestlen 8 /* s->window + bestlen */
24 #define chainlenscanend 12 /* high word: current chain len */
25 /* low word: last bytes sought */
26 #define scanstart 16 /* first two bytes of string */
27 #define scanalign 20 /* dword-misalignment of string */
28 #define nicematch 24 /* a good enough match size */
29 #define bestlen 28 /* size of best match so far */
30 #define scan 32 /* ptr to string wanting match */
32 #define LocalVarsSize (36)
37 /* return address 52 */
38 #define deflatestate 56 /* the function arguments */
41 /* Offsets for fields in the deflate_state structure. These numbers
42 * are calculated from the definition of deflate_state, with the
43 * assumption that the compiler will dword-align the fields. (Thus,
44 * changing the definition of deflate_state could easily cause this
45 * program to crash horribly, without so much as a warning at
46 * compile time. Sigh.)
49 /* All the +zlib1222add offsets are due to the addition of fields
50 * in zlib in the deflate_state structure since the asm code was first written
51 * (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").
52 * (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").
53 * if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").
56 #define zlib1222add (8)
58 #define dsWSize (36+zlib1222add)
59 #define dsWMask (44+zlib1222add)
60 #define dsWindow (48+zlib1222add)
61 #define dsPrev (56+zlib1222add)
62 #define dsMatchLen (88+zlib1222add)
63 #define dsPrevMatch (92+zlib1222add)
64 #define dsStrStart (100+zlib1222add)
65 #define dsMatchStart (104+zlib1222add)
66 #define dsLookahead (108+zlib1222add)
67 #define dsPrevLen (112+zlib1222add)
68 #define dsMaxChainLen (116+zlib1222add)
69 #define dsGoodMatch (132+zlib1222add)
70 #define dsNiceMatch (136+zlib1222add)
75 .globl match_init, longest_match
79 /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
83 /* Save registers that the compiler may be using, and adjust %esp to */
84 /* make room for our stack frame. */
90 subl $LocalVarsSize, %esp
92 /* Retrieve the function arguments. %ecx will hold cur_match */
93 /* throughout the entire function. %edx will hold the pointer to the */
94 /* deflate_state structure during the function's setup (before */
95 /* entering the main loop). */
97 movl deflatestate(%esp), %edx
98 movl curmatch(%esp), %ecx
100 /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
102 movl dsNiceMatch(%edx), %eax
103 movl dsLookahead(%edx), %ebx
107 LookaheadLess: movl %ebx, nicematch(%esp)
109 /* register Bytef *scan = s->window + s->strstart; */
111 movl dsWindow(%edx), %esi
112 movl %esi, window(%esp)
113 movl dsStrStart(%edx), %ebp
114 lea (%esi,%ebp), %edi
115 movl %edi, scan(%esp)
117 /* Determine how many bytes the scan ptr is off from being */
123 movl %eax, scanalign(%esp)
125 /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
126 /* s->strstart - (IPos)MAX_DIST(s) : NIL; */
128 movl dsWSize(%edx), %eax
129 subl $MIN_LOOKAHEAD, %eax
135 /* unsigned chain_length = s->max_chain_length; */
136 /* if (s->prev_length >= s->good_match) { */
137 /* chain_length >>= 2; */
140 movl dsPrevLen(%edx), %eax
141 movl dsGoodMatch(%edx), %ebx
143 movl dsMaxChainLen(%edx), %ebx
148 /* chainlen is decremented once beforehand so that the function can */
149 /* use the sign flag instead of the zero flag for the exit test. */
150 /* It is then shifted into the high word, to make room for the scanend */
151 /* scanend value, which it will always accompany. */
156 /* int best_len = s->prev_length; */
158 movl dsPrevLen(%edx), %eax
159 movl %eax, bestlen(%esp)
161 /* Store the sum of s->window + best_len in %esi locally, and in %esi. */
164 movl %esi, windowbestlen(%esp)
166 /* register ush scan_start = *(ushf*)scan; */
167 /* register ush scan_end = *(ushf*)(scan+best_len-1); */
170 movw %bx, scanstart(%esp)
171 movw -1(%edi,%eax), %bx
172 movl %ebx, chainlenscanend(%esp)
174 /* Posf *prev = s->prev; */
175 /* uInt wmask = s->w_mask; */
177 movl dsPrev(%edx), %edi
178 movl dsWMask(%edx), %edx
179 mov %edx, wmask(%esp)
181 /* Jump into the main loop. */
188 * match = s->window + cur_match;
189 * if (*(ushf*)(match+best_len-1) != scan_end ||
190 * *(ushf*)match != scan_start) continue;
192 * } while ((cur_match = prev[cur_match & wmask]) > limit
193 * && --chain_length != 0);
195 * Here is the inner loop of the function. The function will spend the
196 * majority of its time in this loop, and majority of that time will
197 * be spent in the first ten instructions.
200 * %ebx = chainlenscanend - i.e., ((chainlen << 16) | scanend)
202 * %edx = curmatch & wmask
203 * %esi = windowbestlen - i.e., (window + bestlen)
207 * Two optimization notes on the choice of instructions:
209 * The first instruction uses a 16-bit address, which costs an extra,
210 * unpairable cycle. This is cheaper than doing a 32-bit access and
211 * zeroing the high word, due to the 3-cycle misalignment penalty which
212 * would occur half the time. This also turns out to be cheaper than
213 * doing two separate 8-bit accesses, as the memory is so rarely in the
216 * The window buffer, however, apparently spends a lot of time in the
217 * cache, and so it is faster to retrieve the word at the end of the
218 * match string with two 8-bit loads. The instructions that test the
219 * word at the beginning of the match string, however, are executed
220 * much less frequently, and there it was cheaper to use 16-bit
221 * instructions, which avoided the necessity of saving off and
222 * subsequently reloading one of the other registers.
226 movw (%edi,%edx,2), %cx /* 2 U pipe */
227 movl wmask(%esp), %edx /* 2 V pipe */
228 cmpl %ebp, %ecx /* 3 U pipe */
229 jbe LeaveNow /* 3 V pipe */
230 subl $0x00010000, %ebx /* 4 U pipe */
231 js LeaveNow /* 4 V pipe */
232 LoopEntry: movb -1(%esi,%ecx), %al /* 5 U pipe */
233 andl %ecx, %edx /* 5 V pipe */
234 cmpb %bl, %al /* 6 U pipe */
235 jnz LookupLoop /* 6 V pipe */
236 movb (%esi,%ecx), %ah
239 movl window(%esp), %eax
240 movw (%eax,%ecx), %ax
241 cmpw scanstart(%esp), %ax
244 /* Store the current value of chainlen. */
246 movl %ebx, chainlenscanend(%esp)
248 /* Point %edi to the string under scrutiny, and %esi to the string we */
249 /* are hoping to match it up with. In actuality, %esi and %edi are */
250 /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
251 /* initialized to -(MAX_MATCH_8 - scanalign). */
253 movl window(%esp), %esi
254 movl scan(%esp), %edi
256 movl scanalign(%esp), %eax
257 movl $(-MAX_MATCH_8), %edx
258 lea MAX_MATCH_8(%edi,%eax), %edi
259 lea MAX_MATCH_8(%esi,%eax), %esi
261 /* Test the strings for equality, 8 bytes at a time. At the end,
262 * adjust %edx so that it is offset to the exact byte that mismatched.
264 * We already know at this point that the first three bytes of the
265 * strings match each other, and they can be safely passed over before
266 * starting the compare loop. So what this code does is skip over 0-3
267 * bytes, as much as necessary in order to dword-align the %edi
268 * pointer. (%esi will still be misaligned three times out of four.)
270 * It should be confessed that this loop usually does not represent
271 * much of the total running time. Replacing it with a more
272 * straightforward "rep cmpsb" would not drastically degrade
276 movl (%esi,%edx), %eax
277 movl (%edi,%edx), %ebx
280 movl 4(%esi,%edx), %eax
281 movl 4(%edi,%edx), %ebx
287 LeaveLoopCmps4: addl $4, %edx
288 LeaveLoopCmps: testl $0x0000FFFF, %eax
292 LenLower: subb $1, %al
295 /* Calculate the length of the match. If it is longer than MAX_MATCH, */
296 /* then automatically accept it as the best possible match and leave. */
298 lea (%edi,%edx), %eax
299 movl scan(%esp), %edi
301 cmpl $MAX_MATCH, %eax
304 /* If the length of the match is not longer than the best match we */
305 /* have so far, then forget it and return to the lookup loop. */
307 movl deflatestate(%esp), %edx
308 movl bestlen(%esp), %ebx
311 movl chainlenscanend(%esp), %ebx
312 movl windowbestlen(%esp), %esi
313 movl dsPrev(%edx), %edi
314 movl wmask(%esp), %edx
318 /* s->match_start = cur_match; */
319 /* best_len = len; */
320 /* if (len >= nice_match) break; */
321 /* scan_end = *(ushf*)(scan+best_len-1); */
323 LongerMatch: movl nicematch(%esp), %ebx
324 movl %eax, bestlen(%esp)
325 movl %ecx, dsMatchStart(%edx)
328 movl window(%esp), %esi
330 movl %esi, windowbestlen(%esp)
331 movl chainlenscanend(%esp), %ebx
332 movw -1(%edi,%eax), %bx
333 movl dsPrev(%edx), %edi
334 movl %ebx, chainlenscanend(%esp)
335 movl wmask(%esp), %edx
339 /* Accept the current string, with the maximum possible length. */
341 LenMaximum: movl deflatestate(%esp), %edx
342 movl $MAX_MATCH, bestlen(%esp)
343 movl %ecx, dsMatchStart(%edx)
345 /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
346 /* return s->lookahead; */
349 movl deflatestate(%esp), %edx
350 movl bestlen(%esp), %ebx
351 movl dsLookahead(%edx), %eax
357 /* Restore the stack and return from whence we came. */
359 addl $LocalVarsSize, %esp