1 /* Optimized strcmp implementation for PowerPC64.
2 Copyright (C) 2003-2013 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/>. */
23 /* See strlen.s for comments on how the end-of-string testing works. */
25 /* int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t size [r5]) */
27 EALIGN (BP_SYM(strncmp), 4, 0)
32 #define rSTR1 r3 /* first string arg */
33 #define rSTR2 r4 /* second string arg */
34 #define rN r5 /* max string length */
35 /* Note: The Bounded pointer support in this code is broken. This code
36 was inherited from PPC32 and that support was never completed.
37 Current PPC gcc does not support -fbounds-check or -fbounded-pointers. */
38 #define rWORD1 r6 /* current word in s1 */
39 #define rWORD2 r7 /* current word in s2 */
42 #define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
43 #define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */
44 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
45 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
51 clrldi. rTMP, rTMP, 61
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
64 mtctr rTMP /* Power4 wants mtctr 1st in dispatch group. */
68 insrdi r7F7F, r7F7F, 32, 0
69 add rFEFE, rFEFE, rTMP
74 bne- cr1, L(different)
76 L(g1): add rTMP, rFEFE, rWORD1
77 nor rNEG, r7F7F, 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... */
88 and rTMP, r7F7F, rWORD1
91 xor. rBITDIF, rWORD1, rWORD2
95 cntlzd rBITDIF, rBITDIF
98 cmpd cr1, rNEG, rBITDIF
99 sub rRTN, rWORD1, rWORD2
109 ldu rWORD1, -8(rSTR1)
110 xor. rBITDIF, rWORD1, rWORD2
111 sub rRTN, rWORD1, rWORD2
117 srdi rWORD2, rWORD2, 56
118 srdi rWORD1, rWORD1, 56
119 sub rRTN, rWORD1, rWORD2
123 /* Oh well. In this case, we just do a byte-by-byte comparison. */
126 and. rTMP, rTMP, rNEG
127 cmpd cr1, rWORD1, rWORD2
130 bne- cr1, L(different)
134 mtctr rN /* Power4 wants mtctr 1st in dispatch group */
146 lbzu rWORD3, 1(rSTR1)
147 lbzu rWORD4, 1(rSTR2)
153 lbzu rWORD1, 1(rSTR1)
154 lbzu rWORD2, 1(rSTR2)
160 lbzu rWORD3, 1(rSTR1)
161 lbzu rWORD4, 1(rSTR2)
167 lbzu rWORD1, 1(rSTR1)
168 lbzu rWORD2, 1(rSTR2)
171 L(u3): sub rRTN, rWORD3, rWORD4
173 L(u4): sub rRTN, rWORD1, rWORD2
178 END (BP_SYM (strncmp))
179 libc_hidden_builtin_def (strncmp)