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