1 /* Optimized strcmp implementation for POWER7/PowerPC64.
2 Copyright (C) 2010-2019 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],
32 ENTRY_TOCLESS (STRNCMP, 5)
37 #define rSTR1 r3 /* first string arg */
38 #define rSTR2 r4 /* second string arg */
39 #define rN r5 /* max string length */
40 #define rWORD1 r6 /* current word in s1 */
41 #define rWORD2 r7 /* current word in s2 */
44 #define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
45 #define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */
46 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
47 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
60 /* We are doubleword aligned so set up for two loops. first a double word
61 loop, then fall into the byte loop if any residual. */
64 addi rFEFE,rFEFE,-0x101
65 addi r7F7F,r7F7F,0x7f7f
73 insrdi r7F7F,r7F7F,32,0
81 L(g1): add rTMP,rFEFE,rWORD1
85 cmpd cr1,rWORD1,rWORD2
88 /* OK. We've hit the end of the string. We need to be careful that
89 we don't compare two strings as different because of gunk beyond
90 the end of the strings... */
92 #ifdef __LITTLE_ENDIAN__
96 andc rTMP2, rTMP2, rTMP
97 rldimi rTMP2, rTMP2, 1, 0
98 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
99 and rWORD1, rWORD1, rTMP2
100 cmpd cr1, rWORD1, rWORD2
102 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
103 addi rNEG, rBITDIF, 1
104 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
105 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
106 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
107 andc rWORD2, rWORD2, rNEG
108 xor. rBITDIF, rWORD1, rWORD2
109 sub rRTN, rWORD1, rWORD2
111 sradi rRTN, rRTN, 63 /* must return an int. */
120 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
121 addi rNEG, rBITDIF, 1
122 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
123 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
124 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
125 andc rWORD2, rWORD2, rNEG
126 xor. rBITDIF, rWORD1, rWORD2
127 sub rRTN, rWORD1, rWORD2
133 sradi rRTN, rWORD2, 63
139 and rTMP,r7F7F,rWORD1
142 xor. rBITDIF,rWORD1,rWORD2
145 cntlzd rBITDIF,rBITDIF
148 cmpd cr1,rNEG,rBITDIF
149 sub rRTN,rWORD1,rWORD2
151 sradi rRTN,rRTN,63 /* must return an int. */
160 xor. rBITDIF,rWORD1,rWORD2
161 sub rRTN,rWORD1,rWORD2
172 /* Oh well. In this case, we just do a byte-by-byte comparison. */
176 cmpd cr1,rWORD1,rWORD2
220 L(u3): sub rRTN,rWORD3,rWORD4
222 L(u4): sub rRTN,rWORD1,rWORD2
228 libc_hidden_builtin_def (strncmp)