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