memcmp.S revision 1.1 1 /* $NetBSD: memcmp.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 *
34 *
35 * int memcmp(const char *s1, const char *s2, size_t n);
36 *
37 * for (; n-- != 0; s1++, s2++) {
38 * if (*s1 < *s2)
39 * return -1;
40 * if (*s1 > *s2)
41 * return 1;
42 * }
43 * return 0;
44 *
45 * Return: ((s1 > s2) ? 1 : (s1 < s2) ? -1 : 0)
46 *
47 * ==========================================================================
48 */
49
50 #include <machine/asm.h>
51
52 .text
53 .align 4
54 /* LINTSTUB: Func: void *memcmp(const void *, const void *, size_t) */
55 ENTRY(memcmp)
56
57 /*
58 * Check count passed in R5. If zero, return 0; otherwise continue.
59 */
60 l.sfeqi r5, 0 /* nothing to compare? */
61 l.bf .Lret_0 /* yes, return equality */
62 l.nop
63
64 #ifdef _KERNEL
65 l.sfeqi r5, 6 /* less than two words? */
66 l.bnf .Lsixbyte_compare /* yes, just compare by bytes */
67 l.nop
68 #endif
69
70 l.sfgesi r5, 7 /* less than two words? */
71 l.bnf .Lbyte_compare /* yes, just compare by bytes */
72 l.nop
73
74 l.xor r6, r3, r4 /* check alignment compatibility */
75 l.andi r6, r6, 3 /* only care about the two bits */
76 l.sfeqi r6, 0 /* same alignment? */
77 l.bnf .Lmisaligned /* no, avoid alignment errors */
78 l.nop
79
80 /*
81 * At this point, we know we read the data via word accesses.
82 */
83
84 l.andi r7, r3, 3 /* check alignment */
85 l.sfeqi r7, 0 /* word aligned? */
86 l.bf .Lword_compare /* yes, it is. */
87
88 l.sub r3, r3, r7 /* align string 1 */
89 l.sub r4, r4, r7 /* align string 2 */
90 l.add r5, r5, r7 /* pad length */
91
92 l.lwz r15, 0(r3) /* load word from s1 */
93 l.lwz r17, 0(r4) /* load word from s2 */
94
95 l.slli r7, r7, 3 /* bytes to bits */
96 l.sll r15, r15, r7 /* shift away leading bytes */
97 l.sll r17, r17, r7 /* shift away leading bytes */
98 l.j .Lword_compare /* now we can compare them */
99 l.nop
100
101 .Lword_loop:
102 l.lwz r15, 0(r3) /* load s1 word */
103 l.lwz r17, 0(r4) /* load s2 word */
104 .Lword_compare:
105 l.sfeq r15, r17 /* compare s1 and s2 words */
106 l.bnf .Lall_done /* different? we're done */
107
108 l.addi r3, r3, 4 /* advance s1 one word */
109 l.addi r4, r4, 4 /* advance s2 one word */
110 l.addi r5, r5, -4 /* decrement one word */
111 l.sfgtsi r5, 4 /* at least more than a word? */
112 l.bf .Lword_loop /* yes, loop around */
113 l.nop
114 l.sfeqi r5, 0 /* nothing left? */
115 l.bf .Lret_0 /* yes, return equality */
116 l.nop
117
118 /*
119 * Fall through to handle the last word
120 */
121
122 l.sub r3, r0, r5 /* If count <= 4, handle */
123 l.andi r3, r3, 3 /* mask off low 2 bits */
124 l.slli r3, r3, 3 /* count *= 8 */
125 l.srl r15, r15, r3 /* discard extra s1 bytes */
126 l.srl r17, r17, r3 /* discard extra s2 bytes */
127
128 l.sfeq r17, r15 /* compare result */
129 l.bnf .Lall_done
130 .Lret_0:
131 l.addi r11, r0, 0
132 l.jr lr
133 l.nop
134
135 /*
136 * The two string don't have the same word alignment.
137 */
138 .Lmisaligned:
139 l.sfeqi r6, 2 /* check for halfword alignment */
140 l.bnf .Lbyte_compare
141 l.nop
142 l.andi r7, r3, 1
143 l.sfeqi r7, 0
144 l.bf .Lhalfword_loop
145 l.nop
146 l.addi r5, r5, 1
147 l.addi r3, r3, -1
148 l.addi r4, r4, -1
149 l.lbz r15, 1(r3)
150 l.lbz r17, 1(r4)
151 l.j .Lhalfword_compare
152 l.nop
153 .Lhalfword_loop:
154 l.lhz r15, 0(r3)
155 l.lhz r17, 0(r4)
156 .Lhalfword_compare:
157 l.sfeq r15, r17
158 l.bnf .Lall_done
159 l.nop
160 l.addi r3, r3, 2
161 l.addi r4, r4, 2
162 l.addi r5, r5, -2
163 l.sfgesi r5, 2
164 l.bf .Lhalfword_loop
165 l.nop
166
167 .Lbyte_compare:
168 l.addi r5, r5, -1
169 l.sfgesi r5, 0
170 l.bnf .Lret_0
171 l.nop
172 l.lbz r15, 0(r3)
173 l.lbz r17, 0(r4)
174 l.addi r3, r3, 1
175 l.addi r4, r4, 1
176 l.sfeq r15, r17
177 l.bf .Lbyte_compare
178 l.nop
179
180 .Lall_done:
181 l.sub r11, r15, r17 /* subtract s2 from s1 */
182 l.srai r11, r11, 30 /* replicate sign bit thru bit 1 */
183 l.ori r11, r11, 1 /* make sure bit 0 is set */
184 l.jr lr
185 l.nop
186
187 #ifdef _KERNEL
188 .Lsixbyte_compare:
189 l.or r7, r3, r4
190 l.andi r7, r7, 1
191 l.sfeqi r7, 0
192 l.bnf .Lbyte_compare
193 l.nop
194 l.lhz r15, 0(r3)
195 l.lhz r17, 0(r4)
196 l.sfeq r15, r17
197 l.bnf .Lall_done
198 l.nop
199 l.lhz r15, 2(r3)
200 l.lhz r17, 2(r4)
201 l.sfeq r15, r17
202 l.bnf .Lall_done
203 l.nop
204 l.lhz r15, 4(r3)
205 l.lhz r17, 4(r4)
206 l.sfeq r15, r17
207 l.bnf .Lall_done
208 l.nop
209 l.addi r11, r0, 0
210 l.jr lr
211 l.nop
212 #endif
213 END(memcmp)
214