--- /dev/null
+;uInt longest_match_x64(\r
+; deflate_state *s,\r
+; IPos cur_match); /* current match */\r
+\r
+; gvmat64.asm -- Asm portion of the optimized longest_match for 32 bits x86_64\r
+; (AMD64 on Athlon 64, Opteron, Phenom\r
+; and Intel EM64T on Pentium 4 with EM64T, Pentium D, Core 2 Duo, Core I5/I7)\r
+; Copyright (C) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.\r
+;\r
+; File written by Gilles Vollant, by converting to assembly the longest_match\r
+; from Jean-loup Gailly in deflate.c of zLib and infoZip zip.\r
+;\r
+; and by taking inspiration on asm686 with masm, optimised assembly code\r
+; from Brian Raiter, written 1998\r
+;\r
+; This software is provided 'as-is', without any express or implied\r
+; warranty. In no event will the authors be held liable for any damages\r
+; arising from the use of this software.\r
+;\r
+; Permission is granted to anyone to use this software for any purpose,\r
+; including commercial applications, and to alter it and redistribute it\r
+; freely, subject to the following restrictions:\r
+;\r
+; 1. The origin of this software must not be misrepresented; you must not\r
+; claim that you wrote the original software. If you use this software\r
+; in a product, an acknowledgment in the product documentation would be\r
+; appreciated but is not required.\r
+; 2. Altered source versions must be plainly marked as such, and must not be\r
+; misrepresented as being the original software\r
+; 3. This notice may not be removed or altered from any source distribution.\r
+;\r
+;\r
+;\r
+; http://www.zlib.net\r
+; http://www.winimage.com/zLibDll\r
+; http://www.muppetlabs.com/~breadbox/software/assembly.html\r
+;\r
+; to compile this file for infozip Zip, I use option:\r
+; ml64.exe /Flgvmat64 /c /Zi /DINFOZIP gvmat64.asm\r
+;\r
+; to compile this file for zLib, I use option:\r
+; ml64.exe /Flgvmat64 /c /Zi gvmat64.asm\r
+; Be carrefull to adapt zlib1222add below to your version of zLib\r
+; (if you use a version of zLib before 1.0.4 or after 1.2.2.2, change\r
+; value of zlib1222add later)\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
+;uInt longest_match(s, cur_match)\r
+; deflate_state *s;\r
+; IPos cur_match; /* current match */\r
+.code\r
+longest_match PROC\r
+\r
+\r
+;LocalVarsSize equ 88\r
+ LocalVarsSize equ 72\r
+\r
+; register used : rax,rbx,rcx,rdx,rsi,rdi,r8,r9,r10,r11,r12\r
+; free register : r14,r15\r
+; register can be saved : rsp\r
+\r
+ chainlenwmask equ rsp + 8 - LocalVarsSize ; high word: current chain len\r
+ ; low word: s->wmask\r
+;window equ rsp + xx - LocalVarsSize ; local copy of s->window ; stored in r10\r
+;windowbestlen equ rsp + xx - LocalVarsSize ; s->window + bestlen , use r10+r11\r
+;scanstart equ rsp + xx - LocalVarsSize ; first two bytes of string ; stored in r12w\r
+;scanend equ rsp + xx - LocalVarsSize ; last two bytes of string use ebx\r
+;scanalign equ rsp + xx - LocalVarsSize ; dword-misalignment of string r13\r
+;bestlen equ rsp + xx - LocalVarsSize ; size of best match so far -> r11d\r
+;scan equ rsp + xx - LocalVarsSize ; ptr to string wanting match -> r9\r
+IFDEF INFOZIP\r
+ELSE\r
+ nicematch equ (rsp + 16 - LocalVarsSize) ; a good enough match size\r
+ENDIF\r
+\r
+save_rdi equ rsp + 24 - LocalVarsSize\r
+save_rsi equ rsp + 32 - LocalVarsSize\r
+save_rbx equ rsp + 40 - LocalVarsSize\r
+save_rbp equ rsp + 48 - LocalVarsSize\r
+save_r12 equ rsp + 56 - LocalVarsSize\r
+save_r13 equ rsp + 64 - LocalVarsSize\r
+;save_r14 equ rsp + 72 - LocalVarsSize\r
+;save_r15 equ rsp + 80 - LocalVarsSize\r
+\r
+\r
+; summary of register usage\r
+; scanend ebx\r
+; scanendw bx\r
+; chainlenwmask edx\r
+; curmatch rsi\r
+; curmatchd esi\r
+; windowbestlen r8\r
+; scanalign r9\r
+; scanalignd r9d\r
+; window r10\r
+; bestlen r11\r
+; bestlend r11d\r
+; scanstart r12d\r
+; scanstartw r12w\r
+; scan r13\r
+; nicematch r14d\r
+; limit r15\r
+; limitd r15d\r
+; prev rcx\r
+\r
+; all the +4 offsets are due to the addition of pending_buf_size (in zlib\r
+; in the deflate_state structure since the asm code was first written\r
+; (if you compile with zlib 1.0.4 or older, remove the +4).\r
+; Note : these value are good with a 8 bytes boundary pack structure\r
+\r
+\r
+ MAX_MATCH equ 258\r
+ MIN_MATCH equ 3\r
+ MIN_LOOKAHEAD equ (MAX_MATCH+MIN_MATCH+1)\r
+\r
+\r
+;;; Offsets for fields in the deflate_state structure. These numbers\r
+;;; are calculated from the definition of deflate_state, with the\r
+;;; assumption that the compiler will dword-align the fields. (Thus,\r
+;;; changing the definition of deflate_state could easily cause this\r
+;;; program to crash horribly, without so much as a warning at\r
+;;; compile time. Sigh.)\r
+\r
+; all the +zlib1222add offsets are due to the addition of fields\r
+; in zlib in the deflate_state structure since the asm code was first written\r
+; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").\r
+; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").\r
+; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").\r
+\r
+\r
+IFDEF INFOZIP\r
+\r
+_DATA SEGMENT\r
+COMM window_size:DWORD\r
+; WMask ; 7fff\r
+COMM window:BYTE:010040H\r
+COMM prev:WORD:08000H\r
+; MatchLen : unused\r
+; PrevMatch : unused\r
+COMM strstart:DWORD\r
+COMM match_start:DWORD\r
+; Lookahead : ignore\r
+COMM prev_length:DWORD ; PrevLen\r
+COMM max_chain_length:DWORD\r
+COMM good_match:DWORD\r
+COMM nice_match:DWORD\r
+prev_ad equ OFFSET prev\r
+window_ad equ OFFSET window\r
+nicematch equ nice_match\r
+_DATA ENDS\r
+WMask equ 07fffh\r
+\r
+ELSE\r
+\r
+ IFNDEF zlib1222add\r
+ zlib1222add equ 8\r
+ ENDIF\r
+dsWSize equ 56+zlib1222add+(zlib1222add/2)\r
+dsWMask equ 64+zlib1222add+(zlib1222add/2)\r
+dsWindow equ 72+zlib1222add\r
+dsPrev equ 88+zlib1222add\r
+dsMatchLen equ 128+zlib1222add\r
+dsPrevMatch equ 132+zlib1222add\r
+dsStrStart equ 140+zlib1222add\r
+dsMatchStart equ 144+zlib1222add\r
+dsLookahead equ 148+zlib1222add\r
+dsPrevLen equ 152+zlib1222add\r
+dsMaxChainLen equ 156+zlib1222add\r
+dsGoodMatch equ 172+zlib1222add\r
+dsNiceMatch equ 176+zlib1222add\r
+\r
+window_size equ [ rcx + dsWSize]\r
+WMask equ [ rcx + dsWMask]\r
+window_ad equ [ rcx + dsWindow]\r
+prev_ad equ [ rcx + dsPrev]\r
+strstart equ [ rcx + dsStrStart]\r
+match_start equ [ rcx + dsMatchStart]\r
+Lookahead equ [ rcx + dsLookahead] ; 0ffffffffh on infozip\r
+prev_length equ [ rcx + dsPrevLen]\r
+max_chain_length equ [ rcx + dsMaxChainLen]\r
+good_match equ [ rcx + dsGoodMatch]\r
+nice_match equ [ rcx + dsNiceMatch]\r
+ENDIF\r
+\r
+; parameter 1 in r8(deflate state s), param 2 in rdx (cur match)\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, r9, r10, and r11, which are scratch.\r
+\r
+\r
+\r
+;;; Save registers that the compiler may be using, and adjust esp to\r
+;;; make room for our stack frame.\r
+\r
+\r
+;;; Retrieve the function arguments. r8d will hold cur_match\r
+;;; throughout the entire function. edx will hold the pointer to the\r
+;;; deflate_state structure during the function's setup (before\r
+;;; entering the main loop.\r
+\r
+; parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match)\r
+\r
+; this clear high 32 bits of r8, which can be garbage in both r8 and rdx\r
+\r
+ mov [save_rdi],rdi\r
+ mov [save_rsi],rsi\r
+ mov [save_rbx],rbx\r
+ mov [save_rbp],rbp\r
+IFDEF INFOZIP\r
+ mov r8d,ecx\r
+ELSE\r
+ mov r8d,edx\r
+ENDIF\r
+ mov [save_r12],r12\r
+ mov [save_r13],r13\r
+; mov [save_r14],r14\r
+; mov [save_r15],r15\r
+\r
+\r
+;;; uInt wmask = s->w_mask;\r
+;;; unsigned chain_length = s->max_chain_length;\r
+;;; if (s->prev_length >= s->good_match) {\r
+;;; chain_length >>= 2;\r
+;;; }\r
+\r
+ mov edi, prev_length\r
+ mov esi, good_match\r
+ mov eax, WMask\r
+ mov ebx, max_chain_length\r
+ cmp edi, esi\r
+ jl LastMatchGood\r
+ shr ebx, 2\r
+LastMatchGood:\r
+\r
+;;; chainlen is decremented once beforehand so that the function can\r
+;;; use the sign flag instead of the zero flag for the exit test.\r
+;;; It is then shifted into the high word, to make room for the wmask\r
+;;; value, which it will always accompany.\r
+\r
+ dec ebx\r
+ shl ebx, 16\r
+ or ebx, eax\r
+\r
+;;; on zlib only\r
+;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;\r
+\r
+IFDEF INFOZIP\r
+ mov [chainlenwmask], ebx\r
+; on infozip nice_match = [nice_match]\r
+ELSE\r
+ mov eax, nice_match\r
+ mov [chainlenwmask], ebx\r
+ mov r10d, Lookahead\r
+ cmp r10d, eax\r
+ cmovnl r10d, eax\r
+ mov [nicematch],r10d\r
+ENDIF\r
+\r
+;;; register Bytef *scan = s->window + s->strstart;\r
+ mov r10, window_ad\r
+ mov ebp, strstart\r
+ lea r13, [r10 + rbp]\r
+\r
+;;; Determine how many bytes the scan ptr is off from being\r
+;;; dword-aligned.\r
+\r
+ mov r9,r13\r
+ neg r13\r
+ and r13,3\r
+\r
+;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?\r
+;;; s->strstart - (IPos)MAX_DIST(s) : NIL;\r
+IFDEF INFOZIP\r
+ mov eax,07efah ; MAX_DIST = (WSIZE-MIN_LOOKAHEAD) (0x8000-(3+8+1))\r
+ELSE\r
+ mov eax, window_size\r
+ sub eax, MIN_LOOKAHEAD\r
+ENDIF\r
+ xor edi,edi\r
+ sub ebp, eax\r
+\r
+ mov r11d, prev_length\r
+\r
+ cmovng ebp,edi\r
+\r
+;;; int best_len = s->prev_length;\r
+\r
+\r
+;;; Store the sum of s->window + best_len in esi locally, and in esi.\r
+\r
+ lea rsi,[r10+r11]\r
+\r
+;;; register ush scan_start = *(ushf*)scan;\r
+;;; register ush scan_end = *(ushf*)(scan+best_len-1);\r
+;;; Posf *prev = s->prev;\r
+\r
+ movzx r12d,word ptr [r9]\r
+ movzx ebx, word ptr [r9 + r11 - 1]\r
+\r
+ mov rdi, prev_ad\r
+\r
+;;; Jump into the main loop.\r
+\r
+ mov edx, [chainlenwmask]\r
+\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ jz LookupLoopIsZero\r
+\r
+LookupLoop1:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ jbe LeaveNow\r
+ sub edx, 00010000h\r
+ js LeaveNow\r
+\r
+LoopEntry1:\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ jz LookupLoopIsZero\r
+\r
+LookupLoop2:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ jbe LeaveNow\r
+ sub edx, 00010000h\r
+ js LeaveNow\r
+\r
+LoopEntry2:\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ jz LookupLoopIsZero\r
+\r
+LookupLoop4:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ jbe LeaveNow\r
+ sub edx, 00010000h\r
+ js LeaveNow\r
+\r
+LoopEntry4:\r
+\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ jnz LookupLoop1\r
+ jmp LookupLoopIsZero\r
+\r
+\r
+;;; do {\r
+;;; match = s->window + cur_match;\r
+;;; if (*(ushf*)(match+best_len-1) != scan_end ||\r
+;;; *(ushf*)match != scan_start) continue;\r
+;;; [...]\r
+;;; } while ((cur_match = prev[cur_match & wmask]) > limit\r
+;;; && --chain_length != 0);\r
+;;;\r
+;;; Here is the inner loop of the function. The function will spend the\r
+;;; majority of its time in this loop, and majority of that time will\r
+;;; be spent in the first ten instructions.\r
+;;;\r
+;;; Within this loop:\r
+;;; ebx = scanend\r
+;;; r8d = curmatch\r
+;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)\r
+;;; esi = windowbestlen - i.e., (window + bestlen)\r
+;;; edi = prev\r
+;;; ebp = limit\r
+\r
+LookupLoop:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ jbe LeaveNow\r
+ sub edx, 00010000h\r
+ js LeaveNow\r
+\r
+LoopEntry:\r
+\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ jnz LookupLoop1\r
+LookupLoopIsZero:\r
+ cmp r12w, word ptr [r10 + r8]\r
+ jnz LookupLoop1\r
+\r
+\r
+;;; Store the current value of chainlen.\r
+ mov [chainlenwmask], edx\r
+\r
+;;; Point edi to the string under scrutiny, and esi to the string we\r
+;;; are hoping to match it up with. In actuality, esi and edi are\r
+;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is\r
+;;; initialized to -(MAX_MATCH_8 - scanalign).\r
+\r
+ lea rsi,[r8+r10]\r
+ mov rdx, 0fffffffffffffef8h; -(MAX_MATCH_8)\r
+ lea rsi, [rsi + r13 + 0108h] ;MAX_MATCH_8]\r
+ lea rdi, [r9 + r13 + 0108h] ;MAX_MATCH_8]\r
+\r
+ prefetcht1 [rsi+rdx]\r
+ prefetcht1 [rdi+rdx]\r
+\r
+\r
+;;; Test the strings for equality, 8 bytes at a time. At the end,\r
+;;; adjust rdx so that it is offset to the exact byte that mismatched.\r
+;;;\r
+;;; We already know at this point that the first three bytes of the\r
+;;; strings match each other, and they can be safely passed over before\r
+;;; starting the compare loop. So what this code does is skip over 0-3\r
+;;; bytes, as much as necessary in order to dword-align the edi\r
+;;; pointer. (rsi will still be misaligned three times out of four.)\r
+;;;\r
+;;; It should be confessed that this loop usually does not represent\r
+;;; much of the total running time. Replacing it with a more\r
+;;; straightforward "rep cmpsb" would not drastically degrade\r
+;;; performance.\r
+\r
+\r
+LoopCmps:\r
+ mov rax, [rsi + rdx]\r
+ xor rax, [rdi + rdx]\r
+ jnz LeaveLoopCmps\r
+\r
+ mov rax, [rsi + rdx + 8]\r
+ xor rax, [rdi + rdx + 8]\r
+ jnz LeaveLoopCmps8\r
+\r
+\r
+ mov rax, [rsi + rdx + 8+8]\r
+ xor rax, [rdi + rdx + 8+8]\r
+ jnz LeaveLoopCmps16\r
+\r
+ add rdx,8+8+8\r
+\r
+ jnz short LoopCmps\r
+ jmp short LenMaximum\r
+LeaveLoopCmps16: add rdx,8\r
+LeaveLoopCmps8: add rdx,8\r
+LeaveLoopCmps:\r
+\r
+ test eax, 0000FFFFh\r
+ jnz LenLower\r
+\r
+ test eax,0ffffffffh\r
+\r
+ jnz LenLower32\r
+\r
+ add rdx,4\r
+ shr rax,32\r
+ or ax,ax\r
+ jnz LenLower\r
+\r
+LenLower32:\r
+ shr eax,16\r
+ add rdx,2\r
+LenLower: sub al, 1\r
+ adc rdx, 0\r
+;;; Calculate the length of the match. If it is longer than MAX_MATCH,\r
+;;; then automatically accept it as the best possible match and leave.\r
+\r
+ lea rax, [rdi + rdx]\r
+ sub rax, r9\r
+ cmp eax, MAX_MATCH\r
+ jge LenMaximum\r
+\r
+;;; If the length of the match is not longer than the best match we\r
+;;; have so far, then forget it and return to the lookup loop.\r
+;///////////////////////////////////\r
+\r
+ cmp eax, r11d\r
+ jg LongerMatch\r
+\r
+ lea rsi,[r10+r11]\r
+\r
+ mov rdi, prev_ad\r
+ mov edx, [chainlenwmask]\r
+ jmp LookupLoop\r
+\r
+;;; s->match_start = cur_match;\r
+;;; best_len = len;\r
+;;; if (len >= nice_match) break;\r
+;;; scan_end = *(ushf*)(scan+best_len-1);\r
+\r
+LongerMatch:\r
+ mov r11d, eax\r
+ mov match_start, r8d\r
+ cmp eax, [nicematch]\r
+ jge LeaveNow\r
+\r
+ lea rsi,[r10+rax]\r
+\r
+ movzx ebx, word ptr [r9 + rax - 1]\r
+ mov rdi, prev_ad\r
+ mov edx, [chainlenwmask]\r
+ jmp LookupLoop\r
+\r
+;;; Accept the current string, with the maximum possible length.\r
+\r
+LenMaximum:\r
+ mov r11d,MAX_MATCH\r
+ mov match_start, r8d\r
+\r
+;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len;\r
+;;; return s->lookahead;\r
+\r
+LeaveNow:\r
+IFDEF INFOZIP\r
+ mov eax,r11d\r
+ELSE\r
+ mov eax, Lookahead\r
+ cmp r11d, eax\r
+ cmovng eax, r11d\r
+ENDIF\r
+\r
+;;; Restore the stack and return from whence we came.\r
+\r
+\r
+ mov rsi,[save_rsi]\r
+ mov rdi,[save_rdi]\r
+ mov rbx,[save_rbx]\r
+ mov rbp,[save_rbp]\r
+ mov r12,[save_r12]\r
+ mov r13,[save_r13]\r
+; mov r14,[save_r14]\r
+; mov r15,[save_r15]\r
+\r
+\r
+ ret 0\r
+; please don't remove this string !\r
+; Your can freely use gvmat64 in any free or commercial app\r
+; but it is far better don't remove the string in the binary!\r
+ 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\r
+longest_match ENDP\r
+\r
+match_init PROC\r
+ ret 0\r
+match_init ENDP\r
+\r
+\r
+END\r