Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / lib / libc / i386 / string / strncmp.S
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  * $FreeBSD: src/lib/libc/i386/string/strncmp.S,v 1.6 1999/08/27 23:59:35 peter Exp $
31  * $DragonFly: src/lib/libc/i386/string/strncmp.S,v 1.3 2003/12/06 03:11:36 drhodus Exp $
32  */
33
34 #include "DEFS.h"
35
36 /*
37  * strncmp(s1, s2, n)
38  *      return an integer greater than, equal to, or less than 0,
39  *      according as the first n characters of string s1 is greater
40  *      than, equal to, or less than the string s2.
41  *
42  * %eax - pointer to s1
43  * %ecx - pointer to s2
44  * %edx - length
45  *
46  * Written by:
47  *      J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
48  */
49
50 /*
51  * I've unrolled the loop eight times: large enough to make a
52  * significant difference, and small enough not to totally trash the
53  * cache.
54  *
55  * TODO: change all the jz's back to je for consistency.
56  */
57
58 ENTRY(strncmp)
59         pushl   %ebx
60         movl    8(%esp),%eax
61         movl    12(%esp),%ecx
62         movl    16(%esp),%edx
63         testl   %edx,%edx
64         jmp     L2                      /* Jump into the loop! */
65
66         .align 2,0x90
67 L1:     incl    %eax
68         incl    %ecx
69         decl    %edx
70 L2:     jz      L4                      /* strings are equal */
71         movb    (%eax),%bl
72         testb   %bl,%bl
73         jz      L3
74         cmpb    %bl,(%ecx)
75         jne     L3
76
77 /*
78  * XXX it might be best to move the next 4 instructions to the end of the
79  * unrolled part of the loop.  The unrolled part would then be
80  *      movb n(%eax),%bl; testb %bl, %bl; je L3; cmpb n(%ecx); jne L3
81  * or maybe better
82  *      movb n(%eax),%bl; cmpb n(%ecx); jne L3; testb %bl,%bl; je return_0
83  * for n = 0, 1, ..., 8.  The end of the loop would be
84  *      L1: addl $8,%eax; addl $8,%ecx; subl $8,%edx; cmpl $8,%edx; jae Lx
85  * where residual counts of 0 to 7 are handled at Lx.  However, this would
86  * be slower for short strings.  Cache effects are probably not so
87  * important because we are only handling a byte at a time.
88  */
89         incl    %eax
90         incl    %ecx
91         decl    %edx
92         jz      L4
93         movb    (%eax),%bl
94         testb   %bl,%bl
95         jz      L3
96         cmpb    %bl,(%ecx)
97         jne     L3
98
99         incl    %eax
100         incl    %ecx
101         decl    %edx
102         jz      L4
103         movb    (%eax),%bl
104         testb   %bl,%bl
105         jz      L3
106         cmpb    %bl,(%ecx)
107         jne     L3
108
109         incl    %eax
110         incl    %ecx
111         decl    %edx
112         jz      L4
113         movb    (%eax),%bl
114         testb   %bl,%bl
115         jz      L3
116         cmpb    %bl,(%ecx)
117         jne     L3
118
119         incl    %eax
120         incl    %ecx
121         decl    %edx
122         jz      L4
123         movb    (%eax),%bl
124         testb   %bl,%bl
125         jz      L3
126         cmpb    %bl,(%ecx)
127         jne     L3
128
129         incl    %eax
130         incl    %ecx
131         decl    %edx
132         jz      L4
133         movb    (%eax),%bl
134         testb   %bl,%bl
135         jz      L3
136         cmpb    %bl,(%ecx)
137         jne     L3
138
139         incl    %eax
140         incl    %ecx
141         decl    %edx
142         jz      L4
143         movb    (%eax),%bl
144         testb   %bl,%bl
145         jz      L3
146         cmpb    %bl,(%ecx)
147         jne     L3
148
149         incl    %eax
150         incl    %ecx
151         decl    %edx
152         jz      L4
153         movb    (%eax),%bl
154         testb   %bl,%bl
155         jz      L3
156         cmpb    %bl,(%ecx)
157         je      L1
158
159         .align 2,0x90
160 L3:     movzbl  (%eax),%eax             /* unsigned comparison */
161         movzbl  (%ecx),%ecx
162         subl    %ecx,%eax
163         popl    %ebx
164         ret
165         .align 2,0x90
166 L4:     xorl    %eax,%eax
167         popl    %ebx
168         ret