S390: Optimize strncpy and wcsncpy.
[glibc.git] / sysdeps / s390 / multiarch / wcsncpy-vx.S
blob8d692c0c6ecd03fa4d9da9dcc04f140ef305aa88
1 /* Vector optimized 32/64 bit S/390 version of wcsncpy.
2    Copyright (C) 2015 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 #if defined HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc)
21 # include "sysdep.h"
22 # include "asm-syntax.h"
24         .text
26 /* wchar_t *wcsncpy (const wchar_t *dest, const wchar_t *src, size_t n)
27    Copy at most n characters of string  src to dest.
29    Register usage:
30    -r0=dest pointer for return
31    -r1=tmp, zero byte index
32    -r2=dest
33    -r3=src
34    -r4=n
35    -r5=current_len
36    -r6=tmp, loaded bytes
37    -r7=tmp, border
38    -v16=part of src
39    -v17=index of zero
40    -v18=part of src
41    -v31=register save area for r6, r7
43 ENTRY(__wcsncpy_vx)
44         .machine "z13"
45         .machinemode "zarch_nohighgprs"
47 # if !defined __s390x__
48         llgfr   %r4,%r4
49 # endif /* !defined __s390x__ */
51         clgfi   %r4,0
52         ber     %r14            /* Nothing to do, if n == 0.  */
54         vlbb    %v16,0(%r3),6   /* Load s until next 4k-byte boundary.  */
56         tmll    %r3,3           /* Test if s is 4-byte aligned?  */
57         jne     .Lfallback      /* And use common-code variant if not.  */
59         vlvgp   %v31,%r6,%r7    /* Save registers.  */
60         lgr     %r0,%r2         /* Save destination pointer for return.  */
62         lcbb    %r6,0(%r3),6    /* Get bytes to 4k-byte boundary or 16.  */
63         llgfr   %r6,%r6         /* Convert 32bit to 64bit.  */
65         lghi    %r5,0           /* current_len = 0.  */
67         /* Check range of maxlen and convert to byte-count.  */
68 # ifdef __s390x__
69         tmhh    %r4,49152       /* Test bit 0 or 1 of n.  */
70         lghi    %r1,-4          /* Max byte-count is 18446744073709551612.  */
71 # else
72         tmlh    %r4,49152       /* Test bit 0 or 1 of n.  */
73         llilf   %r1,4294967292  /* Max byte-count is 4294967292.  */
74 # endif /* !__s390x__ */
75         sllg    %r4,%r4,2       /* Convert character-count to byte-count.  */
76         locgrne %r4,%r1         /* Use max byte-count, if bit 0/1 was one.  */
78         clgrjle %r4,%r6,.Lremaining_v16 /* If n <= loaded-bytes
79                                            -> process remaining.  */
81         /* n > loaded-byte-count.  */
82         vfenezf %v17,%v16,%v16  /* Find element not equal with zero search.  */
83         vlgvb   %r1,%v17,7      /* Load zero index or 16 if not found.  */
84         aghi    %r1,3           /* Also copy remaining bytes of zero.  */
85         clrjl   %r1,%r6,.Lfound_v16_store /* Found zero within loaded bytes,
86                                              copy and return.  */
88         /* Align s to 16 byte.  */
89         risbgn  %r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
90         lghi    %r5,15          /* current_len = 15.  */
91         slr     %r5,%r7         /* Compute highest index to 16byte boundary.  */
93         /* Zero not found and n > loaded-byte-count.  */
94         vstl    %v16,%r5,0(%r2) /* Copy loaded characters - no zero.  */
95         ahi     %r5,1           /* Start loop at next character.  */
97         /* Now we are 16byte aligned, so we can load
98            a full vreg without page fault.  */
99         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
100         aghi    %r1,64
101         clgrjl  %r1,%r4,.Lloop64
103         vl      %v16,0(%r5,%r3) /* Load s.  */
104         clgijl  %r4,17,.Lremaining_v16  /* If n <=16, process remaining
105                                            bytes.  */
106 .Llt64:
107         lgr     %r7,%r4
108         slgfi   %r7,16          /* border_len = maxlen - 16.  */
110         clgrjhe %r5,%r7,.Lremaining_v16 /* If current_len >= border
111                                                then process remaining bytes.  */
112         vfenezfs %v17,%v16,%v16 /* Find element not equal with zero search.  */
113         je      .Lfound_v16     /* Jump away if zero was found.  */
114         vl      %v18,16(%r5,%r3) /* Load next part of s.  */
115         vst     %v16,0(%r5,%r2) /* Store previous part without zero to dst.  */
116         aghi    %r5,16
118         clgrjhe %r5,%r7,.Lremaining_v18
119         vfenezfs %v17,%v18,%v18
120         je      .Lfound_v18
121         vl      %v16,16(%r5,%r3)
122         vst     %v18,0(%r5,%r2)
123         aghi    %r5,16
125         clgrjhe %r5,%r7,.Lremaining_v16
126         vfenezfs %v17,%v16,%v16
127         je      .Lfound_v16
128         vl      %v18,16(%r5,%r3)
129         vst     %v16,0(%r5,%r2)
130         aghi    %r5,16
132 .Lremaining_v18:
133         vlr     %v16,%v18
134 .Lremaining_v16:
135         /* v16 contains the remaining bytes [1...16].
136            Store remaining bytes and append string-termination.  */
137         vfenezf %v17,%v16,%v16  /* Find element not equal with zero search.  */
138         slgrk   %r7,%r4,%r5     /* Remaining bytes = maxlen - current_len.  */
139         aghi    %r7,-1          /* vstl needs highest index.  */
140         la      %r2,0(%r5,%r2)  /* vstl has no index register.  */
141         vlgvb   %r1,%v17,7      /* Load zero index or 16 if not found.  */
142         aghi    %r1,3           /* Also copy remaining bytes of zero.  */
143         /* Zero in remaining bytes? -> jump away (zero-index < max-index)
144            Do not jump away if zero-index == max-index,
145            but simply copy zero with vstl below.  */
146         clrjl   %r1,%r7,.Lfound_v16_store
147         vstl    %v16,%r7,0(%r2) /* Store remaining bytes without null
148                                    termination!.  */
149 .Lend:
150         /* Restore saved registers.  */
151         vlgvg   %r6,%v31,0
152         vlgvg   %r7,%v31,1
153         lgr     %r2,%r0         /* Load saved dest-ptr.  */
154         br      %r14
156 .Lfound_v16_32:
157         aghi    %r5,32
158         j       .Lfound_v16
159 .Lfound_v18_48:
160         aghi    %r5,32
161 .Lfound_v18_16:
162         aghi    %r5,16
163 .Lfound_v18:
164         vlr     %v16,%v18
165 .Lfound_v16:
166         /* v16 contains a zero. Store remaining bytes to zero. current_len
167            has not reached border, thus checking for n is not needed! */
168         vlgvb   %r1,%v17,7      /* Load byte index of zero.  */
169         la      %r2,0(%r5,%r2)  /* vstl has no support for index-register.  */
170         aghi    %r1,3           /* Also copy remaining bytes of zero.  */
171 .Lfound_v16_store:
172         vstl    %v16,%r1,0(%r2) /* Copy characters including zero.  */
173         /* Fill remaining bytes with zero - remaining count always > 0.  */
174         algr    %r5,%r1         /* Remaining bytes (=%r4) = ...  */
175         slgr    %r4,%r5         /* = maxlen - (currlen + zero_index + 1).  */
176         la      %r2,0(%r1,%r2)  /* Pointer to zero. start filling beyond.  */
177         aghi    %r4,-2          /* mvc with exrl needs count - 1.
178                                    (additional -1, see remaining bytes above) */
179         srlg    %r6,%r4,8       /* Split into 256 byte blocks.  */
180         ltgr    %r6,%r6
181         je      .Lzero_lt256
182 .Lzero_loop256:
183         mvc     1(256,%r2),0(%r2) /* Fill 256 zeros at once.  */
184         la      %r2,256(%r2)
185         brctg   %r6,.Lzero_loop256 /* Loop until all blocks are processed.  */
186 .Lzero_lt256:
187         exrl    %r4,.Lmvc_lt256
188         j       .Lend
189 .Lmvc_lt256:
190         mvc     1(1,%r2),0(%r2)
192         /* Find zero in 16byte aligned loop.  */
193 .Lloop64:
194         vl      %v16,0(%r5,%r3) /* Load s.  */
195         vfenezfs %v17,%v16,%v16 /* Find element not equal with zero search.  */
196         je      .Lfound_v16     /* Jump away if zero was found.  */
197         vl      %v18,16(%r5,%r3) /* Load next part of s.  */
198         vst     %v16,0(%r5,%r2) /* Store previous part without zero to dst.  */
199         vfenezfs %v17,%v18,%v18
200         je      .Lfound_v18_16
201         vl      %v16,32(%r5,%r3)
202         vst     %v18,16(%r5,%r2)
203         vfenezfs %v17,%v16,%v16
204         je      .Lfound_v16_32
205         vl      %v18,48(%r5,%r3)
206         vst     %v16,32(%r5,%r2)
207         vfenezfs %v17,%v18,%v18
208         je      .Lfound_v18_48
209         vst     %v18,48(%r5,%r2)
211         aghi    %r5,64
212         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
213         aghi    %r1,64
214         clgrjl  %r1,%r4,.Lloop64
216         vl      %v16,0(%r5,%r3) /* Load s.  */
217         j       .Llt64
219 .Lfallback:
220         jg      __wcsncpy_c
221 END(__wcsncpy_vx)
223 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */