S390: Optimize strchrnul and wcschrnul.
[glibc.git] / sysdeps / s390 / multiarch / strncat-vx.S
blob4435d9f8d2cfed4b8d5f27eb4c4cd62e72b599c4
1 /* Vector optimized 32/64 bit S/390 version of strncat.
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 /* char * strncat (const char *dest, const char *src, size_t n)
27    Concatenate two strings - at most n characters of src.
29    Register usage:
30    -r0=saved dest pointer for return
31    -r1=tmp
32    -r2=dest
33    -r3=src
34    -r4=n
35    -r5=current_len
36    -r6=tmp
37    -r7=tmp
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(__strncat_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.  */
53         lgr     %r0,%r2         /* Save destination pointer for return.  */
54         vlvgp   %v31,%r6,%r7    /* Save registers.  */
56         /* STRLEN
57            %r1 = loaded bytes (tmp)
58            %r6 = zero byte index (tmp)
59            %r2 = dst
60         */
61         vlbb    %v16,0(%r2),6   /* Load s until next 4k-byte boundary.  */
62         lcbb    %r1,0(%r2),6    /* Get bytes to 4k-byte boundary or 16.  */
64         vfenezb %v16,%v16,%v16  /* Find element not equal with zero search.  */
65         vlgvb   %r5,%v16,7      /* Load zero index or 16 if not found.  */
66         clrjl   %r5,%r1,.Llen_end /* Found zero within loaded bytes, end.  */
68         /* Align s to 16 byte.  */
69         risbgn  %r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
70         lghi    %r5,16          /* current_len = 16.  */
71         slr     %r5,%r1         /* Compute bytes to 16bytes boundary.  */
73         /* Find zero in 16byte aligned loop.  */
74 .Llen_loop:
75         vl      %v16,0(%r5,%r2) /* Load s.  */
76         vfenezbs %v16,%v16,%v16 /* Find element not equal with zero search.  */
77         je      .Llen_found     /* Jump away if zero was found.  */
78         vl      %v16,16(%r5,%r2)
79         vfenezbs %v16,%v16,%v16
80         je      .Llen_found16
81         vl      %v16,32(%r5,%r2)
82         vfenezbs %v16,%v16,%v16
83         je      .Llen_found32
84         vl      %v16,48(%r5,%r2)
85         vfenezbs %v16,%v16,%v16
86         je      .Llen_found48
88         aghi    %r5,64
89         j       .Llen_loop      /* No zero -> loop.  */
91 .Llen_found48:
92         aghi    %r5,16
93 .Llen_found32:
94         aghi    %r5,16
95 .Llen_found16:
96         aghi    %r5,16
97 .Llen_found:
98         vlgvb   %r1,%v16,7      /* Load byte index of zero.  */
99         algr    %r5,%r1
101 .Llen_end:
102         /* STRCPY
103            %r1 = zero byte index (tmp)
104            %r6 = loaded bytes (tmp)
105            %r3 = curr src pointer
106            %r2 = curr dst pointer
107            %r7 = border, tmp
108         */
109         la      %r2,0(%r5,%r2)  /* strcpy at end of dst-string.  */
111         vlbb    %v16,0(%r3),6   /* Load s until next 4k-byte boundary.  */
112         lcbb    %r6,0(%r3),6    /* Get bytes to 4k-byte boundary or 16.  */
113         llgfr   %r6,%r6         /* Convert 32bit to 64bit.  */
115         lghi    %r5,0           /* current_len = 0.  */
117         clgrjle %r4,%r6,.Lcpy_remaining_v16 /* If n <= loaded-bytes
118                                                -> process remaining.  */
120         /* n > loaded-byte-count.  */
121         vfenezb %v17,%v16,%v16  /* Find element not equal with zero search.  */
122         vlgvb   %r1,%v17,7      /* Load zero index or 16 if not found.  */
123         clrjl   %r1,%r6,.Lcpy_found_v16_store /* Found zero within loaded
124                                                  bytes, copy and return.  */
126         /* Align s to 16 byte.  */
127         risbgn  %r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
128         lghi    %r5,15          /* current_len = 15.  */
129         slr     %r5,%r7         /* Compute highest index to 16byte boundary.  */
131         /* Zero not found and n > loaded-byte-count.  */
132         vstl    %v16,%r5,0(%r2) /* Copy loaded characters - no zero.  */
133         ahi     %r5,1           /* Start loop at next character.  */
135         /*
136           Now we are 16byte aligned, so we can load a full vreg
137           without page fault.
138          */
139         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
140         aghi    %r1,64
141         clgrjl  %r1,%r4,.Lcpy_loop64
143         vl      %v16,0(%r5,%r3) /* Load s.  */
144         clgijl  %r4,17,.Lcpy_remaining_v16 /* If n <=16,
145                                                process remaining bytes.  */
146 .Lcpy_lt64:
147         lgr     %r7,%r4
148         slgfi   %r7,16          /* border_len = n - 16.  */
150         /* If current_len >= border then process remaining bytes.  */
151         clgrjhe %r5,%r7,.Lcpy_remaining_v16
152         vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search.  */
153         je      .Lcpy_found_v16 /* Jump away if zero was found.  */
154         vl      %v18,16(%r5,%r3) /* Load next part of s.  */
155         vst     %v16,0(%r5,%r2) /* Store previous part without zero to dst.  */
156         aghi    %r5,16
158         clgrjhe %r5,%r7,.Lcpy_remaining_v18
159         vfenezbs %v17,%v18,%v18
160         je      .Lcpy_found_v18
161         vl      %v16,16(%r5,%r3)
162         vst     %v18,0(%r5,%r2)
163         aghi    %r5,16
165         clgrjhe %r5,%r7,.Lcpy_remaining_v16
166         vfenezbs %v17,%v16,%v16
167         je      .Lcpy_found_v16
168         vl      %v18,16(%r5,%r3)
169         vst     %v16,0(%r5,%r2)
170         aghi    %r5,16
172 .Lcpy_remaining_v18:
173         vlr     %v16,%v18
174 .Lcpy_remaining_v16:
175         /* v16 contains the remaining bytes [1...16].
176            Store remaining bytes and append string-termination.  */
177         vfenezb %v17,%v16,%v16  /* Find element not equal with zero search.  */
178         slgrk   %r7,%r4,%r5     /* Remaining bytes = maxlen - current_len.  */
179         aghi    %r7,-1          /* vstl needs highest index.  */
180         vlgvb   %r1,%v17,7      /* Load zero index or 16 if not found.  */
181         la      %r2,0(%r5,%r2)  /* vstl has no index register.  */
182         /* Zero-index within remaining-bytes, store up to zero and end.  */
183         clgrjle %r1,%r7,.Lcpy_found_v16_store
184         vstl    %v16,%r7,0(%r2) /* Store remaining bytes.  */
185         lghi    %r1,0
186         stc     %r1,1(%r7,%r2)  /* Store string-null-termination beyond n.  */
187 .Lcpy_end:
188         /* Restore saved registers.  */
189         vlgvg   %r6,%v31,0
190         vlgvg   %r7,%v31,1
191         lgr     %r2,%r0         /* Load saved dest-ptr.  */
192         br      %r14
194 .Lcpy_found_v16_32:
195         aghi    %r5,32
196         j       .Lcpy_found_v16
197 .Lcpy_found_v18_48:
198         aghi    %r5,32
199 .Lcpy_found_v18_16:
200         aghi    %r5,16
201 .Lcpy_found_v18:
202         vlr     %v16,%v18
203 .Lcpy_found_v16:
204         /* v16 contains a zero. Store remaining bytes to zero. current_len
205            has not reached border, thus checking for n is not needed!  */
206         vlgvb   %r1,%v17,7      /* Load byte index of zero.  */
207         la      %r2,0(%r5,%r2)
208 .Lcpy_found_v16_store:
209         vstl    %v16,%r1,0(%r2) /* Copy characters including zero.  */
210         j       .Lcpy_end
212         /* Find zero in 16byte aligned loop.  */
213 .Lcpy_loop64:
214         vl      %v16,0(%r5,%r3) /* Load s.  */
215         vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search.  */
216         je      .Lcpy_found_v16 /* Jump away if zero was found.  */
217         vl      %v18,16(%r5,%r3) /* Load next part of s.  */
218         vst     %v16,0(%r5,%r2) /* Store previous part without zero to dst.  */
219         vfenezbs %v17,%v18,%v18
220         je      .Lcpy_found_v18_16
221         vl      %v16,32(%r5,%r3)
222         vst     %v18,16(%r5,%r2)
223         vfenezbs %v17,%v16,%v16
224         je      .Lcpy_found_v16_32
225         vl      %v18,48(%r5,%r3)
226         vst     %v16,32(%r5,%r2)
227         vfenezbs %v17,%v18,%v18
228         je      .Lcpy_found_v18_48
229         vst     %v18,48(%r5,%r2)
231         aghi    %r5,64
232         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
233         aghi    %r1,64
234         clgrjl  %r1,%r4,.Lcpy_loop64
236         vl      %v16,0(%r5,%r3) /* Load s.  */
237         j       .Lcpy_lt64
238 END(__strncat_vx)
239 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */