cannam@89: /* match.S -- x86 assembly version of the zlib longest_match() function. cannam@89: * Optimized for the Intel 686 chips (PPro and later). cannam@89: * cannam@89: * Copyright (C) 1998, 2007 Brian Raiter cannam@89: * cannam@89: * This software is provided 'as-is', without any express or implied cannam@89: * warranty. In no event will the author 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: #ifndef NO_UNDERLINE cannam@89: #define match_init _match_init cannam@89: #define longest_match _longest_match cannam@89: #endif cannam@89: cannam@89: #define MAX_MATCH (258) cannam@89: #define MIN_MATCH (3) cannam@89: #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1) cannam@89: #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7) cannam@89: cannam@89: /* stack frame offsets */ cannam@89: cannam@89: #define chainlenwmask 0 /* high word: current chain len */ cannam@89: /* low word: s->wmask */ cannam@89: #define window 4 /* local copy of s->window */ cannam@89: #define windowbestlen 8 /* s->window + bestlen */ cannam@89: #define scanstart 16 /* first two bytes of string */ cannam@89: #define scanend 12 /* last two bytes of string */ cannam@89: #define scanalign 20 /* dword-misalignment of string */ cannam@89: #define nicematch 24 /* a good enough match size */ cannam@89: #define bestlen 28 /* size of best match so far */ cannam@89: #define scan 32 /* ptr to string wanting match */ cannam@89: cannam@89: #define LocalVarsSize (36) cannam@89: /* saved ebx 36 */ cannam@89: /* saved edi 40 */ cannam@89: /* saved esi 44 */ cannam@89: /* saved ebp 48 */ cannam@89: /* return address 52 */ cannam@89: #define deflatestate 56 /* the function arguments */ cannam@89: #define curmatch 60 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: cannam@89: #define zlib1222add (8) cannam@89: cannam@89: #define dsWSize (36+zlib1222add) cannam@89: #define dsWMask (44+zlib1222add) cannam@89: #define dsWindow (48+zlib1222add) cannam@89: #define dsPrev (56+zlib1222add) cannam@89: #define dsMatchLen (88+zlib1222add) cannam@89: #define dsPrevMatch (92+zlib1222add) cannam@89: #define dsStrStart (100+zlib1222add) cannam@89: #define dsMatchStart (104+zlib1222add) cannam@89: #define dsLookahead (108+zlib1222add) cannam@89: #define dsPrevLen (112+zlib1222add) cannam@89: #define dsMaxChainLen (116+zlib1222add) cannam@89: #define dsGoodMatch (132+zlib1222add) cannam@89: #define dsNiceMatch (136+zlib1222add) cannam@89: cannam@89: cannam@89: .file "match.S" cannam@89: cannam@89: .globl match_init, longest_match cannam@89: cannam@89: .text cannam@89: cannam@89: /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */ cannam@89: .cfi_sections .debug_frame cannam@89: cannam@89: longest_match: cannam@89: cannam@89: .cfi_startproc 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: pushl %ebp cannam@89: .cfi_def_cfa_offset 8 cannam@89: .cfi_offset ebp, -8 cannam@89: pushl %edi cannam@89: .cfi_def_cfa_offset 12 cannam@89: pushl %esi cannam@89: .cfi_def_cfa_offset 16 cannam@89: pushl %ebx cannam@89: .cfi_def_cfa_offset 20 cannam@89: subl $LocalVarsSize, %esp cannam@89: .cfi_def_cfa_offset LocalVarsSize+20 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: movl deflatestate(%esp), %edx cannam@89: movl curmatch(%esp), %ecx 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: movl dsPrevLen(%edx), %eax cannam@89: movl dsGoodMatch(%edx), %ebx cannam@89: cmpl %ebx, %eax cannam@89: movl dsWMask(%edx), %eax cannam@89: movl dsMaxChainLen(%edx), %ebx cannam@89: jl LastMatchGood cannam@89: shrl $2, %ebx 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: decl %ebx cannam@89: shll $16, %ebx cannam@89: orl %eax, %ebx cannam@89: movl %ebx, chainlenwmask(%esp) cannam@89: cannam@89: /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */ cannam@89: cannam@89: movl dsNiceMatch(%edx), %eax cannam@89: movl dsLookahead(%edx), %ebx cannam@89: cmpl %eax, %ebx cannam@89: jl LookaheadLess cannam@89: movl %eax, %ebx cannam@89: LookaheadLess: movl %ebx, nicematch(%esp) cannam@89: cannam@89: /* register Bytef *scan = s->window + s->strstart; */ cannam@89: cannam@89: movl dsWindow(%edx), %esi cannam@89: movl %esi, window(%esp) cannam@89: movl dsStrStart(%edx), %ebp cannam@89: lea (%esi,%ebp), %edi cannam@89: movl %edi, scan(%esp) cannam@89: cannam@89: /* Determine how many bytes the scan ptr is off from being */ cannam@89: /* dword-aligned. */ cannam@89: cannam@89: movl %edi, %eax cannam@89: negl %eax cannam@89: andl $3, %eax cannam@89: movl %eax, scanalign(%esp) 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: movl dsWSize(%edx), %eax cannam@89: subl $MIN_LOOKAHEAD, %eax cannam@89: subl %eax, %ebp cannam@89: jg LimitPositive cannam@89: xorl %ebp, %ebp cannam@89: LimitPositive: cannam@89: cannam@89: /* int best_len = s->prev_length; */ cannam@89: cannam@89: movl dsPrevLen(%edx), %eax cannam@89: movl %eax, bestlen(%esp) cannam@89: cannam@89: /* Store the sum of s->window + best_len in %esi locally, and in %esi. */ cannam@89: cannam@89: addl %eax, %esi cannam@89: movl %esi, windowbestlen(%esp) 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: movzwl (%edi), %ebx cannam@89: movl %ebx, scanstart(%esp) cannam@89: movzwl -1(%edi,%eax), %ebx cannam@89: movl %ebx, scanend(%esp) cannam@89: movl dsPrev(%edx), %edi cannam@89: cannam@89: /* Jump into the main loop. */ cannam@89: cannam@89: movl chainlenwmask(%esp), %edx cannam@89: jmp LoopEntry cannam@89: cannam@89: .balign 16 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: andl %edx, %ecx cannam@89: movzwl (%edi,%ecx,2), %ecx cannam@89: cmpl %ebp, %ecx cannam@89: jbe LeaveNow cannam@89: subl $0x00010000, %edx cannam@89: js LeaveNow cannam@89: LoopEntry: movzwl -1(%esi,%ecx), %eax cannam@89: cmpl %ebx, %eax cannam@89: jnz LookupLoop cannam@89: movl window(%esp), %eax cannam@89: movzwl (%eax,%ecx), %eax cannam@89: cmpl scanstart(%esp), %eax cannam@89: jnz LookupLoop cannam@89: cannam@89: /* Store the current value of chainlen. */ cannam@89: cannam@89: movl %edx, chainlenwmask(%esp) 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: movl window(%esp), %esi cannam@89: movl scan(%esp), %edi cannam@89: addl %ecx, %esi cannam@89: movl scanalign(%esp), %eax cannam@89: movl $(-MAX_MATCH_8), %edx cannam@89: lea MAX_MATCH_8(%edi,%eax), %edi cannam@89: lea MAX_MATCH_8(%esi,%eax), %esi 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: movl (%esi,%edx), %eax cannam@89: xorl (%edi,%edx), %eax cannam@89: jnz LeaveLoopCmps cannam@89: movl 4(%esi,%edx), %eax cannam@89: xorl 4(%edi,%edx), %eax cannam@89: jnz LeaveLoopCmps4 cannam@89: addl $8, %edx cannam@89: jnz LoopCmps cannam@89: jmp LenMaximum cannam@89: LeaveLoopCmps4: addl $4, %edx cannam@89: LeaveLoopCmps: testl $0x0000FFFF, %eax cannam@89: jnz LenLower cannam@89: addl $2, %edx cannam@89: shrl $16, %eax cannam@89: LenLower: subb $1, %al cannam@89: adcl $0, %edx 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 (%edi,%edx), %eax cannam@89: movl scan(%esp), %edi cannam@89: subl %edi, %eax cannam@89: cmpl $MAX_MATCH, %eax 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: movl deflatestate(%esp), %edx cannam@89: movl bestlen(%esp), %ebx cannam@89: cmpl %ebx, %eax cannam@89: jg LongerMatch cannam@89: movl windowbestlen(%esp), %esi cannam@89: movl dsPrev(%edx), %edi cannam@89: movl scanend(%esp), %ebx cannam@89: movl chainlenwmask(%esp), %edx 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: movl nicematch(%esp), %ebx cannam@89: movl %eax, bestlen(%esp) cannam@89: movl %ecx, dsMatchStart(%edx) cannam@89: cmpl %ebx, %eax cannam@89: jge LeaveNow cannam@89: movl window(%esp), %esi cannam@89: addl %eax, %esi cannam@89: movl %esi, windowbestlen(%esp) cannam@89: movzwl -1(%edi,%eax), %ebx cannam@89: movl dsPrev(%edx), %edi cannam@89: movl %ebx, scanend(%esp) cannam@89: movl chainlenwmask(%esp), %edx cannam@89: jmp LookupLoop cannam@89: cannam@89: /* Accept the current string, with the maximum possible length. */ cannam@89: cannam@89: LenMaximum: movl deflatestate(%esp), %edx cannam@89: movl $MAX_MATCH, bestlen(%esp) cannam@89: movl %ecx, dsMatchStart(%edx) 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: movl deflatestate(%esp), %edx cannam@89: movl bestlen(%esp), %ebx cannam@89: movl dsLookahead(%edx), %eax cannam@89: cmpl %eax, %ebx cannam@89: jg LookaheadRet cannam@89: movl %ebx, %eax cannam@89: LookaheadRet: cannam@89: cannam@89: /* Restore the stack and return from whence we came. */ cannam@89: cannam@89: addl $LocalVarsSize, %esp cannam@89: .cfi_def_cfa_offset 20 cannam@89: popl %ebx cannam@89: .cfi_def_cfa_offset 16 cannam@89: popl %esi cannam@89: .cfi_def_cfa_offset 12 cannam@89: popl %edi cannam@89: .cfi_def_cfa_offset 8 cannam@89: popl %ebp cannam@89: .cfi_def_cfa_offset 4 cannam@89: .cfi_endproc cannam@89: match_init: ret