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