2 ;uInt longest_match_x64(
4 ; IPos cur_match); // current match
6 ; gvmat64.S -- Asm portion of the optimized longest_match for 32 bits x86_64
7 ; (AMD64 on Athlon 64, Opteron, Phenom
8 ; and Intel EM64T on Pentium 4 with EM64T, Pentium D, Core 2 Duo, Core I5/I7)
9 ; this file is translation from gvmat64.asm to GCC 4.x (for Linux, Mac XCode)
10 ; Copyright (C) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.
12 ; File written by Gilles Vollant, by converting to assembly the longest_match
13 ; from Jean-loup Gailly in deflate.c of zLib and infoZip zip.
14 ; and by taking inspiration on asm686 with masm, optimised assembly code
15 ; from Brian Raiter, written 1998
17 ; This software is provided 'as-is', without any express or implied
18 ; warranty. In no event will the authors be held liable for any damages
19 ; arising from the use of this software.
21 ; Permission is granted to anyone to use this software for any purpose,
22 ; including commercial applications, and to alter it and redistribute it
23 ; freely, subject to the following restrictions:
25 ; 1. The origin of this software must not be misrepresented; you must not
26 ; claim that you wrote the original software. If you use this software
27 ; in a product, an acknowledgment in the product documentation would be
28 ; appreciated but is not required.
29 ; 2. Altered source versions must be plainly marked as such, and must not be
30 ; misrepresented as being the original software
31 ; 3. This notice may not be removed or altered from any source distribution.
34 ; http://www.winimage.com/zLibDll
35 ; http://www.muppetlabs.com/~breadbox/software/assembly.html
37 ; to compile this file for zLib, I use option:
38 ; gcc -c -arch x86_64 gvmat64.S
41 ;uInt longest_match(s, cur_match)
43 ; IPos cur_match; // current match /
45 ; with XCode for Mac, I had strange error with some jump on intel syntax
46 ; this is why BEFORE_JMP and AFTER_JMP are used
50 #define BEFORE_JMP .att_syntax
51 #define AFTER_JMP .intel_syntax noprefix
54 # define match_init _match_init
55 # define longest_match _longest_match
58 .intel_syntax noprefix
60 .globl match_init, longest_match
66 #define LocalVarsSize 96
68 ; register used : rax,rbx,rcx,rdx,rsi,rdi,r8,r9,r10,r11,r12
69 ; free register : r14,r15
70 ; register can be saved : rsp
73 #define chainlenwmask (rsp + 8 - LocalVarsSize)
74 #define nicematch (rsp + 16 - LocalVarsSize)
76 #define save_rdi (rsp + 24 - LocalVarsSize)
77 #define save_rsi (rsp + 32 - LocalVarsSize)
78 #define save_rbx (rsp + 40 - LocalVarsSize)
79 #define save_rbp (rsp + 48 - LocalVarsSize)
80 #define save_r12 (rsp + 56 - LocalVarsSize)
81 #define save_r13 (rsp + 64 - LocalVarsSize)
82 #define save_r14 (rsp + 72 - LocalVarsSize)
83 #define save_r15 (rsp + 80 - LocalVarsSize)
87 ; all the +4 offsets are due to the addition of pending_buf_size (in zlib
88 ; in the deflate_state structure since the asm code was first written
89 ; (if you compile with zlib 1.0.4 or older, remove the +4).
90 ; Note : these value are good with a 8 bytes boundary pack structure
95 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
98 ;;; Offsets for fields in the deflate_state structure. These numbers
99 ;;; are calculated from the definition of deflate_state, with the
100 ;;; assumption that the compiler will dword-align the fields. (Thus,
101 ;;; changing the definition of deflate_state could easily cause this
102 ;;; program to crash horribly, without so much as a warning at
103 ;;; compile time. Sigh.)
105 ; all the +zlib1222add offsets are due to the addition of fields
106 ; in zlib in the deflate_state structure since the asm code was first written
107 ; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").
108 ; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").
109 ; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").
114 /* you can check the structure offset by running
123 deflate_state *s=&ds;
124 printf("size pointer=%u\n",(int)sizeof(void*));
126 printf("#define dsWSize %u\n",(int)(((char*)&(s->w_size))-((char*)s)));
127 printf("#define dsWMask %u\n",(int)(((char*)&(s->w_mask))-((char*)s)));
128 printf("#define dsWindow %u\n",(int)(((char*)&(s->window))-((char*)s)));
129 printf("#define dsPrev %u\n",(int)(((char*)&(s->prev))-((char*)s)));
130 printf("#define dsMatchLen %u\n",(int)(((char*)&(s->match_length))-((char*)s)));
131 printf("#define dsPrevMatch %u\n",(int)(((char*)&(s->prev_match))-((char*)s)));
132 printf("#define dsStrStart %u\n",(int)(((char*)&(s->strstart))-((char*)s)));
133 printf("#define dsMatchStart %u\n",(int)(((char*)&(s->match_start))-((char*)s)));
134 printf("#define dsLookahead %u\n",(int)(((char*)&(s->lookahead))-((char*)s)));
135 printf("#define dsPrevLen %u\n",(int)(((char*)&(s->prev_length))-((char*)s)));
136 printf("#define dsMaxChainLen %u\n",(int)(((char*)&(s->max_chain_length))-((char*)s)));
137 printf("#define dsGoodMatch %u\n",(int)(((char*)&(s->good_match))-((char*)s)));
138 printf("#define dsNiceMatch %u\n",(int)(((char*)&(s->nice_match))-((char*)s)));
146 #define dsMatchLen 144
147 #define dsPrevMatch 148
148 #define dsStrStart 156
149 #define dsMatchStart 160
150 #define dsLookahead 164
151 #define dsPrevLen 168
152 #define dsMaxChainLen 172
153 #define dsGoodMatch 188
154 #define dsNiceMatch 192
156 #define window_size [ rcx + dsWSize]
157 #define WMask [ rcx + dsWMask]
158 #define window_ad [ rcx + dsWindow]
159 #define prev_ad [ rcx + dsPrev]
160 #define strstart [ rcx + dsStrStart]
161 #define match_start [ rcx + dsMatchStart]
162 #define Lookahead [ rcx + dsLookahead] //; 0ffffffffh on infozip
163 #define prev_length [ rcx + dsPrevLen]
164 #define max_chain_length [ rcx + dsMaxChainLen]
165 #define good_match [ rcx + dsGoodMatch]
166 #define nice_match [ rcx + dsNiceMatch]
170 ; parameter 1 in rcx(deflate state s), param 2 in rdx (cur match)
172 ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
173 ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
175 ; All registers must be preserved across the call, except for
176 ; rax, rcx, rdx, r8, r9, r10, and r11, which are scratch.
179 ; gcc on macosx-linux:
180 ; see http://www.x86-64.org/documentation/abi-0.99.pdf
181 ; param 1 in rdi, param 2 in rsi
182 ; rbx, rsp, rbp, r12 to r15 must be preserved
184 ;;; Save registers that the compiler may be using, and adjust esp to
185 ;;; make room for our stack frame.
188 ;;; Retrieve the function arguments. r8d will hold cur_match
189 ;;; throughout the entire function. edx will hold the pointer to the
190 ;;; deflate_state structure during the function's setup (before
191 ;;; entering the main loop.
193 ; ms: parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match)
194 ; mac: param 1 in rdi, param 2 rsi
195 ; this clear high 32 bits of r8, which can be garbage in both r8 and rdx
212 //;;; uInt wmask = s->w_mask;
213 //;;; unsigned chain_length = s->max_chain_length;
214 //;;; if (s->prev_length >= s->good_match) {
215 //;;; chain_length >>= 2;
222 mov ebx, max_chain_length
228 //;;; chainlen is decremented once beforehand so that the function can
229 //;;; use the sign flag instead of the zero flag for the exit test.
230 //;;; It is then shifted into the high word, to make room for the wmask
231 //;;; value, which it will always accompany.
238 //;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
243 mov [chainlenwmask], ebx
251 //;;; register Bytef *scan = s->window + s->strstart;
256 //;;; Determine how many bytes the scan ptr is off from being
263 //;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
264 //;;; s->strstart - (IPos)MAX_DIST(s) : NIL;
268 sub eax, MIN_LOOKAHEAD
274 mov r11d, prev_length
278 //;;; int best_len = s->prev_length;
281 //;;; Store the sum of s->window + best_len in esi locally, and in esi.
285 //;;; register ush scan_start = *(ushf*)scan;
286 //;;; register ush scan_end = *(ushf*)(scan+best_len-1);
287 //;;; Posf *prev = s->prev;
289 movzx r12d,word ptr [r9]
290 movzx ebx, word ptr [r9 + r11 - 1]
294 //;;; Jump into the main loop.
296 mov edx, [chainlenwmask]
298 cmp bx,word ptr [rsi + r8 - 1]
306 movzx r8d, word ptr [rdi + r8*2]
318 cmp bx,word ptr [rsi + r8 - 1]
326 movzx r8d, word ptr [rdi + r8*2]
337 cmp bx,word ptr [rsi + r8 - 1]
345 movzx r8d, word ptr [rdi + r8*2]
357 cmp bx,word ptr [rsi + r8 - 1]
364 ;;; match = s->window + cur_match;
365 ;;; if (*(ushf*)(match+best_len-1) != scan_end ||
366 ;;; *(ushf*)match != scan_start) continue;
368 ;;; } while ((cur_match = prev[cur_match & wmask]) > limit
369 ;;; && --chain_length != 0);
371 ;;; Here is the inner loop of the function. The function will spend the
372 ;;; majority of its time in this loop, and majority of that time will
373 ;;; be spent in the first ten instructions.
375 ;;; Within this loop:
378 ;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
379 ;;; esi = windowbestlen - i.e., (window + bestlen)
387 movzx r8d, word ptr [rdi + r8*2]
399 cmp bx,word ptr [rsi + r8 - 1]
404 cmp r12w, word ptr [r10 + r8]
410 //;;; Store the current value of chainlen.
411 mov [chainlenwmask], edx
413 ;;; Point edi to the string under scrutiny, and esi to the string we
414 ;;; are hoping to match it up with. In actuality, esi and edi are
415 ;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is
416 ;;; initialized to -(MAX_MATCH_8 - scanalign).
419 mov rdx, 0xfffffffffffffef8 //; -(MAX_MATCH_8)
420 lea rsi, [rsi + r13 + 0x0108] //;MAX_MATCH_8]
421 lea rdi, [r9 + r13 + 0x0108] //;MAX_MATCH_8]
427 ;;; Test the strings for equality, 8 bytes at a time. At the end,
428 ;;; adjust rdx so that it is offset to the exact byte that mismatched.
430 ;;; We already know at this point that the first three bytes of the
431 ;;; strings match each other, and they can be safely passed over before
432 ;;; starting the compare loop. So what this code does is skip over 0-3
433 ;;; bytes, as much as necessary in order to dword-align the edi
434 ;;; pointer. (rsi will still be misaligned three times out of four.)
436 ;;; It should be confessed that this loop usually does not represent
437 ;;; much of the total running time. Replacing it with a more
438 ;;; straightforward "rep cmpsb" would not drastically degrade
447 mov rax, [rsi + rdx + 8]
448 xor rax, [rdi + rdx + 8]
452 mov rax, [rsi + rdx + 8+8]
453 xor rax, [rdi + rdx + 8+8]
463 LeaveLoopCmps16: add rdx,8
464 LeaveLoopCmps8: add rdx,8
488 //;;; Calculate the length of the match. If it is longer than MAX_MATCH,
489 //;;; then automatically accept it as the best possible match and leave.
498 ;;; If the length of the match is not longer than the best match we
499 ;;; have so far, then forget it and return to the lookup loop.
500 ;///////////////////////////////////
508 mov edx, [chainlenwmask]
513 ;;; s->match_start = cur_match;
515 ;;; if (len >= nice_match) break;
516 ;;; scan_end = *(ushf*)(scan+best_len-1);
528 movzx ebx, word ptr [r9 + rax - 1]
530 mov edx, [chainlenwmask]
535 //;;; Accept the current string, with the maximum possible length.
541 //;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
542 //;;; return s->lookahead;
551 //;;; Restore the stack and return from whence we came.
554 // mov rsi,[save_rsi]
555 // mov rdi,[save_rdi]
565 //; please don't remove this string !
566 //; Your can freely use gvmat64 in any free or commercial app
567 //; but it is far better don't remove the string in the binary!
568 // db 0dh,0ah,"asm686 with masm, optimised assembly code from Brian Raiter, written 1998, converted to amd 64 by Gilles Vollant 2005",0dh,0ah,0