xref: /freebsd/lib/libc/i386/string/strncmp.S (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1/*
2 * Copyright (c) 1993,94 Winning Strategies, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Winning Strategies, Inc.
16 * 4. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#if defined(LIBC_RCS) && !defined(lint)
32	.text
33        .asciz "$FreeBSD$"
34#endif /* LIBC_RCS and not lint */
35
36#include <machine/asm.h>
37
38/*
39 * strncmp(s1, s2, n)
40 *	return an integer greater than, equal to, or less than 0,
41 *	according as the first n characters of string s1 is greater
42 *	than, equal to, or less than the string s2.
43 *
44 * %eax - pointer to s1
45 * %ecx - pointer to s2
46 * %edx - length
47 *
48 * Written by:
49 *	J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
50 */
51
52/*
53 * I've unrolled the loop eight times: large enough to make a
54 * significant difference, and small enough not to totally trash the
55 * cache.
56 *
57 * TODO: change all the jz's back to je for consistency.
58 */
59
60ENTRY(strncmp)
61	pushl	%ebx
62	movl	8(%esp),%eax
63	movl	12(%esp),%ecx
64	movl	16(%esp),%edx
65	testl	%edx,%edx
66	jmp	L2			/* Jump into the loop! */
67
68	.align 2,0x90
69L1:	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