1/* Vector optimized 32/64 bit S/390 version of memchr.
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-memchr.h>
20#if HAVE_MEMCHR_Z13
21
22# include "sysdep.h"
23# include "asm-syntax.h"
24
25	.text
26
27/* void *memchr (const void *s, int 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(MEMCHR_Z13)
43
44	.machine "z13"
45	.machinemode "zarch_nohighgprs"
46
47# if !defined __s390x__
48	llgfr	%r4,%r4
49# endif /* !defined __s390x__ */
50
51	clgije	%r4,0,.Lnf_end	/* If len == 0 then exit.  */
52
53	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
54	lcbb	%r0,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
55	llgfr	%r0,%r0		/* Convert 32bit to 64bit.  */
56
57	vlvgb	%v18,%r3,0	/* Generate vector which elements are all c.
58				   if c > 255, c will be truncated.  */
59	vrepb	%v18,%v18,0
60	lghi	%r5,16		/* current_len = 16.  */
61
62	clgrjhe	%r0,%r4,.Llastcmp /* If (bytes to boundary) >= n,
63				     jump to lastcmp.  */
64
65	vfeebs	%v17,%v16,%v18	/* Find c.  */
66	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
67	clgrjl	%r1,%r0,.Lfound2 /* Found c is within loaded bytes.  */
68
69	/* Align s to 16 byte.  */
70	risbgn	%r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
71	slr	%r5,%r1		/* Compute bytes to 16bytes boundary.  */
72
73	lgr	%r0,%r5		/* If %r5 + 64 < n? -> loop64.  */
74	aghi	%r0,64
75	clgrjl	%r0,%r4,.Lloop64
76.Llt64:
77	vl	%v16,0(%r5,%r2)
78	aghi	%r5,16
79	clgrjhe	%r5,%r4,.Llastcmp /* Do last compare if curr-len >= n.  */
80	vfeebs	%v17,%v16,%v18	/* Find c.  */
81	jl	.Lfound	/* Jump away if c was found.  */
82
83	vl	%v16,0(%r5,%r2)
84	aghi	%r5,16
85	clgrjhe	%r5,%r4,.Llastcmp
86	vfeebs	%v17,%v16,%v18
87	jl	.Lfound
88
89	vl	%v16,0(%r5,%r2)
90	aghi	%r5,16
91	clgrjhe	%r5,%r4,.Llastcmp
92	vfeebs	%v17,%v16,%v18
93	jl	.Lfound
94
95	vl	%v16,0(%r5,%r2)
96	aghi	%r5,16
97
98.Llastcmp:
99	/* Use comparision result only if located within first n characters.
100	   %r5: current_len;
101	   %r4: n;
102	   (current_len - n): [0...16[
103	   first ignored match index: vr-width - (current_len - n) ]0...16]
104	*/
105	vfeebs	%v17,%v16,%v18	/* Find c.  */
106	slgrk	%r4,%r5,%r4	/* %r5 = current_len - n.  */
107	lghi	%r0,16		/* Register width = 16.  */
108	vlgvb	%r1,%v17,7	/* Extract found index or 16 if all equal.  */
109	slr	%r0,%r4		/* %r0 = first ignored match index.  */
110	clrjl	%r1,%r0,.Lfound2 /* Go away if miscompare is below n bytes.  */
111	/* c not found within n-bytes.  */
112.Lnf_end:
113	lghi	%r2,0		/* Return null.  */
114	br	%r14
115
116.Lfound48:
117	aghi	%r5,16
118.Lfound32:
119	aghi	%r5,16
120.Lfound16:
121	aghi	%r5,16
122.Lfound0:
123	aghi	%r5,16
124.Lfound:
125	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
126.Lfound2:
127	slgfi	%r5,16		/* current_len -=16 */
128	algr	%r5,%r1		/* Zero byte index is added to current len.  */
129	la	%r2,0(%r5,%r2)	/* Return pointer to c.  */
130	br	%r14
131
132
133.Lloop64:
134	vl	%v16,0(%r5,%r2)
135	vfeebs	%v17,%v16,%v18	/* Find c.  */
136	jl	.Lfound0	/* Jump away if c was found.  */
137	vl	%v16,16(%r5,%r2)
138	vfeebs	%v17,%v16,%v18
139	jl	.Lfound16
140	vl	%v16,32(%r5,%r2)
141	vfeebs	%v17,%v16,%v18
142	jl	.Lfound32
143	vl	%v16,48(%r5,%r2)
144	vfeebs	%v17,%v16,%v18
145	jl	.Lfound48
146
147	aghi	%r5,64
148	lgr	%r0,%r5		/* If %r5 + 64 < n? -> loop64.  */
149	aghi	%r0,64
150	clgrjl	%r0,%r4,.Lloop64
151
152	j	.Llt64
153END(MEMCHR_Z13)
154
155# if ! HAVE_MEMCHR_IFUNC
156strong_alias (MEMCHR_Z13, __memchr)
157weak_alias (__memchr, memchr)
158# endif
159
160# if ! HAVE_MEMCHR_Z900_G5 && defined SHARED && IS_IN (libc)
161strong_alias (MEMCHR_Z13, __GI_memchr)
162# endif
163#endif
164