-; inffasx64.asm is a hand tuned assembler version of inffast.c - fast decoding\r
-; version for AMD64 on Windows using Microsoft C compiler\r
-;\r
-; inffasx64.asm is automatically convert from AMD64 portion of inffas86.c\r
-; inffasx64.asm is called by inffas8664.c, which contain more info.\r
-\r
-\r
-; to compile this file, I use option\r
-; ml64.exe /Flinffasx64 /c /Zi inffasx64.asm\r
-; with Microsoft Macro Assembler (x64) for AMD64\r
-;\r
-\r
-; This file compile with Microsoft Macro Assembler (x64) for AMD64\r
-;\r
-; ml64.exe is given with Visual Studio 2005/2008/2010 and Windows WDK\r
-;\r
-; (you can get Windows WDK with ml64 for AMD64 from\r
-; http://www.microsoft.com/whdc/Devtools/wdk/default.mspx for low price)\r
-;\r
-\r
-\r
-.code\r
-inffas8664fnc PROC\r
-\r
-; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and\r
-; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp\r
-;\r
-; All registers must be preserved across the call, except for\r
-; rax, rcx, rdx, r8, r-9, r10, and r11, which are scratch.\r
-\r
-\r
- mov [rsp-8],rsi\r
- mov [rsp-16],rdi\r
- mov [rsp-24],r12\r
- mov [rsp-32],r13\r
- mov [rsp-40],r14\r
- mov [rsp-48],r15\r
- mov [rsp-56],rbx\r
-\r
- mov rax,rcx\r
-\r
- mov [rax+8], rbp ; /* save regs rbp and rsp */\r
- mov [rax], rsp\r
-\r
- mov rsp, rax ; /* make rsp point to &ar */\r
-\r
- mov rsi, [rsp+16] ; /* rsi = in */\r
- mov rdi, [rsp+32] ; /* rdi = out */\r
- mov r9, [rsp+24] ; /* r9 = last */\r
- mov r10, [rsp+48] ; /* r10 = end */\r
- mov rbp, [rsp+64] ; /* rbp = lcode */\r
- mov r11, [rsp+72] ; /* r11 = dcode */\r
- mov rdx, [rsp+80] ; /* rdx = hold */\r
- mov ebx, [rsp+88] ; /* ebx = bits */\r
- mov r12d, [rsp+100] ; /* r12d = lmask */\r
- mov r13d, [rsp+104] ; /* r13d = dmask */\r
- ; /* r14d = len */\r
- ; /* r15d = dist */\r
-\r
-\r
- cld\r
- cmp r10, rdi\r
- je L_one_time ; /* if only one decode left */\r
- cmp r9, rsi\r
-\r
- jne L_do_loop\r
-\r
-\r
-L_one_time:\r
- mov r8, r12 ; /* r8 = lmask */\r
- cmp bl, 32\r
- ja L_get_length_code_one_time\r
-\r
- lodsd ; /* eax = *(uint *)in++ */\r
- mov cl, bl ; /* cl = bits, needs it for shifting */\r
- add bl, 32 ; /* bits += 32 */\r
- shl rax, cl\r
- or rdx, rax ; /* hold |= *((uint *)in)++ << bits */\r
- jmp L_get_length_code_one_time\r
-\r
-ALIGN 4\r
-L_while_test:\r
- cmp r10, rdi\r
- jbe L_break_loop\r
- cmp r9, rsi\r
- jbe L_break_loop\r
-\r
-L_do_loop:\r
- mov r8, r12 ; /* r8 = lmask */\r
- cmp bl, 32\r
- ja L_get_length_code ; /* if (32 < bits) */\r
-\r
- lodsd ; /* eax = *(uint *)in++ */\r
- mov cl, bl ; /* cl = bits, needs it for shifting */\r
- add bl, 32 ; /* bits += 32 */\r
- shl rax, cl\r
- or rdx, rax ; /* hold |= *((uint *)in)++ << bits */\r
-\r
-L_get_length_code:\r
- and r8, rdx ; /* r8 &= hold */\r
- mov eax, [rbp+r8*4] ; /* eax = lcode[hold & lmask] */\r
-\r
- mov cl, ah ; /* cl = this.bits */\r
- sub bl, ah ; /* bits -= this.bits */\r
- shr rdx, cl ; /* hold >>= this.bits */\r
-\r
- test al, al\r
- jnz L_test_for_length_base ; /* if (op != 0) 45.7% */\r
-\r
- mov r8, r12 ; /* r8 = lmask */\r
- shr eax, 16 ; /* output this.val char */\r
- stosb\r
-\r
-L_get_length_code_one_time:\r
- and r8, rdx ; /* r8 &= hold */\r
- mov eax, [rbp+r8*4] ; /* eax = lcode[hold & lmask] */\r
-\r
-L_dolen:\r
- mov cl, ah ; /* cl = this.bits */\r
- sub bl, ah ; /* bits -= this.bits */\r
- shr rdx, cl ; /* hold >>= this.bits */\r
-\r
- test al, al\r
- jnz L_test_for_length_base ; /* if (op != 0) 45.7% */\r
-\r
- shr eax, 16 ; /* output this.val char */\r
- stosb\r
- jmp L_while_test\r
-\r
-ALIGN 4\r
-L_test_for_length_base:\r
- mov r14d, eax ; /* len = this */\r
- shr r14d, 16 ; /* len = this.val */\r
- mov cl, al\r
-\r
- test al, 16\r
- jz L_test_for_second_level_length ; /* if ((op & 16) == 0) 8% */\r
- and cl, 15 ; /* op &= 15 */\r
- jz L_decode_distance ; /* if (!op) */\r
-\r
-L_add_bits_to_len:\r
- sub bl, cl\r
- xor eax, eax\r
- inc eax\r
- shl eax, cl\r
- dec eax\r
- and eax, edx ; /* eax &= hold */\r
- shr rdx, cl\r
- add r14d, eax ; /* len += hold & mask[op] */\r
-\r
-L_decode_distance:\r
- mov r8, r13 ; /* r8 = dmask */\r
- cmp bl, 32\r
- ja L_get_distance_code ; /* if (32 < bits) */\r
-\r
- lodsd ; /* eax = *(uint *)in++ */\r
- mov cl, bl ; /* cl = bits, needs it for shifting */\r
- add bl, 32 ; /* bits += 32 */\r
- shl rax, cl\r
- or rdx, rax ; /* hold |= *((uint *)in)++ << bits */\r
-\r
-L_get_distance_code:\r
- and r8, rdx ; /* r8 &= hold */\r
- mov eax, [r11+r8*4] ; /* eax = dcode[hold & dmask] */\r
-\r
-L_dodist:\r
- mov r15d, eax ; /* dist = this */\r
- shr r15d, 16 ; /* dist = this.val */\r
- mov cl, ah\r
- sub bl, ah ; /* bits -= this.bits */\r
- shr rdx, cl ; /* hold >>= this.bits */\r
- mov cl, al ; /* cl = this.op */\r
-\r
- test al, 16 ; /* if ((op & 16) == 0) */\r
- jz L_test_for_second_level_dist\r
- and cl, 15 ; /* op &= 15 */\r
- jz L_check_dist_one\r
-\r
-L_add_bits_to_dist:\r
- sub bl, cl\r
- xor eax, eax\r
- inc eax\r
- shl eax, cl\r
- dec eax ; /* (1 << op) - 1 */\r
- and eax, edx ; /* eax &= hold */\r
- shr rdx, cl\r
- add r15d, eax ; /* dist += hold & ((1 << op) - 1) */\r
-\r
-L_check_window:\r
- mov r8, rsi ; /* save in so from can use it's reg */\r
- mov rax, rdi\r
- sub rax, [rsp+40] ; /* nbytes = out - beg */\r
-\r
- cmp eax, r15d\r
- jb L_clip_window ; /* if (dist > nbytes) 4.2% */\r
-\r
- mov ecx, r14d ; /* ecx = len */\r
- mov rsi, rdi\r
- sub rsi, r15 ; /* from = out - dist */\r
-\r
- sar ecx, 1\r
- jnc L_copy_two ; /* if len % 2 == 0 */\r
-\r
- rep movsw\r
- mov al, [rsi]\r
- mov [rdi], al\r
- inc rdi\r
-\r
- mov rsi, r8 ; /* move in back to %rsi, toss from */\r
- jmp L_while_test\r
-\r
-L_copy_two:\r
- rep movsw\r
- mov rsi, r8 ; /* move in back to %rsi, toss from */\r
- jmp L_while_test\r
-\r
-ALIGN 4\r
-L_check_dist_one:\r
- cmp r15d, 1 ; /* if dist 1, is a memset */\r
- jne L_check_window\r
- cmp [rsp+40], rdi ; /* if out == beg, outside window */\r
- je L_check_window\r
-\r
- mov ecx, r14d ; /* ecx = len */\r
- mov al, [rdi-1]\r
- mov ah, al\r
-\r
- sar ecx, 1\r
- jnc L_set_two\r
- mov [rdi], al\r
- inc rdi\r
-\r
-L_set_two:\r
- rep stosw\r
- jmp L_while_test\r
-\r
-ALIGN 4\r
-L_test_for_second_level_length:\r
- test al, 64\r
- jnz L_test_for_end_of_block ; /* if ((op & 64) != 0) */\r
-\r
- xor eax, eax\r
- inc eax\r
- shl eax, cl\r
- dec eax\r
- and eax, edx ; /* eax &= hold */\r
- add eax, r14d ; /* eax += len */\r
- mov eax, [rbp+rax*4] ; /* eax = lcode[val+(hold&mask[op])]*/\r
- jmp L_dolen\r
-\r
-ALIGN 4\r
-L_test_for_second_level_dist:\r
- test al, 64\r
- jnz L_invalid_distance_code ; /* if ((op & 64) != 0) */\r
-\r
- xor eax, eax\r
- inc eax\r
- shl eax, cl\r
- dec eax\r
- and eax, edx ; /* eax &= hold */\r
- add eax, r15d ; /* eax += dist */\r
- mov eax, [r11+rax*4] ; /* eax = dcode[val+(hold&mask[op])]*/\r
- jmp L_dodist\r
-\r
-ALIGN 4\r
-L_clip_window:\r
- mov ecx, eax ; /* ecx = nbytes */\r
- mov eax, [rsp+92] ; /* eax = wsize, prepare for dist cmp */\r
- neg ecx ; /* nbytes = -nbytes */\r
-\r
- cmp eax, r15d\r
- jb L_invalid_distance_too_far ; /* if (dist > wsize) */\r
-\r
- add ecx, r15d ; /* nbytes = dist - nbytes */\r
- cmp dword ptr [rsp+96], 0\r
- jne L_wrap_around_window ; /* if (write != 0) */\r
-\r
- mov rsi, [rsp+56] ; /* from = window */\r
- sub eax, ecx ; /* eax -= nbytes */\r
- add rsi, rax ; /* from += wsize - nbytes */\r
-\r
- mov eax, r14d ; /* eax = len */\r
- cmp r14d, ecx\r
- jbe L_do_copy ; /* if (nbytes >= len) */\r
-\r
- sub eax, ecx ; /* eax -= nbytes */\r
- rep movsb\r
- mov rsi, rdi\r
- sub rsi, r15 ; /* from = &out[ -dist ] */\r
- jmp L_do_copy\r
-\r
-ALIGN 4\r
-L_wrap_around_window:\r
- mov eax, [rsp+96] ; /* eax = write */\r
- cmp ecx, eax\r
- jbe L_contiguous_in_window ; /* if (write >= nbytes) */\r
-\r
- mov esi, [rsp+92] ; /* from = wsize */\r
- add rsi, [rsp+56] ; /* from += window */\r
- add rsi, rax ; /* from += write */\r
- sub rsi, rcx ; /* from -= nbytes */\r
- sub ecx, eax ; /* nbytes -= write */\r
-\r
- mov eax, r14d ; /* eax = len */\r
- cmp eax, ecx\r
- jbe L_do_copy ; /* if (nbytes >= len) */\r
-\r
- sub eax, ecx ; /* len -= nbytes */\r
- rep movsb\r
- mov rsi, [rsp+56] ; /* from = window */\r
- mov ecx, [rsp+96] ; /* nbytes = write */\r
- cmp eax, ecx\r
- jbe L_do_copy ; /* if (nbytes >= len) */\r
-\r
- sub eax, ecx ; /* len -= nbytes */\r
- rep movsb\r
- mov rsi, rdi\r
- sub rsi, r15 ; /* from = out - dist */\r
- jmp L_do_copy\r
-\r
-ALIGN 4\r
-L_contiguous_in_window:\r
- mov rsi, [rsp+56] ; /* rsi = window */\r
- add rsi, rax\r
- sub rsi, rcx ; /* from += write - nbytes */\r
-\r
- mov eax, r14d ; /* eax = len */\r
- cmp eax, ecx\r
- jbe L_do_copy ; /* if (nbytes >= len) */\r
-\r
- sub eax, ecx ; /* len -= nbytes */\r
- rep movsb\r
- mov rsi, rdi\r
- sub rsi, r15 ; /* from = out - dist */\r
- jmp L_do_copy ; /* if (nbytes >= len) */\r
-\r
-ALIGN 4\r
-L_do_copy:\r
- mov ecx, eax ; /* ecx = len */\r
- rep movsb\r
-\r
- mov rsi, r8 ; /* move in back to %esi, toss from */\r
- jmp L_while_test\r
-\r
-L_test_for_end_of_block:\r
- test al, 32\r
- jz L_invalid_literal_length_code\r
- mov dword ptr [rsp+116], 1\r
- jmp L_break_loop_with_status\r
-\r
-L_invalid_literal_length_code:\r
- mov dword ptr [rsp+116], 2\r
- jmp L_break_loop_with_status\r
-\r
-L_invalid_distance_code:\r
- mov dword ptr [rsp+116], 3\r
- jmp L_break_loop_with_status\r
-\r
-L_invalid_distance_too_far:\r
- mov dword ptr [rsp+116], 4\r
- jmp L_break_loop_with_status\r
-\r
-L_break_loop:\r
- mov dword ptr [rsp+116], 0\r
-\r
-L_break_loop_with_status:\r
-; /* put in, out, bits, and hold back into ar and pop esp */\r
- mov [rsp+16], rsi ; /* in */\r
- mov [rsp+32], rdi ; /* out */\r
- mov [rsp+88], ebx ; /* bits */\r
- mov [rsp+80], rdx ; /* hold */\r
-\r
- mov rax, [rsp] ; /* restore rbp and rsp */\r
- mov rbp, [rsp+8]\r
- mov rsp, rax\r
-\r
-\r
-\r
- mov rsi,[rsp-8]\r
- mov rdi,[rsp-16]\r
- mov r12,[rsp-24]\r
- mov r13,[rsp-32]\r
- mov r14,[rsp-40]\r
- mov r15,[rsp-48]\r
- mov rbx,[rsp-56]\r
-\r
- ret 0\r
-; :\r
-; : "m" (ar)\r
-; : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",\r
-; "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"\r
-; );\r
-\r
-inffas8664fnc ENDP\r
-;_TEXT ENDS\r
-END\r