|
Packit |
6c4009 |
/* Optimized strcmp implementation for PowerPC64.
|
|
Packit |
6c4009 |
Copyright (C) 2003-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See strlen.s for comments on how the end-of-string testing works. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t size [r5]) */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef STRNCMP
|
|
Packit |
6c4009 |
# define STRNCMP strncmp
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY_TOCLESS (STRNCMP, 4)
|
|
Packit |
6c4009 |
CALL_MCOUNT 3
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define rTMP2 r0
|
|
Packit |
6c4009 |
#define rRTN r3
|
|
Packit |
6c4009 |
#define rSTR1 r3 /* first string arg */
|
|
Packit |
6c4009 |
#define rSTR2 r4 /* second string arg */
|
|
Packit |
6c4009 |
#define rN r5 /* max string length */
|
|
Packit |
6c4009 |
#define rWORD1 r6 /* current word in s1 */
|
|
Packit |
6c4009 |
#define rWORD2 r7 /* current word in s2 */
|
|
Packit |
6c4009 |
#define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
|
|
Packit |
6c4009 |
#define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */
|
|
Packit |
6c4009 |
#define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
|
|
Packit |
6c4009 |
#define rBITDIF r11 /* bits that differ in s1 & s2 words */
|
|
Packit |
6c4009 |
#define rTMP r12
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
dcbt 0,rSTR1
|
|
Packit |
6c4009 |
or rTMP, rSTR2, rSTR1
|
|
Packit |
6c4009 |
lis r7F7F, 0x7f7f
|
|
Packit |
6c4009 |
dcbt 0,rSTR2
|
|
Packit |
6c4009 |
clrldi. rTMP, rTMP, 61
|
|
Packit |
6c4009 |
cmpldi cr1, rN, 0
|
|
Packit |
6c4009 |
lis rFEFE, -0x101
|
|
Packit |
6c4009 |
bne L(unaligned)
|
|
Packit |
6c4009 |
/* We are doubleword aligned so set up for two loops. first a double word
|
|
Packit |
6c4009 |
loop, then fall into the byte loop if any residual. */
|
|
Packit |
6c4009 |
srdi. rTMP, rN, 3
|
|
Packit |
6c4009 |
clrldi rN, rN, 61
|
|
Packit |
6c4009 |
addi rFEFE, rFEFE, -0x101
|
|
Packit |
6c4009 |
addi r7F7F, r7F7F, 0x7f7f
|
|
Packit |
6c4009 |
cmpldi cr1, rN, 0
|
|
Packit |
6c4009 |
beq L(unaligned)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
mtctr rTMP /* Power4 wants mtctr 1st in dispatch group. */
|
|
Packit |
6c4009 |
ld rWORD1, 0(rSTR1)
|
|
Packit |
6c4009 |
ld rWORD2, 0(rSTR2)
|
|
Packit |
6c4009 |
sldi rTMP, rFEFE, 32
|
|
Packit |
6c4009 |
insrdi r7F7F, r7F7F, 32, 0
|
|
Packit |
6c4009 |
add rFEFE, rFEFE, rTMP
|
|
Packit |
6c4009 |
b L(g1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
L(g0):
|
|
Packit |
6c4009 |
ldu rWORD1, 8(rSTR1)
|
|
Packit |
6c4009 |
bne- cr1, L(different)
|
|
Packit |
6c4009 |
ldu rWORD2, 8(rSTR2)
|
|
Packit |
6c4009 |
L(g1): add rTMP, rFEFE, rWORD1
|
|
Packit |
6c4009 |
nor rNEG, r7F7F, rWORD1
|
|
Packit |
6c4009 |
bdz L(tail)
|
|
Packit |
6c4009 |
and. rTMP, rTMP, rNEG
|
|
Packit |
6c4009 |
cmpd cr1, rWORD1, rWORD2
|
|
Packit |
6c4009 |
beq+ L(g0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* OK. We've hit the end of the string. We need to be careful that
|
|
Packit |
6c4009 |
we don't compare two strings as different because of gunk beyond
|
|
Packit |
6c4009 |
the end of the strings... */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef __LITTLE_ENDIAN__
|
|
Packit |
6c4009 |
L(endstring):
|
|
Packit |
6c4009 |
addi rTMP2, rTMP, -1
|
|
Packit |
6c4009 |
beq cr1, L(equal)
|
|
Packit |
6c4009 |
andc rTMP2, rTMP2, rTMP
|
|
Packit |
6c4009 |
rldimi rTMP2, rTMP2, 1, 0
|
|
Packit |
6c4009 |
and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
|
|
Packit |
6c4009 |
and rWORD1, rWORD1, rTMP2
|
|
Packit |
6c4009 |
cmpd cr1, rWORD1, rWORD2
|
|
Packit |
6c4009 |
beq cr1, L(equal)
|
|
Packit |
6c4009 |
xor rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ. */
|
|
Packit |
6c4009 |
neg rNEG, rBITDIF
|
|
Packit |
6c4009 |
and rNEG, rNEG, rBITDIF /* rNEG has LS bit that differs. */
|
|
Packit |
6c4009 |
cntlzd rNEG, rNEG /* bitcount of the bit. */
|
|
Packit |
6c4009 |
andi. rNEG, rNEG, 56 /* bitcount to LS byte that differs. */
|
|
Packit |
6c4009 |
sld rWORD1, rWORD1, rNEG /* shift left to clear MS bytes. */
|
|
Packit |
6c4009 |
sld rWORD2, rWORD2, rNEG
|
|
Packit |
6c4009 |
xor. rBITDIF, rWORD1, rWORD2
|
|
Packit |
6c4009 |
sub rRTN, rWORD1, rWORD2
|
|
Packit |
6c4009 |
blt- L(highbit)
|
|
Packit |
6c4009 |
sradi rRTN, rRTN, 63 /* must return an int. */
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
L(equal):
|
|
Packit |
6c4009 |
li rRTN, 0
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
L(different):
|
|
Packit |
6c4009 |
ld rWORD1, -8(rSTR1)
|
|
Packit |
6c4009 |
xor rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ. */
|
|
Packit |
6c4009 |
neg rNEG, rBITDIF
|
|
Packit |
6c4009 |
and rNEG, rNEG, rBITDIF /* rNEG has LS bit that differs. */
|
|
Packit |
6c4009 |
cntlzd rNEG, rNEG /* bitcount of the bit. */
|
|
Packit |
6c4009 |
andi. rNEG, rNEG, 56 /* bitcount to LS byte that differs. */
|
|
Packit |
6c4009 |
sld rWORD1, rWORD1, rNEG /* shift left to clear MS bytes. */
|
|
Packit |
6c4009 |
sld rWORD2, rWORD2, rNEG
|
|
Packit |
6c4009 |
xor. rBITDIF, rWORD1, rWORD2
|
|
Packit |
6c4009 |
sub rRTN, rWORD1, rWORD2
|
|
Packit |
6c4009 |
blt- L(highbit)
|
|
Packit |
6c4009 |
sradi rRTN, rRTN, 63
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
L(highbit):
|
|
Packit |
6c4009 |
sradi rRTN, rWORD2, 63
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
L(endstring):
|
|
Packit |
6c4009 |
and rTMP, r7F7F, rWORD1
|
|
Packit |
6c4009 |
beq cr1, L(equal)
|
|
Packit |
6c4009 |
add rTMP, rTMP, r7F7F
|
|
Packit |
6c4009 |
xor. rBITDIF, rWORD1, rWORD2
|
|
Packit |
6c4009 |
andc rNEG, rNEG, rTMP
|
|
Packit |
6c4009 |
blt- L(highbit)
|
|
Packit |
6c4009 |
cntlzd rBITDIF, rBITDIF
|
|
Packit |
6c4009 |
cntlzd rNEG, rNEG
|
|
Packit |
6c4009 |
addi rNEG, rNEG, 7
|
|
Packit |
6c4009 |
cmpd cr1, rNEG, rBITDIF
|
|
Packit |
6c4009 |
sub rRTN, rWORD1, rWORD2
|
|
Packit |
6c4009 |
blt- cr1, L(equal)
|
|
Packit |
6c4009 |
sradi rRTN, rRTN, 63 /* must return an int. */
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
L(equal):
|
|
Packit |
6c4009 |
li rRTN, 0
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
L(different):
|
|
Packit |
6c4009 |
ld rWORD1, -8(rSTR1)
|
|
Packit |
6c4009 |
xor. rBITDIF, rWORD1, rWORD2
|
|
Packit |
6c4009 |
sub rRTN, rWORD1, rWORD2
|
|
Packit |
6c4009 |
blt- L(highbit)
|
|
Packit |
6c4009 |
sradi rRTN, rRTN, 63
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
L(highbit):
|
|
Packit |
6c4009 |
sradi rRTN, rWORD2, 63
|
|
Packit |
6c4009 |
ori rRTN, rRTN, 1
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Oh well. In this case, we just do a byte-by-byte comparison. */
|
|
Packit |
6c4009 |
.align 4
|
|
Packit |
6c4009 |
L(tail):
|
|
Packit |
6c4009 |
and. rTMP, rTMP, rNEG
|
|
Packit |
6c4009 |
cmpd cr1, rWORD1, rWORD2
|
|
Packit |
6c4009 |
bne- L(endstring)
|
|
Packit |
6c4009 |
addi rSTR1, rSTR1, 8
|
|
Packit |
6c4009 |
bne- cr1, L(different)
|
|
Packit |
6c4009 |
addi rSTR2, rSTR2, 8
|
|
Packit |
6c4009 |
cmpldi cr1, rN, 0
|
|
Packit |
6c4009 |
L(unaligned):
|
|
Packit |
6c4009 |
mtctr rN /* Power4 wants mtctr 1st in dispatch group */
|
|
Packit |
6c4009 |
bgt cr1, L(uz)
|
|
Packit |
6c4009 |
L(ux):
|
|
Packit |
6c4009 |
li rRTN, 0
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
.align 4
|
|
Packit |
6c4009 |
L(uz):
|
|
Packit |
6c4009 |
lbz rWORD1, 0(rSTR1)
|
|
Packit |
6c4009 |
lbz rWORD2, 0(rSTR2)
|
|
Packit |
6c4009 |
nop
|
|
Packit |
6c4009 |
b L(u1)
|
|
Packit |
6c4009 |
L(u0):
|
|
Packit |
6c4009 |
lbzu rWORD2, 1(rSTR2)
|
|
Packit |
6c4009 |
L(u1):
|
|
Packit |
6c4009 |
bdz L(u3)
|
|
Packit |
6c4009 |
cmpdi cr1, rWORD1, 0
|
|
Packit |
6c4009 |
cmpd rWORD1, rWORD2
|
|
Packit |
6c4009 |
beq- cr1, L(u3)
|
|
Packit |
6c4009 |
lbzu rWORD1, 1(rSTR1)
|
|
Packit |
6c4009 |
bne- L(u2)
|
|
Packit |
6c4009 |
lbzu rWORD2, 1(rSTR2)
|
|
Packit |
6c4009 |
bdz L(u3)
|
|
Packit |
6c4009 |
cmpdi cr1, rWORD1, 0
|
|
Packit |
6c4009 |
cmpd rWORD1, rWORD2
|
|
Packit |
6c4009 |
bne- L(u3)
|
|
Packit |
6c4009 |
lbzu rWORD1, 1(rSTR1)
|
|
Packit |
6c4009 |
bne+ cr1, L(u0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
L(u2): lbzu rWORD1, -1(rSTR1)
|
|
Packit |
6c4009 |
L(u3): sub rRTN, rWORD1, rWORD2
|
|
Packit |
6c4009 |
blr
|
|
Packit |
6c4009 |
END (STRNCMP)
|
|
Packit |
6c4009 |
libc_hidden_builtin_def (strncmp)
|