Fix spelling errors in sysdeps/powerpc files.
[glibc.git] / sysdeps / powerpc / powerpc32 / strncmp.S
blobd9e274b69b1f432b9b78523328a49a64ec378bbd
1 /* Optimized strcmp implementation for PowerPC32.
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/>.  */
19 #include <sysdep.h>
20 #include <bp-sym.h>
21 #include <bp-asm.h>
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)
29 #define rTMP    r0
30 #define rRTN    r3
31 #define rSTR1   r3      /* first string arg */
32 #define rSTR2   r4      /* second string arg */
33 #define rN      r5      /* max string length */
34 /* Note:  The Bounded pointer support in this code is broken.  This code
35    was inherited from PPC32 and that support was never completed.
36    Current PPC gcc does not support -fbounds-check or -fbounded-pointers.  */
37 #define rWORD1  r6      /* current word in s1 */
38 #define rWORD2  r7      /* current word in s2 */
39 #define rFEFE   r8      /* constant 0xfefefeff (-0x01010101) */
40 #define r7F7F   r9      /* constant 0x7f7f7f7f */
41 #define rNEG    r10     /* ~(word in s1 | 0x7f7f7f7f) */
42 #define rBITDIF r11     /* bits that differ in s1 & s2 words */
44         dcbt    0,rSTR1
45         or      rTMP, rSTR2, rSTR1
46         lis     r7F7F, 0x7f7f
47         dcbt    0,rSTR2
48         clrlwi. rTMP, rTMP, 30
49         cmplwi  cr1, rN, 0
50         lis     rFEFE, -0x101
51         bne     L(unaligned)
52 /* We are word aligned so set up for two loops.  first a word
53    loop, then fall into the byte loop if any residual.  */
54         srwi.   rTMP, rN, 2
55         clrlwi  rN, rN, 30
56         addi    rFEFE, rFEFE, -0x101
57         addi    r7F7F, r7F7F, 0x7f7f
58         cmplwi  cr1, rN, 0
59         beq     L(unaligned)
61         mtctr   rTMP    /* Power4 wants mtctr 1st in dispatch group.  */
62         lwz     rWORD1, 0(rSTR1)
63         lwz     rWORD2, 0(rSTR2)
64         b       L(g1)
66 L(g0):
67         lwzu    rWORD1, 4(rSTR1)
68         bne-    cr1, L(different)
69         lwzu    rWORD2, 4(rSTR2)
70 L(g1):  add     rTMP, rFEFE, rWORD1
71         nor     rNEG, r7F7F, rWORD1
72         bdz     L(tail)
73         and.    rTMP, rTMP, rNEG
74         cmpw    cr1, rWORD1, rWORD2
75         beq+    L(g0)
77 /* OK. We've hit the end of the string. We need to be careful that
78    we don't compare two strings as different because of gunk beyond
79    the end of the strings...  */
81 L(endstring):
82         and     rTMP, r7F7F, rWORD1
83         beq     cr1, L(equal)
84         add     rTMP, rTMP, r7F7F
85         xor.    rBITDIF, rWORD1, rWORD2
87         andc    rNEG, rNEG, rTMP
88         blt-    L(highbit)
89         cntlzw  rBITDIF, rBITDIF
90         cntlzw  rNEG, rNEG
91         addi    rNEG, rNEG, 7
92         cmpw    cr1, rNEG, rBITDIF
93         sub     rRTN, rWORD1, rWORD2
94         blt-    cr1, L(equal)
95         srawi   rRTN, rRTN, 31
96         ori     rRTN, rRTN, 1
97         blr
98 L(equal):
99         li      rRTN, 0
100         blr
102 L(different):
103         lwzu    rWORD1, -4(rSTR1)
104         xor.    rBITDIF, rWORD1, rWORD2
105         sub     rRTN, rWORD1, rWORD2
106         blt-    L(highbit)
107         srawi   rRTN, rRTN, 31
108         ori     rRTN, rRTN, 1
109         blr
110 L(highbit):
111         srwi    rWORD2, rWORD2, 24
112         srwi    rWORD1, rWORD1, 24
113         sub     rRTN, rWORD1, rWORD2
114         blr
117 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
118         .align 4
119 L(tail):
120         and.    rTMP, rTMP, rNEG
121         cmpw    cr1, rWORD1, rWORD2
122         bne-    L(endstring)
123         addi    rSTR1, rSTR1, 4
124         bne-    cr1, L(different)
125         addi    rSTR2, rSTR2, 4
126         cmplwi  cr1, rN, 0
127 L(unaligned):
128         mtctr   rN      /* Power4 wants mtctr 1st in dispatch group */
129         bgt     cr1, L(uz)
130 L(ux):
131         li      rRTN, 0
132         blr
133         .align 4
134 L(uz):
135         lbz     rWORD1, 0(rSTR1)
136         lbz     rWORD2, 0(rSTR2)
137         nop
138         b       L(u1)
139 L(u0):
140         lbzu    rWORD2, 1(rSTR2)
141 L(u1):
142         bdz     L(u3)
143         cmpwi   cr1, rWORD1, 0
144         cmpw    rWORD1, rWORD2
145         beq-    cr1, L(u3)
146         lbzu    rWORD1, 1(rSTR1)
147         bne-    L(u2)
148         lbzu    rWORD2, 1(rSTR2)
149         bdz     L(u3)
150         cmpwi   cr1, rWORD1, 0
151         cmpw    rWORD1, rWORD2
152         bne-    L(u3)
153         lbzu    rWORD1, 1(rSTR1)
154         bne+    cr1, L(u0)
156 L(u2):  lbzu    rWORD1, -1(rSTR1)
157 L(u3):  sub     rRTN, rWORD1, rWORD2
158         blr
159 END (BP_SYM (strncmp))
160 libc_hidden_builtin_def (strncmp)