1 /* Optimized strcmp implementation for POWER7/PowerPC64.
2 Copyright (C) 2010-2015 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] strncmp (const char *s1 [r3],
32 #define rSTR1 r3 /* first string arg */
33 #define rSTR2 r4 /* second string arg */
34 #define rN r5 /* max string length */
35 #define rWORD1 r6 /* current word in s1 */
36 #define rWORD2 r7 /* current word in s2 */
39 #define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
40 #define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */
41 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
42 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
55 /* We are doubleword aligned so set up for two loops. first a double word
56 loop, then fall into the byte loop if any residual. */
59 addi rFEFE,rFEFE,-0x101
60 addi r7F7F,r7F7F,0x7f7f
68 insrdi r7F7F,r7F7F,32,0
76 L(g1): add rTMP,rFEFE,rWORD1
80 cmpd cr1,rWORD1,rWORD2
83 /* OK. We've hit the end of the string. We need to be careful that
84 we don't compare two strings as different because of gunk beyond
85 the end of the strings... */
87 #ifdef __LITTLE_ENDIAN__
91 andc rTMP2, rTMP2, rTMP
92 rldimi rTMP2, rTMP2, 1, 0
93 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
94 and rWORD1, rWORD1, rTMP2
95 cmpd cr1, rWORD1, rWORD2
97 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
99 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
100 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
101 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
102 andc rWORD2, rWORD2, rNEG
103 xor. rBITDIF, rWORD1, rWORD2
104 sub rRTN, rWORD1, rWORD2
106 sradi rRTN, rRTN, 63 /* must return an int. */
115 cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */
116 addi rNEG, rBITDIF, 1
117 orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */
118 sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */
119 andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */
120 andc rWORD2, rWORD2, rNEG
121 xor. rBITDIF, rWORD1, rWORD2
122 sub rRTN, rWORD1, rWORD2
128 sradi rRTN, rWORD2, 63
134 and rTMP,r7F7F,rWORD1
137 xor. rBITDIF,rWORD1,rWORD2
140 cntlzd rBITDIF,rBITDIF
143 cmpd cr1,rNEG,rBITDIF
144 sub rRTN,rWORD1,rWORD2
146 sradi rRTN,rRTN,63 /* must return an int. */
155 xor. rBITDIF,rWORD1,rWORD2
156 sub rRTN,rWORD1,rWORD2
167 /* Oh well. In this case, we just do a byte-by-byte comparison. */
171 cmpd cr1,rWORD1,rWORD2
215 L(u3): sub rRTN,rWORD3,rWORD4
217 L(u4): sub rRTN,rWORD1,rWORD2
223 libc_hidden_builtin_def (strncmp)