forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strcmp.S
367 lines (313 loc) · 9.66 KB
/
strcmp.S
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*-
* Copyright (c) 2018 Instituto de Pesquisas Eldorado
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#if 0
RCSID("$NetBSD: strcmp.S,v 1.0 2018/05/10 12:33:02 alexandre Exp $")
#endif
#ifdef USE_VSX
#define STRCMP_BUILD_FUNCTION_NAME(name) name ## _vsx
/* Define VSX instructions. */
#define XX1_A(a) ((a & 0x1f) << 16)
#define XX1_B(b) ((b & 0x1f) << 11)
#define XX1_T(t) ((((t & 0x1f) << 21) | ((t & 0x20) >> 5)))
#define XX1_S(s) XX1_T(s)
#define XX3_T(t) XX1_T(t)
#define XX3_A(a) ((((a & 0x1f) << 16) | ((a & 0x20) >> 3)))
#define XX3_B(b) ((((b & 0x1f) << 11) | ((b & 0x20) >> 4)))
#define XX3_BF(bf) ((bf & 0x3f) << 23)
#define XX3_DM(dm) ((dm & 0x3) << 8)
#define LXVD2X_INST ((31 << 26) | (844 << 1))
#define XXLOR_INST ((15 << 28) | (73 << 4))
#define XXLXOR_INST ((15 << 28) | (77 << 4))
#define XSCMPODP_INST ((15 << 28) | (43 << 3))
#define XXPERMDI_INST ((15 << 28) | (5 << 4))
#define MFVSRD_INST ((31 << 26) | 0x66)
#define LXVD2X(t, a, b) .long (LXVD2X_INST | XX1_A(a) | XX1_B(b) | XX1_T(t))
#define XXLOR(t, a, b) .long (XXLOR_INST | XX3_A(a) | XX3_B(b) | XX3_T(t))
#define XXLXOR(t, a, b) .long (XXLXOR_INST | XX3_A(a) | XX3_B(b) | XX3_T(t))
#define XSCMPODP(bf, a, b) .long (XSCMPODP_INST | XX3_A(a) | XX3_B(b) | \
XX3_BF(bf))
#define XXPERMDI(t, a, b, dm) .long (XXPERMDI_INST | XX3_A(a) | \
XX3_B(b) | XX3_T(t) | XX3_DM(dm))
#define MFVSRD(a, s) .long (MFVSRD_INST | XX1_A(a) | XX1_S(s))
#define STRCMP_MULTI_ALIGNMENT_BYTES 16
#else
#define STRCMP_BUILD_FUNCTION_NAME(name) name ## _plain
#define STRCMP_MULTI_ALIGNMENT_BYTES 8
#endif
/* Alignment mask. */
#define STRCMP_MULTI_ALIGNMENT_MASK (STRCMP_MULTI_ALIGNMENT_BYTES - 1)
ENTRY(STRCMP_FUNCTION_NAME)
/* Check alignment of parameter 1. */
andi. %r7,%r3,STRCMP_MULTI_ALIGNMENT_MASK
beq .Lstrcmp_param1_aligned
lbz %r5,0(%r3) /* Load chars. */
lbz %r6,0(%r4)
cmpdi %r5,0 /* Check if char is zero. */
beq+ .Lstrcmp_end
cmpd %r5,%r6 /* Check if chars are different. */
bne+ .Lstrcmp_end
.Lstrcmp_param1_align_loop:
lbzu %r5,1(%r3) /* Load chars. */
lbzu %r6,1(%r4)
cmpdi %r5,0 /* Check if char is zero. */
beq+ .Lstrcmp_end
cmpd %r5,%r6 /* Check if chars are different. */
bne+ .Lstrcmp_end
andi. %r7,%r3,STRCMP_MULTI_ALIGNMENT_MASK /* Check alignment. */
bne+ .Lstrcmp_param1_align_loop
.Lstrcmp_param1_aligned:
/* If parameter 2 is aligned compare by qword/word,
* else compare by byte. */
andi. %r7,%r4,STRCMP_MULTI_ALIGNMENT_MASK
#ifdef USE_VSX
beq .Lstrcmp_compare_by_qword
#else
beq .Lstrcmp_compare_by_word
#endif
lbz %r5,0(%r3) /* Load chars. */
lbz %r6,0(%r4)
cmpdi %r5,0 /* Check if char is zero. */
beq+ .Lstrcmp_end
cmpd %r5,%r6 /* Check if chars are different. */
bne+ .Lstrcmp_end
.Lstrcmp_compare_by_byte_loop:
/* Check 1st byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 2nd byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 3rd byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 4th byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 5th byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 6th byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 7th byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
bne .Lstrcmp_end
/* Check 8th byte. */
lbzu %r5,1(%r3)
lbzu %r6,1(%r4)
cmpdi %r5,0
beq .Lstrcmp_end
cmpd %r5,%r6
beq+ .Lstrcmp_compare_by_byte_loop
.Lstrcmp_end:
sub %r3,%r5,%r6
blr
#ifdef USE_VSX
.Lstrcmp_compare_by_qword:
.align 5
XXLXOR(32,32,32) /* VS32 (VR0) <- Zero */
/* Load qwords. */
LXVD2X(33,0,3) /* VS33 (VR1) <- Parameter_1 */
LXVD2X(34,0,4) /* VS34 (VR2) <- Parameter_2 */
/* Check if qwords are different. */
XXLXOR(35,33,34) /* VS35 (VR3) <- xor(Parameter_1, Parameter_2) */
/* Check if qwords contain zero. */
vcmpequb 4,1,0 /* VS36 (VR4) <- cmpb(Parameter_1, Zero) */
/*
* If qwords are different or contain zero,
* find what byte is different or contains zero,
* else load next double words.
*/
XXLOR(2,35,36) /* VS2 <- or(VS35 (VR3), VS36 (VR4)) */
XSCMPODP(7,2,32) /* cmp(VS2:0:32, Zero) */
bne cr7, .Lstrcmp_compare_by_qword_found_zero_difference
XXPERMDI(2,2,2,2) /* Swap bytes. */
XSCMPODP(7,2,32) /* cmp(VS2:0:32, Zero) */
bne cr7,.Lstrcmp_compare_by_qword_found_zero_difference_increment
.Lstrcmp_compare_by_qword_loop:
/* Increment data addresses. */
addi %r3,%r3,16
addi %r4,%r4,16
/* Load qwords. */
LXVD2X(33,0,3) /* VS33 (VR1) <- Parameter_1 */
LXVD2X(34,0,4) /* VS34 (VR2) <- Parameter_2 */
/* Check if qwords are different. */
XXLXOR(35,33,34) /* VS35 (VR3) <- xor(Parameter_1, Parameter_2) */
/* Check if qwords contain zero. */
vcmpequb 4,1,0 /* VS36 (VR4) <- cmpb(Parameter_1, Zero) */
/*
* If qwords are different or contain zero,
* find what byte is different or contains zero,
* else load next double words.
*/
XXLOR(2,35,36) /* VS2 <- or(VS35 (VR3), VS36 (VR4)) */
XSCMPODP(7,2,32) /* cmp(VS2:0:32, Zero) */
bne cr7, .Lstrcmp_compare_by_qword_found_zero_difference
XXPERMDI(2,2,2,2) /* Swap bytes. */
XSCMPODP(7,2,32) /* cmp(VS2:0:32, Zero) */
beq cr7, .Lstrcmp_compare_by_qword_loop
.Lstrcmp_compare_by_qword_found_zero_difference_increment:
addi %r3,%r3,8 /* Increment data addresses. */
addi %r4,%r4,8
.Lstrcmp_compare_by_qword_found_zero_difference:
MFVSRD(9,2) /* %r9 <- %VS2:0:32 */
#else
.Lstrcmp_compare_by_word:
ld %r5,0(%r3) /* Load double words. */
ld %r6,0(%r4)
xor %r8,%r8,%r8 /* %r8 <- Zero. */
xor %r0,%r5,%r6 /* Check if double words are different. */
cmpb %r7,%r5,%r8 /* Check if double words contain zero. */
/*
* If double words are different or contain zero,
* find what byte is different or contains zero,
* else load next double words.
*/
or. %r9,%r7,%r0
bne .Lstrcmp_check_zeros_differences
.Lstrcmp_compare_by_word_loop:
addi %r3,%r3,8 /* Increment data addresses. */
addi %r4,%r4,8
ld %r5,0(%r3) /* Load double words. */
ld %r6,0(%r4)
xor %r0,%r5,%r6 /* Check if double words are different. */
cmpb %r7,%r5,%r8 /* Check if double words contain zero. */
/*
* If double words are different or contain zero,
* find what byte is different or contains zero,
* else load next double words.
*/
or. %r9,%r7,%r0
beq .Lstrcmp_compare_by_word_loop
#endif
.Lstrcmp_check_zeros_differences:
/* Find what byte is different or contains zero. */
/* Check 1st byte. */
rldicr. %r0,%r9,0,7
bne .Lstrcmp_found_zero_difference_in_1
/* Check 2nd byte. */
rldicr. %r0,%r9,8,7
bne .Lstrcmp_found_zero_difference_in_2
/* Check 3rd byte. */
rldicr. %r0,%r9,16,7
bne .Lstrcmp_found_zero_difference_in_3
/* Check 4th byte. */
rldicr. %r0,%r9,24,7
bne .Lstrcmp_found_zero_difference_in_4
/* Check 5th byte. */
andis. %r0,%r9,0xff00
bne .Lstrcmp_found_zero_difference_in_5
/* Check 6th byte. */
andis. %r0,%r9,0xff
bne .Lstrcmp_found_zero_difference_in_6
/* Check 7th byte. */
andi. %r0,%r9,0xff00
bne .Lstrcmp_found_zero_difference_in_7
.Lstrcmp_found_zero_difference_in_8:
/* 8th byte is different or contains zero. */
#ifdef USE_VSX
lbz %r5,7(%r3)
lbz %r6,7(%r4)
#else
andi. %r5,%r5,0xff
andi. %r6,%r6,0xff
#endif
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_1:
/* 1st byte is different or contains zero. */
lbz %r5,0(%r3)
lbz %r6,0(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_2:
/* 2nd byte is different or contains zero. */
lbz %r5,1(%r3)
lbz %r6,1(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_3:
/* 3rd byte is different or contains zero. */
lbz %r5,2(%r3)
lbz %r6,2(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_4:
/* 4th byte is different or contains zero. */
lbz %r5,3(%r3)
lbz %r6,3(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_5:
/* 5th byte is different or contains zero. */
lbz %r5,4(%r3)
lbz %r6,4(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_6:
/* 6th byte is different or contains zero. */
lbz %r5,5(%r3)
lbz %r6,5(%r4)
sub %r3,%r5,%r6
blr
.Lstrcmp_found_zero_difference_in_7:
/* 7th byte is different or contains zero. */
lbz %r5,6(%r3)
lbz %r6,6(%r4)
sub %r3,%r5,%r6
blr
END(STRCMP_FUNCTION_NAME)
.section .note.GNU-stack,"",%progbits