1/* Vector optimized 32/64 bit S/390 version of wcscat.
2   Copyright (C) 2015-2022 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
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.
9
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.
14
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/>.  */
18
19#include <ifunc-wcscat.h>
20#if HAVE_WCSCAT_Z13
21
22# include "sysdep.h"
23# include "asm-syntax.h"
24
25	.text
26
27/* wchar_t * wcscat (wchar_t *dest, const wchar_t *src)
28   Concatenate two strings.
29
30   Register usage:
31   -r0=saved dest pointer for return
32   -r1=tmp
33   -r2=dest
34   -r3=src
35   -r4=tmp
36   -r5=current_len
37   -v16=part of src
38   -v17=index of zero
39   -v18=part of src
40*/
41ENTRY(WCSCAT_Z13)
42	.machine "z13"
43	.machinemode "zarch_nohighgprs"
44
45	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
46	lcbb	%r1,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
47
48	/* __wcslen_c can handle non 4byte aligned pointers,
49	   but __wcscpy_c not. Thus if either src or dest is
50	   not 4byte aligned, use __wcscat_c.  */
51	tmll	%r2,3		/* Test if s is 4-byte aligned?   */
52	jne	.Lfallback	/* And use common-code variant if not.  */
53	tmll	%r3,3		/* Test if src is 4-byte aligned?   */
54	jne	.Lfallback	/* And use common-code variant if not.  */
55
56	lgr	%r0,%r2		/* Save destination pointer for return.  */
57
58	/* WCSLEN
59	   r1 = loaded bytes (tmp)
60	   r4 = zero byte index (tmp)
61	   r2 = dst
62	*/
63
64	vfenezf	%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.  */
67
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.  */
72
73	/* Find zero in 16byte aligned loop.  */
74.Llen_loop:
75	vl	%v16,0(%r5,%r2)	/* Load s.  */
76	vfenezfs %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	vfenezfs %v16,%v16,%v16
80	je	.Llen_found16
81	vl	%v16,32(%r5,%r2)
82	vfenezfs %v16,%v16,%v16
83	je	.Llen_found32
84	vl	%v16,48(%r5,%r2)
85	vfenezfs %v16,%v16,%v16
86	je	.Llen_found48
87
88	aghi	%r5,64
89	j	.Llen_loop	/* No zero -> loop.  */
90
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	%r4,%v16,7	/* Load byte index of zero.  */
99	algr	%r5,%r4
100
101.Llen_end:
102	/* WCSCPY
103	   %r1 = loaded bytes (tmp)
104	   %r4 = zero byte index (tmp)
105	   %r3 = curr src pointer
106	   %r2 = curr dst pointer
107	 */
108	la	%r2,0(%r5,%r2)	/* strcpy at end of dst-string.   */
109
110	vlbb	%v16,0(%r3),6	/* Load s until next 4k-byte boundary.  */
111	lcbb	%r1,0(%r3),6	/* Get bytes to 4k-byte boundary or 16.  */
112
113	vfenezf	%v17,%v16,%v16	/* Find element not equal with zero search.  */
114	vlgvb	%r5,%v17,7	/* Load zero index or 16 if not found.  */
115	clrjl	%r5,%r1,.Lcpy_found_align /* If found zero within loaded bytes,
116					     copy bytes before and return.  */
117
118	/* Align s to 16 byte.  */
119	risbgn	%r4,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
120	lghi	%r5,15		/* current_len = 15.  */
121	slr	%r5,%r4		/* Compute highest index to 16byte boundary.  */
122
123	vstl	%v16,%r5,0(%r2)	/* Copy loaded characters - no zero.  */
124	ahi	%r5,1		/* Start loop at next character.  */
125
126	/* Find zero in 16byte aligned loop.  */
127.Lcpy_loop:
128	vl	%v16,0(%r5,%r3)	/* Load s.  */
129	vfenezfs %v17,%v16,%v16	/* Find element not equal with zero search.  */
130	je	.Lcpy_found_v16_0 /* Jump away if zero was found.  */
131	vl	%v18,16(%r5,%r3) /* Load next part of s.  */
132	vst	%v16,0(%r5,%r2)	/* Save previous part without zero to dst.  */
133	vfenezfs %v17,%v18,%v18
134	je	.Lcpy_found_v18_16
135	vl	%v16,32(%r5,%r3)
136	vst	%v18,16(%r5,%r2)
137	vfenezfs %v17,%v16,%v16
138	je	.Lcpy_found_v16_32
139	vl	%v18,48(%r5,%r3)
140	vst	%v16,32(%r5,%r2)
141	vfenezfs %v17,%v18,%v18
142	je	.Lcpy_found_v18_48
143	vst	%v18,48(%r5,%r2)
144
145	aghi	%r5,64
146	j	.Lcpy_loop	/* No zero -> loop.  */
147
148.Lcpy_found_v16_32:
149	aghi	%r5,32
150.Lcpy_found_v16_0:
151	la	%r4,0(%r5,%r2)
152	vlgvb	%r1,%v17,7	/* Load byte index of zero.  */
153	aghi	%r1,3		/* Also copy remaining bytes of zero.  */
154	vstl	%v16,%r1,0(%r4)	/* Copy characters including zero.  */
155	lgr	%r2,%r0		/* Load saved dest-ptr.  */
156	br	%r14
157
158.Lcpy_found_v18_48:
159	aghi	%r5,32
160.Lcpy_found_v18_16:
161	la	%r4,16(%r5,%r2)
162	vlgvb	%r1,%v17,7	/* Load byte index of zero.  */
163	aghi	%r1,3		/* Also copy remaining bytes of zero.  */
164	vstl	%v18,%r1,0(%r4)	/* Copy characters including zero.  */
165	lgr	%r2,%r0		/* Load saved dest-ptr.  */
166	br	%r14
167
168.Lcpy_found_align:
169	aghi	%r5,3		/* Also copy remaining bytes of found zero.  */
170	vstl	%v16,%r5,0(%r2)	/* Copy characters including zero.  */
171	lgr	%r2,%r0		/* Load saved dest-ptr.  */
172	br	%r14
173.Lfallback:
174	jg	WCSCAT_C
175END(WCSCAT_Z13)
176
177# if ! HAVE_WCSCAT_IFUNC
178strong_alias (WCSCAT_Z13, __wcscat)
179weak_alias (__wcscat, wcscat)
180# endif
181#endif
182