1 /* Optimized strcmp implementation for PowerPC64.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 /* See strlen.s for comments on how the end-of-string testing works. */
23 /* int [r3] strcmp (const char *s1 [r3], const char *s2 [r4]) */
30 #define rSTR1 r3 /* first string arg */
31 #define rSTR2 r4 /* second string arg */
32 #define rWORD1 r5 /* current word in s1 */
33 #define rWORD2 r6 /* current word in s2 */
34 #define rFEFE r7 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
35 #define r7F7F r8 /* constant 0x7f7f7f7f7f7f7f7f */
36 #define rNEG r9 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
37 #define rBITDIF r10 /* bits that differ in s1 & s2 words */
43 clrldi. rTMP, rTMP, 61
50 addi rFEFE, rFEFE, -0x101
51 addi r7F7F, r7F7F, 0x7f7f
53 insrdi r7F7F, r7F7F, 32, 0
54 add rFEFE, rFEFE, rTMP
57 L(g0): ldu rWORD1, 8(rSTR1)
60 L(g1): add rTMP, rFEFE, rWORD1
61 nor rNEG, r7F7F, rWORD1
63 cmpd cr1, rWORD1, rWORD2
66 /* OK. We've hit the end of the string. We need to be careful that
67 we don't compare two strings as different because of gunk beyond
68 the end of the strings... */
69 #ifdef __LITTLE_ENDIAN__
73 andc rTMP2, rTMP2, rTMP
74 rldimi rTMP2, rTMP2, 1, 0
75 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
76 and rWORD1, rWORD1, rTMP2
77 cmpd cr1, rWORD1, rWORD2
79 xor rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ. */
81 and rNEG, rNEG, rBITDIF /* rNEG has LS bit that differs. */
82 cntlzd rNEG, rNEG /* bitcount of the bit. */
83 andi. rNEG, rNEG, 56 /* bitcount to LS byte that differs. */
84 sld rWORD1, rWORD1, rNEG /* shift left to clear MS bytes. */
85 sld rWORD2, rWORD2, rNEG
86 xor. rBITDIF, rWORD1, rWORD2
87 sub rRTN, rWORD1, rWORD2
89 sradi rRTN, rRTN, 63 /* must return an int. */
98 xor rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ. */
100 and rNEG, rNEG, rBITDIF /* rNEG has LS bit that differs. */
101 cntlzd rNEG, rNEG /* bitcount of the bit. */
102 andi. rNEG, rNEG, 56 /* bitcount to LS byte that differs. */
103 sld rWORD1, rWORD1, rNEG /* shift left to clear MS bytes. */
104 sld rWORD2, rWORD2, rNEG
105 xor. rBITDIF, rWORD1, rWORD2
106 sub rRTN, rWORD1, rWORD2
112 sradi rRTN, rWORD2, 63
118 and rTMP, r7F7F, rWORD1
120 add rTMP, rTMP, r7F7F
121 xor. rBITDIF, rWORD1, rWORD2
122 andc rNEG, rNEG, rTMP
124 cntlzd rBITDIF, rBITDIF
127 cmpd cr1, rNEG, rBITDIF
128 sub rRTN, rWORD1, rWORD2
130 sradi rRTN, rRTN, 63 /* must return an int. */
139 xor. rBITDIF, rWORD1, rWORD2
140 sub rRTN, rWORD1, rWORD2
146 sradi rRTN, rWORD2, 63
151 /* Oh well. In this case, we just do a byte-by-byte comparison. */
158 L(u0): lbzu rWORD1, 1(rSTR1)
160 lbzu rWORD2, 1(rSTR2)
161 L(u1): cmpwi cr1, rWORD1, 0
165 lbzu rWORD1, 1(rSTR1)
166 lbzu rWORD2, 1(rSTR2)
170 L(u3): sub rRTN, rWORD1, rWORD2
172 L(u4): lbz rWORD1, -1(rSTR1)
173 sub rRTN, rWORD1, rWORD2
176 libc_hidden_builtin_def (strcmp)