1/* Vector optimized 32/64 bit S/390 version of strspn. 2 Copyright (C) 2015-2021 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-strspn.h> 20 21#if HAVE_STRSPN_Z13 22 23# include "sysdep.h" 24# include "asm-syntax.h" 25 26 .text 27 28/* size_t strspn (const char *s, const char * accept) 29 The strspn() function calculates the length of the initial segment 30 of s which consists entirely of characters in accept. 31 32 This method checks the length of accept string. If it fits entirely 33 in one vector register, a fast algorithm is used, which does not need 34 to check multiple parts of accept-string. Otherwise a slower full 35 check of accept-string is used. 36 37 register overview: 38 r3: pointer to start of accept-string 39 r2: pointer to start of search-string 40 r4: loaded byte count of vl search-string 41 r0: found byte index 42 r1: current return len of s 43 v16: search-string 44 v17: accept-string 45 v18: temp-vreg 46 47 ONLY FOR SLOW: 48 v19: first accept-string 49 v20: zero for preparing acc-vector 50 v21: global mask; 1 indicates a match between 51 search-string-vreg and any accept-character 52 v22: current mask; 1 indicates a match between 53 search-string-vreg and any accept-character in current acc-vreg 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, 59 otherwise =0; 60 r9: loaded byte count of vlbb accept-string 61*/ 62ENTRY(STRSPN_Z13) 63 .machine "z13" 64 .machinemode "zarch_nohighgprs" 65 66 /* 67 Check if accept-string fits in one vreg: 68 ---------------------------------------- 69 */ 70 vlbb %v17,0(%r3),6 /* Load accept. */ 71 lcbb %r4,0(%r3),6 72 jo .Lcheck_onbb /* Special case if accept lays 73 on block-boundary. */ 74.Lcheck_notonbb: 75 vistrbs %v17,%v17 /* Fill with zeros after first zero. */ 76 je .Lfast /* Zero found -> accept fits in one vreg. */ 77 j .Lslow /* No zero -> accept exceeds one vreg. */ 78 79.Lcheck_onbb: 80 /* Accept lays on block-boundary. */ 81 vfenezb %v18,%v17,%v17 /* Search zero in loaded accept bytes. */ 82 vlgvb %r0,%v18,7 /* Get index of zero or 16 if not found. */ 83 clrjl %r0,%r4,.Lcheck_notonbb /* Zero index < loaded bytes count -> 84 Accept fits in one vreg; 85 Fill with zeros and proceed 86 with FAST. */ 87 vl %v17,0(%r3) /* Load accept, which exceeds loaded bytes. */ 88 j .Lcheck_notonbb /* Check if accept fits in one vreg. */ 89 90 91 /* 92 Search s for accept in one vreg 93 ------------------------------- 94 */ 95.Lfast: 96 /* Complete accept-string is in v17 and remaining bytes are zero. */ 97 98 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */ 99 lcbb %r1,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */ 100 101 vfaezbs %v16,%v16,%v17,8 /* Find first element in v16 102 unequal to any in v17 103 or first zero element. */ 104 vlgvb %r0,%v16,7 /* Load byte index of found element. */ 105 /* If found index is within loaded bytes (%r0 < %r1), 106 return with found element index (=equal count). */ 107 clr %r0,%r1 108 locgrl %r2,%r0 109 blr %r14 110 111 /* Align s to 16 byte. */ 112 risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */ 113 lghi %r1,16 /* current_len = 16. */ 114 slr %r1,%r4 /* Compute bytes to 16bytes boundary. */ 115 116.Lfast_loop: 117 vl %v16,0(%r1,%r2) /* Load search-string. */ 118 vfaezbs %v16,%v16,%v17,8 /* Find first element in v16 119 unequal to any in v17 120 or first zero element. */ 121 jno .Lfast_loop_found 122 vl %v16,16(%r1,%r2) 123 vfaezbs %v16,%v16,%v17,8 124 jno .Lfast_loop_found16 125 vl %v16,32(%r1,%r2) 126 vfaezbs %v16,%v16,%v17,8 127 jno .Lfast_loop_found32 128 vl %v16,48(%r1,%r2) 129 vfaezbs %v16,%v16,%v17,8 130 jno .Lfast_loop_found48 131 132 aghi %r1,64 133 j .Lfast_loop /* Loop if no element was unequal to accept 134 and not zero. */ 135 136 /* Found unequal or zero element. */ 137.Lfast_loop_found48: 138 aghi %r1,16 139.Lfast_loop_found32: 140 aghi %r1,16 141.Lfast_loop_found16: 142 aghi %r1,16 143.Lfast_loop_found: 144 vlgvb %r0,%v16,7 /* Load byte index of found element. */ 145 algrk %r2,%r1,%r0 /* And add it to current len. */ 146 br %r14 147 148 149 /* 150 Search s for accept in multiple vregs 151 ------------------------------------- 152 */ 153.Lslow: 154 /* Save registers. */ 155 vlvgg %v30,%r6,0 156 vlvgp %v31,%r8,%r9 157 lghi %r1,0 /* current_len = 0. */ 158 159 /* Accept in v17 without zero. */ 160 vlr %v19,%v17 /* Save first acc-part for a fast reload. */ 161 vzero %v20 /* Zero for preparing acc-vector. */ 162 163 /* Align s to 16 byte. */ 164 risbg %r0,%r2,60,128+63,0 /* Test if s is aligned and 165 %r0 = bits 60-63 'and' 15 */ 166 je .Lslow_loop_str /* If s is aligned, loop aligned */ 167 lghi %r4,15 168 slr %r4,%r0 /* Compute highest index to load (15-x). */ 169 vll %v16,%r4,0(%r2) /* Load up to 16byte boundary (vll needs 170 highest index, left bytes are 0). */ 171 ahi %r4,1 /* Work with loaded byte count. */ 172 vzero %v21 /* Zero out global mask. */ 173 lghi %r5,0 /* Set current len of accept-string to zero. */ 174 vfenezb %v18,%v16,%v16 /* Find zero in current string-part. */ 175 lghi %r8,0 /* There is no zero in first accept-part. */ 176 vlgvb %r6,%v18,7 /* Load byte index of zero or 16 177 if there is no zero. */ 178 clr %r4,%r6 /* cc==1 if loaded byte count < zero-index. */ 179 locrl %r6,%r4 /* Load on cc==1. */ 180 j .Lslow_loop_acc 181 182 /* Process s in 16byte aligned loop. */ 183.Lslow_next_str: 184 vlr %v17,%v19 /* Load first part of accept (no zero). */ 185 algfr %r1,%r4 /* Add loaded byte count to current len. */ 186.Lslow_loop_str: 187 vl %v16,0(%r1,%r2) /* Load search-string. */ 188 lghi %r4,16 /* Loaded byte count is 16. */ 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 195.Lslow_loop_acc: 196 vfaeb %v22,%v16,%v17,4 /* Create matching-mask (1 in mask -> 197 character matches any accepted character in 198 this accept-string-part) IN=0, RT=1. */ 199 vo %v21,%v21,%v22 /* global-mask = global- | matching-mask. */ 200 vfenezb %v18,%v21,%v21 /* Find first zero in global-mask. */ 201 vlgvb %r0,%v18,7 /* Get first found zero-index 202 (= first mismatch). */ 203 clrjl %r0,%r6,.Lslow_next_acc /* Mismatch-index < min(lbc,zero-index) 204 -> Process this string-part 205 with next acc-part. */ 206 clrjhe %r0,%r4,.Lslow_next_str /* Found-index >= loaded byte count 207 -> All loaded bytes are matching 208 any accept-character 209 and are not zero. */ 210 /* All bytes are matching any characters in accept-string 211 and search-string is fully processed (found-index == zero-index) */ 212.Lslow_add_lbc_end: 213 algrk %r2,%r1,%r0 /* Add matching characters to current_len. */ 214 /* Restore registers. */ 215 vlgvg %r6,%v30,0 216 vlgvg %r8,%v31,0 217 vlgvg %r9,%v31,1 218 br %r14 219 220 221 222.Lslow_next_acc: 223 clijh %r8,0,.Lslow_add_lbc_end /* There was a zero in last acc-part 224 -> Add found index to current len 225 and end. */ 226 vlbb %v17,16(%r5,%r3),6 /* Load next accept part. */ 227 aghi %r5,16 /* Add current_len of accept-string. */ 228 lcbb %r9,0(%r5,%r3),6 /* Get loaded byte count of accept-string. */ 229 jo .Lslow_next_acc_onbb /* Jump away if accept-string is 230 on block-boundary. */ 231.Lslow_next_acc_notonbb: 232 vistrbs %v17,%v17 /* Fill with zeros after first zero. */ 233 jo .Lslow_loop_acc /* No zero found -> no preparation needed. */ 234 235.Lslow_next_acc_prepare_zero: 236 /* Zero in accept-part: fill zeros with first-accept-character. */ 237 vlgvb %r8,%v17,0 /* Load first element of acc-part. */ 238 clije %r8,0,.Lslow_add_lbc_end /* End if zero is first character 239 in this part of accept-string. */ 240 /* r8>0 -> zero found in this acc-part. */ 241 vrepb %v18,%v17,0 /* Replicate first char accross all chars. */ 242 vceqb %v22,%v20,%v17 /* Create a mask (v22) of null chars 243 by comparing with 0 (v20). */ 244 vsel %v17,%v18,%v17,%v22 /* Replace null chars with first char. */ 245 j .Lslow_loop_acc /* Accept part is prepared -> process. */ 246 247.Lslow_next_acc_onbb: 248 vfenezb %v18,%v17,%v17 /* Find zero in loaded bytes of accept part. */ 249 vlgvb %r8,%v18,7 /* Load byte index of zero. */ 250 clrjl %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes 251 -> Prepare vr. */ 252 vl %v17,0(%r5,%r3) /* Load over boundary ... */ 253 lghi %r8,0 /* r8=0 -> no zero in this part of acc, 254 Check for zero is in jump-target. */ 255 j .Lslow_next_acc_notonbb /* ... and search for zero in 256 fully loaded vreg again. */ 257END(STRSPN_Z13) 258 259# if ! HAVE_STRSPN_IFUNC 260strong_alias (STRSPN_Z13, strspn) 261# endif 262 263# if ! HAVE_STRSPN_C && defined SHARED && IS_IN (libc) 264strong_alias (STRSPN_Z13, __GI_strspn) 265# endif 266 267#endif /* HAVE_STRSPN_Z13 */ 268