1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
.file "strncmp.s"
/*
* strncmp(s1, s2, n)
*
* Compare strings (at most n bytes): s1>s2: >0 s1==s2: 0 s1<s2: <0
*
* Fast assembler language version of the following C-program for strncmp
* which represents the `standard' for the C-library.
*
* int
* strncmp(const char *s1, const char *s2, size_t n)
* {
* n++;
* if (s1 == s2)
* return (0);
* while (--n != 0 && *s1 == *s2++)
* if(*s1++ == '\0')
* return(0);
* return ((n == 0) ? 0 : (*s1 - s2[-1]));
* }
*/
#include <sys/asm_linkage.h>
ENTRY(strncmp)
save %sp, -SA(WINDOWSIZE), %sp
cmp %i2, 8
blu,a,pn %xcc, .cmp_bytes ! for small counts go do bytes
sub %i0, %i1, %i0 ! delay slot, get diff from s1 - s2
andcc %i0, 3, %g0 ! is s1 aligned
1: bz,pn %icc, .iss2 ! if so go check s2
andcc %i1, 3, %i3 ! is s2 aligned
deccc %i2 ! --n >= 0 ?
bcs,pn %xcc, .doneq
nop ! delay slot
ldub [%i0], %i4 ! else cmp one byte
ldub [%i1], %i5
inc %i0
cmp %i4, %i5
bne,pn %icc, .noteqb
inc %i1
tst %i4 ! terminating zero
bnz,pt %icc, 1b
andcc %i0, 3, %g0
b,a .doneq
.iss2:
set 0x7efefeff, %l6
set 0x81010100, %l7
sethi %hi(0xff000000), %l0 ! masks to test for terminating null
sethi %hi(0x00ff0000), %l1
srl %l1, 8, %l2 ! generate 0x0000ff00 mask
bz,pn %icc, .w4cmp ! if s2 word aligned, compare words
cmp %i3, 2 ! check if s2 half aligned
be,pn %icc, .w2cmp
cmp %i3, 1 ! check if aligned to 1 or 3 bytes
.w3cmp: ldub [%i1], %i5
inc 1, %i1
be,pt %icc, .w1cmp
sll %i5, 24, %i5
sub %i0, %i1, %i0
2:
deccc 4, %i2 ! n >= 4 ?
bgeu,a,pt %xcc, 3f
lduw [%i1], %i3 ! delay slot
dec %i1 ! reset s2
inc %i0 ! reset s1 diff
b .cmp_bytes ! do a byte at a time if n < 4
inc 4, %i2
3:
lduw [%i0 + %i1], %i4
inc 4, %i1
srl %i3, 8, %l4 ! merge with the other half
or %l4, %i5, %i5
cmp %i4, %i5
be,pn %icc, 1f
add %i4, %l6, %l3
b,a .noteq
1: xor %l3, %i4, %l3
and %l3, %l7, %l3
cmp %l3, %l7
be,a,pt %icc, 2b
sll %i3, 24, %i5
!
! For 7-bit characters, we know one of the bytes is zero, but for
! 8-bit characters, the zero detection algorithm gives some false
! triggers ... check every byte individually.
!
andcc %i4, %l0, %g0 ! check if first byte was zero
bnz,pt %icc, 1f
andcc %i4, %l1, %g0 ! check if second byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, %l2, %g0 ! check if third byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, 0xff, %g0 ! check if last byte is zero
b,a .doneq
1: bnz,pn %icc, 2b
sll %i3, 24, %i5
b,a .doneq
.w1cmp: clr %l4
lduh [%i1], %l4
inc 2, %i1
sll %l4, 8, %l4
or %i5, %l4, %i5
sub %i0, %i1, %i0
3:
deccc 4, %i2 ! n >= 4 ?
bgeu,a,pt %xcc, 4f
lduw [%i1], %i3 ! delay slot
dec 3, %i1 ! reset s2
inc 3, %i0 ! reset s1 diff
b .cmp_bytes ! do a byte at a time if n < 4
inc 4, %i2
4:
lduw [%i0 + %i1], %i4
inc 4, %i1
srl %i3, 24, %l4 ! merge with the other half
or %l4, %i5, %i5
cmp %i4, %i5
be,pt %icc, 1f
add %i4, %l6, %l3
b,a .noteq
1: xor %l3, %i4, %l3
and %l3, %l7, %l3
cmp %l3, %l7
be,a,pt %icc, 3b
sll %i3, 8, %i5
andcc %i4, %l0, %g0 ! check if first byte was zero
bnz,pt %icc, 1f
andcc %i4, %l1, %g0 ! check if second byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, %l2, %g0 ! check if third byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, 0xff, %g0 ! check if last byte is zero
b,a .doneq
1: bnz,pn %icc, 3b
sll %i3, 8, %i5
b,a .doneq
.w2cmp:
lduh [%i1], %i5 ! read a halfword to align s2
inc 2, %i1
sll %i5, 16, %i5
sub %i0, %i1, %i0
4:
deccc 4, %i2 ! n >= 4 ?
bgeu,a,pt %xcc, 5f
lduw [%i1], %i3 ! delay slot
dec 2, %i1 ! reset s2
inc 2, %i0 ! reset s1 diff
b .cmp_bytes ! do a byte at a time if n < 4
inc 4, %i2 ! delay slot
5:
lduw [%i1 + %i0], %i4 ! read a word from s2
inc 4, %i1
srl %i3, 16, %l4 ! merge with the other half
or %l4, %i5, %i5
cmp %i4, %i5
be,pt %icc, 1f
add %i4, %l6, %l3
b,a .noteq
1: xor %l3, %i4, %l3 ! are any bytes 0?
and %l3, %l7, %l3
cmp %l3, %l7
be,a,pt %icc, 4b
sll %i3, 16, %i5
andcc %i4, %l0, %g0 ! check if first byte was zero
bnz,pt %icc, 1f
andcc %i4, %l1, %g0 ! check if second byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, %l2, %g0 ! check if third byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, 0xff, %g0 ! check if last byte is zero
b,a .doneq
1: bnz,pn %icc, 4b
sll %i3, 16, %i5
b,a .doneq
.w4cmp: sub %i0, %i1, %i0
lduw [%i1], %i5 ! read a word from s1
5: cmp %i2, 0
be,a,pn %xcc, .doneq
nop
lduw [%i1], %i5 ! read a word from s1
deccc 4, %i2 ! n >= 4 ?
bcs,a,pn %xcc, .cmp_bytes ! do a byte at a time if n < 4
inc 4, %i2
lduw [%i1 + %i0], %i4 ! read a word from s2
cmp %i4, %i5
inc 4, %i1
be,pt %icc, 1f
add %i4, %l6, %l3
b,a .noteq
1: xor %l3, %i4, %l3
and %l3, %l7, %l3
cmp %l3, %l7
be,pt %icc, 5b
nop
andcc %i4, %l0, %g0 ! check if first byte was zero
bnz,pt %icc, 1f
andcc %i4, %l1, %g0 ! check if second byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, %l2, %g0 ! check if third byte was zero
b,a .doneq
1: bnz,pt %icc, 1f
andcc %i4, 0xff, %g0 ! check if last byte is zero
b,a .doneq
1: bnz,a,pn %icc, 5b
lduw [%i1], %i5
.doneq: ret
restore %g0, %g0, %o0 ! equal return zero
.noteq: srl %i4, 24, %l4
srl %i5, 24, %l5
subcc %l4, %l5, %i0
bne,pt %icc, 6f
andcc %l4, 0xff, %g0
bz .doneq
sll %i4, 8, %l4
sll %i5, 8, %l5
srl %l4, 24, %l4
srl %l5, 24, %l5
subcc %l4, %l5, %i0
bne,pt %icc, 6f
andcc %l4, 0xff, %g0
bz,pt %icc, .doneq
sll %i4, 16, %l4
sll %i5, 16, %l5
srl %l4, 24, %l4
srl %l5, 24, %l5
subcc %l4, %l5, %i0
bne,pt %icc, 6f
andcc %l4, 0xff, %g0
bz,pt %icc, .doneq
nop
.noteqb:
and %i4, 0xff, %l4
and %i5, 0xff, %l5
subcc %l4, %l5, %i0
6: ret
restore %i0, %g0, %o0
! Do a byte by byte comparison, disregarding alignments
.cmp_bytes:
deccc %i2 ! --n >= 0 ?
1:
bcs,pn %xcc, .doneq
nop ! delay slot
ldub [%i1 + %i0], %i4 ! read a byte from s1
ldub [%i1], %i5 ! read a byte from s2
inc %i1
cmp %i4, %i5
bne,pt %icc, .noteqb
tst %i4 ! terminating zero
bnz,pt %icc, 1b
deccc %i2 ! --n >= 0
b,a .doneq
SET_SIZE(strncmp)
|