1 /* Vector optimized 32/64 bit S/390 version of wcspbrk.
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 /* wchar_t *wcspbrk (const wchar_t *s, const wchar_t * accept)
27 The wcspbrk() 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"
66 tmll %r2,3 /* Test if s is 4-byte aligned? */
67 jne .Lfallback /* And use common-code variant if not. */
70 Check if accept-string fits in one vreg:
71 ----------------------------------------
73 vlbb %v17,0(%r3),6 /* Load accept. */
75 jo .Lcheck_onbb /* Special case if accept lays
79 lghi %r1,0 /* Zero out current len. */
80 vlgvf %r0,%v17,0 /* Get first element. */
81 clije %r0,0,.Lfast_end_null /* Return null if accept is empty. */
83 vistrfs %v17,%v17 /* Fill with zeros after first zero. */
84 je .Lfast /* Zero found -> accept fits in one vreg. */
85 j .Lslow /* No zero -> accept exceeds one vreg */
89 /* Accept lays on block-boundary. */
90 nill %r0,65532 /* Recognize only fully loaded characters. */
91 je .Lcheck_onbb2 /* Reload vr, if we loaded no full wchar_t. */
92 vfenezf %v18,%v17,%v17 /* Search zero in loaded accept bytes. */
93 vlgvb %r4,%v18,7 /* Get index of zero or 16 if not found. */
94 clrjl %r4,%r0,.Lcheck_notonbb /* Zero index < loaded bytes count ->
95 accept fits in one vreg;
96 Fill with zeros and proceed
99 vl %v17,0(%r3) /* Load accept, which exceeds loaded bytes. */
100 j .Lcheck_notonbb /* Check if accept fits in one vreg. */
104 Search s for accept in one vreg
105 -------------------------------
108 /* Complete accept-string in v17 and remaining bytes are zero. */
110 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
111 lcbb %r0,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
113 vfaezfs %v18,%v16,%v17,0 /* Find first element in v16 unequal to any
114 in v17 or first zero element. */
115 vlgvb %r4,%v18,7 /* Load byte index of found element. */
116 /* If found index is within loaded bytes, return with found
117 element index (=equal count). */
118 clrjl %r4,%r0,.Lfast_loop_found2
120 /* Align s to 16 byte. */
121 risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
122 lghi %r1,16 /* current_len = 16. */
123 slr %r1,%r4 /* Compute bytes to 16bytes boundary. */
126 vl %v16,0(%r1,%r2) /* Load search-string. */
127 vfaezfs %v18,%v16,%v17,0 /* Find first element in v16 equal to any
128 in v17 or first zero element. */
129 jno .Lfast_loop_found
132 vfaezfs %v18,%v16,%v17,0
133 jno .Lfast_loop_found16
136 vfaezfs %v18,%v16,%v17,0
137 jno .Lfast_loop_found32
140 vfaezfs %v18,%v16,%v17,0
141 jno .Lfast_loop_found48
144 j .Lfast_loop /* Loop if no element was unequal to accept
147 /* Found equal or zero element. */
155 vlgvb %r4,%v18,7 /* Load byte index of found element. */
157 srlg %r5,%r4,2 /* Convert byte-index to character-index. */
158 vlgvf %r0,%v16,0(%r5) /* Get found element. */
159 clije %r0,0,.Lfast_end_null /* Return null if no accept-char found */
160 algfr %r1,%r4 /* Add found index of char to current len. */
161 la %r2,0(%r1,%r2) /* And return pointer to first equal char. */
165 lghi %r2,0 /* Return null if no character is equal. */
172 Search s for accept in multiple vregs
173 -------------------------------------
176 /* Save registers. */
180 /* Accept in v17 without zero */
181 vlr %v19,%v17 /* Save first acc-part for a fast reload. */
182 vzero %v20 /* Zero for preparing acc-vector. */
183 vone %v24 /* One for checking result of former string. */
185 /* Align s to 16 byte. */
186 risbg %r4,%r2,60,128+63,0 /* Test if s is aligned and
187 %r4 = bits 60-63 'and' 15. */
188 je .Lslow_loop_str /* If s is aligned, loop aligned. */
190 slr %r0,%r4 /* Compute highest index to load (15-x). */
191 vll %v16,%r0,0(%r2) /* Load up to 16byte boundary;
192 needs highest index, left bytes are 0. */
193 ahi %r0,1 /* Work with loaded byte count. */
194 vzero %v21 /* Zero out global mask. */
195 lghi %r5,0 /* Set current len of accept-string to zero. */
196 vfenezf %v18,%v16,%v16 /* Find zero in current string-part. */
197 lghi %r8,0 /* There is no zero in first accept-part. */
198 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
199 clije %r6,0,.Lslow_end_null /* If first element is zero
200 (end of string) -> return null */
201 clr %r0,%r6 /* cc==1 if loaded byte count < zero-index. */
202 locrl %r6,%r0 /* Load on cc==1; zero-index = lbc. */
206 /* Process s in 16byte aligned loop. */
208 /* Check results of former processed str-part. */
209 vfeef %v18,%v21,%v24 /* Find first equal match in global mask
210 (ones in element). */
211 vlgvb %r4,%v18,7 /* Get index of first one (=equal)
212 or 16 if no match. */
213 /* Equal-index < min(zero-index, loaded byte count)
214 -> return pointer to equal element. */
215 clrjl %r4,%r6,.Lslow_index_found
216 /* Zero-index < loaded byte count
217 -> former str-part was last str-part
219 clrjl %r6,%r0,.Lslow_end_null
220 /* All elements are zero (=no match) -> proceed with next str-part. */
222 vlr %v17,%v19 /* Load first part of accept (no zero). */
223 algfr %r1,%r0 /* Add loaded byte count to current len. */
226 vl %v16,0(%r1,%r2) /* Load search-string */
227 lghi %r0,16 /* Loaded byte count is 16. */
228 vzero %v21 /* Zero out global mask. */
229 lghi %r5,0 /* Set current len of accept to zero. */
230 vfenezf %v18,%v16,%v16 /* Find zero in current string-part. */
231 lghi %r8,0 /* There is no zero in first accept-part. */
232 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
233 clije %r6,0,.Lslow_end_null /* If first element is zero
234 (end of string) -> return null. */
237 vfaef %v22,%v16,%v17,4 /* Create matching-mask (1 in mask ->
238 Character matches any accepted character in
239 this accept-string-part) IN=0, RT=1. */
240 vlgvf %r4,%v22,0 /* Get result of first element. */
241 /* First element is equal to any accepted characters
242 (all other parts of accept cannot lead to a match before this one)
243 -> current len is pointing to first element
245 clijh %r4,0,.Lslow_end_found
246 vo %v21,%v21,%v22 /* Global-mask = global-|matching-mask. */
247 /* Proceed with next acc until end of acc is reached. */
251 clijh %r8,0,.Lslow_next_str /* There was a zero in the last acc-part
252 -> add index to current len and
254 vlbb %v17,16(%r5,%r3),6 /* Load next accept part. */
255 aghi %r5,16 /* Increment current len of accept-string. */
256 lcbb %r9,0(%r5,%r3),6 /* Get loaded byte count of accept-string. */
257 jo .Lslow_next_acc_onbb /* Jump away ifaccept-string is
258 on block-boundary. */
259 .Lslow_next_acc_notonbb:
260 vistrfs %v17,%v17 /* Fill with zeros after first zero. */
261 jo .Lslow_loop_acc /* No zero found -> no preparation needed. */
263 .Lslow_next_acc_prepare_zero:
264 /* Zero in accept-part: fill zeros with first-accept-character. */
265 vlgvf %r8,%v17,0 /* Load first element of acc-part. */
266 clije %r8,0,.Lslow_next_str /* Proceed with next string-part,
267 If first char in this part of accept
269 /* r8>0 -> zero found in this acc-part. */
270 vrepf %v18,%v17,0 /* Replicate first char accross all chars. */
271 vceqf %v22,%v20,%v17 /* Create a mask (v22) of null chars
272 by comparing with 0 (v20). */
273 vsel %v17,%v18,%v17,%v22 /* Replace null chars with first char. */
274 j .Lslow_loop_acc /* Accept part is prepared -> process. */
276 .Lslow_next_acc_onbb:
277 nill %r9,65532 /* Recognize only fully loaded characters. */
278 je .Lslow_next_acc_onbb2 /* Reload vr, if no full wchar_t. */
279 vfenezf %v18,%v17,%v17 /* Find zero in loaded bytes of accept part. */
280 vlgvb %r8,%v18,7 /* Load byte index of zero. */
281 clrjl %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes
283 .Lslow_next_acc_onbb2:
284 vl %v17,0(%r5,%r3) /* Load over boundary ... */
285 lghi %r8,0 /* r8=0 -> no zero in this part of acc,
286 check for zero is in jump-target. */
287 j .Lslow_next_acc_notonbb /* ... and search for zero in
288 fully loaded vreg again. */
291 lghi %r1,0 /* Return null if no character is equal. */
295 vlgvb %r4,%v18,7 /* Load byte index of found element. */
296 srlg %r5,%r4,2 /* Convert byte-index to character-index. */
297 vlgvf %r0,%v16,0(%r5) /* Get found element. */
298 clije %r0,0,.Lslow_end_null /* Return null if no acc-char found. */
301 algfr %r1,%r4 /* Add found index of char to current len. */
303 la %r1,0(%r1,%r2) /* And return pointer to first equal char. */
306 /* Restore registers. */
315 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */