1/* Vector optimized 32/64 bit S/390 version of strnlen.
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-strnlen.h>
20
21#if HAVE_STRNLEN_Z13
22
23# include "sysdep.h"
24# include "asm-syntax.h"
25
26	.text
27
28/* size_t strnlen (const char *s, size_t maxlen)
29   Returns the number of characters in s or at most maxlen.
30
31   Register usage:
32   -r1=tmp
33   -r2=address of string
34   -r3=maxlen (number of characters to be read)
35   -r4=tmp
36   -r5=current_len and return_value
37   -v16=part of s
38*/
39ENTRY(STRNLEN_Z13)
40	.machine "z13"
41	.machinemode "zarch_nohighgprs"
42
43# if !defined __s390x__
44	llgfr	%r3,%r3
45# endif /* !defined __s390x__ */
46
47	clgfi	%r3,0		/* if maxlen == 0, return 0.  */
48	locgre	%r2,%r3
49	ber	%r14
50
51	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
52	lcbb	%r1,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
53	llgfr	%r1,%r1		/* Convert 32bit to 64bit.  */
54
55	vfenezb	%v16,%v16,%v16	/* Find element not equal with zero search.  */
56	clgr	%r1,%r3
57	locgrh	%r1,%r3		/* loaded_byte_count
58				   = min (loaded_byte_count, maxlen)  */
59
60	vlgvb	%r5,%v16,7	/* Load zero index or 16 if not found.  */
61	clr	%r5,%r1		/* If found zero within loaded bytes?  */
62	locgrl	%r2,%r5		/* Then copy return value.  */
63	blr	%r14		/* And return.  */
64
65	clgr	%r1,%r3		/* If loaded_byte_count == maxlen?  */
66	locgre	%r2,%r3		/* Then copy return value.  */
67	ber	%r14		/* And return.  */
68
69	/* Align s to 16 byte.  */
70	risbgn	%r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
71	lghi	%r5,16		/* current_len = 16.  */
72	slr	%r5,%r4		/* Compute bytes to 16bytes boundary.  */
73
74	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
75	aghi	%r1,64
76	clgrjl	%r1,%r3,.Lloop64
77
78	/* Find zero in max 64byte with aligned s.  */
79.Llt64:
80	vl	%v16,0(%r5,%r2) /* Load s.  */
81	vfenezbs %v16,%v16,%v16	/* Find element not equal with zero search.  */
82	je	.Lfound		/* Jump away if zero was found.  */
83	aghi	%r5,16
84	clgrjhe	%r5,%r3,.Lfound /* current_len >= maxlen -> end.  */
85	vl	%v16,0(%r5,%r2)
86	vfenezbs %v16,%v16,%v16
87	je	.Lfound
88	aghi	%r5,16
89	clgrjhe	%r5,%r3,.Lfound
90	vl	%v16,0(%r5,%r2)
91	vfenezbs %v16,%v16,%v16
92	je	.Lfound
93	aghi	%r5,16
94	clgrjhe	%r5,%r3,.Lfound
95	vl	%v16,0(%r5,%r2)
96	vfenezbs %v16,%v16,%v16
97	j	.Lfound
98
99.Lfound48:
100	aghi	%r5,16
101.Lfound32:
102	aghi	%r5,16
103.Lfound16:
104	aghi	%r5,16
105.Lfound:
106	vlgvb	%r4,%v16,7	/* Load byte index of zero or 16 if no zero.  */
107	algr	%r5,%r4
108
109	clgr	%r5,%r3
110	locgrh	%r5,%r3		/* Return min (current_len, maxlen).  */
111	lgr	%r2,%r5
112	br	%r14
113
114	/* Find zero in 16 byte aligned loop.  */
115.Lloop64:
116	vl	%v16,0(%r5,%r2) /* Load s.  */
117	vfenezbs %v16,%v16,%v16	/* Find element not equal with zero search.  */
118	je	.Lfound		/* Jump away if zero was found.  */
119	vl	%v16,16(%r5,%r2)
120	vfenezbs %v16,%v16,%v16
121	je	.Lfound16
122	vl	%v16,32(%r5,%r2)
123	vfenezbs %v16,%v16,%v16
124	je	.Lfound32
125	vl	%v16,48(%r5,%r2)
126	vfenezbs %v16,%v16,%v16
127	je	.Lfound48
128
129	aghi	%r5,64
130	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
131	aghi	%r1,64
132	clgrjl	%r1,%r3,.Lloop64
133
134	j	.Llt64
135END(STRNLEN_Z13)
136
137# if ! HAVE_STRNLEN_IFUNC
138strong_alias (STRNLEN_Z13, __strnlen)
139weak_alias (__strnlen, strnlen)
140# endif
141
142# if ! HAVE_STRNLEN_C && defined SHARED && IS_IN (libc)
143strong_alias (STRNLEN_Z13, __GI_strnlen)
144strong_alias (STRNLEN_Z13, __GI___strnlen)
145# endif
146
147#endif /* HAVE_STRNLEN_Z13  */
148