--- /dev/null
+; gvmat32.asm -- Asm portion of the optimized longest_match for 32 bits x86\r
+; Copyright (C) 1995-1996 Jean-loup Gailly and Gilles Vollant.\r
+; File written by Gilles Vollant, by modifiying the longest_match\r
+; from Jean-loup Gailly in deflate.c\r
+;\r
+; http://www.zlib.net\r
+; http://www.winimage.com/zLibDll\r
+; http://www.muppetlabs.com/~breadbox/software/assembly.html\r
+;\r
+; For Visual C++ 4.x and higher and ML 6.x and higher\r
+; ml.exe is in directory \MASM611C of Win95 DDK\r
+; ml.exe is also distributed in http://www.masm32.com/masmdl.htm\r
+; and in VC++2003 toolkit at http://msdn.microsoft.com/visualc/vctoolkit2003/\r
+;\r
+; this file contain two implementation of longest_match\r
+;\r
+; longest_match_7fff : written 1996 by Gilles Vollant optimized for \r
+; first Pentium. Assume s->w_mask == 0x7fff\r
+; longest_match_686 : written by Brian raiter (1998), optimized for Pentium Pro\r
+;\r
+; for using an seembly version of longest_match, you need define ASMV in project\r
+; There is two way in using gvmat32.asm\r
+;\r
+; A) Suggested method\r
+; if you want include both longest_match_7fff and longest_match_686\r
+; compile the asm file running\r
+; ml /coff /Zi /Flgvmat32.lst /c gvmat32.asm\r
+; and include gvmat32c.c in your project\r
+; if you have an old cpu (386,486 or first Pentium) and s->w_mask==0x7fff,\r
+; longest_match_7fff will be used\r
+; if you have a more modern CPU (Pentium Pro, II and higher)\r
+; longest_match_686 will be used\r
+; on old cpu with s->w_mask!=0x7fff, longest_match_686 will be used,\r
+; but this is not a sitation you'll find often\r
+;\r
+; B) Alternative\r
+; if you are not interresed in old cpu performance and want the smaller\r
+; binaries possible\r
+;\r
+; compile the asm file running\r
+; ml /coff /Zi /c /Flgvmat32.lst /DNOOLDPENTIUMCODE gvmat32.asm\r
+; and do not include gvmat32c.c in your project (ou define also \r
+; NOOLDPENTIUMCODE)\r
+;\r
+; note : as I known, longest_match_686 is very faster than longest_match_7fff\r
+; on pentium Pro/II/III, faster (but less) in P4, but it seem\r
+; longest_match_7fff can be faster (very very litte) on AMD Athlon64/K8\r
+;\r
+; see below : zlib1222add must be adjuster if you use a zlib version < 1.2.2.2\r
+\r
+;uInt longest_match_7fff(s, cur_match)\r
+; deflate_state *s;\r
+; IPos cur_match; /* current match */\r
+\r
+ NbStack equ 76\r
+ cur_match equ dword ptr[esp+NbStack-0]\r
+ str_s equ dword ptr[esp+NbStack-4]\r
+; 5 dword on top (ret,ebp,esi,edi,ebx)\r
+ adrret equ dword ptr[esp+NbStack-8]\r
+ pushebp equ dword ptr[esp+NbStack-12]\r
+ pushedi equ dword ptr[esp+NbStack-16]\r
+ pushesi equ dword ptr[esp+NbStack-20]\r
+ pushebx equ dword ptr[esp+NbStack-24]\r
+\r
+ chain_length equ dword ptr [esp+NbStack-28]\r
+ limit equ dword ptr [esp+NbStack-32]\r
+ best_len equ dword ptr [esp+NbStack-36]\r
+ window equ dword ptr [esp+NbStack-40]\r
+ prev equ dword ptr [esp+NbStack-44]\r
+ scan_start equ word ptr [esp+NbStack-48]\r
+ wmask equ dword ptr [esp+NbStack-52]\r
+ match_start_ptr equ dword ptr [esp+NbStack-56]\r
+ nice_match equ dword ptr [esp+NbStack-60]\r
+ scan equ dword ptr [esp+NbStack-64]\r
+\r
+ windowlen equ dword ptr [esp+NbStack-68]\r
+ match_start equ dword ptr [esp+NbStack-72]\r
+ strend equ dword ptr [esp+NbStack-76]\r
+ NbStackAdd equ (NbStack-24)\r
+\r
+ .386p\r
+\r
+ name gvmatch\r
+ .MODEL FLAT\r
+\r
+\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
+ zlib1222add equ 8\r
+\r
+; Note : these value are good with a 8 bytes boundary pack structure\r
+ dep_chain_length equ 74h+zlib1222add\r
+ dep_window equ 30h+zlib1222add\r
+ dep_strstart equ 64h+zlib1222add\r
+ dep_prev_length equ 70h+zlib1222add\r
+ dep_nice_match equ 88h+zlib1222add\r
+ dep_w_size equ 24h+zlib1222add\r
+ dep_prev equ 38h+zlib1222add\r
+ dep_w_mask equ 2ch+zlib1222add\r
+ dep_good_match equ 84h+zlib1222add\r
+ dep_match_start equ 68h+zlib1222add\r
+ dep_lookahead equ 6ch+zlib1222add\r
+\r
+\r
+_TEXT segment\r
+\r
+IFDEF NOUNDERLINE\r
+ IFDEF NOOLDPENTIUMCODE\r
+ public longest_match\r
+ public match_init\r
+ ELSE \r
+ public longest_match_7fff\r
+ public cpudetect32\r
+ public longest_match_686\r
+ ENDIF\r
+ELSE\r
+ IFDEF NOOLDPENTIUMCODE\r
+ public _longest_match\r
+ public _match_init\r
+ ELSE\r
+ public _longest_match_7fff\r
+ public _cpudetect32\r
+ public _longest_match_686\r
+ ENDIF\r
+ENDIF\r
+\r
+ MAX_MATCH equ 258\r
+ MIN_MATCH equ 3\r
+ MIN_LOOKAHEAD equ (MAX_MATCH+MIN_MATCH+1)\r
+\r
+\r
+\r
+IFNDEF NOOLDPENTIUMCODE\r
+IFDEF NOUNDERLINE\r
+longest_match_7fff proc near\r
+ELSE\r
+_longest_match_7fff proc near\r
+ENDIF\r
+\r
+ mov edx,[esp+4]\r
+\r
+\r
+\r
+ push ebp\r
+ push edi\r
+ push esi\r
+ push ebx\r
+\r
+ sub esp,NbStackAdd\r
+\r
+; initialize or check the variables used in match.asm.\r
+ mov ebp,edx\r
+\r
+; chain_length = s->max_chain_length\r
+; if (prev_length>=good_match) chain_length >>= 2\r
+ mov edx,[ebp+dep_chain_length]\r
+ mov ebx,[ebp+dep_prev_length]\r
+ cmp [ebp+dep_good_match],ebx\r
+ ja noshr\r
+ shr edx,2\r
+noshr:\r
+; we increment chain_length because in the asm, the --chain_lenght is in the beginning of the loop\r
+ inc edx\r
+ mov edi,[ebp+dep_nice_match]\r
+ mov chain_length,edx\r
+ mov eax,[ebp+dep_lookahead]\r
+ cmp eax,edi\r
+; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;\r
+ jae nolookaheadnicematch\r
+ mov edi,eax\r
+nolookaheadnicematch:\r
+; best_len = s->prev_length\r
+ mov best_len,ebx\r
+\r
+; window = s->window\r
+ mov esi,[ebp+dep_window]\r
+ mov ecx,[ebp+dep_strstart]\r
+ mov window,esi\r
+\r
+ mov nice_match,edi\r
+; scan = window + strstart\r
+ add esi,ecx\r
+ mov scan,esi\r
+; dx = *window\r
+ mov dx,word ptr [esi]\r
+; bx = *(window+best_len-1)\r
+ mov bx,word ptr [esi+ebx-1]\r
+ add esi,MAX_MATCH-1\r
+; scan_start = *scan\r
+ mov scan_start,dx\r
+; strend = scan + MAX_MATCH-1\r
+ mov strend,esi\r
+; bx = scan_end = *(window+best_len-1)\r
+\r
+; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?\r
+; s->strstart - (IPos)MAX_DIST(s) : NIL;\r
+\r
+ mov esi,[ebp+dep_w_size]\r
+ sub esi,MIN_LOOKAHEAD\r
+; here esi = MAX_DIST(s)\r
+ sub ecx,esi\r
+ ja nodist\r
+ xor ecx,ecx\r
+nodist:\r
+ mov limit,ecx\r
+\r
+; prev = s->prev\r
+ mov edx,[ebp+dep_prev]\r
+ mov prev,edx\r
+\r
+;\r
+ mov edx,dword ptr [ebp+dep_match_start]\r
+ mov bp,scan_start\r
+ mov eax,cur_match\r
+ mov match_start,edx\r
+\r
+ mov edx,window\r
+ mov edi,edx\r
+ add edi,best_len\r
+ mov esi,prev\r
+ dec edi\r
+; windowlen = window + best_len -1\r
+ mov windowlen,edi\r
+\r
+ jmp beginloop2\r
+ align 4\r
+\r
+; here, in the loop\r
+; eax = ax = cur_match\r
+; ecx = limit\r
+; bx = scan_end\r
+; bp = scan_start\r
+; edi = windowlen (window + best_len -1)\r
+; esi = prev\r
+\r
+\r
+;// here; chain_length <=16\r
+normalbeg0add16:\r
+ add chain_length,16\r
+ jz exitloop\r
+normalbeg0:\r
+ cmp word ptr[edi+eax],bx\r
+ je normalbeg2noroll\r
+rcontlabnoroll:\r
+; cur_match = prev[cur_match & wmask]\r
+ and eax,7fffh\r
+ mov ax,word ptr[esi+eax*2]\r
+; if cur_match > limit, go to exitloop\r
+ cmp ecx,eax\r
+ jnb exitloop\r
+; if --chain_length != 0, go to exitloop\r
+ dec chain_length\r
+ jnz normalbeg0\r
+ jmp exitloop\r
+\r
+normalbeg2noroll:\r
+; if (scan_start==*(cur_match+window)) goto normalbeg2\r
+ cmp bp,word ptr[edx+eax]\r
+ jne rcontlabnoroll\r
+ jmp normalbeg2\r
+\r
+contloop3:\r
+ mov edi,windowlen\r
+\r
+; cur_match = prev[cur_match & wmask]\r
+ and eax,7fffh\r
+ mov ax,word ptr[esi+eax*2]\r
+; if cur_match > limit, go to exitloop\r
+ cmp ecx,eax\r
+jnbexitloopshort1:\r
+ jnb exitloop\r
+; if --chain_length != 0, go to exitloop\r
+\r
+\r
+; begin the main loop\r
+beginloop2:\r
+ sub chain_length,16+1\r
+; if chain_length <=16, don't use the unrolled loop\r
+ jna normalbeg0add16\r
+\r
+do16:\r
+ cmp word ptr[edi+eax],bx\r
+ je normalbeg2dc0\r
+\r
+maccn MACRO lab\r
+ and eax,7fffh\r
+ mov ax,word ptr[esi+eax*2]\r
+ cmp ecx,eax\r
+ jnb exitloop\r
+ cmp word ptr[edi+eax],bx\r
+ je lab\r
+ ENDM\r
+\r
+rcontloop0:\r
+ maccn normalbeg2dc1\r
+\r
+rcontloop1:\r
+ maccn normalbeg2dc2\r
+\r
+rcontloop2:\r
+ maccn normalbeg2dc3\r
+\r
+rcontloop3:\r
+ maccn normalbeg2dc4\r
+\r
+rcontloop4:\r
+ maccn normalbeg2dc5\r
+\r
+rcontloop5:\r
+ maccn normalbeg2dc6\r
+\r
+rcontloop6:\r
+ maccn normalbeg2dc7\r
+\r
+rcontloop7:\r
+ maccn normalbeg2dc8\r
+\r
+rcontloop8:\r
+ maccn normalbeg2dc9\r
+\r
+rcontloop9:\r
+ maccn normalbeg2dc10\r
+\r
+rcontloop10:\r
+ maccn short normalbeg2dc11\r
+\r
+rcontloop11:\r
+ maccn short normalbeg2dc12\r
+\r
+rcontloop12:\r
+ maccn short normalbeg2dc13\r
+\r
+rcontloop13:\r
+ maccn short normalbeg2dc14\r
+\r
+rcontloop14:\r
+ maccn short normalbeg2dc15\r
+\r
+rcontloop15:\r
+ and eax,7fffh\r
+ mov ax,word ptr[esi+eax*2]\r
+ cmp ecx,eax\r
+ jnb exitloop\r
+\r
+ sub chain_length,16\r
+ ja do16\r
+ jmp normalbeg0add16\r
+\r
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
+\r
+normbeg MACRO rcontlab,valsub\r
+; if we are here, we know that *(match+best_len-1) == scan_end\r
+ cmp bp,word ptr[edx+eax]\r
+; if (match != scan_start) goto rcontlab\r
+ jne rcontlab\r
+; calculate the good chain_length, and we'll compare scan and match string\r
+ add chain_length,16-valsub\r
+ jmp iseq\r
+ ENDM\r
+\r
+\r
+normalbeg2dc11:\r
+ normbeg rcontloop11,11\r
+\r
+normalbeg2dc12:\r
+ normbeg short rcontloop12,12\r
+\r
+normalbeg2dc13:\r
+ normbeg short rcontloop13,13\r
+\r
+normalbeg2dc14:\r
+ normbeg short rcontloop14,14\r
+\r
+normalbeg2dc15:\r
+ normbeg short rcontloop15,15\r
+\r
+normalbeg2dc10:\r
+ normbeg rcontloop10,10\r
+\r
+normalbeg2dc9:\r
+ normbeg rcontloop9,9\r
+\r
+normalbeg2dc8:\r
+ normbeg rcontloop8,8\r
+\r
+normalbeg2dc7:\r
+ normbeg rcontloop7,7\r
+\r
+normalbeg2dc6:\r
+ normbeg rcontloop6,6\r
+\r
+normalbeg2dc5:\r
+ normbeg rcontloop5,5\r
+\r
+normalbeg2dc4:\r
+ normbeg rcontloop4,4\r
+\r
+normalbeg2dc3:\r
+ normbeg rcontloop3,3\r
+\r
+normalbeg2dc2:\r
+ normbeg rcontloop2,2\r
+\r
+normalbeg2dc1:\r
+ normbeg rcontloop1,1\r
+\r
+normalbeg2dc0:\r
+ normbeg rcontloop0,0\r
+\r
+\r
+; we go in normalbeg2 because *(ushf*)(match+best_len-1) == scan_end\r
+\r
+normalbeg2:\r
+ mov edi,window\r
+\r
+ cmp bp,word ptr[edi+eax]\r
+ jne contloop3 ; if *(ushf*)match != scan_start, continue\r
+\r
+iseq:\r
+; if we are here, we know that *(match+best_len-1) == scan_end\r
+; and (match == scan_start)\r
+\r
+ mov edi,edx\r
+ mov esi,scan ; esi = scan\r
+ add edi,eax ; edi = window + cur_match = match\r
+\r
+ mov edx,[esi+3] ; compare manually dword at match+3\r
+ xor edx,[edi+3] ; and scan +3\r
+\r
+ jz begincompare ; if equal, go to long compare\r
+\r
+; we will determine the unmatch byte and calculate len (in esi)\r
+ or dl,dl\r
+ je eq1rr\r
+ mov esi,3\r
+ jmp trfinval\r
+eq1rr:\r
+ or dx,dx\r
+ je eq1\r
+\r
+ mov esi,4\r
+ jmp trfinval\r
+eq1:\r
+ and edx,0ffffffh\r
+ jz eq11\r
+ mov esi,5\r
+ jmp trfinval\r
+eq11:\r
+ mov esi,6\r
+ jmp trfinval\r
+\r
+begincompare:\r
+ ; here we now scan and match begin same\r
+ add edi,6\r
+ add esi,6\r
+ mov ecx,(MAX_MATCH-(2+4))/4 ; scan for at most MAX_MATCH bytes\r
+ repe cmpsd ; loop until mismatch\r
+\r
+ je trfin ; go to trfin if not unmatch\r
+; we determine the unmatch byte\r
+ sub esi,4\r
+ mov edx,[edi-4]\r
+ xor edx,[esi]\r
+\r
+ or dl,dl\r
+ jnz trfin\r
+ inc esi\r
+\r
+ or dx,dx\r
+ jnz trfin\r
+ inc esi\r
+\r
+ and edx,0ffffffh\r
+ jnz trfin\r
+ inc esi\r
+\r
+trfin:\r
+ sub esi,scan ; esi = len\r
+trfinval:\r
+; here we have finised compare, and esi contain len of equal string\r
+ cmp esi,best_len ; if len > best_len, go newbestlen\r
+ ja short newbestlen\r
+; now we restore edx, ecx and esi, for the big loop\r
+ mov esi,prev\r
+ mov ecx,limit\r
+ mov edx,window\r
+ jmp contloop3\r
+\r
+newbestlen:\r
+ mov best_len,esi ; len become best_len\r
+\r
+ mov match_start,eax ; save new position as match_start\r
+ cmp esi,nice_match ; if best_len >= nice_match, exit\r
+ jae exitloop\r
+ mov ecx,scan\r
+ mov edx,window ; restore edx=window\r
+ add ecx,esi\r
+ add esi,edx\r
+\r
+ dec esi\r
+ mov windowlen,esi ; windowlen = window + best_len-1\r
+ mov bx,[ecx-1] ; bx = *(scan+best_len-1) = scan_end\r
+\r
+; now we restore ecx and esi, for the big loop :\r
+ mov esi,prev\r
+ mov ecx,limit\r
+ jmp contloop3\r
+\r
+exitloop:\r
+; exit : s->match_start=match_start\r
+ mov ebx,match_start\r
+ mov ebp,str_s\r
+ mov ecx,best_len\r
+ mov dword ptr [ebp+dep_match_start],ebx\r
+ mov eax,dword ptr [ebp+dep_lookahead]\r
+ cmp ecx,eax\r
+ ja minexlo\r
+ mov eax,ecx\r
+minexlo:\r
+; return min(best_len,s->lookahead)\r
+\r
+; restore stack and register ebx,esi,edi,ebp\r
+ add esp,NbStackAdd\r
+\r
+ pop ebx\r
+ pop esi\r
+ pop edi\r
+ pop ebp\r
+ ret\r
+InfoAuthor:\r
+; please don't remove this string !\r
+; Your are free use gvmat32 in any fre or commercial apps if you don't remove the string in the binary!\r
+ db 0dh,0ah,"GVMat32 optimised assembly code written 1996-98 by Gilles Vollant",0dh,0ah\r
+\r
+\r
+\r
+IFDEF NOUNDERLINE\r
+longest_match_7fff endp\r
+ELSE\r
+_longest_match_7fff endp\r
+ENDIF\r
+\r
+\r
+IFDEF NOUNDERLINE\r
+cpudetect32 proc near\r
+ELSE\r
+_cpudetect32 proc near\r
+ENDIF\r
+\r
+ push ebx\r
+\r
+ pushfd ; push original EFLAGS\r
+ pop eax ; get original EFLAGS\r
+ mov ecx, eax ; save original EFLAGS\r
+ xor eax, 40000h ; flip AC bit in EFLAGS\r
+ push eax ; save new EFLAGS value on stack\r
+ popfd ; replace current EFLAGS value\r
+ pushfd ; get new EFLAGS\r
+ pop eax ; store new EFLAGS in EAX\r
+ xor eax, ecx ; can\92t toggle AC bit, processor=80386\r
+ jz end_cpu_is_386 ; jump if 80386 processor\r
+ push ecx\r
+ popfd ; restore AC bit in EFLAGS first\r
+\r
+ pushfd\r
+ pushfd\r
+ pop ecx\r
+\r
+ mov eax, ecx ; get original EFLAGS\r
+ xor eax, 200000h ; flip ID bit in EFLAGS\r
+ push eax ; save new EFLAGS value on stack\r
+ popfd ; replace current EFLAGS value\r
+ pushfd ; get new EFLAGS\r
+ pop eax ; store new EFLAGS in EAX\r
+ popfd ; restore original EFLAGS\r
+ xor eax, ecx ; can\92t toggle ID bit,\r
+ je is_old_486 ; processor=old\r
+\r
+ mov eax,1\r
+ db 0fh,0a2h ;CPUID\r
+\r
+exitcpudetect:\r
+ pop ebx\r
+ ret\r
+\r
+end_cpu_is_386:\r
+ mov eax,0300h\r
+ jmp exitcpudetect\r
+\r
+is_old_486:\r
+ mov eax,0400h\r
+ jmp exitcpudetect\r
+\r
+IFDEF NOUNDERLINE\r
+cpudetect32 endp\r
+ELSE\r
+_cpudetect32 endp\r
+ENDIF\r
+ENDIF\r
+\r
+MAX_MATCH equ 258\r
+MIN_MATCH equ 3\r
+MIN_LOOKAHEAD equ (MAX_MATCH + MIN_MATCH + 1)\r
+MAX_MATCH_8_ equ ((MAX_MATCH + 7) AND 0FFF0h)\r
+\r
+\r
+;;; stack frame offsets\r
+\r
+chainlenwmask equ esp + 0 ; high word: current chain len\r
+ ; low word: s->wmask\r
+window equ esp + 4 ; local copy of s->window\r
+windowbestlen equ esp + 8 ; s->window + bestlen\r
+scanstart equ esp + 16 ; first two bytes of string\r
+scanend equ esp + 12 ; last two bytes of string\r
+scanalign equ esp + 20 ; dword-misalignment of string\r
+nicematch equ esp + 24 ; a good enough match size\r
+bestlen equ esp + 28 ; size of best match so far\r
+scan equ esp + 32 ; ptr to string wanting match\r
+\r
+LocalVarsSize equ 36\r
+; saved ebx byte esp + 36\r
+; saved edi byte esp + 40\r
+; saved esi byte esp + 44\r
+; saved ebp byte esp + 48\r
+; return address byte esp + 52\r
+deflatestate equ esp + 56 ; the function arguments\r
+curmatch equ esp + 60\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
+dsWSize equ 36+zlib1222add\r
+dsWMask equ 44+zlib1222add\r
+dsWindow equ 48+zlib1222add\r
+dsPrev equ 56+zlib1222add\r
+dsMatchLen equ 88+zlib1222add\r
+dsPrevMatch equ 92+zlib1222add\r
+dsStrStart equ 100+zlib1222add\r
+dsMatchStart equ 104+zlib1222add\r
+dsLookahead equ 108+zlib1222add\r
+dsPrevLen equ 112+zlib1222add\r
+dsMaxChainLen equ 116+zlib1222add\r
+dsGoodMatch equ 132+zlib1222add\r
+dsNiceMatch equ 136+zlib1222add\r
+\r
+\r
+;;; match.asm -- Pentium-Pro-optimized version of longest_match()\r
+;;; Written for zlib 1.1.2\r
+;;; Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>\r
+;;; You can look at http://www.muppetlabs.com/~breadbox/software/assembly.html\r
+;;;\r
+;;; This is free software; you can redistribute it and/or modify it\r
+;;; under the terms of the GNU General Public License.\r
+\r
+;GLOBAL _longest_match, _match_init\r
+\r
+\r
+;SECTION .text\r
+\r
+;;; uInt longest_match(deflate_state *deflatestate, IPos curmatch)\r
+\r
+;_longest_match:\r
+IFDEF NOOLDPENTIUMCODE\r
+ IFDEF NOUNDERLINE\r
+ longest_match proc near\r
+ ELSE\r
+ _longest_match proc near\r
+ ENDIF\r
+ELSE\r
+ IFDEF NOUNDERLINE\r
+ longest_match_686 proc near\r
+ ELSE\r
+ _longest_match_686 proc near\r
+ ENDIF\r
+ENDIF\r
+\r
+;;; Save registers that the compiler may be using, and adjust esp to\r
+;;; make room for our stack frame.\r
+\r
+ push ebp\r
+ push edi\r
+ push esi\r
+ push ebx\r
+ sub esp, LocalVarsSize\r
+\r
+;;; Retrieve the function arguments. ecx 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
+ mov edx, [deflatestate]\r
+ mov ecx, [curmatch]\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 eax, [edx + dsPrevLen]\r
+ mov ebx, [edx + dsGoodMatch]\r
+ cmp eax, ebx\r
+ mov eax, [edx + dsWMask]\r
+ mov ebx, [edx + dsMaxChainLen]\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
+ mov [chainlenwmask], ebx\r
+\r
+;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;\r
+\r
+ mov eax, [edx + dsNiceMatch]\r
+ mov ebx, [edx + dsLookahead]\r
+ cmp ebx, eax\r
+ jl LookaheadLess\r
+ mov ebx, eax\r
+LookaheadLess: mov [nicematch], ebx\r
+\r
+;;; register Bytef *scan = s->window + s->strstart;\r
+\r
+ mov esi, [edx + dsWindow]\r
+ mov [window], esi\r
+ mov ebp, [edx + dsStrStart]\r
+ lea edi, [esi + ebp]\r
+ mov [scan], edi\r
+\r
+;;; Determine how many bytes the scan ptr is off from being\r
+;;; dword-aligned.\r
+\r
+ mov eax, edi\r
+ neg eax\r
+ and eax, 3\r
+ mov [scanalign], eax\r
+\r
+;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?\r
+;;; s->strstart - (IPos)MAX_DIST(s) : NIL;\r
+\r
+ mov eax, [edx + dsWSize]\r
+ sub eax, MIN_LOOKAHEAD\r
+ sub ebp, eax\r
+ jg LimitPositive\r
+ xor ebp, ebp\r
+LimitPositive:\r
+\r
+;;; int best_len = s->prev_length;\r
+\r
+ mov eax, [edx + dsPrevLen]\r
+ mov [bestlen], eax\r
+\r
+;;; Store the sum of s->window + best_len in esi locally, and in esi.\r
+\r
+ add esi, eax\r
+ mov [windowbestlen], esi\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 ebx, word ptr [edi]\r
+ mov [scanstart], ebx\r
+ movzx ebx, word ptr [edi + eax - 1]\r
+ mov [scanend], ebx\r
+ mov edi, [edx + dsPrev]\r
+\r
+;;; Jump into the main loop.\r
+\r
+ mov edx, [chainlenwmask]\r
+ jmp short LoopEntry\r
+\r
+align 4\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
+;;; ecx = 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 ecx, edx\r
+ movzx ecx, word ptr [edi + ecx*2]\r
+ cmp ecx, ebp\r
+ jbe LeaveNow\r
+ sub edx, 00010000h\r
+ js LeaveNow\r
+LoopEntry: movzx eax, word ptr [esi + ecx - 1]\r
+ cmp eax, ebx\r
+ jnz LookupLoop\r
+ mov eax, [window]\r
+ movzx eax, word ptr [eax + ecx]\r
+ cmp eax, [scanstart]\r
+ jnz LookupLoop\r
+\r
+;;; Store the current value of chainlen.\r
+\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
+ mov esi, [window]\r
+ mov edi, [scan]\r
+ add esi, ecx\r
+ mov eax, [scanalign]\r
+ mov edx, 0fffffef8h; -(MAX_MATCH_8)\r
+ lea edi, [edi + eax + 0108h] ;MAX_MATCH_8]\r
+ lea esi, [esi + eax + 0108h] ;MAX_MATCH_8]\r
+\r
+;;; Test the strings for equality, 8 bytes at a time. At the end,\r
+;;; adjust edx 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. (esi 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
+LoopCmps:\r
+ mov eax, [esi + edx]\r
+ xor eax, [edi + edx]\r
+ jnz LeaveLoopCmps\r
+ mov eax, [esi + edx + 4]\r
+ xor eax, [edi + edx + 4]\r
+ jnz LeaveLoopCmps4\r
+ add edx, 8\r
+ jnz LoopCmps\r
+ jmp short LenMaximum\r
+LeaveLoopCmps4: add edx, 4\r
+LeaveLoopCmps: test eax, 0000FFFFh\r
+ jnz LenLower\r
+ add edx, 2\r
+ shr eax, 16\r
+LenLower: sub al, 1\r
+ adc edx, 0\r
+\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 eax, [edi + edx]\r
+ mov edi, [scan]\r
+ sub eax, edi\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
+ mov edx, [deflatestate]\r
+ mov ebx, [bestlen]\r
+ cmp eax, ebx\r
+ jg LongerMatch\r
+ mov esi, [windowbestlen]\r
+ mov edi, [edx + dsPrev]\r
+ mov ebx, [scanend]\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: mov ebx, [nicematch]\r
+ mov [bestlen], eax\r
+ mov [edx + dsMatchStart], ecx\r
+ cmp eax, ebx\r
+ jge LeaveNow\r
+ mov esi, [window]\r
+ add esi, eax\r
+ mov [windowbestlen], esi\r
+ movzx ebx, word ptr [edi + eax - 1]\r
+ mov edi, [edx + dsPrev]\r
+ mov [scanend], ebx\r
+ mov edx, [chainlenwmask]\r
+ jmp LookupLoop\r
+\r
+;;; Accept the current string, with the maximum possible length.\r
+\r
+LenMaximum: mov edx, [deflatestate]\r
+ mov dword ptr [bestlen], MAX_MATCH\r
+ mov [edx + dsMatchStart], ecx\r
+\r
+;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len;\r
+;;; return s->lookahead;\r
+\r
+LeaveNow:\r
+ mov edx, [deflatestate]\r
+ mov ebx, [bestlen]\r
+ mov eax, [edx + dsLookahead]\r
+ cmp ebx, eax\r
+ jg LookaheadRet\r
+ mov eax, ebx\r
+LookaheadRet:\r
+\r
+;;; Restore the stack and return from whence we came.\r
+\r
+ add esp, LocalVarsSize\r
+ pop ebx\r
+ pop esi\r
+ pop edi\r
+ pop ebp\r
+\r
+ ret\r
+; please don't remove this string !\r
+; Your can freely use gvmat32 in any free or commercial app if you don't remove the string in the binary!\r
+ db 0dh,0ah,"asm686 with masm, optimised assembly code from Brian Raiter, written 1998",0dh,0ah\r
+\r
+\r
+IFDEF NOOLDPENTIUMCODE\r
+ IFDEF NOUNDERLINE\r
+ longest_match endp\r
+ ELSE\r
+ _longest_match endp\r
+ ENDIF\r
+\r
+ IFDEF NOUNDERLINE\r
+ match_init proc near\r
+ ret\r
+ match_init endp\r
+ ELSE\r
+ _match_init proc near\r
+ ret\r
+ _match_init endp\r
+ ENDIF \r
+ELSE\r
+ IFDEF NOUNDERLINE\r
+ longest_match_686 endp\r
+ ELSE\r
+ _longest_match_686 endp\r
+ ENDIF\r
+ENDIF\r
+\r
+_TEXT ends\r
+end\r