2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc32 / strncmp.S
blob3e0fff5ac2961928a839b83d405dc220cc88e788
1 /* Optimized strcmp implementation for PowerPC32.
2    Copyright (C) 2003 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* See strlen.s for comments on how the end-of-string testing works.  */
26 /* int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t size [r5])  */
28 EALIGN (BP_SYM(strncmp), 4, 0)
30 #define rTMP    r0
31 #define rRTN    r3
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 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 */
40 #define rFEFE   r8      /* constant 0xfefefeff (-0x01010101) */
41 #define r7F7F   r9      /* constant 0x7f7f7f7f */
42 #define rNEG    r10     /* ~(word in s1 | 0x7f7f7f7f) */
43 #define rBITDIF r11     /* bits that differ in s1 & s2 words */
45         dcbt    0,rSTR1
46         or      rTMP, rSTR2, rSTR1
47         lis     r7F7F, 0x7f7f
48         dcbt    0,rSTR2
49         clrlwi. rTMP, rTMP, 30
50         cmplwi  cr1, rN, 0
51         lis     rFEFE, -0x101
52         bne     L(unaligned)
53 /* We are word alligned so set up for two loops.  first a word
54    loop, then fall into the byte loop if any residual.  */
55         srwi.   rTMP, rN, 2
56         clrlwi  rN, rN, 30
57         addi    rFEFE, rFEFE, -0x101
58         addi    r7F7F, r7F7F, 0x7f7f
59         cmplwi  cr1, rN, 0      
60         beq     L(unaligned)
62         mtctr   rTMP    /* Power4 wants mtctr 1st in dispatch group.  */
63         lwz     rWORD1, 0(rSTR1)
64         lwz     rWORD2, 0(rSTR2)
65         b       L(g1)
67 L(g0):  
68         lwzu    rWORD1, 4(rSTR1)
69         bne-    cr1, L(different)
70         lwzu    rWORD2, 4(rSTR2)
71 L(g1):  add     rTMP, rFEFE, rWORD1
72         nor     rNEG, r7F7F, rWORD1
73         bdz     L(tail)
74         and.    rTMP, rTMP, rNEG
75         cmpw    cr1, rWORD1, rWORD2
76         beq+    L(g0)
77         
78 /* OK. We've hit the end of the string. We need to be careful that
79    we don't compare two strings as different because of gunk beyond
80    the end of the strings...  */
81         
82 L(endstring):
83         and     rTMP, r7F7F, rWORD1
84         beq     cr1, L(equal)
85         add     rTMP, rTMP, r7F7F
86         xor.    rBITDIF, rWORD1, rWORD2
88         andc    rNEG, rNEG, rTMP
89         blt-    L(highbit)
90         cntlzw  rBITDIF, rBITDIF
91         cntlzw  rNEG, rNEG
92         addi    rNEG, rNEG, 7
93         cmpw    cr1, rNEG, rBITDIF
94         sub     rRTN, rWORD1, rWORD2
95         blt-    cr1, L(equal)
96         srawi   rRTN, rRTN, 31
97         ori     rRTN, rRTN, 1
98         blr
99 L(equal):
100         li      rRTN, 0
101         blr
103 L(different):
104         lwzu    rWORD1, -4(rSTR1)
105         xor.    rBITDIF, rWORD1, rWORD2
106         sub     rRTN, rWORD1, rWORD2
107         blt-    L(highbit)
108         srawi   rRTN, rRTN, 31
109         ori     rRTN, rRTN, 1
110         blr
111 L(highbit):
112         srwi    rWORD2, rWORD2, 24
113         srwi    rWORD1, rWORD1, 24
114         sub     rRTN, rWORD1, rWORD2
115         blr
118 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
119         .align 4
120 L(tail):
121         and.    rTMP, rTMP, rNEG
122         cmpw    cr1, rWORD1, rWORD2
123         bne-    L(endstring)
124         addi    rSTR1, rSTR1, 4
125         bne-    cr1, L(different)
126         addi    rSTR2, rSTR2, 4
127         cmplwi  cr1, rN, 0
128 L(unaligned):
129         mtctr   rN      /* Power4 wants mtctr 1st in dispatch group */
130         bgt     cr1, L(uz)
131 L(ux):
132         li      rRTN, 0
133         blr
134         .align 4
135 L(uz):
136         lbz     rWORD1, 0(rSTR1)
137         lbz     rWORD2, 0(rSTR2)
138         nop
139         b       L(u1)
140 L(u0):
141         lbzu    rWORD2, 1(rSTR2)
142 L(u1):
143         bdz     L(u3)
144         cmpwi   cr1, rWORD1, 0
145         cmpw    rWORD1, rWORD2
146         beq-    cr1, L(u3)
147         lbzu    rWORD1, 1(rSTR1)
148         bne-    L(u2)
149         lbzu    rWORD2, 1(rSTR2)
150         bdz     L(u3)
151         cmpwi   cr1, rWORD1, 0
152         cmpw    rWORD1, rWORD2
153         bne-    L(u3)
154         lbzu    rWORD1, 1(rSTR1)
155         bne+    cr1, L(u0)
157 L(u2):  lbzu    rWORD1, -1(rSTR1)       
158 L(u3):  sub     rRTN, rWORD1, rWORD2
159         blr
160 END (BP_SYM (strncmp))
161 libc_hidden_builtin_def (strncmp)