Fix spelling errors in sysdeps/powerpc files.
[glibc.git] / sysdeps / powerpc / powerpc32 / power4 / strncmp.S
blob50d79dc967e3c0d98ab43ff3e247625d432a623e
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 rWORD3  r10
40 #define rWORD4  r11
41 #define rFEFE   r8      /* constant 0xfefefeff (-0x01010101) */
42 #define r7F7F   r9      /* constant 0x7f7f7f7f */
43 #define rNEG    r10     /* ~(word in s1 | 0x7f7f7f7f) */
44 #define rBITDIF r11     /* bits that differ in s1 & s2 words */
46         dcbt    0,rSTR1
47         or      rTMP, rSTR2, rSTR1
48         lis     r7F7F, 0x7f7f
49         dcbt    0,rSTR2
50         clrlwi. rTMP, rTMP, 30
51         cmplwi  cr1, rN, 0
52         lis     rFEFE, -0x101
53         bne     L(unaligned)
54 /* We are word aligned so set up for two loops.  first a word
55    loop, then fall into the byte loop if any residual.  */
56         srwi.   rTMP, rN, 2
57         clrlwi  rN, rN, 30
58         addi    rFEFE, rFEFE, -0x101
59         addi    r7F7F, r7F7F, 0x7f7f
60         cmplwi  cr1, rN, 0
61         beq     L(unaligned)
63         mtctr   rTMP    /* Power4 wants mtctr 1st in dispatch group.  */
64         lwz     rWORD1, 0(rSTR1)
65         lwz     rWORD2, 0(rSTR2)
66         b       L(g1)
68 L(g0):
69         lwzu    rWORD1, 4(rSTR1)
70         bne-    cr1, L(different)
71         lwzu    rWORD2, 4(rSTR2)
72 L(g1):  add     rTMP, rFEFE, rWORD1
73         nor     rNEG, r7F7F, rWORD1
74         bdz     L(tail)
75         and.    rTMP, rTMP, rNEG
76         cmpw    cr1, rWORD1, rWORD2
77         beq+    L(g0)
79 /* OK. We've hit the end of the string. We need to be careful that
80    we don't compare two strings as different because of gunk beyond
81    the end of the strings...  */
83 L(endstring):
84         and     rTMP, r7F7F, rWORD1
85         beq     cr1, L(equal)
86         add     rTMP, rTMP, r7F7F
87         xor.    rBITDIF, rWORD1, rWORD2
89         andc    rNEG, rNEG, rTMP
90         blt-    L(highbit)
91         cntlzw  rBITDIF, rBITDIF
92         cntlzw  rNEG, rNEG
93         addi    rNEG, rNEG, 7
94         cmpw    cr1, rNEG, rBITDIF
95         sub     rRTN, rWORD1, rWORD2
96         blt-    cr1, L(equal)
97         srawi   rRTN, rRTN, 31
98         ori     rRTN, rRTN, 1
99         blr
100 L(equal):
101         li      rRTN, 0
102         blr
104 L(different):
105         lwzu    rWORD1, -4(rSTR1)
106         xor.    rBITDIF, rWORD1, rWORD2
107         sub     rRTN, rWORD1, rWORD2
108         blt-    L(highbit)
109         srawi   rRTN, rRTN, 31
110         ori     rRTN, rRTN, 1
111         blr
112 L(highbit):
113         srwi    rWORD2, rWORD2, 24
114         srwi    rWORD1, rWORD1, 24
115         sub     rRTN, rWORD1, rWORD2
116         blr
119 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
120         .align 4
121 L(tail):
122         and.    rTMP, rTMP, rNEG
123         cmpw    cr1, rWORD1, rWORD2
124         bne-    L(endstring)
125         addi    rSTR1, rSTR1, 4
126         bne-    cr1, L(different)
127         addi    rSTR2, rSTR2, 4
128         cmplwi  cr1, rN, 0
129 L(unaligned):
130         mtctr   rN      /* Power4 wants mtctr 1st in dispatch group */
131         ble     cr1, L(ux)
132 L(uz):
133         lbz     rWORD1, 0(rSTR1)
134         lbz     rWORD2, 0(rSTR2)
135         .align 4
136 L(u1):
137         cmpwi   cr1, rWORD1, 0
138         bdz     L(u4)
139         cmpw    rWORD1, rWORD2
140         beq-    cr1, L(u4)
141         bne-    L(u4)
142         lbzu    rWORD3, 1(rSTR1)
143         lbzu    rWORD4, 1(rSTR2)
144         cmpwi   cr1, rWORD3, 0
145         bdz     L(u3)
146         cmpw    rWORD3, rWORD4
147         beq-    cr1, L(u3)
148         bne-    L(u3)
149         lbzu    rWORD1, 1(rSTR1)
150         lbzu    rWORD2, 1(rSTR2)
151         cmpwi   cr1, rWORD1, 0
152         bdz     L(u4)
153         cmpw    rWORD1, rWORD2
154         beq-    cr1, L(u4)
155         bne-    L(u4)
156         lbzu    rWORD3, 1(rSTR1)
157         lbzu    rWORD4, 1(rSTR2)
158         cmpwi   cr1, rWORD3, 0
159         bdz     L(u3)
160         cmpw    rWORD3, rWORD4
161         beq-    cr1, L(u3)
162         bne-    L(u3)
163         lbzu    rWORD1, 1(rSTR1)
164         lbzu    rWORD2, 1(rSTR2)
165         b       L(u1)
167 L(u3):  sub     rRTN, rWORD3, rWORD4
168         blr
169 L(u4):  sub     rRTN, rWORD1, rWORD2
170         blr
171 L(ux):
172         li      rRTN, 0
173         blr
174 END (BP_SYM (strncmp))
175 libc_hidden_builtin_def (strncmp)