1 /* Vector optimized 32/64 bit S/390 version of wcsncat.
2 Copyright (C) 2015-2016 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)
22 # include "asm-syntax.h"
26 /* wchar_t * wcsncat (wchar_t *dest, const wchar_t *src, size_t n)
27 Concatenate two strings - at most n characters of src.
30 -r0=saved dest pointer for return
41 -v31=register save area for r6, r7
45 .machinemode "zarch_nohighgprs"
47 # if !defined __s390x__
49 # endif /* !defined __s390x__ */
52 ber %r14 /* Nothing to do, if n == 0. */
54 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
55 lcbb %r1,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
57 /* If either src or dest is not 4byte aligned, use __wcsncat_c. */
58 tmll %r2,3 /* Test if s is 4-byte aligned? */
59 jne .Lfallback /* And use common-code variant if not. */
60 tmll %r3,3 /* Test if src is 4-byte aligned? */
61 jne .Lfallback /* And use common-code variant if not. */
63 lgr %r0,%r2 /* Save destination pointer for return. */
64 vlvgp %v31,%r6,%r7 /* Save registers. */
67 %r1 = loaded bytes (tmp)
68 %r6 = zero byte index (tmp)
71 vfenezf %v16,%v16,%v16 /* Find element not equal with zero search. */
72 vlgvb %r5,%v16,7 /* Load zero index or 16 if not found. */
73 clrjl %r5,%r1,.Llen_end /* Found zero within loaded bytes, end. */
75 /* Align s to 16 byte. */
76 risbgn %r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
77 lghi %r5,16 /* current_len = 16. */
78 slr %r5,%r1 /* Compute bytes to 16bytes boundary. */
80 /* Find zero in 16byte aligned loop. */
82 vl %v16,0(%r5,%r2) /* Load s. */
83 vfenezfs %v16,%v16,%v16 /* Find element not equal with zero search. */
84 je .Llen_found /* Jump away if zero was found. */
86 vfenezfs %v16,%v16,%v16
89 vfenezfs %v16,%v16,%v16
92 vfenezfs %v16,%v16,%v16
96 j .Llen_loop /* No zero -> loop. */
105 vlgvb %r1,%v16,7 /* Load byte index of zero. */
110 %r1 = zero byte index (tmp)
111 %r6 = loaded bytes (tmp)
112 %r3 = curr src pointer
113 %r2 = curr dst pointer
116 la %r2,0(%r5,%r2) /* strcpy at end of dst-string. */
118 vlbb %v16,0(%r3),6 /* Load s until next 4k-byte boundary. */
119 lcbb %r6,0(%r3),6 /* Get bytes to 4k-byte boundary or 16. */
120 llgfr %r6,%r6 /* Convert 32bit to 64bit. */
122 lghi %r5,0 /* current_len = 0. */
124 /* Check range of maxlen and convert to byte-count. */
126 tmhh %r4,49152 /* Test bit 0 or 1 of maxlen. */
127 lghi %r1,-4 /* Max byte-count is 18446744073709551612. */
129 tmlh %r4,49152 /* Test bit 0 or 1 of maxlen. */
130 llilf %r1,4294967292 /* Max byte-count is 4294967292. */
131 # endif /* !__s390x__ */
132 sllg %r4,%r4,2 /* Convert character-count to byte-count. */
133 locgrne %r4,%r1 /* Use max byte-count, if bit 0/1 was one. */
135 clgrjle %r4,%r6,.Lcpy_remaining_v16 /* If n <= loaded-bytes
136 -> process remaining. */
138 /* n > loaded-byte-count. */
139 vfenezf %v17,%v16,%v16 /* Find element not equal with zero search. */
140 vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
141 clrjl %r1,%r6,.Lcpy_found_v16_store /* Found zero within loaded bytes,
144 /* Align s to 16 byte. */
145 risbgn %r1,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
146 lghi %r5,15 /* current_len = 15. */
147 slr %r5,%r1 /* Compute highest index to 16byte boundary. *
149 /* Zero not found and maxlen > loaded-byte-count. */
150 vstl %v16,%r5,0(%r2) /* Copy loaded characters - no zero. */
151 ahi %r5,1 /* Start loop at next character. */
154 Now we are 16byte aligned, so we can load a full vreg
157 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
159 clgrjl %r1,%r4,.Lcpy_loop64
161 vl %v16,0(%r5,%r3) /* Load s. */
162 clgijl %r4,17,.Lcpy_remaining_v16 /* If n <=16,
163 process remaining bytes. */
166 slgfi %r7,16 /* border_len = n - 16. */
168 clgrjhe %r5,%r7,.Lcpy_remaining_v16
169 vfenezfs %v17,%v16,%v16 /* Find element not equal with zero search. */
170 je .Lcpy_found_v16 /* Jump away if zero was found. */
171 vl %v18,16(%r5,%r3) /* Load next part of s. */
172 vst %v16,0(%r5,%r2) /* Save previous part without zero to dst. */
175 clgrjhe %r5,%r7,.Lcpy_remaining_v18
176 vfenezfs %v17,%v18,%v18
182 clgrjhe %r5,%r7,.Lcpy_remaining_v16
183 vfenezfs %v17,%v16,%v16
192 /* v16 contains the remaining bytes [1...16].
193 Store remaining bytes and append string-termination. */
194 vfenezf %v17,%v16,%v16 /* Find element not equal with zero search. */
195 slgrk %r7,%r4,%r5 /* Remaining bytes = maxlen - current_len. */
196 aghi %r7,-1 /* vstl needs highest index. */
197 vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
198 la %r2,0(%r5,%r2) /* vstl has no index register. */
199 /* Zero-index within remaining-bytes, store up to zero and end. */
200 clgrjle %r1,%r7,.Lcpy_found_v16_store
201 vstl %v16,%r7,0(%r2) /* Store remaining bytes. */
203 st %r1,1(%r7,%r2) /* Store string-null-termination beyond n. */
205 /* Restore saved registers. */
208 lgr %r2,%r0 /* Load saved dest-ptr. */
221 /* v16 contains a zero. Store remaining bytes to zero. current_len
222 has not reached border, thus checking for n is not needed! */
223 vlgvb %r1,%v17,7 /* Load byte index of zero. */
225 .Lcpy_found_v16_store:
226 aghi %r1,3 /* Also copy remaining bytes of zero. */
227 vstl %v16,%r1,0(%r2) /* Copy characters including zero. */
230 /* Find zero in 16byte aligned loop. */
238 vfenezfs %v17,%v16,%v16 /* Find element not equal with zero search. */
239 je .Lcpy_found_v16 /* Jump away if zero was found. */
240 vl %v18,16(%r5,%r3) /* Load next part of s. */
241 vst %v16,0(%r5,%r2) /* Save previous part without zero to dst. */
242 vfenezfs %v17,%v18,%v18
243 je .Lcpy_found_v18_16
246 vfenezfs %v17,%v16,%v16
247 je .Lcpy_found_v16_32
250 vfenezfs %v17,%v18,%v18
251 je .Lcpy_found_v18_48
255 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
257 clgrjl %r1,%r4,.Lcpy_loop64
259 vl %v16,0(%r5,%r3) /* Load s. */
265 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */