1 /* Optimized strcmp implementation for POWER7/PowerPC64.
2 Copyright (C) 2010-2017 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/>. */
22 # define STRNCMP strncmp
25 /* See strlen.s for comments on how the end-of-string testing works. */
27 /* int [r3] strncmp (const char *s1 [r3],
36 #define rSTR1 r3 /* first string arg */
37 #define rSTR2 r4 /* second string arg */
38 #define rN r5 /* max string length */
39 #define rWORD1 r6 /* current word in s1 */
40 #define rWORD2 r7 /* current word in s2 */
43 #define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
44 #define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */
45 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
46 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
59 /* We are doubleword aligned so set up for two loops. first a double word
60 loop, then fall into the byte loop if any residual. */
63 addi rFEFE,rFEFE,-0x101
64 addi r7F7F,r7F7F,0x7f7f
72 insrdi r7F7F,r7F7F,32,0
80 L(g1): add rTMP,rFEFE,rWORD1
84 cmpd cr1,rWORD1,rWORD2
87 /* OK. We've hit the end of the string. We need to be careful that
88 we don't compare two strings as different because of gunk beyond
89 the end of the strings... */
91 #ifdef __LITTLE_ENDIAN__
95 andc rTMP2, rTMP2, rTMP
96 rldimi rTMP2, rTMP2, 1, 0
97 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
98 and rWORD1, rWORD1, rTMP2
99 cmpd cr1, rWORD1, rWORD2
101 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
102 addi rNEG, rBITDIF, 1
103 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
104 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
105 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
106 andc rWORD2, rWORD2, rNEG
107 xor. rBITDIF, rWORD1, rWORD2
108 sub rRTN, rWORD1, rWORD2
110 sradi rRTN, rRTN, 63 /* must return an int. */
119 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
120 addi rNEG, rBITDIF, 1
121 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
122 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
123 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
124 andc rWORD2, rWORD2, rNEG
125 xor. rBITDIF, rWORD1, rWORD2
126 sub rRTN, rWORD1, rWORD2
132 sradi rRTN, rWORD2, 63
138 and rTMP,r7F7F,rWORD1
141 xor. rBITDIF,rWORD1,rWORD2
144 cntlzd rBITDIF,rBITDIF
147 cmpd cr1,rNEG,rBITDIF
148 sub rRTN,rWORD1,rWORD2
150 sradi rRTN,rRTN,63 /* must return an int. */
159 xor. rBITDIF,rWORD1,rWORD2
160 sub rRTN,rWORD1,rWORD2
171 /* Oh well. In this case, we just do a byte-by-byte comparison. */
175 cmpd cr1,rWORD1,rWORD2
219 L(u3): sub rRTN,rWORD3,rWORD4
221 L(u4): sub rRTN,rWORD1,rWORD2
227 libc_hidden_builtin_def (strncmp)