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
12 ; ml64.exe is given with Visual Studio 2005, Windows 2003 server DDK
14 ; (you can get Windows 2003 server DDK with ml64 and cl.exe for AMD64 from
15 ; http://www.microsoft.com/whdc/devtools/ddk/default.mspx for low price)
21 ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
22 ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
24 ; All registers must be preserved across the call, except for
25 ; rax, rcx, rdx, r8, r-9, r10, and r11, which are scratch.
38 mov [rax
+8], rbp
; /* save regs rbp and rsp */
41 mov rsp
, rax
; /* make rsp point to &ar */
43 mov rsi
, [rsp
+16] ; /* rsi = in */
44 mov rdi
, [rsp
+32] ; /* rdi = out */
45 mov r9
, [rsp
+24] ; /* r9 = last */
46 mov r10
, [rsp
+48] ; /* r10 = end */
47 mov rbp
, [rsp
+64] ; /* rbp = lcode */
48 mov r11
, [rsp
+72] ; /* r11 = dcode */
49 mov rdx
, [rsp
+80] ; /* rdx = hold */
50 mov ebx, [rsp
+88] ; /* ebx = bits */
51 mov r12d
, [rsp
+100] ; /* r12d = lmask */
52 mov r13d
, [rsp
+104] ; /* r13d = dmask */
59 je L_one_time
; /* if only one decode left */
66 mov r8
, r12
; /* r8 = lmask */
68 ja L_get_length_code_one_time
70 lodsd ; /* eax = *(uint *)in++ */
71 mov cl, bl ; /* cl = bits, needs it for shifting */
72 add bl, 32 ; /* bits += 32 */
74 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
75 jmp L_get_length_code_one_time
85 mov r8
, r12
; /* r8 = lmask */
87 ja L_get_length_code
; /* if (32 < bits) */
89 lodsd ; /* eax = *(uint *)in++ */
90 mov cl, bl ; /* cl = bits, needs it for shifting */
91 add bl, 32 ; /* bits += 32 */
93 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
96 and r8
, rdx
; /* r8 &= hold */
97 mov eax, [rbp
+r8
*4] ; /* eax = lcode[hold & lmask] */
99 mov cl, ah ; /* cl = this.bits */
100 sub bl, ah ; /* bits -= this.bits */
101 shr rdx
, cl ; /* hold >>= this.bits */
104 jnz L_test_for_length_base
; /* if (op != 0) 45.7% */
106 mov r8
, r12
; /* r8 = lmask */
107 shr eax, 16 ; /* output this.val char */
110 L_get_length_code_one_time:
111 and r8
, rdx
; /* r8 &= hold */
112 mov eax, [rbp
+r8
*4] ; /* eax = lcode[hold & lmask] */
115 mov cl, ah ; /* cl = this.bits */
116 sub bl, ah ; /* bits -= this.bits */
117 shr rdx
, cl ; /* hold >>= this.bits */
120 jnz L_test_for_length_base
; /* if (op != 0) 45.7% */
122 shr eax, 16 ; /* output this.val char */
127 L_test_for_length_base:
128 mov r14d
, eax ; /* len = this */
129 shr r14d
, 16 ; /* len = this.val */
133 jz L_test_for_second_level_length
; /* if ((op & 16) == 0) 8% */
134 and cl, 15 ; /* op &= 15 */
135 jz L_decode_distance
; /* if (!op) */
143 and eax, edx ; /* eax &= hold */
145 add r14d
, eax ; /* len += hold & mask[op] */
148 mov r8
, r13
; /* r8 = dmask */
150 ja L_get_distance_code
; /* if (32 < bits) */
152 lodsd ; /* eax = *(uint *)in++ */
153 mov cl, bl ; /* cl = bits, needs it for shifting */
154 add bl, 32 ; /* bits += 32 */
156 or rdx
, rax
; /* hold |= *((uint *)in)++ << bits */
159 and r8
, rdx
; /* r8 &= hold */
160 mov eax, [r11
+r8
*4] ; /* eax = dcode[hold & dmask] */
163 mov r15d
, eax ; /* dist = this */
164 shr r15d
, 16 ; /* dist = this.val */
166 sub bl, ah ; /* bits -= this.bits */
167 shr rdx
, cl ; /* hold >>= this.bits */
168 mov cl, al ; /* cl = this.op */
170 test al, 16 ; /* if ((op & 16) == 0) */
171 jz L_test_for_second_level_dist
172 and cl, 15 ; /* op &= 15 */
180 dec eax ; /* (1 << op) - 1 */
181 and eax, edx ; /* eax &= hold */
183 add r15d
, eax ; /* dist += hold & ((1 << op) - 1) */
186 mov r8
, rsi
; /* save in so from can use it's reg */
188 sub rax
, [rsp
+40] ; /* nbytes = out - beg */
191 jb L_clip_window
; /* if (dist > nbytes) 4.2% */
193 mov ecx, r14d
; /* ecx = len */
195 sub rsi
, r15
; /* from = out - dist */
198 jnc L_copy_two
; /* if len % 2 == 0 */
205 mov rsi
, r8
; /* move in back to %rsi, toss from */
210 mov rsi
, r8
; /* move in back to %rsi, toss from */
215 cmp r15d
, 1 ; /* if dist 1, is a memset */
217 cmp [rsp
+40], rdi
; /* if out == beg, outside window */
220 mov ecx, r14d
; /* ecx = len */
234 L_test_for_second_level_length:
236 jnz L_test_for_end_of_block
; /* if ((op & 64) != 0) */
242 and eax, edx ; /* eax &= hold */
243 add eax, r14d
; /* eax += len */
244 mov eax, [rbp
+rax
*4] ; /* eax = lcode[val+(hold&mask[op])]*/
248 L_test_for_second_level_dist:
250 jnz L_invalid_distance_code
; /* if ((op & 64) != 0) */
256 and eax, edx ; /* eax &= hold */
257 add eax, r15d
; /* eax += dist */
258 mov eax, [r11
+rax
*4] ; /* eax = dcode[val+(hold&mask[op])]*/
263 mov ecx, eax ; /* ecx = nbytes */
264 mov eax, [rsp
+92] ; /* eax = wsize, prepare for dist cmp */
265 neg ecx ; /* nbytes = -nbytes */
268 jb L_invalid_distance_too_far
; /* if (dist > wsize) */
270 add ecx, r15d
; /* nbytes = dist - nbytes */
271 cmp dword ptr [rsp
+96], 0
272 jne L_wrap_around_window
; /* if (write != 0) */
274 mov rsi
, [rsp
+56] ; /* from = window */
275 sub eax, ecx ; /* eax -= nbytes */
276 add rsi
, rax
; /* from += wsize - nbytes */
278 mov eax, r14d
; /* eax = len */
280 jbe L_do_copy
; /* if (nbytes >= len) */
282 sub eax, ecx ; /* eax -= nbytes */
285 sub rsi
, r15
; /* from = &out[ -dist ] */
289 L_wrap_around_window:
290 mov eax, [rsp
+96] ; /* eax = write */
292 jbe L_contiguous_in_window
; /* if (write >= nbytes) */
294 mov esi, [rsp
+92] ; /* from = wsize */
295 add rsi
, [rsp
+56] ; /* from += window */
296 add rsi
, rax
; /* from += write */
297 sub rsi
, rcx
; /* from -= nbytes */
298 sub ecx, eax ; /* nbytes -= write */
300 mov eax, r14d
; /* eax = len */
302 jbe L_do_copy
; /* if (nbytes >= len) */
304 sub eax, ecx ; /* len -= nbytes */
306 mov rsi
, [rsp
+56] ; /* from = window */
307 mov ecx, [rsp
+96] ; /* nbytes = write */
309 jbe L_do_copy
; /* if (nbytes >= len) */
311 sub eax, ecx ; /* len -= nbytes */
314 sub rsi
, r15
; /* from = out - dist */
318 L_contiguous_in_window:
319 mov rsi
, [rsp
+56] ; /* rsi = window */
321 sub rsi
, rcx
; /* from += write - nbytes */
323 mov eax, r14d
; /* eax = len */
325 jbe L_do_copy
; /* if (nbytes >= len) */
327 sub eax, ecx ; /* len -= nbytes */
330 sub rsi
, r15
; /* from = out - dist */
331 jmp L_do_copy
; /* if (nbytes >= len) */
335 mov ecx, eax ; /* ecx = len */
338 mov rsi
, r8
; /* move in back to %esi, toss from */
341 L_test_for_end_of_block:
343 jz L_invalid_literal_length_code
344 mov dword ptr [rsp
+116], 1
345 jmp L_break_loop_with_status
347 L_invalid_literal_length_code:
348 mov dword ptr [rsp
+116], 2
349 jmp L_break_loop_with_status
351 L_invalid_distance_code:
352 mov dword ptr [rsp
+116], 3
353 jmp L_break_loop_with_status
355 L_invalid_distance_too_far:
356 mov dword ptr [rsp
+116], 4
357 jmp L_break_loop_with_status
360 mov dword ptr [rsp
+116], 0
362 L_break_loop_with_status:
363 ; /* put in, out, bits, and hold back into ar and pop esp */
364 mov [rsp
+16], rsi
; /* in */
365 mov [rsp
+32], rdi
; /* out */
366 mov [rsp
+88], ebx ; /* bits */
367 mov [rsp
+80], rdx
; /* hold */
369 mov rax
, [rsp
] ; /* restore rbp and rsp */
386 ; : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
387 ; "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"