cannam@89: ; match686.asm -- Asm portion of the optimized longest_match for 32 bits x86 cannam@89: ; Copyright (C) 1995-1996 Jean-loup Gailly, Brian Raiter and Gilles Vollant. cannam@89: ; File written by Gilles Vollant, by converting match686.S from Brian Raiter cannam@89: ; for MASM. This is as assembly version of longest_match cannam@89: ; from Jean-loup Gailly in deflate.c cannam@89: ; cannam@89: ; http://www.zlib.net cannam@89: ; http://www.winimage.com/zLibDll cannam@89: ; http://www.muppetlabs.com/~breadbox/software/assembly.html cannam@89: ; cannam@89: ; For Visual C++ 4.x and higher and ML 6.x and higher cannam@89: ; ml.exe is distributed in cannam@89: ; http://www.microsoft.com/downloads/details.aspx?FamilyID=7a1c9da0-0510-44a2-b042-7ef370530c64 cannam@89: ; cannam@89: ; this file contain two implementation of longest_match cannam@89: ; cannam@89: ; this longest_match was written by Brian raiter (1998), optimized for Pentium Pro cannam@89: ; (and the faster known version of match_init on modern Core 2 Duo and AMD Phenom) cannam@89: ; cannam@89: ; for using an assembly version of longest_match, you need define ASMV in project cannam@89: ; cannam@89: ; compile the asm file running cannam@89: ; ml /coff /Zi /c /Flmatch686.lst match686.asm cannam@89: ; and do not include match686.obj in your project cannam@89: ; cannam@89: ; note: contrib of zLib 1.2.3 and earlier contained both a deprecated version for cannam@89: ; Pentium (prior Pentium Pro) and this version for Pentium Pro and modern processor cannam@89: ; with autoselect (with cpu detection code) cannam@89: ; if you want support the old pentium optimization, you can still use these version cannam@89: ; cannam@89: ; this file is not optimized for old pentium, but it compatible with all x86 32 bits cannam@89: ; processor (starting 80386) cannam@89: ; cannam@89: ; cannam@89: ; see below : zlib1222add must be adjuster if you use a zlib version < 1.2.2.2 cannam@89: cannam@89: ;uInt longest_match(s, cur_match) cannam@89: ; deflate_state *s; cannam@89: ; IPos cur_match; /* current match */ cannam@89: cannam@89: NbStack equ 76 cannam@89: cur_match equ dword ptr[esp+NbStack-0] cannam@89: str_s equ dword ptr[esp+NbStack-4] cannam@89: ; 5 dword on top (ret,ebp,esi,edi,ebx) cannam@89: adrret equ dword ptr[esp+NbStack-8] cannam@89: pushebp equ dword ptr[esp+NbStack-12] cannam@89: pushedi equ dword ptr[esp+NbStack-16] cannam@89: pushesi equ dword ptr[esp+NbStack-20] cannam@89: pushebx equ dword ptr[esp+NbStack-24] cannam@89: cannam@89: chain_length equ dword ptr [esp+NbStack-28] cannam@89: limit equ dword ptr [esp+NbStack-32] cannam@89: best_len equ dword ptr [esp+NbStack-36] cannam@89: window equ dword ptr [esp+NbStack-40] cannam@89: prev equ dword ptr [esp+NbStack-44] cannam@89: scan_start equ word ptr [esp+NbStack-48] cannam@89: wmask equ dword ptr [esp+NbStack-52] cannam@89: match_start_ptr equ dword ptr [esp+NbStack-56] cannam@89: nice_match equ dword ptr [esp+NbStack-60] cannam@89: scan equ dword ptr [esp+NbStack-64] cannam@89: cannam@89: windowlen equ dword ptr [esp+NbStack-68] cannam@89: match_start equ dword ptr [esp+NbStack-72] cannam@89: strend equ dword ptr [esp+NbStack-76] cannam@89: NbStackAdd equ (NbStack-24) cannam@89: cannam@89: .386p cannam@89: cannam@89: name gvmatch cannam@89: .MODEL FLAT cannam@89: cannam@89: cannam@89: cannam@89: ; all the +zlib1222add offsets are due to the addition of fields cannam@89: ; in zlib in the deflate_state structure since the asm code was first written cannam@89: ; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)"). cannam@89: ; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0"). cannam@89: ; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8"). cannam@89: cannam@89: zlib1222add equ 8 cannam@89: cannam@89: ; Note : these value are good with a 8 bytes boundary pack structure cannam@89: dep_chain_length equ 74h+zlib1222add cannam@89: dep_window equ 30h+zlib1222add cannam@89: dep_strstart equ 64h+zlib1222add cannam@89: dep_prev_length equ 70h+zlib1222add cannam@89: dep_nice_match equ 88h+zlib1222add cannam@89: dep_w_size equ 24h+zlib1222add cannam@89: dep_prev equ 38h+zlib1222add cannam@89: dep_w_mask equ 2ch+zlib1222add cannam@89: dep_good_match equ 84h+zlib1222add cannam@89: dep_match_start equ 68h+zlib1222add cannam@89: dep_lookahead equ 6ch+zlib1222add cannam@89: cannam@89: cannam@89: _TEXT segment cannam@89: cannam@89: IFDEF NOUNDERLINE cannam@89: public longest_match cannam@89: public match_init cannam@89: ELSE cannam@89: public _longest_match cannam@89: public _match_init cannam@89: ENDIF cannam@89: cannam@89: MAX_MATCH equ 258 cannam@89: MIN_MATCH equ 3 cannam@89: MIN_LOOKAHEAD equ (MAX_MATCH+MIN_MATCH+1) cannam@89: cannam@89: cannam@89: cannam@89: MAX_MATCH equ 258 cannam@89: MIN_MATCH equ 3 cannam@89: MIN_LOOKAHEAD equ (MAX_MATCH + MIN_MATCH + 1) cannam@89: MAX_MATCH_8_ equ ((MAX_MATCH + 7) AND 0FFF0h) cannam@89: cannam@89: cannam@89: ;;; stack frame offsets cannam@89: cannam@89: chainlenwmask equ esp + 0 ; high word: current chain len cannam@89: ; low word: s->wmask cannam@89: window equ esp + 4 ; local copy of s->window cannam@89: windowbestlen equ esp + 8 ; s->window + bestlen cannam@89: scanstart equ esp + 16 ; first two bytes of string cannam@89: scanend equ esp + 12 ; last two bytes of string cannam@89: scanalign equ esp + 20 ; dword-misalignment of string cannam@89: nicematch equ esp + 24 ; a good enough match size cannam@89: bestlen equ esp + 28 ; size of best match so far cannam@89: scan equ esp + 32 ; ptr to string wanting match cannam@89: cannam@89: LocalVarsSize equ 36 cannam@89: ; saved ebx byte esp + 36 cannam@89: ; saved edi byte esp + 40 cannam@89: ; saved esi byte esp + 44 cannam@89: ; saved ebp byte esp + 48 cannam@89: ; return address byte esp + 52 cannam@89: deflatestate equ esp + 56 ; the function arguments cannam@89: curmatch equ esp + 60 cannam@89: cannam@89: ;;; Offsets for fields in the deflate_state structure. These numbers cannam@89: ;;; are calculated from the definition of deflate_state, with the cannam@89: ;;; assumption that the compiler will dword-align the fields. (Thus, cannam@89: ;;; changing the definition of deflate_state could easily cause this cannam@89: ;;; program to crash horribly, without so much as a warning at cannam@89: ;;; compile time. Sigh.) cannam@89: cannam@89: dsWSize equ 36+zlib1222add cannam@89: dsWMask equ 44+zlib1222add cannam@89: dsWindow equ 48+zlib1222add cannam@89: dsPrev equ 56+zlib1222add cannam@89: dsMatchLen equ 88+zlib1222add cannam@89: dsPrevMatch equ 92+zlib1222add cannam@89: dsStrStart equ 100+zlib1222add cannam@89: dsMatchStart equ 104+zlib1222add cannam@89: dsLookahead equ 108+zlib1222add cannam@89: dsPrevLen equ 112+zlib1222add cannam@89: dsMaxChainLen equ 116+zlib1222add cannam@89: dsGoodMatch equ 132+zlib1222add cannam@89: dsNiceMatch equ 136+zlib1222add cannam@89: cannam@89: cannam@89: ;;; match686.asm -- Pentium-Pro-optimized version of longest_match() cannam@89: ;;; Written for zlib 1.1.2 cannam@89: ;;; Copyright (C) 1998 Brian Raiter cannam@89: ;;; You can look at http://www.muppetlabs.com/~breadbox/software/assembly.html cannam@89: ;;; cannam@89: ;; cannam@89: ;; This software is provided 'as-is', without any express or implied cannam@89: ;; warranty. In no event will the authors be held liable for any damages cannam@89: ;; arising from the use of this software. cannam@89: ;; cannam@89: ;; Permission is granted to anyone to use this software for any purpose, cannam@89: ;; including commercial applications, and to alter it and redistribute it cannam@89: ;; freely, subject to the following restrictions: cannam@89: ;; cannam@89: ;; 1. The origin of this software must not be misrepresented; you must not cannam@89: ;; claim that you wrote the original software. If you use this software cannam@89: ;; in a product, an acknowledgment in the product documentation would be cannam@89: ;; appreciated but is not required. cannam@89: ;; 2. Altered source versions must be plainly marked as such, and must not be cannam@89: ;; misrepresented as being the original software cannam@89: ;; 3. This notice may not be removed or altered from any source distribution. cannam@89: ;; cannam@89: cannam@89: ;GLOBAL _longest_match, _match_init cannam@89: cannam@89: cannam@89: ;SECTION .text cannam@89: cannam@89: ;;; uInt longest_match(deflate_state *deflatestate, IPos curmatch) cannam@89: cannam@89: ;_longest_match: cannam@89: IFDEF NOUNDERLINE cannam@89: longest_match proc near cannam@89: ELSE cannam@89: _longest_match proc near cannam@89: ENDIF cannam@89: .FPO (9, 4, 0, 0, 1, 0) cannam@89: cannam@89: ;;; Save registers that the compiler may be using, and adjust esp to cannam@89: ;;; make room for our stack frame. cannam@89: cannam@89: push ebp cannam@89: push edi cannam@89: push esi cannam@89: push ebx cannam@89: sub esp, LocalVarsSize cannam@89: cannam@89: ;;; Retrieve the function arguments. ecx will hold cur_match cannam@89: ;;; throughout the entire function. edx will hold the pointer to the cannam@89: ;;; deflate_state structure during the function's setup (before cannam@89: ;;; entering the main loop. cannam@89: cannam@89: mov edx, [deflatestate] cannam@89: mov ecx, [curmatch] cannam@89: cannam@89: ;;; uInt wmask = s->w_mask; cannam@89: ;;; unsigned chain_length = s->max_chain_length; cannam@89: ;;; if (s->prev_length >= s->good_match) { cannam@89: ;;; chain_length >>= 2; cannam@89: ;;; } cannam@89: cannam@89: mov eax, [edx + dsPrevLen] cannam@89: mov ebx, [edx + dsGoodMatch] cannam@89: cmp eax, ebx cannam@89: mov eax, [edx + dsWMask] cannam@89: mov ebx, [edx + dsMaxChainLen] cannam@89: jl LastMatchGood cannam@89: shr ebx, 2 cannam@89: LastMatchGood: cannam@89: cannam@89: ;;; chainlen is decremented once beforehand so that the function can cannam@89: ;;; use the sign flag instead of the zero flag for the exit test. cannam@89: ;;; It is then shifted into the high word, to make room for the wmask cannam@89: ;;; value, which it will always accompany. cannam@89: cannam@89: dec ebx cannam@89: shl ebx, 16 cannam@89: or ebx, eax cannam@89: mov [chainlenwmask], ebx cannam@89: cannam@89: ;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; cannam@89: cannam@89: mov eax, [edx + dsNiceMatch] cannam@89: mov ebx, [edx + dsLookahead] cannam@89: cmp ebx, eax cannam@89: jl LookaheadLess cannam@89: mov ebx, eax cannam@89: LookaheadLess: mov [nicematch], ebx cannam@89: cannam@89: ;;; register Bytef *scan = s->window + s->strstart; cannam@89: cannam@89: mov esi, [edx + dsWindow] cannam@89: mov [window], esi cannam@89: mov ebp, [edx + dsStrStart] cannam@89: lea edi, [esi + ebp] cannam@89: mov [scan], edi cannam@89: cannam@89: ;;; Determine how many bytes the scan ptr is off from being cannam@89: ;;; dword-aligned. cannam@89: cannam@89: mov eax, edi cannam@89: neg eax cannam@89: and eax, 3 cannam@89: mov [scanalign], eax cannam@89: cannam@89: ;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ? cannam@89: ;;; s->strstart - (IPos)MAX_DIST(s) : NIL; cannam@89: cannam@89: mov eax, [edx + dsWSize] cannam@89: sub eax, MIN_LOOKAHEAD cannam@89: sub ebp, eax cannam@89: jg LimitPositive cannam@89: xor ebp, ebp cannam@89: LimitPositive: cannam@89: cannam@89: ;;; int best_len = s->prev_length; cannam@89: cannam@89: mov eax, [edx + dsPrevLen] cannam@89: mov [bestlen], eax cannam@89: cannam@89: ;;; Store the sum of s->window + best_len in esi locally, and in esi. cannam@89: cannam@89: add esi, eax cannam@89: mov [windowbestlen], esi cannam@89: cannam@89: ;;; register ush scan_start = *(ushf*)scan; cannam@89: ;;; register ush scan_end = *(ushf*)(scan+best_len-1); cannam@89: ;;; Posf *prev = s->prev; cannam@89: cannam@89: movzx ebx, word ptr [edi] cannam@89: mov [scanstart], ebx cannam@89: movzx ebx, word ptr [edi + eax - 1] cannam@89: mov [scanend], ebx cannam@89: mov edi, [edx + dsPrev] cannam@89: cannam@89: ;;; Jump into the main loop. cannam@89: cannam@89: mov edx, [chainlenwmask] cannam@89: jmp short LoopEntry cannam@89: cannam@89: align 4 cannam@89: cannam@89: ;;; do { cannam@89: ;;; match = s->window + cur_match; cannam@89: ;;; if (*(ushf*)(match+best_len-1) != scan_end || cannam@89: ;;; *(ushf*)match != scan_start) continue; cannam@89: ;;; [...] cannam@89: ;;; } while ((cur_match = prev[cur_match & wmask]) > limit cannam@89: ;;; && --chain_length != 0); cannam@89: ;;; cannam@89: ;;; Here is the inner loop of the function. The function will spend the cannam@89: ;;; majority of its time in this loop, and majority of that time will cannam@89: ;;; be spent in the first ten instructions. cannam@89: ;;; cannam@89: ;;; Within this loop: cannam@89: ;;; ebx = scanend cannam@89: ;;; ecx = curmatch cannam@89: ;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask) cannam@89: ;;; esi = windowbestlen - i.e., (window + bestlen) cannam@89: ;;; edi = prev cannam@89: ;;; ebp = limit cannam@89: cannam@89: LookupLoop: cannam@89: and ecx, edx cannam@89: movzx ecx, word ptr [edi + ecx*2] cannam@89: cmp ecx, ebp cannam@89: jbe LeaveNow cannam@89: sub edx, 00010000h cannam@89: js LeaveNow cannam@89: LoopEntry: movzx eax, word ptr [esi + ecx - 1] cannam@89: cmp eax, ebx cannam@89: jnz LookupLoop cannam@89: mov eax, [window] cannam@89: movzx eax, word ptr [eax + ecx] cannam@89: cmp eax, [scanstart] cannam@89: jnz LookupLoop cannam@89: cannam@89: ;;; Store the current value of chainlen. cannam@89: cannam@89: mov [chainlenwmask], edx cannam@89: cannam@89: ;;; Point edi to the string under scrutiny, and esi to the string we cannam@89: ;;; are hoping to match it up with. In actuality, esi and edi are cannam@89: ;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is cannam@89: ;;; initialized to -(MAX_MATCH_8 - scanalign). cannam@89: cannam@89: mov esi, [window] cannam@89: mov edi, [scan] cannam@89: add esi, ecx cannam@89: mov eax, [scanalign] cannam@89: mov edx, 0fffffef8h; -(MAX_MATCH_8) cannam@89: lea edi, [edi + eax + 0108h] ;MAX_MATCH_8] cannam@89: lea esi, [esi + eax + 0108h] ;MAX_MATCH_8] cannam@89: cannam@89: ;;; Test the strings for equality, 8 bytes at a time. At the end, cannam@89: ;;; adjust edx so that it is offset to the exact byte that mismatched. cannam@89: ;;; cannam@89: ;;; We already know at this point that the first three bytes of the cannam@89: ;;; strings match each other, and they can be safely passed over before cannam@89: ;;; starting the compare loop. So what this code does is skip over 0-3 cannam@89: ;;; bytes, as much as necessary in order to dword-align the edi cannam@89: ;;; pointer. (esi will still be misaligned three times out of four.) cannam@89: ;;; cannam@89: ;;; It should be confessed that this loop usually does not represent cannam@89: ;;; much of the total running time. Replacing it with a more cannam@89: ;;; straightforward "rep cmpsb" would not drastically degrade cannam@89: ;;; performance. cannam@89: cannam@89: LoopCmps: cannam@89: mov eax, [esi + edx] cannam@89: xor eax, [edi + edx] cannam@89: jnz LeaveLoopCmps cannam@89: mov eax, [esi + edx + 4] cannam@89: xor eax, [edi + edx + 4] cannam@89: jnz LeaveLoopCmps4 cannam@89: add edx, 8 cannam@89: jnz LoopCmps cannam@89: jmp short LenMaximum cannam@89: LeaveLoopCmps4: add edx, 4 cannam@89: LeaveLoopCmps: test eax, 0000FFFFh cannam@89: jnz LenLower cannam@89: add edx, 2 cannam@89: shr eax, 16 cannam@89: LenLower: sub al, 1 cannam@89: adc edx, 0 cannam@89: cannam@89: ;;; Calculate the length of the match. If it is longer than MAX_MATCH, cannam@89: ;;; then automatically accept it as the best possible match and leave. cannam@89: cannam@89: lea eax, [edi + edx] cannam@89: mov edi, [scan] cannam@89: sub eax, edi cannam@89: cmp eax, MAX_MATCH cannam@89: jge LenMaximum cannam@89: cannam@89: ;;; If the length of the match is not longer than the best match we cannam@89: ;;; have so far, then forget it and return to the lookup loop. cannam@89: cannam@89: mov edx, [deflatestate] cannam@89: mov ebx, [bestlen] cannam@89: cmp eax, ebx cannam@89: jg LongerMatch cannam@89: mov esi, [windowbestlen] cannam@89: mov edi, [edx + dsPrev] cannam@89: mov ebx, [scanend] cannam@89: mov edx, [chainlenwmask] cannam@89: jmp LookupLoop cannam@89: cannam@89: ;;; s->match_start = cur_match; cannam@89: ;;; best_len = len; cannam@89: ;;; if (len >= nice_match) break; cannam@89: ;;; scan_end = *(ushf*)(scan+best_len-1); cannam@89: cannam@89: LongerMatch: mov ebx, [nicematch] cannam@89: mov [bestlen], eax cannam@89: mov [edx + dsMatchStart], ecx cannam@89: cmp eax, ebx cannam@89: jge LeaveNow cannam@89: mov esi, [window] cannam@89: add esi, eax cannam@89: mov [windowbestlen], esi cannam@89: movzx ebx, word ptr [edi + eax - 1] cannam@89: mov edi, [edx + dsPrev] cannam@89: mov [scanend], ebx cannam@89: mov edx, [chainlenwmask] cannam@89: jmp LookupLoop cannam@89: cannam@89: ;;; Accept the current string, with the maximum possible length. cannam@89: cannam@89: LenMaximum: mov edx, [deflatestate] cannam@89: mov dword ptr [bestlen], MAX_MATCH cannam@89: mov [edx + dsMatchStart], ecx cannam@89: cannam@89: ;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len; cannam@89: ;;; return s->lookahead; cannam@89: cannam@89: LeaveNow: cannam@89: mov edx, [deflatestate] cannam@89: mov ebx, [bestlen] cannam@89: mov eax, [edx + dsLookahead] cannam@89: cmp ebx, eax cannam@89: jg LookaheadRet cannam@89: mov eax, ebx cannam@89: LookaheadRet: cannam@89: cannam@89: ;;; Restore the stack and return from whence we came. cannam@89: cannam@89: add esp, LocalVarsSize cannam@89: pop ebx cannam@89: pop esi cannam@89: pop edi cannam@89: pop ebp cannam@89: cannam@89: ret cannam@89: ; please don't remove this string ! cannam@89: ; Your can freely use match686 in any free or commercial app if you don't remove the string in the binary! cannam@89: db 0dh,0ah,"asm686 with masm, optimised assembly code from Brian Raiter, written 1998",0dh,0ah cannam@89: cannam@89: cannam@89: IFDEF NOUNDERLINE cannam@89: longest_match endp cannam@89: ELSE cannam@89: _longest_match endp cannam@89: ENDIF cannam@89: cannam@89: IFDEF NOUNDERLINE cannam@89: match_init proc near cannam@89: ret cannam@89: match_init endp cannam@89: ELSE cannam@89: _match_init proc near cannam@89: ret cannam@89: _match_init endp cannam@89: ENDIF cannam@89: cannam@89: cannam@89: _TEXT ends cannam@89: end