1/* Vector optimized 32/64 bit S/390 version of strpbrk.
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-strpbrk.h>
20
21#if HAVE_STRPBRK_Z13
22
23# include "sysdep.h"
24# include "asm-syntax.h"
25
26 .text
27
28/* char *strpbrk (const char *s, const char * accept)
29 The strpbrk() function locates the first occurrence in the string s
30 of any of the characters in the string accept and returns a pointer
31 to that character or NULL if not found.
32
33 This method checks the length of accept string. If it fits entirely
34 in one vector register, a fast algorithm is used, which does not need
35 to check multiple parts of accept-string. Otherwise a slower full
36 check of accept-string is used.
37
38 register overview:
39 r3: pointer to start of accept-string
40 r2: pointer to start of search-string
41 r0: loaded byte count of vlbb search-string (32bit unsigned)
42 r4: found byte index (32bit unsigned)
43 r1: current return len (64bit unsigned)
44 v16: search-string
45 v17: accept-string
46 v18: temp-vreg
47
48 ONLY FOR SLOW:
49 v19: first accept-string
50 v20: zero for preparing acc-vector
51 v21: global mask; 1 indicates a match between
52 search-string-vreg and any accept-character
53 v22: current mask; 1 indicates a match between
54 search-string-vreg and any accept-character in current acc-vreg
55 v24: one for result-checking of former string-part
56 v30, v31: for re-/storing registers r6, r8, r9
57 r5: current len of accept-string
58 r6: zero-index in search-string or 16 if no zero
59 or min(zero-index, loaded byte count)
60 r8: >0, if former accept-string-part contains a zero,
61 otherwise =0;
62 r9: loaded byte count of vlbb accept-string
63*/
64ENTRY(STRPBRK_Z13)
65 .machine "z13"
66 .machinemode "zarch_nohighgprs"
67
68 /*
69 Check if accept-string fits in one vreg:
70 ----------------------------------------
71 */
72 vlbb %v17,0(%r3),6 /* Load accept. */
73 lghi %r1,0 /* Zero out current len. */
74 vlgvb %r0,%v17,0 /* Get first element. */
75 clije %r0,0,.Lfast_end_null /* Return null if accept is empty. */
76 lcbb %r0,0(%r3),6
77 jo .Lcheck_onbb /* Special case if accept lays
78 on block-boundary. */
79.Lcheck_notonbb:
80 vistrbs %v17,%v17 /* Fill with zeros after first zero. */
81 je .Lfast /* Zero found -> accept fits in one vreg. */
82 j .Lslow /* No zero -> accept exceeds one vreg */
83
84
85.Lcheck_onbb:
86 /* Accept lays on block-boundary. */
87 vfenezb %v18,%v17,%v17 /* Search zero in loaded accept bytes. */
88 vlgvb %r4,%v18,7 /* Get index of zero or 16 if not found. */
89 clrjl %r4,%r0,.Lcheck_notonbb /* Zero index < loaded bytes count ->
90 Accept fits in one vreg;
91 Fill with zeros and proceed
92 with FAST. */
93 vl %v17,0(%r3) /* Load accept, which exceeds loaded bytes. */
94 j .Lcheck_notonbb /* Check if accept fits in one vreg. */
95
96
97 /*
98 Search s for accept in one vreg
99 -------------------------------
100 */
101.Lfast:
102 /* Complete accept-string in v17 and remaining bytes are zero. */
103
104 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
105 lcbb %r0,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
106
107 vfaezbs %v18,%v16,%v17,0 /* Find first element in v16 unequal to any
108 in v17 or first zero element. */
109
110 vlgvb %r4,%v18,7 /* Load byte index of found element. */
111 /* If found index is within loaded bytes, return with found
112 element index (=equal count). */
113 clrjl %r4,%r0,.Lfast_loop_found2
114
115 /* Align s to 16 byte. */
116 risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
117 lghi %r1,16 /* current_len = 16. */
118 slr %r1,%r4 /* Compute bytes to 16bytes boundary. */
119
120 /* Process s in 16byte aligned loop. */
121.Lfast_loop:
122 vl %v16,0(%r1,%r2) /* Load search-string. */
123 vfaezbs %v18,%v16,%v17,0 /* Find first element in v16 equal to any
124 in v17 or first zero element. */
125 jno .Lfast_loop_found
126
127 vl %v16,16(%r1,%r2)
128 vfaezbs %v18,%v16,%v17,0
129 jno .Lfast_loop_found16
130
131 vl %v16,32(%r1,%r2)
132 vfaezbs %v18,%v16,%v17,0
133 jno .Lfast_loop_found32
134
135 vl %v16,48(%r1,%r2)
136 vfaezbs %v18,%v16,%v17,0
137 jno .Lfast_loop_found48
138
139 aghi %r1,64
140 j .Lfast_loop /* Loop if no element was unequal to accept
141 and not zero. */
142
143 /* Found equal or zero element. */
144.Lfast_loop_found48:
145 aghi %r1,16
146.Lfast_loop_found32:
147 aghi %r1,16
148.Lfast_loop_found16:
149 aghi %r1,16
150.Lfast_loop_found:
151 vlgvb %r4,%v18,7 /* Load byte index of found element. */
152.Lfast_loop_found2:
153 vlgvb %r0,%v16,0(%r4) /* Get found element. */
154 clije %r0,0,.Lfast_end_null /* Return null if no accept-char found */
155 algfr %r1,%r4 /* Add found index of char to current len. */
156 la %r2,0(%r1,%r2) /* And return pointer to first equal char. */
157 br %r14
158
159.Lfast_end_null:
160 lghi %r2,0 /* Return null if no character is equal. */
161 br %r14
162
163
164
165
166 /*
167 Search s for accept in multiple vregs
168 -------------------------------------
169 */
170.Lslow:
171 /* Save registers. */
172 vlvgg %v30,%r6,0
173 vlvgp %v31,%r8,%r9
174
175 /* accept in v17 without zero. */
176 vlr %v19,%v17 /* Save first acc-part for a fast reload. */
177 vzero %v20 /* Zero for preparing acc-vector. */
178 vone %v24 /* One for checking result of former string. */
179
180 /* Align s to 16 byte. */
181 risbg %r4,%r2,60,128+63,0 /* Test if s is aligned and
182 %r4 = bits 60-63 'and' 15. */
183 je .Lslow_loop_str /* If s is aligned, loop aligned. */
184 lghi %r0,15
185 slr %r0,%r4 /* Compute highest index to load (15-x). */
186 vll %v16,%r0,0(%r2) /* Load up to 16 byte boundary (vll needs
187 highest index, remaining bytes are 0). */
188 ahi %r0,1 /* Work with loaded byte count. */
189 vzero %v21 /* Zero out global mask. */
190 lghi %r5,0 /* Set current len of accept-string to zero. */
191 vfenezb %v18,%v16,%v16 /* Find zero in current string-part. */
192 lghi %r8,0 /* There is no zero in first accept-part. */
193 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
194 clije %r6,0,.Lslow_end_null /* If first element is zero
195 (end of string) -> return null */
196 clr %r0,%r6 /* cc==1 if loaded byte count < zero-index. */
197 locrl %r6,%r0 /* Load on cc==1; zero-index = lbc. */
198 j .Lslow_loop_acc
199
200
201 /* Process s in 16byte aligned loop. */
202.Lslow_next_str:
203 /* Check results of former processed str-part. */
204 vfeeb %v18,%v21,%v24 /* Find first equal match in global mask
205 (ones in element). */
206 vlgvb %r4,%v18,7 /* Get index of first one (=equal)
207 or 16 if no match. */
208 /* Equal-index < min(zero-index, loaded byte count)
209 -> return pointer to equal element. */
210 clrjl %r4,%r6,.Lslow_index_found
211 /* Zero-index < loaded byte count
212 -> former str-part was last str-part
213 -> return null */
214 clrjl %r6,%r0,.Lslow_end_null
215 /* All elements are zero (=no match) -> proceed with next str-part. */
216
217 vlr %v17,%v19 /* Load first part of accept (no zero). */
218 algfr %r1,%r0 /* Add loaded byte count to current len. */
219
220.Lslow_loop_str:
221 vl %v16,0(%r1,%r2) /* Load search-string */
222 lghi %r0,16 /* Loaded byte count is 16. */
223 vzero %v21 /* Zero out global mask. */
224 lghi %r5,0 /* Set current len of accept to zero. */
225 vfenezb %v18,%v16,%v16 /* Find zero in current string-part. */
226 lghi %r8,0 /* There is no zero in first accept-part. */
227 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
228 clije %r6,0,.Lslow_end_null /* If first element is zero
229 (end of string) -> return null. */
230
231.Lslow_loop_acc:
232 vfaeb %v22,%v16,%v17,4 /* Create matching-mask (1 in mask ->
233 Character matches any accepted character in
234 this accept-string-part) IN=0, RT=1. */
235 vlgvb %r4,%v22,0 /* Get result of first element. */
236 /* First element is equal to any accepted characters
237 (all other parts of accept cannot lead to a match before this one)
238 -> current len is pointing to first element
239 -> return found */
240 clijh %r4,0,.Lslow_end_found
241 vo %v21,%v21,%v22 /* Global-mask = global-|matching-mask. */
242 /* Proceed with next acc until end of acc is reached. */
243
244
245.Lslow_next_acc:
246 clijh %r8,0,.Lslow_next_str /* There was a zero in the last acc-part
247 -> add index to current_len and
248 end. */
249 vlbb %v17,16(%r5,%r3),6 /* Load next accept part. */
250 aghi %r5,16 /* Increment current len of accept-string. */
251 lcbb %r9,0(%r5,%r3),6 /* Get loaded byte count of accept-string. */
252 jo .Lslow_next_acc_onbb /* Jump away ifaccept-string is
253 on block-boundary. */
254.Lslow_next_acc_notonbb:
255 vistrbs %v17,%v17 /* Fill with zeros after first zero. */
256 jo .Lslow_loop_acc /* No zero found -> no preparation needed. */
257
258.Lslow_next_acc_prepare_zero:
259 /* Zero in accept-part: fill zeros with first-accept-character. */
260 vlgvb %r8,%v17,0 /* Load first element of acc-part. */
261 clije %r8,0,.Lslow_next_str /* Proceed with next string-part,
262 if first char in this part of accept
263 is a zero. */
264 /* r8>0 -> zero found in this acc-part. */
265 vrepb %v18,%v17,0 /* Replicate first char accross all chars. */
266 vceqb %v22,%v20,%v17 /* Create a mask (v22) of null chars
267 by comparing with 0 (v20). */
268 vsel %v17,%v18,%v17,%v22 /* Replace null chars with first char. */
269 j .Lslow_loop_acc /* Accept part is prepared -> process. */
270
271.Lslow_next_acc_onbb:
272 vfenezb %v18,%v17,%v17 /* Find zero in loaded bytes of accept part. */
273 vlgvb %r8,%v18,7 /* Load byte index of zero. */
274 clrjl %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes
275 -> Prepare vreg. */
276 vl %v17,0(%r5,%r3) /* Load over boundary ... */
277 lghi %r8,0 /* r8=0 -> no zero in this part of acc,
278 check for zero is in jump-target. */
279 j .Lslow_next_acc_notonbb /* ... and search for zero in
280 fully loaded vreg again. */
281
282.Lslow_end_null:
283 lghi %r1,0 /* Return null if no character is equal. */
284 j .Lslow_end
285
286.Lslow_loop_found:
287 vlgvb %r4,%v18,7 /* Load byte index of found element. */
288 vlgvb %r0,%v16,0(%r4) /* Get found element. */
289 clije %r0,0,.Lslow_end_null /* Return null if no acc-char found. */
290
291.Lslow_index_found:
292 algfr %r1,%r4 /* Add found index of char to current len. */
293.Lslow_end_found:
294 la %r1,0(%r1,%r2) /* And return pointer to first equal char. */
295
296.Lslow_end:
297 /* Restore registers. */
298 vlgvg %r6,%v30,0
299 vlgvg %r8,%v31,0
300 vlgvg %r9,%v31,1
301 lgr %r2,%r1
302 br %r14
303END(STRPBRK_Z13)
304
305# if ! HAVE_STRPBRK_IFUNC
306strong_alias (STRPBRK_Z13, strpbrk)
307# endif
308
309# if ! HAVE_STRPBRK_C && defined SHARED && IS_IN (libc)
310strong_alias (STRPBRK_Z13, __GI_strpbrk)
311# endif
312
313#endif /* HAVE_STRPBRK_Z13 */
314

source code of glibc/sysdeps/s390/strpbrk-vx.S