1/* Vector optimized 32/64 bit S/390 version of wmemchr.
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-wmemchr.h>
20#if HAVE_WMEMCHR_Z13
21
22# include "sysdep.h"
23# include "asm-syntax.h"
24
25	.text
26
27/* wchar_t *wmemchr (const wchar_t *s, wchar_t c, size_t n)
28   Scans memory for character c
29   and returns pointer to first c.
30
31   Register usage:
32   -r0=tmp
33   -r1=tmp
34   -r2=s
35   -r3=c
36   -r4=n
37   -r5=current_len
38   -v16=part of s
39   -v17=index of found c
40   -v18=c replicated
41*/
42ENTRY(WMEMCHR_Z13)
43	.machine "z13"
44	.machinemode "zarch_nohighgprs"
45
46# if !defined __s390x__
47	llgfr	%r4,%r4
48# endif /* !defined __s390x__ */
49
50	clgije	%r4,0,.Lnf_end	/* If len == 0 then exit.  */
51
52	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
53	lcbb	%r0,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
54	llgfr	%r0,%r0		/* Convert 32bit to 64bit.  */
55
56	tmll	%r2,3		/* Test if s is 4-byte aligned?  */
57	jne	.Lfallback	/* And use common-code variant if not.  */
58
59	vlvgf	%v18,%r3,0	/* Generate vector which elements are all c.  */
60	vrepf	%v18,%v18,0
61	lghi	%r5,16		/* current_len = 16.  */
62
63	/* Check range of maxlen and convert to byte-count.  */
64# ifdef __s390x__
65	tmhh	%r4,49152	/* Test bit 0 or 1 of maxlen.  */
66	lghi	%r1,-4		/* Max byte-count is 18446744073709551612.  */
67# else
68	tmlh	%r4,49152	/* Test bit 0 or 1 of maxlen.  */
69	llilf	%r1,4294967292	/* Max byte-count is 4294967292.  */
70# endif /* !__s390x__ */
71	sllg	%r4,%r4,2	/* Convert character-count to byte-count.  */
72	locgrne	%r4,%r1		/* Use max byte-count, if bit 0/1 was one.  */
73
74	clgrjhe	%r0,%r4,.Llastcmp /* If (bytes to boundary) >= n,
75				     jump to lastcmp.  */
76
77	vfeefs	%v17,%v16,%v18	/* Find c.  */
78	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
79	clgrjl	%r1,%r0,.Lfound2 /* Found c is within loaded bytes.  */
80
81	/* Align s to 16 byte.  */
82	risbgn	%r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
83	slr	%r5,%r1		/* Compute bytes to 16bytes boundary.  */
84
85	lgr	%r0,%r5		/* If %r5 + 64 < n? -> loop64.  */
86	aghi	%r0,64
87	clgrjl	%r0,%r4,.Lloop64
88.Llt64:
89	vl	%v16,0(%r5,%r2)
90	aghi	%r5,16
91	clgrjhe	%r5,%r4,.Llastcmp /* Do last compare if curr-len >= n.  */
92	vfeefs	%v17,%v16,%v18	/* Find c.  */
93	jl	.Lfound		/* Jump away if c was found.  */
94
95	vl	%v16,0(%r5,%r2)
96	aghi	%r5,16
97	clgrjhe	%r5,%r4,.Llastcmp
98	vfeefs	%v17,%v16,%v18
99	jl	.Lfound
100
101	vl	%v16,0(%r5,%r2)
102	aghi	%r5,16
103	clgrjhe	%r5,%r4,.Llastcmp
104	vfeefs	%v17,%v16,%v18
105	jl	.Lfound
106
107	vl	%v16,0(%r5,%r2)
108	aghi	%r5,16
109
110.Llastcmp:
111	/* Use comparision result only if located within first n characters.
112	   %r5: current_len;
113	   %r4: n;
114	   (current_len - n): [0...16[
115	   first ignored match index = vr-width - (current_len - n) ]0...16]
116	*/
117	vfeefs	%v17,%v16,%v18	/* Find c.  */
118	slgrk	%r4,%r5,%r4	/* %r5 = current_len - n.  */
119	lghi	%r0,16		/* Register width = 16.  */
120	vlgvb	%r1,%v17,7	/* Extract found index or 16 if all equal.  */
121	slr	%r0,%r4		/* %r0 = first ignored match index.  */
122	clrjl	%r1,%r0,.Lfound2 /* Go away if miscompare is below n bytes.  */
123	/* c not found within n-bytes.  */
124.Lnf_end:
125	lghi	%r2,0		/* Return null.  */
126	br	%r14
127
128.Lfound48:
129	aghi	%r5,16
130.Lfound32:
131	aghi	%r5,16
132.Lfound16:
133	aghi	%r5,16
134.Lfound0:
135	aghi	%r5,16
136.Lfound:
137	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
138.Lfound2:
139	slgfi	%r5,16		/* current_len -=16 */
140	algr	%r5,%r1		/* Zero byte index is added to current len.  */
141	la	%r2,0(%r5,%r2)	/* Return pointer to c.  */
142	br	%r14
143
144.Lloop64:
145	vl	%v16,0(%r5,%r2)
146	vfeefs	%v17,%v16,%v18	/* Find c.  */
147	jl	.Lfound0	/* Jump away if c was found.  */
148	vl	%v16,16(%r5,%r2)
149	vfeefs	%v17,%v16,%v18
150	jl	.Lfound16
151	vl	%v16,32(%r5,%r2)
152	vfeefs	%v17,%v16,%v18
153	jl	.Lfound32
154	vl	%v16,48(%r5,%r2)
155	vfeefs	%v17,%v16,%v18
156	jl	.Lfound48
157
158	aghi	%r5,64
159	lgr	%r0,%r5		/* If %r5 + 64 < n? -> loop64.  */
160	aghi	%r0,64
161	clgrjl	%r0,%r4,.Lloop64
162
163	j	.Llt64
164.Lfallback:
165	jg	WMEMCHR_C
166END(WMEMCHR_Z13)
167
168# if ! HAVE_WMEMCHR_IFUNC
169strong_alias (WMEMCHR_Z13, __wmemchr)
170weak_alias (__wmemchr, wmemchr)
171# endif
172
173# if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT \
174	&& defined SHARED && IS_IN (libc)
175strong_alias (WMEMCHR_Z13, __GI___wmemchr)
176weak_alias (WMEMCHR_Z13, __GI_wmemchr)
177# endif
178#endif
179