1 /* Vector optimized 32/64 bit S/390 version of strpbrk.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #if defined HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc)
22 # include "asm-syntax.h"
26 /* char *strpbrk (const char *s, const char * accept)
27 The strpbrk() function locates the first occurrence in the string s
28 of any of the characters in the string accept and returns a pointer
29 to that character or NULL if not found.
31 This method checks the length of accept string. If it fits entirely
32 in one vector register, a fast algorithm is used, which does not need
33 to check multiple parts of accept-string. Otherwise a slower full
34 check of accept-string is used.
37 r3: pointer to start of accept-string
38 r2: pointer to start of search-string
39 r0: loaded byte count of vlbb search-string (32bit unsigned)
40 r4: found byte index (32bit unsigned)
41 r1: current return len (64bit unsigned)
47 v19: first accept-string
48 v20: zero for preparing acc-vector
49 v21: global mask; 1 indicates a match between
50 search-string-vreg and any accept-character
51 v22: current mask; 1 indicates a match between
52 search-string-vreg and any accept-character in current acc-vreg
53 v24: one for result-checking of former string-part
54 v30, v31: for re-/storing registers r6, r8, r9
55 r5: current len of accept-string
56 r6: zero-index in search-string or 16 if no zero
57 or min(zero-index, loaded byte count)
58 r8: >0, if former accept-string-part contains a zero,
60 r9: loaded byte count of vlbb accept-string
64 .machinemode "zarch_nohighgprs"
67 Check if accept-string fits in one vreg:
68 ----------------------------------------
70 vlbb %v17,0(%r3),6 /* Load accept. */
71 lghi %r1,0 /* Zero out current len. */
72 vlgvb %r0,%v17,0 /* Get first element. */
73 clije %r0,0,.Lfast_end_null /* Return null if accept is empty. */
75 jo .Lcheck_onbb /* Special case if accept lays
78 vistrbs %v17,%v17 /* Fill with zeros after first zero. */
79 je .Lfast /* Zero found -> accept fits in one vreg. */
80 j .Lslow /* No zero -> accept exceeds one vreg */
84 /* Accept lays on block-boundary. */
85 vfenezb %v18,%v17,%v17 /* Search zero in loaded accept bytes. */
86 vlgvb %r4,%v18,7 /* Get index of zero or 16 if not found. */
87 clrjl %r4,%r0,.Lcheck_notonbb /* Zero index < loaded bytes count ->
88 Accept fits in one vreg;
89 Fill with zeros and proceed
91 vl %v17,0(%r3) /* Load accept, which exceeds loaded bytes. */
92 j .Lcheck_notonbb /* Check if accept fits in one vreg. */
96 Search s for accept in one vreg
97 -------------------------------
100 /* Complete accept-string in v17 and remaining bytes are zero. */
102 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
103 lcbb %r0,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
105 vfaezbs %v18,%v16,%v17,0 /* Find first element in v16 unequal to any
106 in v17 or first zero element. */
108 vlgvb %r4,%v18,7 /* Load byte index of found element. */
109 /* If found index is within loaded bytes, return with found
110 element index (=equal count). */
111 clrjl %r4,%r0,.Lfast_loop_found2
113 /* Align s to 16 byte. */
114 risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
115 lghi %r1,16 /* current_len = 16. */
116 slr %r1,%r4 /* Compute bytes to 16bytes boundary. */
118 /* Process s in 16byte aligned loop. */
120 vl %v16,0(%r1,%r2) /* Load search-string. */
121 vfaezbs %v18,%v16,%v17,0 /* Find first element in v16 equal to any
122 in v17 or first zero element. */
123 jno .Lfast_loop_found
126 vfaezbs %v18,%v16,%v17,0
127 jno .Lfast_loop_found16
130 vfaezbs %v18,%v16,%v17,0
131 jno .Lfast_loop_found32
134 vfaezbs %v18,%v16,%v17,0
135 jno .Lfast_loop_found48
138 j .Lfast_loop /* Loop if no element was unequal to accept
141 /* Found equal or zero element. */
149 vlgvb %r4,%v18,7 /* Load byte index of found element. */
151 vlgvb %r0,%v16,0(%r4) /* Get found element. */
152 clije %r0,0,.Lfast_end_null /* Return null if no accept-char found */
153 algfr %r1,%r4 /* Add found index of char to current len. */
154 la %r2,0(%r1,%r2) /* And return pointer to first equal char. */
158 lghi %r2,0 /* Return null if no character is equal. */
165 Search s for accept in multiple vregs
166 -------------------------------------
169 /* Save registers. */
173 /* accept in v17 without zero. */
174 vlr %v19,%v17 /* Save first acc-part for a fast reload. */
175 vzero %v20 /* Zero for preparing acc-vector. */
176 vone %v24 /* One for checking result of former string. */
178 /* Align s to 16 byte. */
179 risbg %r4,%r2,60,128+63,0 /* Test if s is aligned and
180 %r4 = bits 60-63 'and' 15. */
181 je .Lslow_loop_str /* If s is aligned, loop aligned. */
183 slr %r0,%r4 /* Compute highest index to load (15-x). */
184 vll %v16,%r0,0(%r2) /* Load up to 16 byte boundary (vll needs
185 highest index, remaining bytes are 0). */
186 ahi %r0,1 /* Work with loaded byte count. */
187 vzero %v21 /* Zero out global mask. */
188 lghi %r5,0 /* Set current len of accept-string to zero. */
189 vfenezb %v18,%v16,%v16 /* Find zero in current string-part. */
190 lghi %r8,0 /* There is no zero in first accept-part. */
191 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
192 clije %r6,0,.Lslow_end_null /* If first element is zero
193 (end of string) -> return null */
194 clr %r0,%r6 /* cc==1 if loaded byte count < zero-index. */
195 locrl %r6,%r0 /* Load on cc==1; zero-index = lbc. */
199 /* Process s in 16byte aligned loop. */
201 /* Check results of former processed str-part. */
202 vfeeb %v18,%v21,%v24 /* Find first equal match in global mask
203 (ones in element). */
204 vlgvb %r4,%v18,7 /* Get index of first one (=equal)
205 or 16 if no match. */
206 /* Equal-index < min(zero-index, loaded byte count)
207 -> return pointer to equal element. */
208 clrjl %r4,%r6,.Lslow_index_found
209 /* Zero-index < loaded byte count
210 -> former str-part was last str-part
212 clrjl %r6,%r0,.Lslow_end_null
213 /* All elements are zero (=no match) -> proceed with next str-part. */
215 vlr %v17,%v19 /* Load first part of accept (no zero). */
216 algfr %r1,%r0 /* Add loaded byte count to current len. */
219 vl %v16,0(%r1,%r2) /* Load search-string */
220 lghi %r0,16 /* Loaded byte count is 16. */
221 vzero %v21 /* Zero out global mask. */
222 lghi %r5,0 /* Set current len of accept to zero. */
223 vfenezb %v18,%v16,%v16 /* Find zero in current string-part. */
224 lghi %r8,0 /* There is no zero in first accept-part. */
225 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
226 clije %r6,0,.Lslow_end_null /* If first element is zero
227 (end of string) -> return null. */
230 vfaeb %v22,%v16,%v17,4 /* Create matching-mask (1 in mask ->
231 Character matches any accepted character in
232 this accept-string-part) IN=0, RT=1. */
233 vlgvb %r4,%v22,0 /* Get result of first element. */
234 /* First element is equal to any accepted characters
235 (all other parts of accept cannot lead to a match before this one)
236 -> current len is pointing to first element
238 clijh %r4,0,.Lslow_end_found
239 vo %v21,%v21,%v22 /* Global-mask = global-|matching-mask. */
240 /* Proceed with next acc until end of acc is reached. */
244 clijh %r8,0,.Lslow_next_str /* There was a zero in the last acc-part
245 -> add index to current_len and
247 vlbb %v17,16(%r5,%r3),6 /* Load next accept part. */
248 aghi %r5,16 /* Increment current len of accept-string. */
249 lcbb %r9,0(%r5,%r3),6 /* Get loaded byte count of accept-string. */
250 jo .Lslow_next_acc_onbb /* Jump away ifaccept-string is
251 on block-boundary. */
252 .Lslow_next_acc_notonbb:
253 vistrbs %v17,%v17 /* Fill with zeros after first zero. */
254 jo .Lslow_loop_acc /* No zero found -> no preparation needed. */
256 .Lslow_next_acc_prepare_zero:
257 /* Zero in accept-part: fill zeros with first-accept-character. */
258 vlgvb %r8,%v17,0 /* Load first element of acc-part. */
259 clije %r8,0,.Lslow_next_str /* Proceed with next string-part,
260 if first char in this part of accept
262 /* r8>0 -> zero found in this acc-part. */
263 vrepb %v18,%v17,0 /* Replicate first char accross all chars. */
264 vceqb %v22,%v20,%v17 /* Create a mask (v22) of null chars
265 by comparing with 0 (v20). */
266 vsel %v17,%v18,%v17,%v22 /* Replace null chars with first char. */
267 j .Lslow_loop_acc /* Accept part is prepared -> process. */
269 .Lslow_next_acc_onbb:
270 vfenezb %v18,%v17,%v17 /* Find zero in loaded bytes of accept part. */
271 vlgvb %r8,%v18,7 /* Load byte index of zero. */
272 clrjl %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes
274 vl %v17,0(%r5,%r3) /* Load over boundary ... */
275 lghi %r8,0 /* r8=0 -> no zero in this part of acc,
276 check for zero is in jump-target. */
277 j .Lslow_next_acc_notonbb /* ... and search for zero in
278 fully loaded vreg again. */
281 lghi %r1,0 /* Return null if no character is equal. */
285 vlgvb %r4,%v18,7 /* Load byte index of found element. */
286 vlgvb %r0,%v16,0(%r4) /* Get found element. */
287 clije %r0,0,.Lslow_end_null /* Return null if no acc-char found. */
290 algfr %r1,%r4 /* Add found index of char to current len. */
292 la %r1,0(%r1,%r2) /* And return pointer to first equal char. */
295 /* Restore registers. */
302 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */