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