1 /* stpncpy(DST, SRC, COUNT) - Copy no more than N characters of
2 SRC to DEST, returning the address of the terminating '\0' in
3 DEST, if any, or else DEST + N.
5 Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
7 Contributed by Jakub Jelinek <jj@ultra.linux.cz> and
8 Jan Vondrak <jvon4518@ss1000.ms.mff.cuni.cz>.
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with the GNU C Library; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 .register %g2, #scratch
31 .register %g3, #scratch
32 .register %g6, #scratch
35 /* Normally, this uses
36 ((xword - 0x0101010101010101) & 0x8080808080808080) test
37 to find out if any byte in xword could be zero. This is fast, but
38 also gives false alarm for any byte in range 0x81-0xff. It does
39 not matter for correctness, as if this test tells us there could
40 be some zero byte, we check it byte by byte, but if bytes with
41 high bits set are common in the strings, then this will give poor
42 performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
43 will use one tick slower, but more precise test
44 ((xword - 0x0101010101010101) & (~xword) & 0x8080808080808080),
45 which does not give any false alarms (but if some bits are set,
46 one cannot assume from it which bytes are zero and which are not).
47 It is yet to be measured, what is the correct default for glibc
48 in these days for an average user.
54 sethi %hi(0x01010101), %g1 /* IEU0 Group */
56 brz,pn %o2, 19f /* CTI+IEU1 */
59 be,pn %XCC, 19f /* CTI */
61 or %g1, %lo(0x01010101), %g1 /* IEU1 */
62 andcc %o0, 7, %g0 /* IEU1 Group */
64 sllx %g1, 32, %g2 /* IEU0 */
65 bne,pn %icc, 26f /* CTI */
66 or %g1, %g2, %g1 /* IEU0 Group */
67 andcc %o1, 7, %g3 /* IEU1 */
69 bne,pn %icc, 28f /* CTI */
70 sllx %g1, 7, %g2 /* IEU0 Group */
71 ldx [%o1], %o3 /* Load */
72 1: add %o1, 8, %o1 /* IEU1 */
74 2: subcc %o2, 8, %o2 /* IEU1 Group */
75 bl,pn %XCC, 18f /* CTI */
76 sub %o3, %g1, %o4 /* IEU0 */
77 add %o0, 8, %o0 /* IEU0 Group */
79 #ifdef EIGHTBIT_NOT_MORE
80 andn %o4, %o3, %o4 /* IEU1 */
82 mov %o3, %g3 /* IEU1 */
83 ldxa [%o1] ASI_PNF, %o3 /* Load */
84 add %o1, 8, %o1 /* IEU0 Group */
85 andcc %o4, %g2, %g0 /* IEU1 */
87 be,a,pt %xcc, 2b /* CTI */
88 stx %g3, [%o0-8] /* Store Group */
89 srlx %g3, 56, %g5 /* IEU0 Group */
90 andcc %g5, 0xff, %g0 /* IEU1 Group */
92 be,pn %icc, 16f /* CTI */
93 srlx %g3, 48, %g4 /* IEU0 */
94 andcc %g4, 0xff, %g0 /* IEU1 Group */
95 be,pn %icc, 15f /* CTI */
97 srlx %g3, 40, %g5 /* IEU0 */
98 andcc %g5, 0xff, %g0 /* IEU1 Group */
99 be,pn %icc, 14f /* CTI */
100 srlx %g3, 32, %g4 /* IEU0 */
102 andcc %g4, 0xff, %g0 /* IEU1 Group */
103 be,pn %icc, 13f /* CTI */
104 srlx %g3, 24, %g5 /* IEU0 */
105 andcc %g5, 0xff, %g0 /* IEU1 Group */
107 be,pn %icc, 12f /* CTI */
108 srlx %g3, 16, %g4 /* IEU0 */
109 andcc %g4, 0xff, %g0 /* IEU1 Group */
110 be,pn %icc, 11f /* CTI */
112 srlx %g3, 8, %g5 /* IEU0 */
113 andcc %g5, 0xff, %g0 /* IEU1 Group */
114 be,pn %icc, 10f /* CTI */
115 sub %o0, 1, %g6 /* IEU0 */
117 andcc %g3, 0xff, %g0 /* IEU1 Group */
118 bne,pt %icc, 2b /* CTI */
119 3: stx %g3, [%o0-8] /* Store */
120 andncc %o2, 31, %g3 /* IEU1 Group */
122 4: be,pn %XCC, 41f /* CTI */
123 and %o2, 31, %o2 /* IEU1 Group */
124 40: stx %g0, [%o0] /* Store */
125 stx %g0, [%o0 + 8] /* Store Group */
127 subcc %g3, 32, %g3 /* IEU1 */
128 stx %g0, [%o0 + 16] /* Store Group */
129 stx %g0, [%o0 + 24] /* Store Group */
130 bne,pt %XCC, 40b /* CTI */
132 add %o0, 32, %o0 /* IEU0 */
133 41: subcc %o2, 8, %o2 /* IEU1 Group */
134 bl,a,pn %XCC, 6f /* CTI */
135 andcc %o2, 4, %g0 /* IEU1 Group */
137 5: stx %g0, [%o0] /* Store */
138 subcc %o2, 8, %o2 /* IEU1 Group */
139 bge,pt %XCC, 5b /* CTI */
140 add %o0, 8, %o0 /* IEU0 */
142 andcc %o2, 4, %g0 /* IEU1 Group */
143 6: be,a,pn %icc, 7f /* CTI */
144 andcc %o2, 2, %g0 /* IEU1 Group */
145 stw %g0, [%o0] /* Store */
147 add %o0, 4, %o0 /* IEU0 */
148 andcc %o2, 2, %g0 /* IEU1 Group */
149 7: be,a,pn %icc, 8f /* CTI */
150 andcc %o2, 1, %g0 /* IEU1 Group */
152 sth %g0, [%o0] /* Store */
153 add %o0, 2, %o0 /* IEU0 */
154 andcc %o2, 1, %g0 /* IEU1 Group */
155 8: bne,a,pn %icc, 9f /* CTI */
157 stb %g0, [%o0] /* Store */
158 9: retl /* CTI+IEU1 Group */
159 mov %g6, %o0 /* IEU0 */
160 10: subcc %o0, 2, %g6 /* IEU1 Group */
162 ba,pt %xcc, 3b /* CTI */
163 sllx %g5, 8, %g3 /* IEU0 */
164 11: subcc %o0, 3, %g6 /* IEU1 Group */
165 ba,pt %xcc, 3b /* CTI */
167 sllx %g4, 16, %g3 /* IEU0 */
168 12: subcc %o0, 4, %g6 /* IEU1 Group */
169 ba,pt %xcc, 3b /* CTI */
170 sllx %g5, 24, %g3 /* IEU0 */
172 13: subcc %o0, 5, %g6 /* IEU1 Group */
173 ba,pt %xcc, 3b /* CTI */
174 sllx %g4, 32, %g3 /* IEU0 */
175 14: subcc %o0, 6, %g6 /* IEU1 Group */
177 ba,pt %xcc, 3b /* CTI */
178 sllx %g5, 40, %g3 /* IEU0 */
179 15: subcc %o0, 7, %g6 /* IEU1 Group */
180 ba,pt %xcc, 3b /* CTI */
182 sllx %g4, 48, %g3 /* IEU0 */
183 16: subcc %o0, 8, %g6 /* IEU1 Group */
184 ba,pt %xcc, 3b /* CTI */
188 17: or %o3, %o4, %o3 /* IEU0 Group */
189 sub %o3, %g1, %o4 /* IEU1 */
190 18: addcc %o2, 8, %o2 /* IEU1 Group */
191 be,pn %XCC, 19f /* CTI */
193 andcc %o4, %g2, %g0 /* IEU1 Group */
194 be,pt %xcc, 21f /* CTI */
195 srlx %o3, 56, %g5 /* IEU0 */
196 andcc %g5, 0xff, %g0 /* IEU1 Group */
198 be,pn %icc, 20f /* CTI */
199 stb %g5, [%o0] /* Store */
200 add %o0, 1, %o0 /* IEU0 Group */
201 subcc %o2, 1, %o2 /* IEU1 */
203 be,pn %XCC, 19f /* CTI */
204 srlx %o3, 48, %g5 /* IEU0 Group */
205 andcc %g5, 0xff, %g0 /* IEU1 Group */
206 be,pn %icc, 20f /* CTI */
208 stb %g5, [%o0] /* Store */
209 add %o0, 1, %o0 /* IEU0 Group */
210 subcc %o2, 1, %o2 /* IEU1 */
211 be,pn %XCC, 19f /* CTI */
213 srlx %o3, 40, %g5 /* IEU0 Group */
214 andcc %g5, 0xff, %g0 /* IEU1 Group */
215 be,pn %icc, 20f /* CTI */
216 stb %g5, [%o0] /* Store */
218 add %o0, 1, %o0 /* IEU0 Group */
219 subcc %o2, 1, %o2 /* IEU1 */
220 be,pn %XCC, 19f /* CTI */
221 srlx %o3, 32, %g5 /* IEU0 Group */
223 andcc %g5, 0xff, %g0 /* IEU1 Group */
224 be,pn %icc, 20f /* CTI */
225 stb %g5, [%o0] /* Store */
226 add %o0, 1, %o0 /* IEU0 Group */
228 subcc %o2, 1, %o2 /* IEU1 */
229 be,pn %XCC, 19f /* CTI */
230 srlx %o3, 24, %g5 /* IEU0 Group */
231 andcc %g5, 0xff, %g0 /* IEU1 Group */
233 be,pn %icc, 20f /* CTI */
234 stb %g5, [%o0] /* Store */
235 add %o0, 1, %o0 /* IEU0 Group */
236 subcc %o2, 1, %o2 /* IEU1 */
238 be,pn %XCC, 19f /* CTI */
239 srlx %o3, 16, %g5 /* IEU0 Group */
240 andcc %g5, 0xff, %g0 /* IEU1 Group */
241 be,pn %icc, 20f /* CTI */
243 stb %g5, [%o0] /* Store */
244 add %o0, 1, %o0 /* IEU0 Group */
245 subcc %o2, 1, %o2 /* IEU1 */
246 be,pn %XCC, 19f /* CTI */
248 srlx %o3, 8, %o3 /* IEU0 Group */
249 stb %o3, [%o0] /* Store */
250 59: add %o0, 1, %o2 /* IEU1 */
251 andcc %o3, 0xff, %g0 /* IEU1 Group */
253 retl /* CTI+IEU1 Group */
254 movne %icc, %o2, %o0 /* Single Group */
255 19: retl /* CTI+IEU1 Group */
258 20: mov %o0, %g6 /* IEU0 Group */
259 subcc %o2, 1, %o2 /* IEU1 */
260 be,pn %XCC, 51f /* CTI */
261 add %o0, 1, %o0 /* IEU0 Group */
263 50: stb %g0, [%o0] /* Store Group */
264 subcc %o2, 1, %o2 /* IEU1 Group */
265 bne,pt %XCC, 50b /* CTI */
266 add %o0, 1, %o0 /* IEU0 */
268 51: retl /* CTI+IEU1 Group */
269 mov %g6, %o0 /* IEU0 */
272 21: andcc %o2, 4, %g0 /* IEU1 Group */
273 be,pn %icc, 22f /* CTI */
274 srlx %o3, 32, %g5 /* IEU0 */
275 stw %g5, [%o0] /* Store Group */
277 add %o0, 4, %o0 /* IEU0 */
278 mov %o3, %g5 /* IEU1 */
279 22: andcc %o2, 2, %g0 /* IEU1 Group */
280 be,pn %icc, 23f /* CTI */
282 srlx %g5, 16, %g4 /* IEU0 */
283 sth %g4, [%o0] /* Store Group */
284 add %o0, 2, %o0 /* IEU0 */
285 mov %g5, %g4 /* IEU1 */
287 23: srlx %g4, 8, %g4 /* IEU0 Group */
288 andcc %o2, 1, %g0 /* IEU1 */
289 bne,a,pn %icc, 24f /* CTI */
290 stb %g4, [%o0] /* Store Group */
292 retl /* CTI+IEU1 Group */
294 24: retl /* CTI+IEU1 Group */
295 add %o0, 1, %o0 /* IEU0 */
298 55: sub %o0, 1, %g6 /* IEU0 Group */
299 25: andcc %o0, 7, %g0 /* IEU1 */
300 be,a,pn %icc, 4b /* CTI */
301 andncc %o2, 31, %g3 /* IEU1 Group */
303 stb %g0, [%o0] /* Store Group */
304 subcc %o2, 1, %o2 /* IEU1 */
305 bne,pt %XCC, 25b /* CTI */
306 add %o0, 1, %o0 /* IEU0 Group */
308 retl /* CTI+IEU1 Group */
309 mov %g6, %o0 /* IEU0 */
312 26: ldub [%o1], %o3 /* Load */
313 sllx %g1, 7, %g2 /* IEU0 Group */
314 stb %o3, [%o0] /* Store */
315 27: subcc %o2, 1, %o2 /* IEU1 */
317 be,pn %XCC, 59b /* CTI */
318 add %o1, 1, %o1 /* IEU0 Group */
319 add %o0, 1, %o0 /* IEU1 */
320 andcc %o3, 0xff, %g0 /* IEU1 Group */
322 be,pn %icc, 55b /* CTI */
323 lduba [%o1] ASI_PNF, %o3 /* Load */
324 andcc %o0, 7, %g0 /* IEU1 Group */
325 bne,a,pt %icc, 27b /* CTI */
327 stb %o3, [%o0] /* Store */
328 andcc %o1, 7, %g3 /* IEU1 Group */
329 be,a,pt %icc, 1b /* CTI */
330 ldx [%o1], %o3 /* Load */
332 28: orcc %g0, 64, %g4 /* IEU1 Group */
333 sllx %g3, 3, %g5 /* IEU0 */
334 sub %g4, %g5, %g4 /* IEU0 Group */
335 sub %o1, %g3, %o1 /* IEU1 */
336 /* %g1 = 0101010101010101
337 %g2 = 8080808080808080
338 %g3 = source alignment
339 %g5 = number of bits to shift left
340 %g4 = number of bits to shift right */
342 ldxa [%o1] ASI_PNF, %o5 /* Load Group */
343 addcc %o1, 8, %o1 /* IEU1 */
344 29: sllx %o5, %g5, %o3 /* IEU0 Group */
345 ldxa [%o1] ASI_PNF, %o5 /* Load */
347 subcc %o2, 8, %o2 /* IEU1 */
348 bl,pn %XCC, 17b /* CTI */
349 srlx %o5, %g4, %o4 /* IEU0 Group */
350 add %o1, 8, %o1 /* IEU1 */
352 or %o3, %o4, %o3 /* IEU0 Group */
353 add %o0, 8, %o0 /* IEU1 */
354 sub %o3, %g1, %o4 /* IEU0 Group */
355 #ifdef EIGHTBIT_NOT_RARE
356 andn %o4, %o3, %o4 /* IEU0 Group */
358 andcc %o4, %g2, %g0 /* IEU1 Group */
360 be,a,pt %xcc, 29b /* CTI */
361 stx %o3, [%o0-8] /* Store */
362 srlx %o3, 56, %o4 /* IEU0 Group */
363 andcc %o4, 0xff, %g0 /* IEU1 Group */
365 be,pn %icc, 36f /* CTI */
366 srlx %o3, 48, %g6 /* IEU0 */
367 andcc %g6, 0xff, %g0 /* IEU1 Group */
368 be,pn %icc, 35f /* CTI */
370 srlx %o3, 40, %o4 /* IEU0 */
371 andcc %o4, 0xff, %g0 /* IEU1 Group */
372 be,pn %icc, 34f /* CTI */
373 srlx %o3, 32, %g6 /* IEU0 */
375 andcc %g6, 0xff, %g0 /* IEU1 Group */
376 be,pn %icc, 33f /* CTI */
377 srlx %o3, 24, %o4 /* IEU0 */
378 andcc %o4, 0xff, %g0 /* IEU1 Group */
380 be,pn %icc, 32f /* CTI */
381 srlx %o3, 16, %g6 /* IEU0 */
382 andcc %g6, 0xff, %g0 /* IEU1 Group */
383 be,pn %icc, 31f /* CTI */
385 srlx %o3, 8, %o4 /* IEU0 */
386 andcc %o4, 0xff, %g0 /* IEU1 Group */
387 be,pn %icc, 30f /* CTI */
388 andcc %o3, 0xff, %g0 /* IEU1 Group */
390 bne,pn %icc, 29b /* CTI */
391 stx %o3, [%o0-8] /* Store */
392 sub %o0, 1, %g6 /* IEU0 Group */
393 ba,pt %xcc, 4b /* CTI */
395 andncc %o2, 31, %g3 /* IEU1 */
396 30: subcc %o0, 2, %g6 /* IEU0 */
397 ba,pt %xcc, 3b /* CTI */
398 sllx %o4, 8, %g3 /* IEU0 Group */
400 31: sllx %g6, 16, %g3 /* IEU0 Group */
401 ba,pt %xcc, 3b /* CTI */
402 sub %o0, 3, %g6 /* IEU1 */
403 32: subcc %o0, 4, %g6 /* IEU1 Group */
405 ba,pt %xcc, 3b /* CTI */
406 sllx %o4, 24, %g3 /* IEU0 */
407 33: sllx %g6, 32, %g3 /* IEU0 Group */
408 ba,pt %xcc, 3b /* CTI */
410 sub %o0, 5, %g6 /* IEU1 */
411 34: subcc %o0, 6, %g6 /* IEU1 Group */
412 ba,pt %xcc, 3b /* CTI */
413 sllx %o4, 40, %g3 /* IEU0 */
415 35: sllx %g6, 48, %g3 /* IEU0 Group */
416 ba,pt %xcc, 3b /* CTI */
417 sub %o0, 7, %g6 /* IEU1 */
418 36: subcc %o0, 8, %g6 /* IEU1 Group */
420 ba,pt %xcc, 3b /* CTI */
421 sllx %o4, 56, %g3 /* IEU0 */
424 libc_hidden_def (__stpncpy)
425 weak_alias (__stpncpy, stpncpy)