Chris@4: /* match.S -- x86 assembly version of the zlib longest_match() function. Chris@4: * Optimized for the Intel 686 chips (PPro and later). Chris@4: * Chris@4: * Copyright (C) 1998, 2007 Brian Raiter Chris@4: * Chris@4: * This software is provided 'as-is', without any express or implied Chris@4: * warranty. In no event will the author be held liable for any damages Chris@4: * arising from the use of this software. Chris@4: * Chris@4: * Permission is granted to anyone to use this software for any purpose, Chris@4: * including commercial applications, and to alter it and redistribute it Chris@4: * freely, subject to the following restrictions: Chris@4: * Chris@4: * 1. The origin of this software must not be misrepresented; you must not Chris@4: * claim that you wrote the original software. If you use this software Chris@4: * in a product, an acknowledgment in the product documentation would be Chris@4: * appreciated but is not required. Chris@4: * 2. Altered source versions must be plainly marked as such, and must not be Chris@4: * misrepresented as being the original software. Chris@4: * 3. This notice may not be removed or altered from any source distribution. Chris@4: */ Chris@4: Chris@4: #ifndef NO_UNDERLINE Chris@4: #define match_init _match_init Chris@4: #define longest_match _longest_match Chris@4: #endif Chris@4: Chris@4: #define MAX_MATCH (258) Chris@4: #define MIN_MATCH (3) Chris@4: #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1) Chris@4: #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7) Chris@4: Chris@4: /* stack frame offsets */ Chris@4: Chris@4: #define chainlenwmask 0 /* high word: current chain len */ Chris@4: /* low word: s->wmask */ Chris@4: #define window 4 /* local copy of s->window */ Chris@4: #define windowbestlen 8 /* s->window + bestlen */ Chris@4: #define scanstart 16 /* first two bytes of string */ Chris@4: #define scanend 12 /* last two bytes of string */ Chris@4: #define scanalign 20 /* dword-misalignment of string */ Chris@4: #define nicematch 24 /* a good enough match size */ Chris@4: #define bestlen 28 /* size of best match so far */ Chris@4: #define scan 32 /* ptr to string wanting match */ Chris@4: Chris@4: #define LocalVarsSize (36) Chris@4: /* saved ebx 36 */ Chris@4: /* saved edi 40 */ Chris@4: /* saved esi 44 */ Chris@4: /* saved ebp 48 */ Chris@4: /* return address 52 */ Chris@4: #define deflatestate 56 /* the function arguments */ Chris@4: #define curmatch 60 Chris@4: Chris@4: /* All the +zlib1222add offsets are due to the addition of fields Chris@4: * in zlib in the deflate_state structure since the asm code was first written Chris@4: * (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)"). Chris@4: * (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0"). Chris@4: * if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8"). Chris@4: */ Chris@4: Chris@4: #define zlib1222add (8) Chris@4: Chris@4: #define dsWSize (36+zlib1222add) Chris@4: #define dsWMask (44+zlib1222add) Chris@4: #define dsWindow (48+zlib1222add) Chris@4: #define dsPrev (56+zlib1222add) Chris@4: #define dsMatchLen (88+zlib1222add) Chris@4: #define dsPrevMatch (92+zlib1222add) Chris@4: #define dsStrStart (100+zlib1222add) Chris@4: #define dsMatchStart (104+zlib1222add) Chris@4: #define dsLookahead (108+zlib1222add) Chris@4: #define dsPrevLen (112+zlib1222add) Chris@4: #define dsMaxChainLen (116+zlib1222add) Chris@4: #define dsGoodMatch (132+zlib1222add) Chris@4: #define dsNiceMatch (136+zlib1222add) Chris@4: Chris@4: Chris@4: .file "match.S" Chris@4: Chris@4: .globl match_init, longest_match Chris@4: Chris@4: .text Chris@4: Chris@4: /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */ Chris@4: .cfi_sections .debug_frame Chris@4: Chris@4: longest_match: Chris@4: Chris@4: .cfi_startproc Chris@4: /* Save registers that the compiler may be using, and adjust %esp to */ Chris@4: /* make room for our stack frame. */ Chris@4: Chris@4: pushl %ebp Chris@4: .cfi_def_cfa_offset 8 Chris@4: .cfi_offset ebp, -8 Chris@4: pushl %edi Chris@4: .cfi_def_cfa_offset 12 Chris@4: pushl %esi Chris@4: .cfi_def_cfa_offset 16 Chris@4: pushl %ebx Chris@4: .cfi_def_cfa_offset 20 Chris@4: subl $LocalVarsSize, %esp Chris@4: .cfi_def_cfa_offset LocalVarsSize+20 Chris@4: Chris@4: /* Retrieve the function arguments. %ecx will hold cur_match */ Chris@4: /* throughout the entire function. %edx will hold the pointer to the */ Chris@4: /* deflate_state structure during the function's setup (before */ Chris@4: /* entering the main loop). */ Chris@4: Chris@4: movl deflatestate(%esp), %edx Chris@4: movl curmatch(%esp), %ecx Chris@4: Chris@4: /* uInt wmask = s->w_mask; */ Chris@4: /* unsigned chain_length = s->max_chain_length; */ Chris@4: /* if (s->prev_length >= s->good_match) { */ Chris@4: /* chain_length >>= 2; */ Chris@4: /* } */ Chris@4: Chris@4: movl dsPrevLen(%edx), %eax Chris@4: movl dsGoodMatch(%edx), %ebx Chris@4: cmpl %ebx, %eax Chris@4: movl dsWMask(%edx), %eax Chris@4: movl dsMaxChainLen(%edx), %ebx Chris@4: jl LastMatchGood Chris@4: shrl $2, %ebx Chris@4: LastMatchGood: Chris@4: Chris@4: /* chainlen is decremented once beforehand so that the function can */ Chris@4: /* use the sign flag instead of the zero flag for the exit test. */ Chris@4: /* It is then shifted into the high word, to make room for the wmask */ Chris@4: /* value, which it will always accompany. */ Chris@4: Chris@4: decl %ebx Chris@4: shll $16, %ebx Chris@4: orl %eax, %ebx Chris@4: movl %ebx, chainlenwmask(%esp) Chris@4: Chris@4: /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */ Chris@4: Chris@4: movl dsNiceMatch(%edx), %eax Chris@4: movl dsLookahead(%edx), %ebx Chris@4: cmpl %eax, %ebx Chris@4: jl LookaheadLess Chris@4: movl %eax, %ebx Chris@4: LookaheadLess: movl %ebx, nicematch(%esp) Chris@4: Chris@4: /* register Bytef *scan = s->window + s->strstart; */ Chris@4: Chris@4: movl dsWindow(%edx), %esi Chris@4: movl %esi, window(%esp) Chris@4: movl dsStrStart(%edx), %ebp Chris@4: lea (%esi,%ebp), %edi Chris@4: movl %edi, scan(%esp) Chris@4: Chris@4: /* Determine how many bytes the scan ptr is off from being */ Chris@4: /* dword-aligned. */ Chris@4: Chris@4: movl %edi, %eax Chris@4: negl %eax Chris@4: andl $3, %eax Chris@4: movl %eax, scanalign(%esp) Chris@4: Chris@4: /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */ Chris@4: /* s->strstart - (IPos)MAX_DIST(s) : NIL; */ Chris@4: Chris@4: movl dsWSize(%edx), %eax Chris@4: subl $MIN_LOOKAHEAD, %eax Chris@4: subl %eax, %ebp Chris@4: jg LimitPositive Chris@4: xorl %ebp, %ebp Chris@4: LimitPositive: Chris@4: Chris@4: /* int best_len = s->prev_length; */ Chris@4: Chris@4: movl dsPrevLen(%edx), %eax Chris@4: movl %eax, bestlen(%esp) Chris@4: Chris@4: /* Store the sum of s->window + best_len in %esi locally, and in %esi. */ Chris@4: Chris@4: addl %eax, %esi Chris@4: movl %esi, windowbestlen(%esp) Chris@4: Chris@4: /* register ush scan_start = *(ushf*)scan; */ Chris@4: /* register ush scan_end = *(ushf*)(scan+best_len-1); */ Chris@4: /* Posf *prev = s->prev; */ Chris@4: Chris@4: movzwl (%edi), %ebx Chris@4: movl %ebx, scanstart(%esp) Chris@4: movzwl -1(%edi,%eax), %ebx Chris@4: movl %ebx, scanend(%esp) Chris@4: movl dsPrev(%edx), %edi Chris@4: Chris@4: /* Jump into the main loop. */ Chris@4: Chris@4: movl chainlenwmask(%esp), %edx Chris@4: jmp LoopEntry Chris@4: Chris@4: .balign 16 Chris@4: Chris@4: /* do { Chris@4: * match = s->window + cur_match; Chris@4: * if (*(ushf*)(match+best_len-1) != scan_end || Chris@4: * *(ushf*)match != scan_start) continue; Chris@4: * [...] Chris@4: * } while ((cur_match = prev[cur_match & wmask]) > limit Chris@4: * && --chain_length != 0); Chris@4: * Chris@4: * Here is the inner loop of the function. The function will spend the Chris@4: * majority of its time in this loop, and majority of that time will Chris@4: * be spent in the first ten instructions. Chris@4: * Chris@4: * Within this loop: Chris@4: * %ebx = scanend Chris@4: * %ecx = curmatch Chris@4: * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask) Chris@4: * %esi = windowbestlen - i.e., (window + bestlen) Chris@4: * %edi = prev Chris@4: * %ebp = limit Chris@4: */ Chris@4: LookupLoop: Chris@4: andl %edx, %ecx Chris@4: movzwl (%edi,%ecx,2), %ecx Chris@4: cmpl %ebp, %ecx Chris@4: jbe LeaveNow Chris@4: subl $0x00010000, %edx Chris@4: js LeaveNow Chris@4: LoopEntry: movzwl -1(%esi,%ecx), %eax Chris@4: cmpl %ebx, %eax Chris@4: jnz LookupLoop Chris@4: movl window(%esp), %eax Chris@4: movzwl (%eax,%ecx), %eax Chris@4: cmpl scanstart(%esp), %eax Chris@4: jnz LookupLoop Chris@4: Chris@4: /* Store the current value of chainlen. */ Chris@4: Chris@4: movl %edx, chainlenwmask(%esp) Chris@4: Chris@4: /* Point %edi to the string under scrutiny, and %esi to the string we */ Chris@4: /* are hoping to match it up with. In actuality, %esi and %edi are */ Chris@4: /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */ Chris@4: /* initialized to -(MAX_MATCH_8 - scanalign). */ Chris@4: Chris@4: movl window(%esp), %esi Chris@4: movl scan(%esp), %edi Chris@4: addl %ecx, %esi Chris@4: movl scanalign(%esp), %eax Chris@4: movl $(-MAX_MATCH_8), %edx Chris@4: lea MAX_MATCH_8(%edi,%eax), %edi Chris@4: lea MAX_MATCH_8(%esi,%eax), %esi Chris@4: Chris@4: /* Test the strings for equality, 8 bytes at a time. At the end, Chris@4: * adjust %edx so that it is offset to the exact byte that mismatched. Chris@4: * Chris@4: * We already know at this point that the first three bytes of the Chris@4: * strings match each other, and they can be safely passed over before Chris@4: * starting the compare loop. So what this code does is skip over 0-3 Chris@4: * bytes, as much as necessary in order to dword-align the %edi Chris@4: * pointer. (%esi will still be misaligned three times out of four.) Chris@4: * Chris@4: * It should be confessed that this loop usually does not represent Chris@4: * much of the total running time. Replacing it with a more Chris@4: * straightforward "rep cmpsb" would not drastically degrade Chris@4: * performance. Chris@4: */ Chris@4: LoopCmps: Chris@4: movl (%esi,%edx), %eax Chris@4: xorl (%edi,%edx), %eax Chris@4: jnz LeaveLoopCmps Chris@4: movl 4(%esi,%edx), %eax Chris@4: xorl 4(%edi,%edx), %eax Chris@4: jnz LeaveLoopCmps4 Chris@4: addl $8, %edx Chris@4: jnz LoopCmps Chris@4: jmp LenMaximum Chris@4: LeaveLoopCmps4: addl $4, %edx Chris@4: LeaveLoopCmps: testl $0x0000FFFF, %eax Chris@4: jnz LenLower Chris@4: addl $2, %edx Chris@4: shrl $16, %eax Chris@4: LenLower: subb $1, %al Chris@4: adcl $0, %edx Chris@4: Chris@4: /* Calculate the length of the match. If it is longer than MAX_MATCH, */ Chris@4: /* then automatically accept it as the best possible match and leave. */ Chris@4: Chris@4: lea (%edi,%edx), %eax Chris@4: movl scan(%esp), %edi Chris@4: subl %edi, %eax Chris@4: cmpl $MAX_MATCH, %eax Chris@4: jge LenMaximum Chris@4: Chris@4: /* If the length of the match is not longer than the best match we */ Chris@4: /* have so far, then forget it and return to the lookup loop. */ Chris@4: Chris@4: movl deflatestate(%esp), %edx Chris@4: movl bestlen(%esp), %ebx Chris@4: cmpl %ebx, %eax Chris@4: jg LongerMatch Chris@4: movl windowbestlen(%esp), %esi Chris@4: movl dsPrev(%edx), %edi Chris@4: movl scanend(%esp), %ebx Chris@4: movl chainlenwmask(%esp), %edx Chris@4: jmp LookupLoop Chris@4: Chris@4: /* s->match_start = cur_match; */ Chris@4: /* best_len = len; */ Chris@4: /* if (len >= nice_match) break; */ Chris@4: /* scan_end = *(ushf*)(scan+best_len-1); */ Chris@4: Chris@4: LongerMatch: movl nicematch(%esp), %ebx Chris@4: movl %eax, bestlen(%esp) Chris@4: movl %ecx, dsMatchStart(%edx) Chris@4: cmpl %ebx, %eax Chris@4: jge LeaveNow Chris@4: movl window(%esp), %esi Chris@4: addl %eax, %esi Chris@4: movl %esi, windowbestlen(%esp) Chris@4: movzwl -1(%edi,%eax), %ebx Chris@4: movl dsPrev(%edx), %edi Chris@4: movl %ebx, scanend(%esp) Chris@4: movl chainlenwmask(%esp), %edx Chris@4: jmp LookupLoop Chris@4: Chris@4: /* Accept the current string, with the maximum possible length. */ Chris@4: Chris@4: LenMaximum: movl deflatestate(%esp), %edx Chris@4: movl $MAX_MATCH, bestlen(%esp) Chris@4: movl %ecx, dsMatchStart(%edx) Chris@4: Chris@4: /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */ Chris@4: /* return s->lookahead; */ Chris@4: Chris@4: LeaveNow: Chris@4: movl deflatestate(%esp), %edx Chris@4: movl bestlen(%esp), %ebx Chris@4: movl dsLookahead(%edx), %eax Chris@4: cmpl %eax, %ebx Chris@4: jg LookaheadRet Chris@4: movl %ebx, %eax Chris@4: LookaheadRet: Chris@4: Chris@4: /* Restore the stack and return from whence we came. */ Chris@4: Chris@4: addl $LocalVarsSize, %esp Chris@4: .cfi_def_cfa_offset 20 Chris@4: popl %ebx Chris@4: .cfi_def_cfa_offset 16 Chris@4: popl %esi Chris@4: .cfi_def_cfa_offset 12 Chris@4: popl %edi Chris@4: .cfi_def_cfa_offset 8 Chris@4: popl %ebp Chris@4: .cfi_def_cfa_offset 4 Chris@4: .cfi_endproc Chris@4: match_init: ret