Chris@4
|
1 /*
|
Chris@4
|
2 ;uInt longest_match_x64(
|
Chris@4
|
3 ; deflate_state *s,
|
Chris@4
|
4 ; IPos cur_match); // current match
|
Chris@4
|
5
|
Chris@4
|
6 ; gvmat64.S -- Asm portion of the optimized longest_match for 32 bits x86_64
|
Chris@4
|
7 ; (AMD64 on Athlon 64, Opteron, Phenom
|
Chris@4
|
8 ; and Intel EM64T on Pentium 4 with EM64T, Pentium D, Core 2 Duo, Core I5/I7)
|
Chris@4
|
9 ; this file is translation from gvmat64.asm to GCC 4.x (for Linux, Mac XCode)
|
Chris@4
|
10 ; Copyright (C) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.
|
Chris@4
|
11 ;
|
Chris@4
|
12 ; File written by Gilles Vollant, by converting to assembly the longest_match
|
Chris@4
|
13 ; from Jean-loup Gailly in deflate.c of zLib and infoZip zip.
|
Chris@4
|
14 ; and by taking inspiration on asm686 with masm, optimised assembly code
|
Chris@4
|
15 ; from Brian Raiter, written 1998
|
Chris@4
|
16 ;
|
Chris@4
|
17 ; This software is provided 'as-is', without any express or implied
|
Chris@4
|
18 ; warranty. In no event will the authors be held liable for any damages
|
Chris@4
|
19 ; arising from the use of this software.
|
Chris@4
|
20 ;
|
Chris@4
|
21 ; Permission is granted to anyone to use this software for any purpose,
|
Chris@4
|
22 ; including commercial applications, and to alter it and redistribute it
|
Chris@4
|
23 ; freely, subject to the following restrictions:
|
Chris@4
|
24 ;
|
Chris@4
|
25 ; 1. The origin of this software must not be misrepresented; you must not
|
Chris@4
|
26 ; claim that you wrote the original software. If you use this software
|
Chris@4
|
27 ; in a product, an acknowledgment in the product documentation would be
|
Chris@4
|
28 ; appreciated but is not required.
|
Chris@4
|
29 ; 2. Altered source versions must be plainly marked as such, and must not be
|
Chris@4
|
30 ; misrepresented as being the original software
|
Chris@4
|
31 ; 3. This notice may not be removed or altered from any source distribution.
|
Chris@4
|
32 ;
|
Chris@4
|
33 ; http://www.zlib.net
|
Chris@4
|
34 ; http://www.winimage.com/zLibDll
|
Chris@4
|
35 ; http://www.muppetlabs.com/~breadbox/software/assembly.html
|
Chris@4
|
36 ;
|
Chris@4
|
37 ; to compile this file for zLib, I use option:
|
Chris@4
|
38 ; gcc -c -arch x86_64 gvmat64.S
|
Chris@4
|
39
|
Chris@4
|
40
|
Chris@4
|
41 ;uInt longest_match(s, cur_match)
|
Chris@4
|
42 ; deflate_state *s;
|
Chris@4
|
43 ; IPos cur_match; // current match /
|
Chris@4
|
44 ;
|
Chris@4
|
45 ; with XCode for Mac, I had strange error with some jump on intel syntax
|
Chris@4
|
46 ; this is why BEFORE_JMP and AFTER_JMP are used
|
Chris@4
|
47 */
|
Chris@4
|
48
|
Chris@4
|
49
|
Chris@4
|
50 #define BEFORE_JMP .att_syntax
|
Chris@4
|
51 #define AFTER_JMP .intel_syntax noprefix
|
Chris@4
|
52
|
Chris@4
|
53 #ifndef NO_UNDERLINE
|
Chris@4
|
54 # define match_init _match_init
|
Chris@4
|
55 # define longest_match _longest_match
|
Chris@4
|
56 #endif
|
Chris@4
|
57
|
Chris@4
|
58 .intel_syntax noprefix
|
Chris@4
|
59
|
Chris@4
|
60 .globl match_init, longest_match
|
Chris@4
|
61 .text
|
Chris@4
|
62 longest_match:
|
Chris@4
|
63
|
Chris@4
|
64
|
Chris@4
|
65
|
Chris@4
|
66 #define LocalVarsSize 96
|
Chris@4
|
67 /*
|
Chris@4
|
68 ; register used : rax,rbx,rcx,rdx,rsi,rdi,r8,r9,r10,r11,r12
|
Chris@4
|
69 ; free register : r14,r15
|
Chris@4
|
70 ; register can be saved : rsp
|
Chris@4
|
71 */
|
Chris@4
|
72
|
Chris@4
|
73 #define chainlenwmask (rsp + 8 - LocalVarsSize)
|
Chris@4
|
74 #define nicematch (rsp + 16 - LocalVarsSize)
|
Chris@4
|
75
|
Chris@4
|
76 #define save_rdi (rsp + 24 - LocalVarsSize)
|
Chris@4
|
77 #define save_rsi (rsp + 32 - LocalVarsSize)
|
Chris@4
|
78 #define save_rbx (rsp + 40 - LocalVarsSize)
|
Chris@4
|
79 #define save_rbp (rsp + 48 - LocalVarsSize)
|
Chris@4
|
80 #define save_r12 (rsp + 56 - LocalVarsSize)
|
Chris@4
|
81 #define save_r13 (rsp + 64 - LocalVarsSize)
|
Chris@4
|
82 #define save_r14 (rsp + 72 - LocalVarsSize)
|
Chris@4
|
83 #define save_r15 (rsp + 80 - LocalVarsSize)
|
Chris@4
|
84
|
Chris@4
|
85
|
Chris@4
|
86 /*
|
Chris@4
|
87 ; all the +4 offsets are due to the addition of pending_buf_size (in zlib
|
Chris@4
|
88 ; in the deflate_state structure since the asm code was first written
|
Chris@4
|
89 ; (if you compile with zlib 1.0.4 or older, remove the +4).
|
Chris@4
|
90 ; Note : these value are good with a 8 bytes boundary pack structure
|
Chris@4
|
91 */
|
Chris@4
|
92
|
Chris@4
|
93 #define MAX_MATCH 258
|
Chris@4
|
94 #define MIN_MATCH 3
|
Chris@4
|
95 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
|
Chris@4
|
96
|
Chris@4
|
97 /*
|
Chris@4
|
98 ;;; Offsets for fields in the deflate_state structure. These numbers
|
Chris@4
|
99 ;;; are calculated from the definition of deflate_state, with the
|
Chris@4
|
100 ;;; assumption that the compiler will dword-align the fields. (Thus,
|
Chris@4
|
101 ;;; changing the definition of deflate_state could easily cause this
|
Chris@4
|
102 ;;; program to crash horribly, without so much as a warning at
|
Chris@4
|
103 ;;; compile time. Sigh.)
|
Chris@4
|
104
|
Chris@4
|
105 ; all the +zlib1222add offsets are due to the addition of fields
|
Chris@4
|
106 ; in zlib in the deflate_state structure since the asm code was first written
|
Chris@4
|
107 ; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").
|
Chris@4
|
108 ; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").
|
Chris@4
|
109 ; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").
|
Chris@4
|
110 */
|
Chris@4
|
111
|
Chris@4
|
112
|
Chris@4
|
113
|
Chris@4
|
114 /* you can check the structure offset by running
|
Chris@4
|
115
|
Chris@4
|
116 #include <stdlib.h>
|
Chris@4
|
117 #include <stdio.h>
|
Chris@4
|
118 #include "deflate.h"
|
Chris@4
|
119
|
Chris@4
|
120 void print_depl()
|
Chris@4
|
121 {
|
Chris@4
|
122 deflate_state ds;
|
Chris@4
|
123 deflate_state *s=&ds;
|
Chris@4
|
124 printf("size pointer=%u\n",(int)sizeof(void*));
|
Chris@4
|
125
|
Chris@4
|
126 printf("#define dsWSize %u\n",(int)(((char*)&(s->w_size))-((char*)s)));
|
Chris@4
|
127 printf("#define dsWMask %u\n",(int)(((char*)&(s->w_mask))-((char*)s)));
|
Chris@4
|
128 printf("#define dsWindow %u\n",(int)(((char*)&(s->window))-((char*)s)));
|
Chris@4
|
129 printf("#define dsPrev %u\n",(int)(((char*)&(s->prev))-((char*)s)));
|
Chris@4
|
130 printf("#define dsMatchLen %u\n",(int)(((char*)&(s->match_length))-((char*)s)));
|
Chris@4
|
131 printf("#define dsPrevMatch %u\n",(int)(((char*)&(s->prev_match))-((char*)s)));
|
Chris@4
|
132 printf("#define dsStrStart %u\n",(int)(((char*)&(s->strstart))-((char*)s)));
|
Chris@4
|
133 printf("#define dsMatchStart %u\n",(int)(((char*)&(s->match_start))-((char*)s)));
|
Chris@4
|
134 printf("#define dsLookahead %u\n",(int)(((char*)&(s->lookahead))-((char*)s)));
|
Chris@4
|
135 printf("#define dsPrevLen %u\n",(int)(((char*)&(s->prev_length))-((char*)s)));
|
Chris@4
|
136 printf("#define dsMaxChainLen %u\n",(int)(((char*)&(s->max_chain_length))-((char*)s)));
|
Chris@4
|
137 printf("#define dsGoodMatch %u\n",(int)(((char*)&(s->good_match))-((char*)s)));
|
Chris@4
|
138 printf("#define dsNiceMatch %u\n",(int)(((char*)&(s->nice_match))-((char*)s)));
|
Chris@4
|
139 }
|
Chris@4
|
140 */
|
Chris@4
|
141
|
Chris@4
|
142 #define dsWSize 68
|
Chris@4
|
143 #define dsWMask 76
|
Chris@4
|
144 #define dsWindow 80
|
Chris@4
|
145 #define dsPrev 96
|
Chris@4
|
146 #define dsMatchLen 144
|
Chris@4
|
147 #define dsPrevMatch 148
|
Chris@4
|
148 #define dsStrStart 156
|
Chris@4
|
149 #define dsMatchStart 160
|
Chris@4
|
150 #define dsLookahead 164
|
Chris@4
|
151 #define dsPrevLen 168
|
Chris@4
|
152 #define dsMaxChainLen 172
|
Chris@4
|
153 #define dsGoodMatch 188
|
Chris@4
|
154 #define dsNiceMatch 192
|
Chris@4
|
155
|
Chris@4
|
156 #define window_size [ rcx + dsWSize]
|
Chris@4
|
157 #define WMask [ rcx + dsWMask]
|
Chris@4
|
158 #define window_ad [ rcx + dsWindow]
|
Chris@4
|
159 #define prev_ad [ rcx + dsPrev]
|
Chris@4
|
160 #define strstart [ rcx + dsStrStart]
|
Chris@4
|
161 #define match_start [ rcx + dsMatchStart]
|
Chris@4
|
162 #define Lookahead [ rcx + dsLookahead] //; 0ffffffffh on infozip
|
Chris@4
|
163 #define prev_length [ rcx + dsPrevLen]
|
Chris@4
|
164 #define max_chain_length [ rcx + dsMaxChainLen]
|
Chris@4
|
165 #define good_match [ rcx + dsGoodMatch]
|
Chris@4
|
166 #define nice_match [ rcx + dsNiceMatch]
|
Chris@4
|
167
|
Chris@4
|
168 /*
|
Chris@4
|
169 ; windows:
|
Chris@4
|
170 ; parameter 1 in rcx(deflate state s), param 2 in rdx (cur match)
|
Chris@4
|
171
|
Chris@4
|
172 ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
|
Chris@4
|
173 ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
|
Chris@4
|
174 ;
|
Chris@4
|
175 ; All registers must be preserved across the call, except for
|
Chris@4
|
176 ; rax, rcx, rdx, r8, r9, r10, and r11, which are scratch.
|
Chris@4
|
177
|
Chris@4
|
178 ;
|
Chris@4
|
179 ; gcc on macosx-linux:
|
Chris@4
|
180 ; see http://www.x86-64.org/documentation/abi-0.99.pdf
|
Chris@4
|
181 ; param 1 in rdi, param 2 in rsi
|
Chris@4
|
182 ; rbx, rsp, rbp, r12 to r15 must be preserved
|
Chris@4
|
183
|
Chris@4
|
184 ;;; Save registers that the compiler may be using, and adjust esp to
|
Chris@4
|
185 ;;; make room for our stack frame.
|
Chris@4
|
186
|
Chris@4
|
187
|
Chris@4
|
188 ;;; Retrieve the function arguments. r8d will hold cur_match
|
Chris@4
|
189 ;;; throughout the entire function. edx will hold the pointer to the
|
Chris@4
|
190 ;;; deflate_state structure during the function's setup (before
|
Chris@4
|
191 ;;; entering the main loop.
|
Chris@4
|
192
|
Chris@4
|
193 ; ms: parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match)
|
Chris@4
|
194 ; mac: param 1 in rdi, param 2 rsi
|
Chris@4
|
195 ; this clear high 32 bits of r8, which can be garbage in both r8 and rdx
|
Chris@4
|
196 */
|
Chris@4
|
197 mov [save_rbx],rbx
|
Chris@4
|
198 mov [save_rbp],rbp
|
Chris@4
|
199
|
Chris@4
|
200
|
Chris@4
|
201 mov rcx,rdi
|
Chris@4
|
202
|
Chris@4
|
203 mov r8d,esi
|
Chris@4
|
204
|
Chris@4
|
205
|
Chris@4
|
206 mov [save_r12],r12
|
Chris@4
|
207 mov [save_r13],r13
|
Chris@4
|
208 mov [save_r14],r14
|
Chris@4
|
209 mov [save_r15],r15
|
Chris@4
|
210
|
Chris@4
|
211
|
Chris@4
|
212 //;;; uInt wmask = s->w_mask;
|
Chris@4
|
213 //;;; unsigned chain_length = s->max_chain_length;
|
Chris@4
|
214 //;;; if (s->prev_length >= s->good_match) {
|
Chris@4
|
215 //;;; chain_length >>= 2;
|
Chris@4
|
216 //;;; }
|
Chris@4
|
217
|
Chris@4
|
218
|
Chris@4
|
219 mov edi, prev_length
|
Chris@4
|
220 mov esi, good_match
|
Chris@4
|
221 mov eax, WMask
|
Chris@4
|
222 mov ebx, max_chain_length
|
Chris@4
|
223 cmp edi, esi
|
Chris@4
|
224 jl LastMatchGood
|
Chris@4
|
225 shr ebx, 2
|
Chris@4
|
226 LastMatchGood:
|
Chris@4
|
227
|
Chris@4
|
228 //;;; chainlen is decremented once beforehand so that the function can
|
Chris@4
|
229 //;;; use the sign flag instead of the zero flag for the exit test.
|
Chris@4
|
230 //;;; It is then shifted into the high word, to make room for the wmask
|
Chris@4
|
231 //;;; value, which it will always accompany.
|
Chris@4
|
232
|
Chris@4
|
233 dec ebx
|
Chris@4
|
234 shl ebx, 16
|
Chris@4
|
235 or ebx, eax
|
Chris@4
|
236
|
Chris@4
|
237 //;;; on zlib only
|
Chris@4
|
238 //;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
|
Chris@4
|
239
|
Chris@4
|
240
|
Chris@4
|
241
|
Chris@4
|
242 mov eax, nice_match
|
Chris@4
|
243 mov [chainlenwmask], ebx
|
Chris@4
|
244 mov r10d, Lookahead
|
Chris@4
|
245 cmp r10d, eax
|
Chris@4
|
246 cmovnl r10d, eax
|
Chris@4
|
247 mov [nicematch],r10d
|
Chris@4
|
248
|
Chris@4
|
249
|
Chris@4
|
250
|
Chris@4
|
251 //;;; register Bytef *scan = s->window + s->strstart;
|
Chris@4
|
252 mov r10, window_ad
|
Chris@4
|
253 mov ebp, strstart
|
Chris@4
|
254 lea r13, [r10 + rbp]
|
Chris@4
|
255
|
Chris@4
|
256 //;;; Determine how many bytes the scan ptr is off from being
|
Chris@4
|
257 //;;; dword-aligned.
|
Chris@4
|
258
|
Chris@4
|
259 mov r9,r13
|
Chris@4
|
260 neg r13
|
Chris@4
|
261 and r13,3
|
Chris@4
|
262
|
Chris@4
|
263 //;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
|
Chris@4
|
264 //;;; s->strstart - (IPos)MAX_DIST(s) : NIL;
|
Chris@4
|
265
|
Chris@4
|
266
|
Chris@4
|
267 mov eax, window_size
|
Chris@4
|
268 sub eax, MIN_LOOKAHEAD
|
Chris@4
|
269
|
Chris@4
|
270
|
Chris@4
|
271 xor edi,edi
|
Chris@4
|
272 sub ebp, eax
|
Chris@4
|
273
|
Chris@4
|
274 mov r11d, prev_length
|
Chris@4
|
275
|
Chris@4
|
276 cmovng ebp,edi
|
Chris@4
|
277
|
Chris@4
|
278 //;;; int best_len = s->prev_length;
|
Chris@4
|
279
|
Chris@4
|
280
|
Chris@4
|
281 //;;; Store the sum of s->window + best_len in esi locally, and in esi.
|
Chris@4
|
282
|
Chris@4
|
283 lea rsi,[r10+r11]
|
Chris@4
|
284
|
Chris@4
|
285 //;;; register ush scan_start = *(ushf*)scan;
|
Chris@4
|
286 //;;; register ush scan_end = *(ushf*)(scan+best_len-1);
|
Chris@4
|
287 //;;; Posf *prev = s->prev;
|
Chris@4
|
288
|
Chris@4
|
289 movzx r12d,word ptr [r9]
|
Chris@4
|
290 movzx ebx, word ptr [r9 + r11 - 1]
|
Chris@4
|
291
|
Chris@4
|
292 mov rdi, prev_ad
|
Chris@4
|
293
|
Chris@4
|
294 //;;; Jump into the main loop.
|
Chris@4
|
295
|
Chris@4
|
296 mov edx, [chainlenwmask]
|
Chris@4
|
297
|
Chris@4
|
298 cmp bx,word ptr [rsi + r8 - 1]
|
Chris@4
|
299 jz LookupLoopIsZero
|
Chris@4
|
300
|
Chris@4
|
301
|
Chris@4
|
302
|
Chris@4
|
303 LookupLoop1:
|
Chris@4
|
304 and r8d, edx
|
Chris@4
|
305
|
Chris@4
|
306 movzx r8d, word ptr [rdi + r8*2]
|
Chris@4
|
307 cmp r8d, ebp
|
Chris@4
|
308 jbe LeaveNow
|
Chris@4
|
309
|
Chris@4
|
310
|
Chris@4
|
311
|
Chris@4
|
312 sub edx, 0x00010000
|
Chris@4
|
313 BEFORE_JMP
|
Chris@4
|
314 js LeaveNow
|
Chris@4
|
315 AFTER_JMP
|
Chris@4
|
316
|
Chris@4
|
317 LoopEntry1:
|
Chris@4
|
318 cmp bx,word ptr [rsi + r8 - 1]
|
Chris@4
|
319 BEFORE_JMP
|
Chris@4
|
320 jz LookupLoopIsZero
|
Chris@4
|
321 AFTER_JMP
|
Chris@4
|
322
|
Chris@4
|
323 LookupLoop2:
|
Chris@4
|
324 and r8d, edx
|
Chris@4
|
325
|
Chris@4
|
326 movzx r8d, word ptr [rdi + r8*2]
|
Chris@4
|
327 cmp r8d, ebp
|
Chris@4
|
328 BEFORE_JMP
|
Chris@4
|
329 jbe LeaveNow
|
Chris@4
|
330 AFTER_JMP
|
Chris@4
|
331 sub edx, 0x00010000
|
Chris@4
|
332 BEFORE_JMP
|
Chris@4
|
333 js LeaveNow
|
Chris@4
|
334 AFTER_JMP
|
Chris@4
|
335
|
Chris@4
|
336 LoopEntry2:
|
Chris@4
|
337 cmp bx,word ptr [rsi + r8 - 1]
|
Chris@4
|
338 BEFORE_JMP
|
Chris@4
|
339 jz LookupLoopIsZero
|
Chris@4
|
340 AFTER_JMP
|
Chris@4
|
341
|
Chris@4
|
342 LookupLoop4:
|
Chris@4
|
343 and r8d, edx
|
Chris@4
|
344
|
Chris@4
|
345 movzx r8d, word ptr [rdi + r8*2]
|
Chris@4
|
346 cmp r8d, ebp
|
Chris@4
|
347 BEFORE_JMP
|
Chris@4
|
348 jbe LeaveNow
|
Chris@4
|
349 AFTER_JMP
|
Chris@4
|
350 sub edx, 0x00010000
|
Chris@4
|
351 BEFORE_JMP
|
Chris@4
|
352 js LeaveNow
|
Chris@4
|
353 AFTER_JMP
|
Chris@4
|
354
|
Chris@4
|
355 LoopEntry4:
|
Chris@4
|
356
|
Chris@4
|
357 cmp bx,word ptr [rsi + r8 - 1]
|
Chris@4
|
358 BEFORE_JMP
|
Chris@4
|
359 jnz LookupLoop1
|
Chris@4
|
360 jmp LookupLoopIsZero
|
Chris@4
|
361 AFTER_JMP
|
Chris@4
|
362 /*
|
Chris@4
|
363 ;;; do {
|
Chris@4
|
364 ;;; match = s->window + cur_match;
|
Chris@4
|
365 ;;; if (*(ushf*)(match+best_len-1) != scan_end ||
|
Chris@4
|
366 ;;; *(ushf*)match != scan_start) continue;
|
Chris@4
|
367 ;;; [...]
|
Chris@4
|
368 ;;; } while ((cur_match = prev[cur_match & wmask]) > limit
|
Chris@4
|
369 ;;; && --chain_length != 0);
|
Chris@4
|
370 ;;;
|
Chris@4
|
371 ;;; Here is the inner loop of the function. The function will spend the
|
Chris@4
|
372 ;;; majority of its time in this loop, and majority of that time will
|
Chris@4
|
373 ;;; be spent in the first ten instructions.
|
Chris@4
|
374 ;;;
|
Chris@4
|
375 ;;; Within this loop:
|
Chris@4
|
376 ;;; ebx = scanend
|
Chris@4
|
377 ;;; r8d = curmatch
|
Chris@4
|
378 ;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
|
Chris@4
|
379 ;;; esi = windowbestlen - i.e., (window + bestlen)
|
Chris@4
|
380 ;;; edi = prev
|
Chris@4
|
381 ;;; ebp = limit
|
Chris@4
|
382 */
|
Chris@4
|
383 .balign 16
|
Chris@4
|
384 LookupLoop:
|
Chris@4
|
385 and r8d, edx
|
Chris@4
|
386
|
Chris@4
|
387 movzx r8d, word ptr [rdi + r8*2]
|
Chris@4
|
388 cmp r8d, ebp
|
Chris@4
|
389 BEFORE_JMP
|
Chris@4
|
390 jbe LeaveNow
|
Chris@4
|
391 AFTER_JMP
|
Chris@4
|
392 sub edx, 0x00010000
|
Chris@4
|
393 BEFORE_JMP
|
Chris@4
|
394 js LeaveNow
|
Chris@4
|
395 AFTER_JMP
|
Chris@4
|
396
|
Chris@4
|
397 LoopEntry:
|
Chris@4
|
398
|
Chris@4
|
399 cmp bx,word ptr [rsi + r8 - 1]
|
Chris@4
|
400 BEFORE_JMP
|
Chris@4
|
401 jnz LookupLoop1
|
Chris@4
|
402 AFTER_JMP
|
Chris@4
|
403 LookupLoopIsZero:
|
Chris@4
|
404 cmp r12w, word ptr [r10 + r8]
|
Chris@4
|
405 BEFORE_JMP
|
Chris@4
|
406 jnz LookupLoop1
|
Chris@4
|
407 AFTER_JMP
|
Chris@4
|
408
|
Chris@4
|
409
|
Chris@4
|
410 //;;; Store the current value of chainlen.
|
Chris@4
|
411 mov [chainlenwmask], edx
|
Chris@4
|
412 /*
|
Chris@4
|
413 ;;; Point edi to the string under scrutiny, and esi to the string we
|
Chris@4
|
414 ;;; are hoping to match it up with. In actuality, esi and edi are
|
Chris@4
|
415 ;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is
|
Chris@4
|
416 ;;; initialized to -(MAX_MATCH_8 - scanalign).
|
Chris@4
|
417 */
|
Chris@4
|
418 lea rsi,[r8+r10]
|
Chris@4
|
419 mov rdx, 0xfffffffffffffef8 //; -(MAX_MATCH_8)
|
Chris@4
|
420 lea rsi, [rsi + r13 + 0x0108] //;MAX_MATCH_8]
|
Chris@4
|
421 lea rdi, [r9 + r13 + 0x0108] //;MAX_MATCH_8]
|
Chris@4
|
422
|
Chris@4
|
423 prefetcht1 [rsi+rdx]
|
Chris@4
|
424 prefetcht1 [rdi+rdx]
|
Chris@4
|
425
|
Chris@4
|
426 /*
|
Chris@4
|
427 ;;; Test the strings for equality, 8 bytes at a time. At the end,
|
Chris@4
|
428 ;;; adjust rdx so that it is offset to the exact byte that mismatched.
|
Chris@4
|
429 ;;;
|
Chris@4
|
430 ;;; We already know at this point that the first three bytes of the
|
Chris@4
|
431 ;;; strings match each other, and they can be safely passed over before
|
Chris@4
|
432 ;;; starting the compare loop. So what this code does is skip over 0-3
|
Chris@4
|
433 ;;; bytes, as much as necessary in order to dword-align the edi
|
Chris@4
|
434 ;;; pointer. (rsi will still be misaligned three times out of four.)
|
Chris@4
|
435 ;;;
|
Chris@4
|
436 ;;; It should be confessed that this loop usually does not represent
|
Chris@4
|
437 ;;; much of the total running time. Replacing it with a more
|
Chris@4
|
438 ;;; straightforward "rep cmpsb" would not drastically degrade
|
Chris@4
|
439 ;;; performance.
|
Chris@4
|
440 */
|
Chris@4
|
441
|
Chris@4
|
442 LoopCmps:
|
Chris@4
|
443 mov rax, [rsi + rdx]
|
Chris@4
|
444 xor rax, [rdi + rdx]
|
Chris@4
|
445 jnz LeaveLoopCmps
|
Chris@4
|
446
|
Chris@4
|
447 mov rax, [rsi + rdx + 8]
|
Chris@4
|
448 xor rax, [rdi + rdx + 8]
|
Chris@4
|
449 jnz LeaveLoopCmps8
|
Chris@4
|
450
|
Chris@4
|
451
|
Chris@4
|
452 mov rax, [rsi + rdx + 8+8]
|
Chris@4
|
453 xor rax, [rdi + rdx + 8+8]
|
Chris@4
|
454 jnz LeaveLoopCmps16
|
Chris@4
|
455
|
Chris@4
|
456 add rdx,8+8+8
|
Chris@4
|
457
|
Chris@4
|
458 BEFORE_JMP
|
Chris@4
|
459 jnz LoopCmps
|
Chris@4
|
460 jmp LenMaximum
|
Chris@4
|
461 AFTER_JMP
|
Chris@4
|
462
|
Chris@4
|
463 LeaveLoopCmps16: add rdx,8
|
Chris@4
|
464 LeaveLoopCmps8: add rdx,8
|
Chris@4
|
465 LeaveLoopCmps:
|
Chris@4
|
466
|
Chris@4
|
467 test eax, 0x0000FFFF
|
Chris@4
|
468 jnz LenLower
|
Chris@4
|
469
|
Chris@4
|
470 test eax,0xffffffff
|
Chris@4
|
471
|
Chris@4
|
472 jnz LenLower32
|
Chris@4
|
473
|
Chris@4
|
474 add rdx,4
|
Chris@4
|
475 shr rax,32
|
Chris@4
|
476 or ax,ax
|
Chris@4
|
477 BEFORE_JMP
|
Chris@4
|
478 jnz LenLower
|
Chris@4
|
479 AFTER_JMP
|
Chris@4
|
480
|
Chris@4
|
481 LenLower32:
|
Chris@4
|
482 shr eax,16
|
Chris@4
|
483 add rdx,2
|
Chris@4
|
484
|
Chris@4
|
485 LenLower:
|
Chris@4
|
486 sub al, 1
|
Chris@4
|
487 adc rdx, 0
|
Chris@4
|
488 //;;; Calculate the length of the match. If it is longer than MAX_MATCH,
|
Chris@4
|
489 //;;; then automatically accept it as the best possible match and leave.
|
Chris@4
|
490
|
Chris@4
|
491 lea rax, [rdi + rdx]
|
Chris@4
|
492 sub rax, r9
|
Chris@4
|
493 cmp eax, MAX_MATCH
|
Chris@4
|
494 BEFORE_JMP
|
Chris@4
|
495 jge LenMaximum
|
Chris@4
|
496 AFTER_JMP
|
Chris@4
|
497 /*
|
Chris@4
|
498 ;;; If the length of the match is not longer than the best match we
|
Chris@4
|
499 ;;; have so far, then forget it and return to the lookup loop.
|
Chris@4
|
500 ;///////////////////////////////////
|
Chris@4
|
501 */
|
Chris@4
|
502 cmp eax, r11d
|
Chris@4
|
503 jg LongerMatch
|
Chris@4
|
504
|
Chris@4
|
505 lea rsi,[r10+r11]
|
Chris@4
|
506
|
Chris@4
|
507 mov rdi, prev_ad
|
Chris@4
|
508 mov edx, [chainlenwmask]
|
Chris@4
|
509 BEFORE_JMP
|
Chris@4
|
510 jmp LookupLoop
|
Chris@4
|
511 AFTER_JMP
|
Chris@4
|
512 /*
|
Chris@4
|
513 ;;; s->match_start = cur_match;
|
Chris@4
|
514 ;;; best_len = len;
|
Chris@4
|
515 ;;; if (len >= nice_match) break;
|
Chris@4
|
516 ;;; scan_end = *(ushf*)(scan+best_len-1);
|
Chris@4
|
517 */
|
Chris@4
|
518 LongerMatch:
|
Chris@4
|
519 mov r11d, eax
|
Chris@4
|
520 mov match_start, r8d
|
Chris@4
|
521 cmp eax, [nicematch]
|
Chris@4
|
522 BEFORE_JMP
|
Chris@4
|
523 jge LeaveNow
|
Chris@4
|
524 AFTER_JMP
|
Chris@4
|
525
|
Chris@4
|
526 lea rsi,[r10+rax]
|
Chris@4
|
527
|
Chris@4
|
528 movzx ebx, word ptr [r9 + rax - 1]
|
Chris@4
|
529 mov rdi, prev_ad
|
Chris@4
|
530 mov edx, [chainlenwmask]
|
Chris@4
|
531 BEFORE_JMP
|
Chris@4
|
532 jmp LookupLoop
|
Chris@4
|
533 AFTER_JMP
|
Chris@4
|
534
|
Chris@4
|
535 //;;; Accept the current string, with the maximum possible length.
|
Chris@4
|
536
|
Chris@4
|
537 LenMaximum:
|
Chris@4
|
538 mov r11d,MAX_MATCH
|
Chris@4
|
539 mov match_start, r8d
|
Chris@4
|
540
|
Chris@4
|
541 //;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
|
Chris@4
|
542 //;;; return s->lookahead;
|
Chris@4
|
543
|
Chris@4
|
544 LeaveNow:
|
Chris@4
|
545 mov eax, Lookahead
|
Chris@4
|
546 cmp r11d, eax
|
Chris@4
|
547 cmovng eax, r11d
|
Chris@4
|
548
|
Chris@4
|
549
|
Chris@4
|
550
|
Chris@4
|
551 //;;; Restore the stack and return from whence we came.
|
Chris@4
|
552
|
Chris@4
|
553
|
Chris@4
|
554 // mov rsi,[save_rsi]
|
Chris@4
|
555 // mov rdi,[save_rdi]
|
Chris@4
|
556 mov rbx,[save_rbx]
|
Chris@4
|
557 mov rbp,[save_rbp]
|
Chris@4
|
558 mov r12,[save_r12]
|
Chris@4
|
559 mov r13,[save_r13]
|
Chris@4
|
560 mov r14,[save_r14]
|
Chris@4
|
561 mov r15,[save_r15]
|
Chris@4
|
562
|
Chris@4
|
563
|
Chris@4
|
564 ret 0
|
Chris@4
|
565 //; please don't remove this string !
|
Chris@4
|
566 //; Your can freely use gvmat64 in any free or commercial app
|
Chris@4
|
567 //; but it is far better don't remove the string in the binary!
|
Chris@4
|
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
|
Chris@4
|
569
|
Chris@4
|
570
|
Chris@4
|
571 match_init:
|
Chris@4
|
572 ret 0
|
Chris@4
|
573
|
Chris@4
|
574
|