1e017c78wollman/*
2e017c78wollman * Copyright (c) 1993,94 Winning Strategies, Inc.
3e017c78wollman * All rights reserved.
4e017c78wollman *
5e017c78wollman * Redistribution and use in source and binary forms, with or without
6e017c78wollman * modification, are permitted provided that the following conditions
7e017c78wollman * are met:
8e017c78wollman * 1. Redistributions of source code must retain the above copyright
9e017c78wollman *    notice, this list of conditions and the following disclaimer.
10e017c78wollman * 2. Redistributions in binary form must reproduce the above copyright
11e017c78wollman *    notice, this list of conditions and the following disclaimer in the
12e017c78wollman *    documentation and/or other materials provided with the distribution.
13e017c78wollman * 3. All advertising materials mentioning features or use of this software
14e017c78wollman *    must display the following acknowledgement:
15e017c78wollman *      This product includes software developed by Winning Strategies, Inc.
16e017c78wollman * 4. The name of the author may not be used to endorse or promote products
17e017c78wollman *    derived from this software without specific prior written permission
18e017c78wollman *
19e017c78wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20e017c78wollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21e017c78wollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22e017c78wollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23e017c78wollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24e017c78wollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e017c78wollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e017c78wollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e017c78wollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28e017c78wollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e017c78wollman */
30e017c78wollman
31314a1d3peter#include <machine/asm.h>
32e6788c9obrien__FBSDID("$FreeBSD$");
33e017c78wollman
34e017c78wollman/*
35e017c78wollman * strncmp(s1, s2, n)
36e017c78wollman *	return an integer greater than, equal to, or less than 0,
37e017c78wollman *	according as the first n characters of string s1 is greater
38e017c78wollman *	than, equal to, or less than the string s2.
39e017c78wollman *
40e017c78wollman * %eax - pointer to s1
41e017c78wollman * %ecx - pointer to s2
42e017c78wollman * %edx - length
43e017c78wollman *
44e017c78wollman * Written by:
45e017c78wollman *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
46e017c78wollman */
47e017c78wollman
48e017c78wollman/*
49e017c78wollman * I've unrolled the loop eight times: large enough to make a
50e017c78wollman * significant difference, and small enough not to totally trash the
51e017c78wollman * cache.
528d3ea78bde *
538d3ea78bde * TODO: change all the jz's back to je for consistency.
54e017c78wollman */
55e017c78wollman
56e017c78wollmanENTRY(strncmp)
57e017c78wollman	pushl	%ebx
58e017c78wollman	movl	8(%esp),%eax
59e017c78wollman	movl	12(%esp),%ecx
60e017c78wollman	movl	16(%esp),%edx
61e017c78wollman	testl	%edx,%edx
62e017c78wollman	jmp	L2			/* Jump into the loop! */
63e017c78wollman
64e017c78wollman	.align 2,0x90
65e017c78wollmanL1:	incl	%eax
66e017c78wollman	incl	%ecx
67e017c78wollman	decl	%edx
68e017c78wollmanL2:	jz	L4			/* strings are equal */
69e017c78wollman	movb	(%eax),%bl
70e017c78wollman	testb	%bl,%bl
71e017c78wollman	jz	L3
72e017c78wollman	cmpb	%bl,(%ecx)
73e017c78wollman	jne	L3
74e017c78wollman
758d3ea78bde/*
768d3ea78bde * XXX it might be best to move the next 4 instructions to the end of the
778d3ea78bde * unrolled part of the loop.  The unrolled part would then be
788d3ea78bde *	movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L3
798d3ea78bde * or maybe better
808d3ea78bde *	movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_0
818d3ea78bde * for n = 0, 1, ..., 8.  The end of the loop would be
828d3ea78bde *	L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx
838d3ea78bde * where residual counts of 0 to 7 are handled at Lx.  However, this would
848d3ea78bde * be slower for short strings.  Cache effects are probably not so
858d3ea78bde * important because we are only handling a byte at a time.
868d3ea78bde */
87e017c78wollman	incl	%eax
88e017c78wollman	incl	%ecx
89e017c78wollman	decl	%edx
90e017c78wollman	jz	L4
91e017c78wollman	movb	(%eax),%bl
92e017c78wollman	testb	%bl,%bl
93e017c78wollman	jz	L3
94e017c78wollman	cmpb	%bl,(%ecx)
95e017c78wollman	jne	L3
96e017c78wollman
97e017c78wollman	incl	%eax
98e017c78wollman	incl	%ecx
99e017c78wollman	decl	%edx
100e017c78wollman	jz	L4
101e017c78wollman	movb	(%eax),%bl
102e017c78wollman	testb	%bl,%bl
103e017c78wollman	jz	L3
104e017c78wollman	cmpb	%bl,(%ecx)
105e017c78wollman	jne	L3
106e017c78wollman
107e017c78wollman	incl	%eax
108e017c78wollman	incl	%ecx
109e017c78wollman	decl	%edx
110e017c78wollman	jz	L4
111e017c78wollman	movb	(%eax),%bl
112e017c78wollman	testb	%bl,%bl
113e017c78wollman	jz	L3
114e017c78wollman	cmpb	%bl,(%ecx)
115e017c78wollman	jne	L3
116e017c78wollman
117e017c78wollman	incl	%eax
118e017c78wollman	incl	%ecx
119e017c78wollman	decl	%edx
120e017c78wollman	jz	L4
121e017c78wollman	movb	(%eax),%bl
122e017c78wollman	testb	%bl,%bl
123e017c78wollman	jz	L3
124e017c78wollman	cmpb	%bl,(%ecx)
125e017c78wollman	jne	L3
126e017c78wollman
127e017c78wollman	incl	%eax
128e017c78wollman	incl	%ecx
129e017c78wollman	decl	%edx
130e017c78wollman	jz	L4
131e017c78wollman	movb	(%eax),%bl
132e017c78wollman	testb	%bl,%bl
133e017c78wollman	jz	L3
134e017c78wollman	cmpb	%bl,(%ecx)
135e017c78wollman	jne	L3
136e017c78wollman
137e017c78wollman	incl	%eax
138e017c78wollman	incl	%ecx
139e017c78wollman	decl	%edx
140e017c78wollman	jz	L4
141e017c78wollman	movb	(%eax),%bl
142e017c78wollman	testb	%bl,%bl
143e017c78wollman	jz	L3
144e017c78wollman	cmpb	%bl,(%ecx)
145e017c78wollman	jne	L3
146e017c78wollman
147e017c78wollman	incl	%eax
148e017c78wollman	incl	%ecx
149e017c78wollman	decl	%edx
150e017c78wollman	jz	L4
151e017c78wollman	movb	(%eax),%bl
152e017c78wollman	testb	%bl,%bl
153e017c78wollman	jz	L3
154e017c78wollman	cmpb	%bl,(%ecx)
155e017c78wollman	je	L1
156e017c78wollman
157e017c78wollman	.align 2,0x90
1588d3ea78bdeL3:	movzbl	(%eax),%eax		/* unsigned comparison */
159e017c78wollman	movzbl	(%ecx),%ecx
160e017c78wollman	subl	%ecx,%eax
161e017c78wollman	popl	%ebx
162e017c78wollman	ret
163e017c78wollman	.align 2,0x90
164e017c78wollmanL4:	xorl	%eax,%eax
165e017c78wollman	popl	%ebx
166e017c78wollman	ret
1673375b83peterEND(strncmp)
168a5e01ackib
169a5e01ackib	.section .note.GNU-stack,"",%progbits
170