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 * Copyright (C) 1998, 2007 Brian Raiter <breadbox@muppetlabs.com>
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 * 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 * 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#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* stack frame offsets */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define chainlenwmask 0 /* high word: current chain len */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* low word: s->wmask */
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/* 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/* 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/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Save registers that the compiler may be using, and adjust %esp to */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* make room for our stack frame. */
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/* 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/* 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/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* register Bytef *scan = s->window + s->strstart; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Determine how many bytes the scan ptr is off from being */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* dword-aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* int best_len = s->prev_length; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
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/* Jump into the main loop. */
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 * } while ((cur_match = prev[cur_match & wmask]) > limit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * && --chain_length != 0);
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 * 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/* Store the current value of chainlen. */
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/* 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 * 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 * 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/* 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/* 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/* 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/* Accept the current string, with the maximum possible length. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* return s->lookahead; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Restore the stack and return from whence we came. */