1 ; inffasx64.asm is a hand tuned assembler version of inffast.c - fast decoding
2 ; version for AMD64 on Windows using Microsoft C compiler
4 ; inffasx64.asm is automatically convert from AMD64 portion of inffas86.c
5 ; inffasx64.asm is called by inffas8664.c, which contain more info.
8 ; to compile this file, I use option
9 ; ml64.exe /Flinffasx64 /c /Zi inffasx64.asm
10 ; with Microsoft Macro Assembler (x64) for AMD64
13 ; This file compile with Microsoft Macro Assembler (x64) for AMD64
15 ; ml64.exe is given with Visual Studio 2005/2008/2010 and Windows WDK
17 ; (you can get Windows WDK with ml64 for AMD64 from
18 ; http://www.microsoft.com/whdc/Devtools/wdk/default.mspx for low price)
25 ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
26 ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
28 ; All registers must be preserved across the call, except for
29 ; rax, rcx, rdx, r8, r-9, r10, and r11, which are scratch.
42 mov [rax
+8], rbp
; /* save regs rbp and rsp */
45 mov rsp
, rax
; /* make rsp point to &ar */
47 mov rsi
, [rsp
+16] ; /* rsi = in */
48 mov rdi
, [rsp
+32] ; /* rdi = out */
49 mov r9
, [rsp
+24] ; /* r9 = last */
50 mov r10
, [rsp
+48] ; /* r10 = end */
51 mov rbp
, [rsp
+64] ; /* rbp = lcode */
52 mov r11
, [rsp
+72] ; /* r11 = dcode */
53 mov rdx
, [rsp
+80] ; /* rdx = hold */
54 mov ebx, [rsp
+88] ; /* ebx = bits */
55 mov r12d
, [rsp
+100] ; /* r12d = lmask */
56 mov r13d
, [rsp
+104] ; /* r13d = dmask */
63 je L_one_time
; /* if only one decode left */
70 mov r8
, r12
; /* r8 = lmask */
72 ja L_get_length_code_one_time
74 lodsd ; /* eax = *(uint *)in++ */
75 mov cl, bl ; /* cl = bits, needs it for shifting */
76 add bl, 32 ; /* bits += 32 */
78 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
79 jmp L_get_length_code_one_time
89 mov r8
, r12
; /* r8 = lmask */
91 ja L_get_length_code
; /* if (32 < bits) */
93 lodsd ; /* eax = *(uint *)in++ */
94 mov cl, bl ; /* cl = bits, needs it for shifting */
95 add bl, 32 ; /* bits += 32 */
97 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
100 and r8
, rdx
; /* r8 &= hold */
101 mov eax, [rbp
+r8
*4] ; /* eax = lcode[hold & lmask] */
103 mov cl, ah ; /* cl = this.bits */
104 sub bl, ah ; /* bits -= this.bits */
105 shr rdx
, cl ; /* hold >>= this.bits */
108 jnz L_test_for_length_base
; /* if (op != 0) 45.7% */
110 mov r8
, r12
; /* r8 = lmask */
111 shr eax, 16 ; /* output this.val char */
114 L_get_length_code_one_time:
115 and r8
, rdx
; /* r8 &= hold */
116 mov eax, [rbp
+r8
*4] ; /* eax = lcode[hold & lmask] */
119 mov cl, ah ; /* cl = this.bits */
120 sub bl, ah ; /* bits -= this.bits */
121 shr rdx
, cl ; /* hold >>= this.bits */
124 jnz L_test_for_length_base
; /* if (op != 0) 45.7% */
126 shr eax, 16 ; /* output this.val char */
131 L_test_for_length_base:
132 mov r14d
, eax ; /* len = this */
133 shr r14d
, 16 ; /* len = this.val */
137 jz L_test_for_second_level_length
; /* if ((op & 16) == 0) 8% */
138 and cl, 15 ; /* op &= 15 */
139 jz L_decode_distance
; /* if (!op) */
147 and eax, edx ; /* eax &= hold */
149 add r14d
, eax ; /* len += hold & mask[op] */
152 mov r8
, r13
; /* r8 = dmask */
154 ja L_get_distance_code
; /* if (32 < bits) */
156 lodsd ; /* eax = *(uint *)in++ */
157 mov cl, bl ; /* cl = bits, needs it for shifting */
158 add bl, 32 ; /* bits += 32 */
160 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
163 and r8
, rdx
; /* r8 &= hold */
164 mov eax, [r11
+r8
*4] ; /* eax = dcode[hold & dmask] */
167 mov r15d
, eax ; /* dist = this */
168 shr r15d
, 16 ; /* dist = this.val */
170 sub bl, ah ; /* bits -= this.bits */
171 shr rdx
, cl ; /* hold >>= this.bits */
172 mov cl, al ; /* cl = this.op */
174 test al, 16 ; /* if ((op & 16) == 0) */
175 jz L_test_for_second_level_dist
176 and cl, 15 ; /* op &= 15 */
184 dec eax ; /* (1 << op) - 1 */
185 and eax, edx ; /* eax &= hold */
187 add r15d
, eax ; /* dist += hold & ((1 << op) - 1) */
190 mov r8
, rsi
; /* save in so from can use it's reg */
192 sub rax
, [rsp
+40] ; /* nbytes = out - beg */
195 jb L_clip_window
; /* if (dist > nbytes) 4.2% */
197 mov ecx, r14d
; /* ecx = len */
199 sub rsi
, r15
; /* from = out - dist */
202 jnc L_copy_two
; /* if len % 2 == 0 */
209 mov rsi
, r8
; /* move in back to %rsi, toss from */
214 mov rsi
, r8
; /* move in back to %rsi, toss from */
219 cmp r15d
, 1 ; /* if dist 1, is a memset */
221 cmp [rsp
+40], rdi
; /* if out == beg, outside window */
224 mov ecx, r14d
; /* ecx = len */
238 L_test_for_second_level_length:
240 jnz L_test_for_end_of_block
; /* if ((op & 64) != 0) */
246 and eax, edx ; /* eax &= hold */
247 add eax, r14d
; /* eax += len */
248 mov eax, [rbp
+rax
*4] ; /* eax = lcode[val+(hold&mask[op])]*/
252 L_test_for_second_level_dist:
254 jnz L_invalid_distance_code
; /* if ((op & 64) != 0) */
260 and eax, edx ; /* eax &= hold */
261 add eax, r15d
; /* eax += dist */
262 mov eax, [r11
+rax
*4] ; /* eax = dcode[val+(hold&mask[op])]*/
267 mov ecx, eax ; /* ecx = nbytes */
268 mov eax, [rsp
+92] ; /* eax = wsize, prepare for dist cmp */
269 neg ecx ; /* nbytes = -nbytes */
272 jb L_invalid_distance_too_far
; /* if (dist > wsize) */
274 add ecx, r15d
; /* nbytes = dist - nbytes */
275 cmp dword ptr [rsp
+96], 0
276 jne L_wrap_around_window
; /* if (write != 0) */
278 mov rsi
, [rsp
+56] ; /* from = window */
279 sub eax, ecx ; /* eax -= nbytes */
280 add rsi
, rax
; /* from += wsize - nbytes */
282 mov eax, r14d
; /* eax = len */
284 jbe L_do_copy
; /* if (nbytes >= len) */
286 sub eax, ecx ; /* eax -= nbytes */
289 sub rsi
, r15
; /* from = &out[ -dist ] */
293 L_wrap_around_window:
294 mov eax, [rsp
+96] ; /* eax = write */
296 jbe L_contiguous_in_window
; /* if (write >= nbytes) */
298 mov esi, [rsp
+92] ; /* from = wsize */
299 add rsi
, [rsp
+56] ; /* from += window */
300 add rsi
, rax
; /* from += write */
301 sub rsi
, rcx
; /* from -= nbytes */
302 sub ecx, eax ; /* nbytes -= write */
304 mov eax, r14d
; /* eax = len */
306 jbe L_do_copy
; /* if (nbytes >= len) */
308 sub eax, ecx ; /* len -= nbytes */
310 mov rsi
, [rsp
+56] ; /* from = window */
311 mov ecx, [rsp
+96] ; /* nbytes = write */
313 jbe L_do_copy
; /* if (nbytes >= len) */
315 sub eax, ecx ; /* len -= nbytes */
318 sub rsi
, r15
; /* from = out - dist */
322 L_contiguous_in_window:
323 mov rsi
, [rsp
+56] ; /* rsi = window */
325 sub rsi
, rcx
; /* from += write - nbytes */
327 mov eax, r14d
; /* eax = len */
329 jbe L_do_copy
; /* if (nbytes >= len) */
331 sub eax, ecx ; /* len -= nbytes */
334 sub rsi
, r15
; /* from = out - dist */
335 jmp L_do_copy
; /* if (nbytes >= len) */
339 mov ecx, eax ; /* ecx = len */
342 mov rsi
, r8
; /* move in back to %esi, toss from */
345 L_test_for_end_of_block:
347 jz L_invalid_literal_length_code
348 mov dword ptr [rsp
+116], 1
349 jmp L_break_loop_with_status
351 L_invalid_literal_length_code:
352 mov dword ptr [rsp
+116], 2
353 jmp L_break_loop_with_status
355 L_invalid_distance_code:
356 mov dword ptr [rsp
+116], 3
357 jmp L_break_loop_with_status
359 L_invalid_distance_too_far:
360 mov dword ptr [rsp
+116], 4
361 jmp L_break_loop_with_status
364 mov dword ptr [rsp
+116], 0
366 L_break_loop_with_status:
367 ; /* put in, out, bits, and hold back into ar and pop esp */
368 mov [rsp
+16], rsi
; /* in */
369 mov [rsp
+32], rdi
; /* out */
370 mov [rsp
+88], ebx ; /* bits */
371 mov [rsp
+80], rdx
; /* hold */
373 mov rax
, [rsp
] ; /* restore rbp and rsp */
390 ; : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
391 ; "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"