11849Swollman/*
21849Swollman * Copyright (c) 1993,94 Winning Strategies, Inc.
31849Swollman * All rights reserved.
41849Swollman *
51849Swollman * Redistribution and use in source and binary forms, with or without
61849Swollman * modification, are permitted provided that the following conditions
71849Swollman * are met:
81849Swollman * 1. Redistributions of source code must retain the above copyright
91849Swollman *    notice, this list of conditions and the following disclaimer.
101849Swollman * 2. Redistributions in binary form must reproduce the above copyright
111849Swollman *    notice, this list of conditions and the following disclaimer in the
121849Swollman *    documentation and/or other materials provided with the distribution.
131849Swollman * 3. All advertising materials mentioning features or use of this software
141849Swollman *    must display the following acknowledgement:
151849Swollman *      This product includes software developed by Winning Strategies, Inc.
161849Swollman * 4. The name of the author may not be used to endorse or promote products
171849Swollman *    derived from this software without specific prior written permission
181849Swollman *
191849Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
201849Swollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
211849Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
221849Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
231849Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
241849Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251849Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261849Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271849Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
281849Swollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291849Swollman */
301849Swollman
3185437Speter#include <machine/asm.h>
3293000Sobrien__FBSDID("$FreeBSD$");
331849Swollman
341849Swollman/*
351849Swollman * strncmp(s1, s2, n)
361849Swollman *	return an integer greater than, equal to, or less than 0,
371849Swollman *	according as the first n characters of string s1 is greater
381849Swollman *	than, equal to, or less than the string s2.
391849Swollman *
401849Swollman * %eax - pointer to s1
411849Swollman * %ecx - pointer to s2
421849Swollman * %edx - length
431849Swollman *
441849Swollman * Written by:
451849Swollman *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
461849Swollman */
471849Swollman
481849Swollman/*
491849Swollman * I've unrolled the loop eight times: large enough to make a
501849Swollman * significant difference, and small enough not to totally trash the
511849Swollman * cache.
525243Sbde *
535243Sbde * TODO: change all the jz's back to je for consistency.
541849Swollman */
551849Swollman
561849SwollmanENTRY(strncmp)
571849Swollman	pushl	%ebx
581849Swollman	movl	8(%esp),%eax
591849Swollman	movl	12(%esp),%ecx
601849Swollman	movl	16(%esp),%edx
611849Swollman	testl	%edx,%edx
621849Swollman	jmp	L2			/* Jump into the loop! */
631849Swollman
641849Swollman	.align 2,0x90
651849SwollmanL1:	incl	%eax
661849Swollman	incl	%ecx
671849Swollman	decl	%edx
681849SwollmanL2:	jz	L4			/* strings are equal */
691849Swollman	movb	(%eax),%bl
701849Swollman	testb	%bl,%bl
711849Swollman	jz	L3
721849Swollman	cmpb	%bl,(%ecx)
731849Swollman	jne	L3
741849Swollman
755243Sbde/*
765243Sbde * XXX it might be best to move the next 4 instructions to the end of the
775243Sbde * unrolled part of the loop.  The unrolled part would then be
785243Sbde *	movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L3
795243Sbde * or maybe better
805243Sbde *	movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_0
815243Sbde * for n = 0, 1, ..., 8.  The end of the loop would be
825243Sbde *	L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx
835243Sbde * where residual counts of 0 to 7 are handled at Lx.  However, this would
845243Sbde * be slower for short strings.  Cache effects are probably not so
855243Sbde * important because we are only handling a byte at a time.
865243Sbde */
871849Swollman	incl	%eax
881849Swollman	incl	%ecx
891849Swollman	decl	%edx
901849Swollman	jz	L4
911849Swollman	movb	(%eax),%bl
921849Swollman	testb	%bl,%bl
931849Swollman	jz	L3
941849Swollman	cmpb	%bl,(%ecx)
951849Swollman	jne	L3
961849Swollman
971849Swollman	incl	%eax
981849Swollman	incl	%ecx
991849Swollman	decl	%edx
1001849Swollman	jz	L4
1011849Swollman	movb	(%eax),%bl
1021849Swollman	testb	%bl,%bl
1031849Swollman	jz	L3
1041849Swollman	cmpb	%bl,(%ecx)
1051849Swollman	jne	L3
1061849Swollman
1071849Swollman	incl	%eax
1081849Swollman	incl	%ecx
1091849Swollman	decl	%edx
1101849Swollman	jz	L4
1111849Swollman	movb	(%eax),%bl
1121849Swollman	testb	%bl,%bl
1131849Swollman	jz	L3
1141849Swollman	cmpb	%bl,(%ecx)
1151849Swollman	jne	L3
1161849Swollman
1171849Swollman	incl	%eax
1181849Swollman	incl	%ecx
1191849Swollman	decl	%edx
1201849Swollman	jz	L4
1211849Swollman	movb	(%eax),%bl
1221849Swollman	testb	%bl,%bl
1231849Swollman	jz	L3
1241849Swollman	cmpb	%bl,(%ecx)
1251849Swollman	jne	L3
1261849Swollman
1271849Swollman	incl	%eax
1281849Swollman	incl	%ecx
1291849Swollman	decl	%edx
1301849Swollman	jz	L4
1311849Swollman	movb	(%eax),%bl
1321849Swollman	testb	%bl,%bl
1331849Swollman	jz	L3
1341849Swollman	cmpb	%bl,(%ecx)
1351849Swollman	jne	L3
1361849Swollman
1371849Swollman	incl	%eax
1381849Swollman	incl	%ecx
1391849Swollman	decl	%edx
1401849Swollman	jz	L4
1411849Swollman	movb	(%eax),%bl
1421849Swollman	testb	%bl,%bl
1431849Swollman	jz	L3
1441849Swollman	cmpb	%bl,(%ecx)
1451849Swollman	jne	L3
1461849Swollman
1471849Swollman	incl	%eax
1481849Swollman	incl	%ecx
1491849Swollman	decl	%edx
1501849Swollman	jz	L4
1511849Swollman	movb	(%eax),%bl
1521849Swollman	testb	%bl,%bl
1531849Swollman	jz	L3
1541849Swollman	cmpb	%bl,(%ecx)
1551849Swollman	je	L1
1561849Swollman
1571849Swollman	.align 2,0x90
1585243SbdeL3:	movzbl	(%eax),%eax		/* unsigned comparison */
1591849Swollman	movzbl	(%ecx),%ecx
1601849Swollman	subl	%ecx,%eax
1611849Swollman	popl	%ebx
1621849Swollman	ret
1631849Swollman	.align 2,0x90
1641849SwollmanL4:	xorl	%eax,%eax
1651849Swollman	popl	%ebx
1661849Swollman	ret
167184548SpeterEND(strncmp)
168217106Skib
169217106Skib	.section .note.GNU-stack,"",%progbits
170