about summary refs log tree commit diff
path: root/sysdeps/powerpc/powerpc64/power8/strcasecmp.S
blob: d147c6097767b06f9027d6eb4203a327f5cffdc2 (plain) (blame)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* Optimized strcasecmp implementation for PowerPC64.
   Copyright (C) 2016-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <sysdep.h>
#include <locale-defines.h>

/* int [r3] strcasecmp (const char *s1 [r3], const char *s2 [r4] ) */

#ifndef USE_AS_STRNCASECMP
#  define __STRCASECMP __strcasecmp
#  define STRCASECMP   strcasecmp
#else
#  define __STRCASECMP __strncasecmp
#  define STRCASECMP   strncasecmp
#endif
/* Convert 16 bytes to lowercase and compare */
#define TOLOWER()     \
	vaddubm	v8, v4, v1; \
	vaddubm	v7, v4, v3; \
	vcmpgtub	v8, v8, v2; \
	vsel	v4, v7, v4, v8; \
	vaddubm	v8, v5, v1; \
	vaddubm	v7, v5, v3; \
	vcmpgtub	v8, v8, v2; \
	vsel	v5, v7, v5, v8; \
	vcmpequb.	v7, v5, v4;

/*
 * Get 16 bytes for unaligned case.
 * reg1: Vector to hold next 16 bytes.
 * reg2: Address to read from.
 * reg3: Permute control vector.
 * v8: Tmp vector used to mask unwanted bytes.
 * v9: Tmp vector,0 when null is found on first 16 bytes
 */
#ifdef __LITTLE_ENDIAN__
#define GET16BYTES(reg1, reg2, reg3) \
	lvx	reg1, 0, reg2; \
	vspltisb	v8, -1; \
	vperm	v8, v8, reg1, reg3; \
	vcmpequb.	v8, v0, v8; \
	beq	cr6, 1f; \
	vspltisb	v9, 0; \
	b	2f; \
	.align 4; \
1: \
	addi	r6, reg2, 16; \
	lvx	v9, 0, r6; \
2: \
	vperm	reg1, v9, reg1, reg3;
#else
#define GET16BYTES(reg1, reg2, reg3) \
	lvx	reg1, 0, reg2; \
	vspltisb	 v8, -1; \
	vperm	v8, reg1, v8,  reg3; \
	vcmpequb.	v8, v0, v8; \
	beq	cr6, 1f; \
	vspltisb	v9, 0; \
	b	2f; \
	.align 4; \
1: \
	addi	r6, reg2, 16; \
	lvx	v9, 0, r6; \
2: \
	vperm	reg1, reg1, v9, reg3;
#endif

/* Check null in v4, v5 and convert to lower.  */
#define CHECKNULLANDCONVERT() \
	vcmpequb.	v7, v0, v5; \
	beq	cr6, 3f; \
	vcmpequb.	v7, v0, v4; \
	beq	cr6, 3f; \
	b	L(null_found); \
	.align  4; \
3: \
	TOLOWER()

#ifdef _ARCH_PWR8
#  define VCLZD_V8_v7	vclzd	v8, v7;
#  define MFVRD_R3_V1	mfvrd	r3, v1;
#  define VSUBUDM_V9_V8	vsubudm	v9, v9, v8;
#  define VPOPCNTD_V8_V8	vpopcntd v8, v8;
#  define VADDUQM_V7_V8	vadduqm	v9, v7, v8;
#else
#  define VCLZD_V8_v7	.long	0x11003fc2
#  define MFVRD_R3_V1	.long	0x7c230067
#  define VSUBUDM_V9_V8	.long	0x112944c0
#  define VPOPCNTD_V8_V8	.long	0x110047c3
#  define VADDUQM_V7_V8	.long	0x11274100
#endif

	.machine  power7

ENTRY (__STRCASECMP)
#ifdef USE_AS_STRNCASECMP
	CALL_MCOUNT 3
#else
	CALL_MCOUNT 2
#endif
#define rRTN	r3	/* Return value */
#define rSTR1	r10	/* 1st string */
#define rSTR2	r4	/* 2nd string */
#define rCHAR1	r6	/* Byte read from 1st string */
#define rCHAR2	r7	/* Byte read from 2nd string */
#define rADDR1	r8	/* Address of tolower(rCHAR1) */
#define rADDR2	r12	/* Address of tolower(rCHAR2) */
#define rLWR1	r8	/* Word tolower(rCHAR1) */
#define rLWR2	r12	/* Word tolower(rCHAR2) */
#define rTMP	r9
#define rLOC	r11	/* Default locale address */

	cmpd	cr7, rRTN, rSTR2

	/* Get locale address.  */
	ld 	rTMP, __libc_tsd_LOCALE@got@tprel(r2)
	add 	rLOC, rTMP, __libc_tsd_LOCALE@tls
	ld	rLOC, 0(rLOC)

	mr	rSTR1, rRTN
	li	rRTN, 0
	beqlr	cr7
#ifdef USE_AS_STRNCASECMP
	cmpdi	cr7, r5, 0
	beq	cr7, L(retnull)
	cmpdi	cr7, r5, 16
	blt	cr7, L(bytebybyte)
#endif
	vspltisb	v0, 0
	vspltisb	v8, -1
	/* Check for null in initial characters.
	   Check max of 16 char depending on the alignment.
	   If null is present, proceed byte by byte.  */
	lvx	v4, 0, rSTR1
#ifdef  __LITTLE_ENDIAN__
	lvsr	v10, 0, rSTR1	/* Compute mask.  */
	vperm	v9, v8, v4, v10	/* Mask bits that are not part of string.  */
#else
	lvsl	v10, 0, rSTR1
	vperm	v9, v4, v8, v10
#endif
	vcmpequb.	v9, v0, v9	/* Check for null bytes.  */
	bne	cr6, L(bytebybyte)
	lvx	v5, 0, rSTR2
	/* Calculate alignment.  */
#ifdef __LITTLE_ENDIAN__
	lvsr	v6, 0, rSTR2
	vperm	v9, v8, v5, v6	/* Mask bits that are not part of string.  */
#else
	lvsl	v6, 0, rSTR2
	vperm	v9, v5, v8, v6
#endif
	vcmpequb.	v9, v0, v9	/* Check for null bytes.  */
	bne	cr6, L(bytebybyte)
	/* Check if locale has non ascii characters.  */
	ld	rTMP, 0(rLOC)
	addi r6, rTMP,LOCALE_DATA_VALUES+_NL_CTYPE_NONASCII_CASE*SIZEOF_VALUES
	lwz	rTMP, 0(r6)
	cmpdi	cr7, rTMP, 1
	beq	cr7, L(bytebybyte)

	/* Load vector registers with values used for TOLOWER.  */
	/* Load v1 = 0xbf, v2 = 0x19 v3 = 0x20 in each byte.  */
	vspltisb	v3, 2
	vspltisb	v9, 4
	vsl	v3, v3, v9
	vaddubm	v1, v3, v3
	vnor	v1, v1, v1
	vspltisb	v2, 7
	vsububm	v2, v3, v2

	andi.	rADDR1, rSTR1, 0xF
	beq	cr0, L(align)
	addi	r6, rSTR1, 16
	lvx	v9, 0, r6
	/* Compute 16 bytes from previous two loads.  */
#ifdef __LITTLE_ENDIAN__
	vperm	v4, v9, v4, v10
#else
	vperm	v4, v4, v9, v10
#endif
L(align):
	andi.	rADDR2, rSTR2, 0xF
	beq	cr0, L(align1)
	addi	r6, rSTR2, 16
	lvx	v9, 0, r6
	/* Compute 16 bytes from previous two loads.  */
#ifdef __LITTLE_ENDIAN__
	vperm	v5, v9, v5, v6
#else
	vperm	v5, v5, v9, v6
#endif
L(align1):
	CHECKNULLANDCONVERT()
	blt	cr6, L(match)
	b	L(different)
	.align 	4
L(match):
	clrldi	r6, rSTR1, 60
	subfic	r7, r6, 16
#ifdef USE_AS_STRNCASECMP
	sub	r5, r5, r7
#endif
	add	rSTR1, rSTR1, r7
	add	rSTR2, rSTR2, r7
	andi.	rADDR2, rSTR2, 0xF
	addi	rSTR1, rSTR1, -16
	addi	rSTR2, rSTR2, -16
	beq	cr0, L(aligned)
#ifdef __LITTLE_ENDIAN__
	lvsr	v6, 0, rSTR2
#else
	lvsl	v6, 0, rSTR2
#endif
	/* There are 2 loops depending on the input alignment.
	   Each loop gets 16 bytes from s1 and s2, check for null,
	   convert to lowercase and compare. Loop till difference
	   or null occurs. */
L(s1_align):
	addi	rSTR1, rSTR1, 16
	addi	rSTR2, rSTR2, 16
#ifdef USE_AS_STRNCASECMP
	cmpdi	cr7, r5, 16
	blt	cr7, L(bytebybyte)
	addi	r5, r5, -16
#endif
	lvx	v4, 0, rSTR1
	GET16BYTES(v5, rSTR2, v6)
	CHECKNULLANDCONVERT()
	blt	cr6, L(s1_align)
	b	L(different)
	.align 	4
L(aligned):
	addi	rSTR1, rSTR1, 16
	addi	rSTR2, rSTR2, 16
#ifdef USE_AS_STRNCASECMP
	cmpdi	cr7, r5, 16
	blt	cr7, L(bytebybyte)
	addi	r5, r5, -16
#endif
	lvx	v4, 0, rSTR1
	lvx	v5, 0, rSTR2
	CHECKNULLANDCONVERT()
	blt	cr6, L(aligned)

	/* Calculate and return the difference. */
L(different):
	vaddubm	v1, v3, v3
	vcmpequb	v7, v0, v7
#ifdef __LITTLE_ENDIAN__
	/* Count trailing zero.  */
	vspltisb	v8, -1
	VADDUQM_V7_V8
	vandc	v8, v9, v7
	VPOPCNTD_V8_V8
	vspltb	v6, v8, 15
	vcmpequb.	v6, v6, v1
	blt	cr6, L(shift8)
#else
	/* Count leading zero.  */
	VCLZD_V8_v7
	vspltb	v6, v8, 7
	vcmpequb.	v6, v6, v1
	blt	cr6, L(shift8)
	vsro	v8, v8, v1
#endif
	b	L(skipsum)
	.align  4
L(shift8):
	vsumsws		v8, v8, v0
L(skipsum):
#ifdef __LITTLE_ENDIAN__
	/* Shift registers based on leading zero count.  */
	vsro	v6, v5, v8
	vsro	v7, v4, v8
	/* Merge and move to GPR.  */
	vmrglb	v6, v6, v7
	vslo	v1, v6, v1
	MFVRD_R3_V1
	/* Place the characters that are different in first position.  */
	sldi	rSTR2, rRTN, 56
	srdi	rSTR2, rSTR2, 56
	sldi	rSTR1, rRTN, 48
	srdi	rSTR1, rSTR1, 56
#else
	vslo	v6, v5, v8
	vslo	v7, v4, v8
	vmrghb	v1, v6, v7
	MFVRD_R3_V1
	srdi	rSTR2, rRTN, 48
	sldi	rSTR2, rSTR2, 56
	srdi	rSTR2, rSTR2, 56
	srdi	rSTR1, rRTN, 56
#endif
	subf  	rRTN, rSTR1, rSTR2
	extsw 	rRTN, rRTN
	blr

	.align  4
	/* OK. We've hit the end of the string. We need to be careful that
	   we don't compare two strings as different because of junk beyond
	   the end of the strings...  */
L(null_found):
	vaddubm	v10, v3, v3
#ifdef __LITTLE_ENDIAN__
	/* Count trailing zero.  */
	vspltisb	v8, -1
	VADDUQM_V7_V8
	vandc	v8, v9, v7
	VPOPCNTD_V8_V8
	vspltb	v6, v8, 15
	vcmpequb.	v6, v6, v10
	blt	cr6, L(shift_8)
#else
	/* Count leading zero.  */
	VCLZD_V8_v7
	vspltb	v6, v8, 7
	vcmpequb.	v6, v6, v10
	blt	cr6, L(shift_8)
	vsro	v8, v8, v10
#endif
	b	L(skipsum1)
	.align  4
L(shift_8):
	vsumsws	v8, v8, v0
L(skipsum1):
	/* Calculate shift count based on count of zero.  */
	vspltisb	v10, 7
	vslb	v10, v10, v10
	vsldoi	v9, v0, v10, 1
	VSUBUDM_V9_V8
	vspltisb	v8, 8
	vsldoi	v8, v0, v8, 1
	VSUBUDM_V9_V8
	/* Shift and remove junk after null character.  */
#ifdef __LITTLE_ENDIAN__
	vslo	v5, v5, v9
	vslo	v4, v4, v9
#else
	vsro	v5, v5, v9
	vsro	v4, v4, v9
#endif
	/* Convert and compare 16 bytes.  */
	TOLOWER()
	blt	cr6, L(retnull)
	b	L(different)
	.align  4
L(retnull):
	li	rRTN, 0
	blr
	.align  4
L(bytebybyte):
	/* Unrolling loop for POWER: loads are done with 'lbz' plus
	offset and string descriptors are only updated in the end
	of loop unrolling. */
	ld	rLOC, LOCALE_CTYPE_TOLOWER(rLOC)
	lbz	rCHAR1, 0(rSTR1)	/* Load char from s1 */
	lbz	rCHAR2, 0(rSTR2)	/* Load char from s2 */
#ifdef USE_AS_STRNCASECMP
	rldicl	rTMP, r5, 62, 2
	cmpdi	cr7, rTMP, 0
	beq	cr7, L(lessthan4)
	mtctr	rTMP
#endif
L(loop):
	cmpdi	rCHAR1, 0		/* *s1 == '\0' ? */
	sldi	rADDR1, rCHAR1, 2	/* Calculate address for tolower(*s1) */
	sldi	rADDR2, rCHAR2, 2	/* Calculate address for tolower(*s2) */
	lwzx	rLWR1, rLOC, rADDR1	/* Load tolower(*s1) */
	lwzx	rLWR2, rLOC, rADDR2	/* Load tolower(*s2) */
	cmpw	cr1, rLWR1, rLWR2	/* r = tolower(*s1) == tolower(*s2) ? */
	crorc	4*cr1+eq,eq,4*cr1+eq	/* (*s1 != '\0') || (r == 1) */
	beq	cr1, L(done)
	lbz	rCHAR1, 1(rSTR1)
	lbz	rCHAR2, 1(rSTR2)
	cmpdi	rCHAR1, 0
	sldi	rADDR1, rCHAR1, 2
	sldi	rADDR2, rCHAR2, 2
	lwzx	rLWR1, rLOC, rADDR1
	lwzx	rLWR2, rLOC, rADDR2
	cmpw	cr1, rLWR1, rLWR2
	crorc	4*cr1+eq,eq,4*cr1+eq
	beq	cr1, L(done)
	lbz	rCHAR1, 2(rSTR1)
	lbz	rCHAR2, 2(rSTR2)
	cmpdi	rCHAR1, 0
	sldi	rADDR1, rCHAR1, 2
	sldi	rADDR2, rCHAR2, 2
	lwzx	rLWR1, rLOC, rADDR1
	lwzx	rLWR2, rLOC, rADDR2
	cmpw	cr1, rLWR1, rLWR2
	crorc	4*cr1+eq,eq,4*cr1+eq
	beq	cr1, L(done)
	lbz	rCHAR1, 3(rSTR1)
	lbz	rCHAR2, 3(rSTR2)
	cmpdi	rCHAR1, 0
	/* Increment both string descriptors */
	addi	rSTR1, rSTR1, 4
	addi	rSTR2, rSTR2, 4
	sldi	rADDR1, rCHAR1, 2
	sldi	rADDR2, rCHAR2, 2
	lwzx	rLWR1, rLOC, rADDR1
	lwzx	rLWR2, rLOC, rADDR2
	cmpw	cr1, rLWR1, rLWR2
	crorc	4*cr1+eq,eq,4*cr1+eq
	beq     cr1, L(done)
	lbz	rCHAR1, 0(rSTR1)	/* Load char from s1 */
	lbz	rCHAR2, 0(rSTR2)	/* Load char from s2 */
#ifdef USE_AS_STRNCASECMP
	bdnz	L(loop)
#else
	b	L(loop)
#endif
#ifdef USE_AS_STRNCASECMP
L(lessthan4):
	clrldi	r5, r5, 62
	cmpdi	cr7, r5, 0
	beq	cr7, L(retnull)
	mtctr	r5
L(loop1):
	cmpdi	rCHAR1, 0
	sldi	rADDR1, rCHAR1, 2
	sldi	rADDR2, rCHAR2, 2
	lwzx	rLWR1, rLOC, rADDR1
	lwzx	rLWR2, rLOC, rADDR2
	cmpw	cr1, rLWR1, rLWR2
	crorc	4*cr1+eq,eq,4*cr1+eq
	beq	cr1, L(done)
	addi	rSTR1, rSTR1, 1
	addi	rSTR2, rSTR2, 1
	lbz	rCHAR1, 0(rSTR1)
	lbz	rCHAR2, 0(rSTR2)
	bdnz	L(loop1)
#endif
L(done):
	subf	r0, rLWR2, rLWR1
	extsw	rRTN, r0
	blr
END (__STRCASECMP)

weak_alias (__STRCASECMP, STRCASECMP)
libc_hidden_builtin_def (__STRCASECMP)