2 * match.S -- optimized version of longest_match()
3 * based on the similar work by Gilles Vollant, and Brian Raiter, written 1998
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the BSD License. Use by owners of Che Guevarra
7 * parafernalia is prohibited, where possible, and highly discouraged
12 # define match_init _match_init
13 # define longest_match _longest_match
18 #define chainlenwmask edx /* high word: current chain len low word: s->wmask */
21 #define windowbestlen r8
23 #define scanalignd r9d
27 #define scanstart r12d
28 #define scanstartw r12w
30 #define nicematch r14d
36 * The 258 is a "magic number, not a parameter -- changing it
37 * breaks the hell loose
39 #define MAX_MATCH (258)
41 #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
42 #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
44 /* stack frame offsets */
45 #define LocalVarsSize (112)
46 #define _chainlenwmask ( 8-LocalVarsSize)(%rsp)
47 #define _windowbestlen (16-LocalVarsSize)(%rsp)
48 #define save_r14 (24-LocalVarsSize)(%rsp)
49 #define save_rsi (32-LocalVarsSize)(%rsp)
50 #define save_rbx (40-LocalVarsSize)(%rsp)
51 #define save_r12 (56-LocalVarsSize)(%rsp)
52 #define save_r13 (64-LocalVarsSize)(%rsp)
53 #define save_r15 (80-LocalVarsSize)(%rsp)
56 .globl match_init, longest_match
59 * On AMD64 the first argument of a function (in our case -- the pointer to
60 * deflate_state structure) is passed in %rdi, hence our offsets below are
64 /* you can check the structure offset by running
74 printf("size pointer=%u\n",(int)sizeof(void*));
76 printf("#define dsWSize (%3u)(%%rdi)\n",(int)(((char*)&(s->w_size))-((char*)s)));
77 printf("#define dsWMask (%3u)(%%rdi)\n",(int)(((char*)&(s->w_mask))-((char*)s)));
78 printf("#define dsWindow (%3u)(%%rdi)\n",(int)(((char*)&(s->window))-((char*)s)));
79 printf("#define dsPrev (%3u)(%%rdi)\n",(int)(((char*)&(s->prev))-((char*)s)));
80 printf("#define dsMatchLen (%3u)(%%rdi)\n",(int)(((char*)&(s->match_length))-((char*)s)));
81 printf("#define dsPrevMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->prev_match))-((char*)s)));
82 printf("#define dsStrStart (%3u)(%%rdi)\n",(int)(((char*)&(s->strstart))-((char*)s)));
83 printf("#define dsMatchStart (%3u)(%%rdi)\n",(int)(((char*)&(s->match_start))-((char*)s)));
84 printf("#define dsLookahead (%3u)(%%rdi)\n",(int)(((char*)&(s->lookahead))-((char*)s)));
85 printf("#define dsPrevLen (%3u)(%%rdi)\n",(int)(((char*)&(s->prev_length))-((char*)s)));
86 printf("#define dsMaxChainLen (%3u)(%%rdi)\n",(int)(((char*)&(s->max_chain_length))-((char*)s)));
87 printf("#define dsGoodMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->good_match))-((char*)s)));
88 printf("#define dsNiceMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->nice_match))-((char*)s)));
95 to compile for XCode 3.2 on MacOSX x86_64
96 - run "gcc -g -c -DXCODE_MAC_X64_STRUCTURE amd64-match.S"
100 #ifndef CURRENT_LINX_XCODE_MAC_X64_STRUCTURE
101 #define dsWSize ( 68)(%rdi)
102 #define dsWMask ( 76)(%rdi)
103 #define dsWindow ( 80)(%rdi)
104 #define dsPrev ( 96)(%rdi)
105 #define dsMatchLen (144)(%rdi)
106 #define dsPrevMatch (148)(%rdi)
107 #define dsStrStart (156)(%rdi)
108 #define dsMatchStart (160)(%rdi)
109 #define dsLookahead (164)(%rdi)
110 #define dsPrevLen (168)(%rdi)
111 #define dsMaxChainLen (172)(%rdi)
112 #define dsGoodMatch (188)(%rdi)
113 #define dsNiceMatch (192)(%rdi)
117 #ifndef STRUCT_OFFSET
118 # define STRUCT_OFFSET (0)
122 #define dsWSize ( 56 + STRUCT_OFFSET)(%rdi)
123 #define dsWMask ( 64 + STRUCT_OFFSET)(%rdi)
124 #define dsWindow ( 72 + STRUCT_OFFSET)(%rdi)
125 #define dsPrev ( 88 + STRUCT_OFFSET)(%rdi)
126 #define dsMatchLen (136 + STRUCT_OFFSET)(%rdi)
127 #define dsPrevMatch (140 + STRUCT_OFFSET)(%rdi)
128 #define dsStrStart (148 + STRUCT_OFFSET)(%rdi)
129 #define dsMatchStart (152 + STRUCT_OFFSET)(%rdi)
130 #define dsLookahead (156 + STRUCT_OFFSET)(%rdi)
131 #define dsPrevLen (160 + STRUCT_OFFSET)(%rdi)
132 #define dsMaxChainLen (164 + STRUCT_OFFSET)(%rdi)
133 #define dsGoodMatch (180 + STRUCT_OFFSET)(%rdi)
134 #define dsNiceMatch (184 + STRUCT_OFFSET)(%rdi)
143 /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
147 * Retrieve the function arguments. %curmatch will hold cur_match
148 * throughout the entire function (passed via rsi on amd64).
149 * rdi will hold the pointer to the deflate_state (first arg on amd64)
158 /* uInt wmask = s->w_mask; */
159 /* unsigned chain_length = s->max_chain_length; */
160 /* if (s->prev_length >= s->good_match) { */
161 /* chain_length >>= 2; */
165 movl dsGoodMatch, %ebx
168 movl dsMaxChainLen, %chainlenwmask
170 shrl $2, %chainlenwmask
173 /* chainlen is decremented once beforehand so that the function can */
174 /* use the sign flag instead of the zero flag for the exit test. */
175 /* It is then shifted into the high word, to make room for the wmask */
176 /* value, which it will always accompany. */
179 shll $16, %chainlenwmask
180 orl %eax, %chainlenwmask
182 /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
184 movl dsNiceMatch, %eax
185 movl dsLookahead, %ebx
189 LookaheadLess: movl %ebx, %nicematch
191 /* register Bytef *scan = s->window + s->strstart; */
193 mov dsWindow, %window
194 movl dsStrStart, %limitd
195 lea (%limit, %window), %scan
197 /* Determine how many bytes the scan ptr is off from being */
200 mov %scan, %scanalign
204 /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
205 /* s->strstart - (IPos)MAX_DIST(s) : NIL; */
208 subl $MIN_LOOKAHEAD, %eax
213 /* int best_len = s->prev_length; */
215 movl dsPrevLen, %bestlend
217 /* Store the sum of s->window + best_len in %windowbestlen locally, and in memory. */
219 lea (%window, %bestlen), %windowbestlen
220 mov %windowbestlen, _windowbestlen
222 /* register ush scan_start = *(ushf*)scan; */
223 /* register ush scan_end = *(ushf*)(scan+best_len-1); */
224 /* Posf *prev = s->prev; */
226 movzwl (%scan), %scanstart
227 movzwl -1(%scan, %bestlen), %scanend
230 /* Jump into the main loop. */
232 movl %chainlenwmask, _chainlenwmask
238 * match = s->window + cur_match;
239 * if (*(ushf*)(match+best_len-1) != scan_end ||
240 * *(ushf*)match != scan_start) continue;
242 * } while ((cur_match = prev[cur_match & wmask]) > limit
243 * && --chain_length != 0);
245 * Here is the inner loop of the function. The function will spend the
246 * majority of its time in this loop, and majority of that time will
247 * be spent in the first ten instructions.
250 andl %chainlenwmask, %curmatchd
251 movzwl (%prev, %curmatch, 2), %curmatchd
252 cmpl %limitd, %curmatchd
254 subl $0x00010000, %chainlenwmask
256 LoopEntry: cmpw -1(%windowbestlen, %curmatch), %scanendw
258 cmpw %scanstartw, (%window, %curmatch)
261 /* Store the current value of chainlen. */
262 movl %chainlenwmask, _chainlenwmask
264 /* %scan is the string under scrutiny, and %prev to the string we */
265 /* are hoping to match it up with. In actuality, %esi and %edi are */
266 /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
267 /* initialized to -(MAX_MATCH_8 - scanalign). */
269 mov $(-MAX_MATCH_8), %rdx
270 lea (%curmatch, %window), %windowbestlen
271 lea MAX_MATCH_8(%windowbestlen, %scanalign), %windowbestlen
272 lea MAX_MATCH_8(%scan, %scanalign), %prev
274 /* the prefetching below makes very little difference... */
275 prefetcht1 (%windowbestlen, %rdx)
276 prefetcht1 (%prev, %rdx)
279 * Test the strings for equality, 8 bytes at a time. At the end,
280 * adjust %rdx so that it is offset to the exact byte that mismatched.
282 * It should be confessed that this loop usually does not represent
283 * much of the total running time. Replacing it with a more
284 * straightforward "rep cmpsb" would not drastically degrade
285 * performance -- unrolling it, for example, makes no difference.
288 #undef USE_SSE /* works, but is 6-7% slower, than non-SSE... */
292 /* Preload the SSE registers */
293 movdqu (%windowbestlen, %rdx), %xmm1
294 movdqu (%prev, %rdx), %xmm2
296 movdqu 16(%windowbestlen, %rdx), %xmm3
297 movdqu 16(%prev, %rdx), %xmm4
299 movdqu 32(%windowbestlen, %rdx), %xmm5
300 movdqu 32(%prev, %rdx), %xmm6
302 movdqu 48(%windowbestlen, %rdx), %xmm7
303 movdqu 48(%prev, %rdx), %xmm8
306 /* Check the comparisions' results */
312 /* this is the only iteration of the loop with a possibility of having
313 incremented rdx by 0x108 (each loop iteration add 16*4 = 0x40
314 and (0x40*4)+8=0x108 */
345 LeaveLoopCmps: add %rax, %rdx
347 mov (%windowbestlen, %rdx), %rax
348 xor (%prev, %rdx), %rax
351 mov 8(%windowbestlen, %rdx), %rax
352 xor 8(%prev, %rdx), %rax
355 mov 16(%windowbestlen, %rdx), %rax
356 xor 16(%prev, %rdx), %rax
364 * This three-liner is tantalizingly simple, but bsf is a slow instruction,
365 * and the complicated alternative down below is quite a bit faster. Sad...
368 LeaveLoopCmps: bsf %rax, %rax /* find the first non-zero bit */
369 shrl $3, %eax /* divide by 8 to get the byte */
376 LeaveLoopCmps: testl $0xFFFFFFFF, %eax /* Check the first 4 bytes */
380 Check16: testw $0xFFFF, %ax
384 LenLower: subb $1, %al
389 /* Calculate the length of the match. If it is longer than MAX_MATCH, */
390 /* then automatically accept it as the best possible match and leave. */
392 lea (%prev, %rdx), %rax
394 cmpl $MAX_MATCH, %eax
397 /* If the length of the match is not longer than the best match we */
398 /* have so far, then forget it and return to the lookup loop. */
402 mov _windowbestlen, %windowbestlen
404 movl _chainlenwmask, %edx
407 /* s->match_start = cur_match; */
408 /* best_len = len; */
409 /* if (len >= nice_match) break; */
410 /* scan_end = *(ushf*)(scan+best_len-1); */
414 movl %curmatchd, dsMatchStart
415 cmpl %nicematch, %eax
418 lea (%window, %bestlen), %windowbestlen
419 mov %windowbestlen, _windowbestlen
421 movzwl -1(%scan, %rax), %scanend
423 movl _chainlenwmask, %chainlenwmask
426 /* Accept the current string, with the maximum possible length. */
429 movl $MAX_MATCH, %bestlend
430 movl %curmatchd, dsMatchStart
432 /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
433 /* return s->lookahead; */
436 movl dsLookahead, %eax
438 cmovngl %bestlend, %eax
441 /* Restore the registers and return from whence we came. */