1 /* Vector optimized 32/64 bit S/390 version of strncat.
2 Copyright (C) 2015-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <ifunc-strncat.h>
23 # include "asm-syntax.h"
27 /* char * strncat (const char *dest, const char *src, size_t n)
28 Concatenate two strings - at most n characters of src.
31 -r0=saved dest pointer for return
42 -v31=register save area for r6, r7
46 .machinemode "zarch_nohighgprs"
48 # if !defined __s390x__
50 # endif /* !defined __s390x__ */
53 ber %r14 /* Nothing to do, if n == 0. */
54 lgr %r0,%r2 /* Save destination pointer for return. */
55 vlvgp %v31,%r6,%r7 /* Save registers. */
58 %r1 = loaded bytes (tmp)
59 %r6 = zero byte index (tmp)
62 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
63 lcbb %r1,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
65 vfenezb %v16,%v16,%v16 /* Find element not equal with zero search. */
66 vlgvb %r5,%v16,7 /* Load zero index or 16 if not found. */
67 clrjl %r5,%r1,.Llen_end /* Found zero within loaded bytes, end. */
69 /* Align s to 16 byte. */
70 risbgn %r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
71 lghi %r5,16 /* current_len = 16. */
72 slr %r5,%r1 /* Compute bytes to 16bytes boundary. */
74 /* Find zero in 16byte aligned loop. */
76 vl %v16,0(%r5,%r2) /* Load s. */
77 vfenezbs %v16,%v16,%v16 /* Find element not equal with zero search. */
78 je .Llen_found /* Jump away if zero was found. */
80 vfenezbs %v16,%v16,%v16
83 vfenezbs %v16,%v16,%v16
86 vfenezbs %v16,%v16,%v16
90 j .Llen_loop /* No zero -> loop. */
99 vlgvb %r1,%v16,7 /* Load byte index of zero. */
104 %r1 = zero byte index (tmp)
105 %r6 = loaded bytes (tmp)
106 %r3 = curr src pointer
107 %r2 = curr dst pointer
110 la %r2,0(%r5,%r2) /* strcpy at end of dst-string. */
112 vlbb %v16,0(%r3),6 /* Load s until next 4k-byte boundary. */
113 lcbb %r6,0(%r3),6 /* Get bytes to 4k-byte boundary or 16. */
114 llgfr %r6,%r6 /* Convert 32bit to 64bit. */
116 lghi %r5,0 /* current_len = 0. */
118 clgrjle %r4,%r6,.Lcpy_remaining_v16 /* If n <= loaded-bytes
119 -> process remaining. */
121 /* n > loaded-byte-count. */
122 vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */
123 vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
124 clrjl %r1,%r6,.Lcpy_found_v16_store /* Found zero within loaded
125 bytes, copy and return. */
127 /* Align s to 16 byte. */
128 risbgn %r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
129 lghi %r5,15 /* current_len = 15. */
130 slr %r5,%r7 /* Compute highest index to 16byte boundary. */
132 /* Zero not found and n > loaded-byte-count. */
133 vstl %v16,%r5,0(%r2) /* Copy loaded characters - no zero. */
134 ahi %r5,1 /* Start loop at next character. */
137 Now we are 16byte aligned, so we can load a full vreg
140 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
142 clgrjl %r1,%r4,.Lcpy_loop64
144 vl %v16,0(%r5,%r3) /* Load s. */
145 clgijl %r4,17,.Lcpy_remaining_v16 /* If n <=16,
146 process remaining bytes. */
149 slgfi %r7,16 /* border_len = n - 16. */
151 /* If current_len >= border then process remaining bytes. */
152 clgrjhe %r5,%r7,.Lcpy_remaining_v16
153 vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */
154 je .Lcpy_found_v16 /* Jump away if zero was found. */
155 vl %v18,16(%r5,%r3) /* Load next part of s. */
156 vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */
159 clgrjhe %r5,%r7,.Lcpy_remaining_v18
160 vfenezbs %v17,%v18,%v18
166 clgrjhe %r5,%r7,.Lcpy_remaining_v16
167 vfenezbs %v17,%v16,%v16
176 /* v16 contains the remaining bytes [1...16].
177 Store remaining bytes and append string-termination. */
178 vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */
179 slgrk %r7,%r4,%r5 /* Remaining bytes = maxlen - current_len. */
180 aghi %r7,-1 /* vstl needs highest index. */
181 vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
182 la %r2,0(%r5,%r2) /* vstl has no index register. */
183 /* Zero-index within remaining-bytes, store up to zero and end. */
184 clgrjle %r1,%r7,.Lcpy_found_v16_store
185 vstl %v16,%r7,0(%r2) /* Store remaining bytes. */
187 stc %r1,1(%r7,%r2) /* Store string-null-termination beyond n. */
189 /* Restore saved registers. */
192 lgr %r2,%r0 /* Load saved dest-ptr. */
205 /* v16 contains a zero. Store remaining bytes to zero. current_len
206 has not reached border, thus checking for n is not needed! */
207 vlgvb %r1,%v17,7 /* Load byte index of zero. */
209 .Lcpy_found_v16_store:
210 vstl %v16,%r1,0(%r2) /* Copy characters including zero. */
213 /* Find zero in 16byte aligned loop. */
215 vl %v16,0(%r5,%r3) /* Load s. */
216 vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */
217 je .Lcpy_found_v16 /* Jump away if zero was found. */
218 vl %v18,16(%r5,%r3) /* Load next part of s. */
219 vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */
220 vfenezbs %v17,%v18,%v18
221 je .Lcpy_found_v18_16
224 vfenezbs %v17,%v16,%v16
225 je .Lcpy_found_v16_32
228 vfenezbs %v17,%v18,%v18
229 je .Lcpy_found_v18_48
233 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
235 clgrjl %r1,%r4,.Lcpy_loop64
237 vl %v16,0(%r5,%r3) /* Load s. */
241 # if ! HAVE_STRNCAT_IFUNC
242 strong_alias (STRNCAT_Z13, strncat)
245 # if ! HAVE_STRNCAT_C
246 /* See string/strncat.c and define STRNCAT_PRIMARY. */
247 strong_alias (STRNCAT_Z13, __strncat)
248 # if defined SHARED && IS_IN (libc)
249 strong_alias (__strncat, __GI___strncat)