1 | /* Copyright (C) 1996-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library. If not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | /* Bytewise compare two null-terminated strings. */ |
19 | |
20 | #include <sysdep.h> |
21 | |
22 | .set noat |
23 | .set noreorder |
24 | |
25 | .text |
26 | |
27 | ENTRY(strcmp) |
28 | #ifdef PROF |
29 | ldgp gp, 0(pv) |
30 | lda AT, _mcount |
31 | jmp AT, (AT), _mcount |
32 | .prologue 1 |
33 | #else |
34 | .prologue 0 |
35 | #endif |
36 | |
37 | ldq_u t0, 0(a0) # e0 : give cache time to catch up |
38 | xor a0, a1, t2 # .. e1 : are s1 and s2 co-aligned? |
39 | ldq_u t1, 0(a1) # e0 : |
40 | and t2, 7, t2 # .. e1 : |
41 | lda t3, -1 # e0 : |
42 | bne t2, $unaligned # .. e1 : |
43 | |
44 | /* On entry to this basic block: |
45 | t0 == the first destination word for masking back in |
46 | t1 == the first source word. |
47 | t3 == -1. */ |
48 | |
49 | $aligned: |
50 | mskqh t3, a0, t3 # e0 : |
51 | nop # .. e1 : |
52 | ornot t1, t3, t1 # e0 : |
53 | ornot t0, t3, t0 # .. e1 : |
54 | cmpbge zero, t1, t7 # e0 : bits set iff null found |
55 | bne t7, $eos # e1 (zdb) |
56 | |
57 | /* Aligned compare main loop. |
58 | On entry to this basic block: |
59 | t0 == an s1 word. |
60 | t1 == an s2 word not containing a null. */ |
61 | |
62 | $a_loop: |
63 | xor t0, t1, t2 # e0 : |
64 | bne t2, $wordcmp # .. e1 (zdb) |
65 | ldq_u t1, 8(a1) # e0 : |
66 | ldq_u t0, 8(a0) # .. e1 : |
67 | addq a1, 8, a1 # e0 : |
68 | addq a0, 8, a0 # .. e1 : |
69 | cmpbge zero, t1, t7 # e0 : |
70 | beq t7, $a_loop # .. e1 (zdb) |
71 | br $eos # e1 : |
72 | |
73 | /* The two strings are not co-aligned. Align s1 and cope. */ |
74 | |
75 | $unaligned: |
76 | and a0, 7, t4 # e0 : find s1 misalignment |
77 | and a1, 7, t5 # .. e1 : find s2 misalignment |
78 | subq a1, t4, a1 # e0 : |
79 | |
80 | /* If s2 misalignment is larger than s2 misalignment, we need |
81 | extra startup checks to avoid SEGV. */ |
82 | |
83 | cmplt t4, t5, t8 # .. e1 : |
84 | beq t8, $u_head # e1 : |
85 | |
86 | mskqh t3, t5, t3 # e0 : |
87 | ornot t1, t3, t3 # e0 : |
88 | cmpbge zero, t3, t7 # e1 : is there a zero? |
89 | beq t7, $u_head # e1 : |
90 | |
91 | /* We've found a zero in the first partial word of s2. Align |
92 | our current s1 and s2 words and compare what we've got. */ |
93 | |
94 | extql t1, t5, t1 # e0 : |
95 | extql t0, a0, t0 # e0 : |
96 | cmpbge zero, t1, t7 # .. e1 : find that zero again |
97 | br $eos # e1 : and finish up |
98 | |
99 | .align 3 |
100 | $u_head: |
101 | /* We know just enough now to be able to assemble the first |
102 | full word of s2. We can still find a zero at the end of it. |
103 | |
104 | On entry to this basic block: |
105 | t0 == first word of s1 |
106 | t1 == first partial word of s2. */ |
107 | |
108 | ldq_u t2, 8(a1) # e0 : load second partial s2 word |
109 | lda t3, -1 # .. e1 : create leading garbage mask |
110 | extql t1, a1, t1 # e0 : create first s2 word |
111 | mskqh t3, a0, t3 # e0 : |
112 | extqh t2, a1, t4 # e0 : |
113 | ornot t0, t3, t0 # .. e1 : kill s1 garbage |
114 | or t1, t4, t1 # e0 : s2 word now complete |
115 | cmpbge zero, t0, t7 # .. e1 : find zero in first s1 word |
116 | ornot t1, t3, t1 # e0 : kill s2 garbage |
117 | lda t3, -1 # .. e1 : |
118 | mskql t3, a1, t3 # e0 : mask for s2[1] bits we have seen |
119 | bne t7, $eos # .. e1 : |
120 | xor t0, t1, t4 # e0 : compare aligned words |
121 | bne t4, $wordcmp # .. e1 (zdb) |
122 | or t2, t3, t3 # e0 : |
123 | cmpbge zero, t3, t7 # e1 : |
124 | bne t7, $u_final # e1 : |
125 | |
126 | /* Unaligned copy main loop. In order to avoid reading too much, |
127 | the loop is structured to detect zeros in aligned words from s2. |
128 | This has, unfortunately, effectively pulled half of a loop |
129 | iteration out into the head and half into the tail, but it does |
130 | prevent nastiness from accumulating in the very thing we want |
131 | to run as fast as possible. |
132 | |
133 | On entry to this basic block: |
134 | t2 == the unshifted low-bits from the next s2 word. */ |
135 | |
136 | .align 3 |
137 | $u_loop: |
138 | extql t2, a1, t3 # e0 : |
139 | ldq_u t2, 16(a1) # .. e1 : load next s2 high bits |
140 | ldq_u t0, 8(a0) # e0 : load next s1 word |
141 | addq a1, 8, a1 # .. e1 : |
142 | addq a0, 8, a0 # e0 : |
143 | nop # .. e1 : |
144 | extqh t2, a1, t1 # e0 : |
145 | cmpbge zero, t0, t7 # .. e1 : find zero in current s1 word |
146 | or t1, t3, t1 # e0 : |
147 | bne t7, $eos # .. e1 : |
148 | xor t0, t1, t4 # e0 : compare the words |
149 | bne t4, $wordcmp # .. e1 (zdb) |
150 | cmpbge zero, t2, t4 # e0 : find zero in next low bits |
151 | beq t4, $u_loop # .. e1 (zdb) |
152 | |
153 | /* We've found a zero in the low bits of the last s2 word. Get |
154 | the next s1 word and align them. */ |
155 | $u_final: |
156 | ldq_u t0, 8(a0) # e1 : |
157 | extql t2, a1, t1 # .. e0 : |
158 | cmpbge zero, t1, t7 # e0 : |
159 | |
160 | /* We've found a zero somewhere in a word we just read. |
161 | On entry to this basic block: |
162 | t0 == s1 word |
163 | t1 == s2 word |
164 | t7 == cmpbge mask containing the zero. */ |
165 | |
166 | .align 3 |
167 | $eos: |
168 | negq t7, t6 # e0 : create bytemask of valid data |
169 | and t6, t7, t8 # e1 : |
170 | subq t8, 1, t6 # e0 : |
171 | or t6, t8, t7 # e1 : |
172 | zapnot t0, t7, t0 # e0 : kill the garbage |
173 | zapnot t1, t7, t1 # .. e1 : |
174 | xor t0, t1, v0 # e0 : and compare |
175 | beq v0, $done # .. e1 : |
176 | |
177 | /* Here we have two differing co-aligned words in t0 & t1. |
178 | Bytewise compare them and return (t0 > t1 ? 1 : -1). */ |
179 | $wordcmp: |
180 | cmpbge t0, t1, t2 # e0 : comparison yields bit mask of ge |
181 | cmpbge t1, t0, t3 # .. e1 : |
182 | xor t2, t3, t0 # e0 : bits set iff t0/t1 bytes differ |
183 | negq t0, t1 # e1 : clear all but least bit |
184 | and t0, t1, t0 # e0 : |
185 | lda v0, -1 # .. e1 : |
186 | and t0, t2, t1 # e0 : was bit set in t0 > t1? |
187 | cmovne t1, 1, v0 # .. e1 (zdb) |
188 | |
189 | $done: |
190 | ret # e1 : |
191 | |
192 | END(strcmp) |
193 | libc_hidden_builtin_def (strcmp) |
194 | |