--- /dev/null
+/*\r
+;uInt longest_match_x64(\r
+; deflate_state *s,\r
+; IPos cur_match); // current match \r
+\r
+; gvmat64.S -- 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
+; this file is translation from gvmat64.asm to GCC 4.x (for Linux, Mac XCode)\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
+; 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
+; 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 zLib, I use option:\r
+; gcc -c -arch x86_64 gvmat64.S\r
+\r
+\r
+;uInt longest_match(s, cur_match)\r
+; deflate_state *s;\r
+; IPos cur_match; // current match /\r
+;\r
+; with XCode for Mac, I had strange error with some jump on intel syntax\r
+; this is why BEFORE_JMP and AFTER_JMP are used\r
+ */\r
+\r
+\r
+#define BEFORE_JMP .att_syntax\r
+#define AFTER_JMP .intel_syntax noprefix\r
+\r
+#ifndef NO_UNDERLINE\r
+# define match_init _match_init\r
+# define longest_match _longest_match\r
+#endif\r
+\r
+.intel_syntax noprefix\r
+\r
+.globl match_init, longest_match\r
+.text\r
+longest_match:\r
+\r
+\r
+\r
+#define LocalVarsSize 96\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
+\r
+#define chainlenwmask (rsp + 8 - LocalVarsSize)\r
+#define nicematch (rsp + 16 - LocalVarsSize)\r
+\r
+#define save_rdi (rsp + 24 - LocalVarsSize)\r
+#define save_rsi (rsp + 32 - LocalVarsSize)\r
+#define save_rbx (rsp + 40 - LocalVarsSize)\r
+#define save_rbp (rsp + 48 - LocalVarsSize)\r
+#define save_r12 (rsp + 56 - LocalVarsSize)\r
+#define save_r13 (rsp + 64 - LocalVarsSize)\r
+#define save_r14 (rsp + 72 - LocalVarsSize)\r
+#define save_r15 (rsp + 80 - LocalVarsSize)\r
+\r
+\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
+#define MAX_MATCH 258\r
+#define MIN_MATCH 3\r
+#define MIN_LOOKAHEAD (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
+\r
+\r
+/* you can check the structure offset by running\r
+\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+#include "deflate.h"\r
+\r
+void print_depl()\r
+{\r
+deflate_state ds;\r
+deflate_state *s=&ds;\r
+printf("size pointer=%u\n",(int)sizeof(void*));\r
+\r
+printf("#define dsWSize %u\n",(int)(((char*)&(s->w_size))-((char*)s)));\r
+printf("#define dsWMask %u\n",(int)(((char*)&(s->w_mask))-((char*)s)));\r
+printf("#define dsWindow %u\n",(int)(((char*)&(s->window))-((char*)s)));\r
+printf("#define dsPrev %u\n",(int)(((char*)&(s->prev))-((char*)s)));\r
+printf("#define dsMatchLen %u\n",(int)(((char*)&(s->match_length))-((char*)s)));\r
+printf("#define dsPrevMatch %u\n",(int)(((char*)&(s->prev_match))-((char*)s)));\r
+printf("#define dsStrStart %u\n",(int)(((char*)&(s->strstart))-((char*)s)));\r
+printf("#define dsMatchStart %u\n",(int)(((char*)&(s->match_start))-((char*)s)));\r
+printf("#define dsLookahead %u\n",(int)(((char*)&(s->lookahead))-((char*)s)));\r
+printf("#define dsPrevLen %u\n",(int)(((char*)&(s->prev_length))-((char*)s)));\r
+printf("#define dsMaxChainLen %u\n",(int)(((char*)&(s->max_chain_length))-((char*)s)));\r
+printf("#define dsGoodMatch %u\n",(int)(((char*)&(s->good_match))-((char*)s)));\r
+printf("#define dsNiceMatch %u\n",(int)(((char*)&(s->nice_match))-((char*)s)));\r
+}\r
+*/\r
+\r
+#define dsWSize 68\r
+#define dsWMask 76\r
+#define dsWindow 80\r
+#define dsPrev 96\r
+#define dsMatchLen 144\r
+#define dsPrevMatch 148\r
+#define dsStrStart 156\r
+#define dsMatchStart 160\r
+#define dsLookahead 164\r
+#define dsPrevLen 168\r
+#define dsMaxChainLen 172\r
+#define dsGoodMatch 188\r
+#define dsNiceMatch 192\r
+\r
+#define window_size [ rcx + dsWSize]\r
+#define WMask [ rcx + dsWMask]\r
+#define window_ad [ rcx + dsWindow]\r
+#define prev_ad [ rcx + dsPrev]\r
+#define strstart [ rcx + dsStrStart]\r
+#define match_start [ rcx + dsMatchStart]\r
+#define Lookahead [ rcx + dsLookahead] //; 0ffffffffh on infozip\r
+#define prev_length [ rcx + dsPrevLen]\r
+#define max_chain_length [ rcx + dsMaxChainLen]\r
+#define good_match [ rcx + dsGoodMatch]\r
+#define nice_match [ rcx + dsNiceMatch]\r
+\r
+/*\r
+; windows:\r
+; parameter 1 in rcx(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
+; gcc on macosx-linux:\r
+; see http://www.x86-64.org/documentation/abi-0.99.pdf\r
+; param 1 in rdi, param 2 in rsi\r
+; rbx, rsp, rbp, r12 to r15 must be preserved\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
+; ms: parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match)\r
+; mac: param 1 in rdi, param 2 rsi\r
+; this clear high 32 bits of r8, which can be garbage in both r8 and rdx\r
+*/\r
+ mov [save_rbx],rbx\r
+ mov [save_rbp],rbp\r
+\r
+\r
+ mov rcx,rdi\r
+\r
+ mov r8d,esi\r
+\r
+\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
+\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
+\r
+\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
+\r
+\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
+\r
+\r
+ mov eax, window_size\r
+ sub eax, MIN_LOOKAHEAD\r
+\r
+\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
+ \r
+ \r
+LookupLoop1:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ jbe LeaveNow\r
+ \r
+ \r
+ \r
+ sub edx, 0x00010000\r
+ BEFORE_JMP\r
+ js LeaveNow\r
+ AFTER_JMP\r
+\r
+LoopEntry1:\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ BEFORE_JMP\r
+ jz LookupLoopIsZero\r
+ AFTER_JMP\r
+\r
+LookupLoop2:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ BEFORE_JMP\r
+ jbe LeaveNow\r
+ AFTER_JMP\r
+ sub edx, 0x00010000\r
+ BEFORE_JMP\r
+ js LeaveNow\r
+ AFTER_JMP\r
+\r
+LoopEntry2:\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ BEFORE_JMP\r
+ jz LookupLoopIsZero\r
+ AFTER_JMP\r
+\r
+LookupLoop4:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ BEFORE_JMP\r
+ jbe LeaveNow\r
+ AFTER_JMP\r
+ sub edx, 0x00010000\r
+ BEFORE_JMP\r
+ js LeaveNow\r
+ AFTER_JMP\r
+\r
+LoopEntry4:\r
+\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ BEFORE_JMP\r
+ jnz LookupLoop1\r
+ jmp LookupLoopIsZero\r
+ AFTER_JMP\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
+.balign 16\r
+LookupLoop:\r
+ and r8d, edx\r
+\r
+ movzx r8d, word ptr [rdi + r8*2]\r
+ cmp r8d, ebp\r
+ BEFORE_JMP\r
+ jbe LeaveNow\r
+ AFTER_JMP\r
+ sub edx, 0x00010000\r
+ BEFORE_JMP\r
+ js LeaveNow\r
+ AFTER_JMP\r
+\r
+LoopEntry:\r
+\r
+ cmp bx,word ptr [rsi + r8 - 1]\r
+ BEFORE_JMP\r
+ jnz LookupLoop1\r
+ AFTER_JMP\r
+LookupLoopIsZero:\r
+ cmp r12w, word ptr [r10 + r8]\r
+ BEFORE_JMP\r
+ jnz LookupLoop1\r
+ AFTER_JMP\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, 0xfffffffffffffef8 //; -(MAX_MATCH_8)\r
+ lea rsi, [rsi + r13 + 0x0108] //;MAX_MATCH_8]\r
+ lea rdi, [r9 + r13 + 0x0108] //;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
+ BEFORE_JMP\r
+ jnz LoopCmps\r
+ jmp LenMaximum\r
+ AFTER_JMP\r
+ \r
+LeaveLoopCmps16: add rdx,8\r
+LeaveLoopCmps8: add rdx,8\r
+LeaveLoopCmps:\r
+\r
+ test eax, 0x0000FFFF\r
+ jnz LenLower\r
+\r
+ test eax,0xffffffff\r
+\r
+ jnz LenLower32\r
+\r
+ add rdx,4\r
+ shr rax,32\r
+ or ax,ax\r
+ BEFORE_JMP\r
+ jnz LenLower\r
+ AFTER_JMP\r
+\r
+LenLower32:\r
+ shr eax,16\r
+ add rdx,2\r
+ \r
+LenLower: \r
+ 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
+ BEFORE_JMP\r
+ jge LenMaximum\r
+ AFTER_JMP\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
+ BEFORE_JMP\r
+ jmp LookupLoop\r
+ AFTER_JMP\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
+ BEFORE_JMP\r
+ jge LeaveNow\r
+ AFTER_JMP\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
+ BEFORE_JMP\r
+ jmp LookupLoop\r
+ AFTER_JMP\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
+ mov eax, Lookahead\r
+ cmp r11d, eax\r
+ cmovng eax, r11d\r
+\r
+\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
+\r
+\r
+match_init:\r
+ ret 0\r
+\r
+\r