Add hidden_def.
[glibc.git] / sysdeps / sparc / sparc64 / strncpy.S
blob23f8f4fa7d2044b412cf27cb85241f4a76366d52
1 /* strncpy(DST, SRC, COUNT) - Copy no more than COUNT bytes of the
2    null-terminated string from SRC to DST.  If SRC does not cover all of
3    COUNT, the balance is zeroed.
4    For SPARC v9.
5    Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
6    This file is part of the GNU C Library.
7    Contributed by Jan Vondrak <jvon4518@ss1000.ms.mff.cuni.cz> and
8                   Jakub Jelinek <jj@ultra.linux.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
23    02111-1307 USA.  */
25 #include <sysdep.h>
26 #include <asm/asi.h>
27 #ifndef XCC
28 #define XCC xcc
29 #define USE_BPR
30         .register       %g2, #scratch
31         .register       %g3, #scratch
32         .register       %g6, #scratch
33 #endif
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.
49          */
51         .text
52         .align          32
53 ENTRY(strncpy)
54         sethi           %hi(0x01010101), %g1            /* IEU0         Group           */
55 #ifdef USE_BPR
56         brz,pn          %o2, 19f                        /* CTI+IEU1                     */
57 #else
58         tst             %o2                             /* IEU1                         */
59         be,pn           %XCC, 19f                       /* CTI                          */
60 #endif
61          mov            %o0, %g6                        /* IEU0         Group           */
62         or              %g1, %lo(0x01010101), %g1       /* IEU1                         */
64         andcc           %o0, 7, %g0                     /* IEU1         Group           */
65         sllx            %g1, 32, %g2                    /* IEU0                         */
66         bne,pn          %icc, 26f                       /* CTI                          */
67          or             %g1, %g2, %g1                   /* IEU0         Group           */
69         andcc           %o1, 7, %g3                     /* IEU1                         */
70         bne,pn          %icc, 28f                       /* CTI                          */
71          sllx           %g1, 7, %g2                     /* IEU0         Group           */
72         ldx             [%o1], %o3                      /* Load                         */
74 1:      add             %o1, 8, %o1                     /* IEU1                         */
75 2:      subcc           %o2, 8, %o2                     /* IEU1         Group           */
76         bl,pn           %XCC, 18f                       /* CTI                          */
77          sub            %o3, %g1, %o4                   /* IEU0                         */
79         add             %o0, 8, %o0                     /* IEU0         Group           */
80 #ifdef EIGHTBIT_NOT_MORE
81         andn            %o4, %o3, %o4                   /* IEU1                         */
82 #endif
83         mov             %o3, %g3                        /* IEU1                         */
84         ldxa            [%o1] ASI_PNF, %o3              /* Load                         */
85         add             %o1, 8, %o1                     /* IEU0         Group           */
87         andcc           %o4, %g2, %g0                   /* IEU1                         */
88         be,a,pt         %xcc, 2b                        /* CTI                          */
89          stx            %g3, [%o0-8]                    /* Store        Group           */
90         srlx            %g3, 56, %g5                    /* IEU0         Group           */
92         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
93         be,pn           %icc, 16f                       /* CTI                          */
94          srlx           %g3, 48, %g4                    /* IEU0                         */
95         andcc           %g4, 0xff, %g0                  /* IEU1         Group           */
97         be,pn           %icc, 15f                       /* CTI                          */
98          srlx           %g3, 40, %g5                    /* IEU0                         */
99         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
100         be,pn           %icc, 14f                       /* CTI                          */
102          srlx           %g3, 32, %g4                    /* IEU0                         */
103         andcc           %g4, 0xff, %g0                  /* IEU1         Group           */
104         be,pn           %icc, 13f                       /* CTI                          */
105          srlx           %g3, 24, %g5                    /* IEU0                         */
107         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
108         be,pn           %icc, 12f                       /* CTI                          */
109          srlx           %g3, 16, %g4                    /* IEU0                         */
110         andcc           %g4, 0xff, %g0                  /* IEU1         Group           */
112         be,pn           %icc, 11f                       /* CTI                          */
113          srlx           %g3, 8, %g5                     /* IEU0                         */
114         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
115         be,pn           %icc, 10f                       /* CTI                          */
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                         */
161         .align          16
162 10:     ba,pt           %xcc, 3b                        /* CTI                          */
163          sllx           %g5, 8, %g3                     /* IEU0                         */
164 11:     ba,pt           %xcc, 3b                        /* CTI          Group           */
165          sllx           %g4, 16, %g3                    /* IEU0                         */
167 12:     ba,pt           %xcc, 3b                        /* CTI          Group           */
168          sllx           %g5, 24, %g3                    /* IEU0                         */
169 13:     ba,pt           %xcc, 3b                        /* CTI          Group           */
170          sllx           %g4, 32, %g3                    /* IEU0                         */
172 14:     ba,pt           %xcc, 3b                        /* CTI          Group           */
173          sllx           %g5, 40, %g3                    /* IEU0                         */
174 15:     ba,pt           %xcc, 3b                        /* CTI          Group           */
175          sllx           %g4, 48, %g3                    /* IEU0                         */
177 16:     ba,pt           %xcc, 3b                        /* CTI                          */
178          sllx           %g5, 56, %g3                    /* IEU0                         */
179 17:     or              %o3, %o4, %o3                   /* IEU0         Group           */
180         sub             %o3, %g1, %o4                   /* IEU1                         */
182 18:     addcc           %o2, 8, %o2                     /* IEU1         Group           */
183         be,pn           %XCC, 19f                       /* CTI                          */
184          andcc          %o4, %g2, %g0                   /* IEU1         Group           */
185         be,pt           %xcc, 21f                       /* CTI                          */
187          srlx           %o3, 56, %g5                    /* IEU0                         */
188         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
189         be,pn           %icc, 20f                       /* CTI                          */
190          stb            %g5, [%o0]                      /* Store                        */
192         add             %o0, 1, %o0                     /* IEU0         Group           */
193         subcc           %o2, 1, %o2                     /* IEU1                         */
194         be,pn           %XCC, 19f                       /* CTI                          */
195          srlx           %o3, 48, %g5                    /* IEU0         Group           */
197         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
198         be,pn           %icc, 20f                       /* CTI                          */
199          stb            %g5, [%o0]                      /* Store                        */
200         add             %o0, 1, %o0                     /* IEU0         Group           */
202         subcc           %o2, 1, %o2                     /* IEU1                         */
203         be,pn           %XCC, 19f                       /* CTI                          */
204          srlx           %o3, 40, %g5                    /* IEU0         Group           */
205         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
207         be,pn           %icc, 20f                       /* CTI                          */
208          stb            %g5, [%o0]                      /* Store                        */
209         add             %o0, 1, %o0                     /* IEU0         Group           */
210         subcc           %o2, 1, %o2                     /* IEU1                         */
212         be,pn           %XCC, 19f                       /* CTI                          */
213          srlx           %o3, 32, %g5                    /* IEU0         Group           */
214         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
215         be,pn           %icc, 20f                       /* CTI                          */
217          stb            %g5, [%o0]                      /* Store                        */
218         add             %o0, 1, %o0                     /* IEU0         Group           */
219         subcc           %o2, 1, %o2                     /* IEU1                         */
220         be,pn           %XCC, 19f                       /* CTI                          */
222          srlx           %o3, 24, %g5                    /* IEU0         Group           */
223         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
224         be,pn           %icc, 20f                       /* CTI                          */
225          stb            %g5, [%o0]                      /* Store                        */
227         add             %o0, 1, %o0                     /* IEU0         Group           */
228         subcc           %o2, 1, %o2                     /* IEU1                         */
229         be,pn           %XCC, 19f                       /* CTI                          */
230          srlx           %o3, 16, %g5                    /* IEU0         Group           */
231         
232         andcc           %g5, 0xff, %g0                  /* IEU1         Group           */
233         be,pn           %icc, 20f                       /* CTI                          */
234          stb            %g5, [%o0]                      /* Store                        */
235         add             %o0, 1, %o0                     /* IEU0         Group           */
237         subcc           %o2, 1, %o2                     /* IEU1                         */
238         be,pn           %XCC, 19f                       /* CTI                          */
239          srlx           %o3, 8, %g5                     /* IEU0         Group           */
240         stb             %g5, [%o0]                      /* Store                        */ 
242 19:     retl                                            /* CTI+IEU1     Group           */
243          mov            %g6, %o0                        /* IEU0                         */
244 50:     stb             %g0, [%o0]                      /* Store        Group           */
245 20:     subcc           %o2, 1, %o2                     /* IEU1         Group           */
247         bne,pt          %XCC, 50b                       /* CTI                          */
248          add            %o0, 1, %o0                     /* IEU0                         */
249         retl                                            /* CTI+IEU1     Group           */
250          mov            %g6, %o0                        /* IEU0                         */
252 21:     andcc           %o2, 4, %g0                     /* IEU1         Group           */
253         be,pn           %icc, 22f                       /* CTI                          */
254          srlx           %o3, 32, %g5                    /* IEU0                         */
255         stw             %g5, [%o0]                      /* Store        Group           */
257         add             %o0, 4, %o0                     /* IEU0                         */
258         mov             %o3, %g5                        /* IEU1                         */
259 22:     andcc           %o2, 2, %g0                     /* IEU1         Group           */
260         be,pn           %icc, 23f                       /* CTI                          */
262          srlx           %g5, 16, %g4                    /* IEU0                         */
263         sth             %g4, [%o0]                      /* Store        Group           */
264         add             %o0, 2, %o0                     /* IEU0                         */
265         mov             %g5, %g4                        /* IEU1                         */
267 23:     srlx            %g4, 8, %g4                     /* IEU0         Group           */
268         andcc           %o2, 1, %g0                     /* IEU1                         */
269         bne,a,pn        %icc, 24f                       /* CTI                          */
270          stb            %g4, [%o0]                      /* Store        Group           */
272 24:     retl                                            /* CTI+IEU1     Group           */
273          mov            %g6, %o0                        /* IEU0                         */
274 25:     andcc           %o0, 7, %g0                     /* IEU1         Group           */
275         be,a,pn         %icc, 4b                        /* CTI                          */
277          andncc         %o2, 31, %g3                    /* IEU1         Group           */
278         stb             %g0, [%o0]                      /* Store        Group           */
279         subcc           %o2, 1, %o2                     /* IEU1                         */
280         bne,pt          %XCC, 25b                       /* CTI                          */
282          add            %o0, 1, %o0                     /* IEU0         Group           */
283         retl                                            /* CTI+IEU1     Group           */
284          mov            %g6, %o0                        /* IEU0                         */
286         .align          16
287 26:     ldub            [%o1], %o3                      /* Load                         */
288         sllx            %g1, 7, %g2                     /* IEU0         Group           */
289         stb             %o3, [%o0]                      /* Store                        */
290 27:     subcc           %o2, 1, %o2                     /* IEU1                         */
292         be,pn           %XCC, 9b                        /* CTI                          */
293          add            %o1, 1, %o1                     /* IEU0         Group           */
294         add             %o0, 1, %o0                     /* IEU1                         */
295         andcc           %o3, 0xff, %g0                  /* IEU1         Group           */
297         be,pn           %icc, 25b                       /* CTI                          */
298          lduba          [%o1] ASI_PNF, %o3              /* Load                         */
299         andcc           %o0, 7, %g0                     /* IEU1         Group           */
300         bne,a,pt        %icc, 27b                       /* CTI                          */
302          stb            %o3, [%o0]                      /* Store                        */
303         andcc           %o1, 7, %g3                     /* IEU1         Group           */
304         be,a,pt         %icc, 1b                        /* CTI                          */
305          ldx            [%o1], %o3                      /* Load                         */
307 28:     orcc            %g0, 64, %g4                    /* IEU1         Group           */
308         sllx            %g3, 3, %g5                     /* IEU0                         */
309         sub             %g4, %g5, %g4                   /* IEU0         Group           */
310         sub             %o1, %g3, %o1                   /* IEU1                         */
311                                                         /* %g1 = 0101010101010101
312                                                            %g2 = 8080808080808080
313                                                            %g3 = source alignment
314                                                            %g5 = number of bits to shift left
315                                                            %g4 = number of bits to shift right */
317         ldxa            [%o1] ASI_PNF, %o5              /* Load         Group           */
318         addcc           %o1, 8, %o1                     /* IEU1                         */
320 29:     sllx            %o5, %g5, %o3                   /* IEU0         Group           */
321         ldxa            [%o1] ASI_PNF, %o5              /* Load                         */
322         subcc           %o2, 8, %o2                     /* IEU1                         */
323         bl,pn           %XCC, 17b                       /* CTI                          */
325          srlx           %o5, %g4, %o4                   /* IEU0         Group           */
326         add             %o1, 8, %o1                     /* IEU1                         */
327         or              %o3, %o4, %o3                   /* IEU0         Group           */
328         add             %o0, 8, %o0                     /* IEU1                         */
330         sub             %o3, %g1, %o4                   /* IEU0         Group           */
331 #ifdef EIGHTBIT_NOT_RARE
332         andn            %o4, %o3, %o4                   /* IEU0         Group           */
333 #endif
334         andcc           %o4, %g2, %g0                   /* IEU1         Group           */
335         be,a,pt         %xcc, 29b                       /* CTI                          */
336          stx            %o3, [%o0-8]                    /* Store                        */
338         srlx            %o3, 56, %o4                    /* IEU0         Group           */
339         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
340         be,pn           %icc, 36f                       /* CTI                          */
341          srlx           %o3, 48, %o4                    /* IEU0                         */
343         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
344         be,pn           %icc, 35f                       /* CTI                          */
345          srlx           %o3, 40, %o4                    /* IEU0                         */
346         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
348         be,pn           %icc, 34f                       /* CTI                          */
349          srlx           %o3, 32, %o4                    /* IEU0                         */
350         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
351         be,pn           %icc, 33f                       /* CTI                          */
353          srlx           %o3, 24, %o4                    /* IEU0                         */
354         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
355         be,pn           %icc, 32f                       /* CTI                          */
356          srlx           %o3, 16, %o4                    /* IEU0                         */
358         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
359         be,pn           %icc, 31f                       /* CTI                          */
360          srlx           %o3, 8, %o4                     /* IEU0                         */
361         andcc           %o4, 0xff, %g0                  /* IEU1         Group           */
363         be,pn           %icc, 30f                       /* CTI                          */
364          andcc          %o3, 0xff, %g0                  /* IEU1         Group           */
365         bne,pn          %icc, 29b                       /* CTI                          */
366          stx            %o3, [%o0-8]                    /* Store                        */
368         ba,pt           %xcc, 4b                        /* CTI          Group           */
369          andncc         %o2, 31, %g3                    /* IEU1                         */
370 30:     srlx            %o3, 8, %o4                     /* IEU0                         */
371         ba,pt           %xcc, 3b                        /* CTI                          */
373          sllx           %o4, 8, %g3                     /* IEU0         Group           */
374 31:     srlx            %o3, 16, %o4                    /* IEU0         Group           */
375         ba,pt           %xcc, 3b                        /* CTI                          */
376          sllx           %o4, 16, %g3                    /* IEU0         Group           */
378 32:     srlx            %o3, 24, %o4                    /* IEU0         Group           */
379         ba,pt           %xcc, 3b                        /* CTI                          */
380          sllx           %o4, 24, %g3                    /* IEU0         Group           */
381 33:     srlx            %o3, 32, %o4                    /* IEU0         Group           */
383         ba,pt           %xcc, 3b                        /* CTI                          */
384          sllx           %o4, 32, %g3                    /* IEU0         Group           */
385 34:     srlx            %o3, 40, %o4                    /* IEU0         Group           */
386         ba,pt           %xcc, 3b                        /* CTI                          */
388          sllx           %o4, 40, %g3                    /* IEU0         Group           */
389 35:     srlx            %o3, 48, %o4                    /* IEU0         Group           */
390         ba,pt           %xcc, 3b                        /* CTI                          */
391          sllx           %o4, 48, %g3                    /* IEU0         Group           */
393 36:     srlx            %o3, 56, %o4                    /* IEU0         Group           */
394         ba,pt           %xcc, 3b                        /* CTI                          */
395          sllx           %o4, 56, %g3                    /* IEU0         Group           */
396 END(strncpy)
397 libc_hidden_builtin_def (strncpy)