strncmp.S revision 85437
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
311849Swollman#if defined(LIBC_RCS) && !defined(lint)
321849Swollman	.text
331849Swollman        .asciz "$FreeBSD: head/lib/libc/i386/string/strncmp.S 85437 2001-10-24 20:29:14Z peter $"
341849Swollman#endif /* LIBC_RCS and not lint */
351849Swollman
361849Swollman#include <machine/asm.h>
371849Swollman
381849Swollman/*
391849Swollman * strncmp(s1, s2, n)
401849Swollman *	return an integer greater than, equal to, or less than 0,
411849Swollman *	according as the first n characters of string s1 is greater
421849Swollman *	than, equal to, or less than the string s2.
431849Swollman *
441849Swollman * %eax - pointer to s1
451849Swollman * %ecx - pointer to s2
461849Swollman * %edx - length
471849Swollman *
481849Swollman * Written by:
491849Swollman *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
501849Swollman */
511849Swollman
521849Swollman/*
531849Swollman * I've unrolled the loop eight times: large enough to make a
541849Swollman * significant difference, and small enough not to totally trash the
551849Swollman * cache.
561849Swollman *
571849Swollman * TODO: change all the jz's back to je for consistency.
581849Swollman */
591849Swollman
601849SwollmanENTRY(strncmp)
611849Swollman	pushl	%ebx
621849Swollman	movl	8(%esp),%eax
631849Swollman	movl	12(%esp),%ecx
641849Swollman	movl	16(%esp),%edx
651849Swollman	testl	%edx,%edx
661849Swollman	jmp	L2			/* Jump into the loop! */
671849Swollman
681849Swollman	.align 2,0x90
691849SwollmanL1:	incl	%eax
70	incl	%ecx
71	decl	%edx
72L2:	jz	L4			/* strings are equal */
73	movb	(%eax),%bl
74	testb	%bl,%bl
75	jz	L3
76	cmpb	%bl,(%ecx)
77	jne	L3
78
79/*
80 * XXX it might be best to move the next 4 instructions to the end of the
81 * unrolled part of the loop.  The unrolled part would then be
82 *	movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L3
83 * or maybe better
84 *	movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_0
85 * for n = 0, 1, ..., 8.  The end of the loop would be
86 *	L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx
87 * where residual counts of 0 to 7 are handled at Lx.  However, this would
88 * be slower for short strings.  Cache effects are probably not so
89 * important because we are only handling a byte at a time.
90 */
91	incl	%eax
92	incl	%ecx
93	decl	%edx
94	jz	L4
95	movb	(%eax),%bl
96	testb	%bl,%bl
97	jz	L3
98	cmpb	%bl,(%ecx)
99	jne	L3
100
101	incl	%eax
102	incl	%ecx
103	decl	%edx
104	jz	L4
105	movb	(%eax),%bl
106	testb	%bl,%bl
107	jz	L3
108	cmpb	%bl,(%ecx)
109	jne	L3
110
111	incl	%eax
112	incl	%ecx
113	decl	%edx
114	jz	L4
115	movb	(%eax),%bl
116	testb	%bl,%bl
117	jz	L3
118	cmpb	%bl,(%ecx)
119	jne	L3
120
121	incl	%eax
122	incl	%ecx
123	decl	%edx
124	jz	L4
125	movb	(%eax),%bl
126	testb	%bl,%bl
127	jz	L3
128	cmpb	%bl,(%ecx)
129	jne	L3
130
131	incl	%eax
132	incl	%ecx
133	decl	%edx
134	jz	L4
135	movb	(%eax),%bl
136	testb	%bl,%bl
137	jz	L3
138	cmpb	%bl,(%ecx)
139	jne	L3
140
141	incl	%eax
142	incl	%ecx
143	decl	%edx
144	jz	L4
145	movb	(%eax),%bl
146	testb	%bl,%bl
147	jz	L3
148	cmpb	%bl,(%ecx)
149	jne	L3
150
151	incl	%eax
152	incl	%ecx
153	decl	%edx
154	jz	L4
155	movb	(%eax),%bl
156	testb	%bl,%bl
157	jz	L3
158	cmpb	%bl,(%ecx)
159	je	L1
160
161	.align 2,0x90
162L3:	movzbl	(%eax),%eax		/* unsigned comparison */
163	movzbl	(%ecx),%ecx
164	subl	%ecx,%eax
165	popl	%ebx
166	ret
167	.align 2,0x90
168L4:	xorl	%eax,%eax
169	popl	%ebx
170	ret
171