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