wcsspn-vx.S 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* Vector optimized 32/64 bit S/390 version of wcsspn.
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <ifunc-wcsspn.h>
  16. #if HAVE_WCSSPN_Z13
  17. # include "sysdep.h"
  18. # include "asm-syntax.h"
  19. .text
  20. /* size_t wcsspn (const wchar_t *s, const wchar_t * accept)
  21. The wcsspn() function calculates the length of the initial segment
  22. of s which consists entirely of characters in accept.
  23. This method checks the length of accept string. If it fits entirely
  24. in one vector register, a fast algorithm is used, which does not need
  25. to check multiple parts of accept-string. Otherwise a slower full
  26. check of accept-string is used.
  27. register overview:
  28. r3: pointer to start of accept-string
  29. r2: pointer to start of search-string
  30. r4: loaded byte count of vl search-string
  31. r0: found byte index
  32. r1: current return len of s
  33. v16: search-string
  34. v17: accept-string
  35. v18: temp-vreg
  36. ONLY FOR SLOW:
  37. v19: first accept-string
  38. v20: zero for preparing acc-vector
  39. v21: global mask; 1 indicates a match between
  40. search-string-vreg and any accept-character
  41. v22: current mask; 1 indicates a match between
  42. search-string-vreg and any accept-character in current acc-vreg
  43. v30, v31: for re-/storing registers r6, r8, r9
  44. r5: current len of accept-string
  45. r6: zero-index in search-string or 16 if no zero
  46. or min(zero-index, loaded byte count)
  47. r8: >0, if former accept-string-part contains a zero,
  48. otherwise =0;
  49. r9: loaded byte count of vlbb accept-string
  50. */
  51. ENTRY(WCSSPN_Z13)
  52. .machine "z13"
  53. .machinemode "zarch_nohighgprs"
  54. tmll %r2,3 /* Test if s is 4-byte aligned? */
  55. jne .Lfallback /* And use common-code variant if not. */
  56. /*
  57. Check if accept-string fits in one vreg:
  58. ----------------------------------------
  59. */
  60. vlbb %v17,0(%r3),6 /* Load accept. */
  61. lcbb %r4,0(%r3),6
  62. jo .Lcheck_onbb /* Special case if accept lays
  63. on block-boundary. */
  64. .Lcheck_notonbb:
  65. vistrfs %v17,%v17 /* Fill with zeros after first zero. */
  66. je .Lfast /* Zero found -> accept fits in one vreg. */
  67. j .Lslow /* No zero -> accept exceeds one vreg. */
  68. .Lcheck_onbb:
  69. /* Accept lays on block-boundary. */
  70. nill %r4,65532 /* Recognize only fully loaded characters. */
  71. je .Lcheck_onbb2 /* Reload vr if no full wchar_t. */
  72. vfenezf %v18,%v17,%v17 /* Search zero in loaded accept bytes. */
  73. vlgvb %r0,%v18,7 /* Get index of zero or 16 if not found. */
  74. clrjl %r0,%r4,.Lcheck_notonbb /* Zero index < loaded bytes count ->
  75. Accept fits in one vreg;
  76. Fill with zeros and proceed
  77. with FAST. */
  78. .Lcheck_onbb2:
  79. vl %v17,0(%r3) /* Load accept, which exceeds loaded bytes. */
  80. j .Lcheck_notonbb /* Check if accept fits in one vreg. */
  81. /*
  82. Search s for accept in one vreg
  83. -------------------------------
  84. */
  85. .Lfast:
  86. /* Complete accept-string in v17 and remaining bytes are zero. */
  87. vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
  88. lcbb %r1,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
  89. vfaezfs %v16,%v16,%v17,8 /* Find first element in v16
  90. unequal to any in v17
  91. or first zero element. */
  92. vlgvb %r0,%v16,7 /* Load byte index of found element. */
  93. /* If found index is within loaded bytes (%r0 < %r1),
  94. return with found element index (=equal count). */
  95. clr %r0,%r1
  96. srlg %r0,%r0,2 /* Convert byte-count to character-count. */
  97. locgrl %r2,%r0
  98. blr %r14
  99. /* Align s to 16 byte. */
  100. risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
  101. lghi %r1,16 /* current_len = 16. */
  102. slr %r1,%r4 /* Compute bytes to 16bytes boundary. */
  103. .Lfast_loop:
  104. vl %v16,0(%r1,%r2) /* Load search-string. */
  105. vfaezfs %v16,%v16,%v17,8 /* Find first element in v16
  106. unequal to any in v17
  107. or first zero element. */
  108. jno .Lfast_loop_found
  109. vl %v16,16(%r1,%r2)
  110. vfaezfs %v16,%v16,%v17,8
  111. jno .Lfast_loop_found16
  112. vl %v16,32(%r1,%r2)
  113. vfaezfs %v16,%v16,%v17,8
  114. jno .Lfast_loop_found32
  115. vl %v16,48(%r1,%r2)
  116. vfaezfs %v16,%v16,%v17,8
  117. jno .Lfast_loop_found48
  118. aghi %r1,64
  119. j .Lfast_loop /* Loop if no element was unequal to accept
  120. and not zero. */
  121. /* Found unequal or zero element. */
  122. .Lfast_loop_found48:
  123. aghi %r1,16
  124. .Lfast_loop_found32:
  125. aghi %r1,16
  126. .Lfast_loop_found16:
  127. aghi %r1,16
  128. .Lfast_loop_found:
  129. vlgvb %r0,%v16,7 /* Load byte index of found element. */
  130. algrk %r2,%r1,%r0 /* And add it to current len. */
  131. srlg %r2,%r2,2 /* Convert byte-count to character-count. */
  132. br %r14
  133. /*
  134. Search s for accept in multiple vregs
  135. -------------------------------------
  136. */
  137. .Lslow:
  138. /* Save registers. */
  139. vlvgg %v30,%r6,0
  140. vlvgp %v31,%r8,%r9
  141. lghi %r1,0 /* Zero out current len. */
  142. /* accept in v17 without zero. */
  143. vlr %v19,%v17 /* Save first acc-part for a fast reload. */
  144. vzero %v20 /* Zero for preparing acc-vector. */
  145. /* Align s to 16 byte. */
  146. risbg %r0,%r2,60,128+63,0 /* Test if s is aligned and
  147. %r0 = bits 60-63 'and' 15. */
  148. je .Lslow_loop_str /* If s is aligned, loop aligned */
  149. lghi %r4,15
  150. slr %r4,%r0 /* Compute highest index to load (15-x). */
  151. vll %v16,%r4,0(%r2) /* Load up to 16byte boundary (vll needs
  152. highest index, remaining bytes are 0). */
  153. aghi %r4,1 /* Work with loaded byte count. */
  154. vzero %v21 /* Zero out global mask. */
  155. lghi %r5,0 /* Set current len of accept-string to zero. */
  156. vfenezf %v18,%v16,%v16 /* Find zero in current string-part. */
  157. lghi %r8,0 /* There is no zero in first accept-part. */
  158. vlgvb %r6,%v18,7 /* Load byte index of zero or 16
  159. if there is no zero. */
  160. clr %r4,%r6 /* cc==1 if loaded byte count < zero-index. */
  161. locrl %r6,%r4 /* Load on cc==1. */
  162. j .Lslow_loop_acc
  163. /* Process s in 16byte aligned loop. */
  164. .Lslow_next_str:
  165. vlr %v17,%v19 /* Load first part of accept (no zero). */
  166. algfr %r1,%r4 /* Add loaded byte count to current len. */
  167. .Lslow_loop_str:
  168. vl %v16,0(%r1,%r2) /* Load search-string. */
  169. lghi %r4,16 /* Loaded byte count is 16. */
  170. vzero %v21 /* Zero out global mask. */
  171. lghi %r5,0 /* Set current len of accept-string to zero. */
  172. vfenezf %v18,%v16,%v16 /* Find zero in current string-part. */
  173. lghi %r8,0 /* There is no zero in first accept-part. */
  174. vlgvb %r6,%v18,7 /* Load byte index of zero or 16 if no zero. */
  175. .Lslow_loop_acc:
  176. vfaef %v22,%v16,%v17,4 /* Create matching-mask (1 in mask ->
  177. character matches any accepted character in
  178. this accept-string-part) IN=0, RT=1. */
  179. vo %v21,%v21,%v22 /* global-mask = global- | matching-mask. */
  180. vfenezf %v18,%v21,%v21 /* Find first zero in global-mask. */
  181. vlgvb %r0,%v18,7 /* Get first found zero-index
  182. (= first mismatch). */
  183. clrjl %r0,%r6,.Lslow_next_acc /* Mismatch-index < min(lbc,zero-index)
  184. -> Process this string-part
  185. with next acc-part. */
  186. clrjhe %r0,%r4,.Lslow_next_str /* Found-index >= loaded byte count
  187. -> All loaded bytes are matching
  188. any accept-character
  189. and are not zero. */
  190. /* All bytes are matching any characters in accept-string
  191. and search-string is fully processed (found-index == zero-index). */
  192. .Lslow_add_lbc_end:
  193. algrk %r2,%r1,%r0 /* Add matching characters to current len. */
  194. srlg %r2,%r2,2 /* Convert byte-count to character-count. */
  195. /* Restore registers. */
  196. vlgvg %r6,%v30,0
  197. vlgvg %r8,%v31,0
  198. vlgvg %r9,%v31,1
  199. br %r14
  200. .Lslow_next_acc:
  201. clijh %r8,0,.Lslow_add_lbc_end /* There was a zero in last acc-part
  202. -> Add found index to current len
  203. and end. */
  204. vlbb %v17,16(%r5,%r3),6 /* Load next accept part. */
  205. aghi %r5,16 /* Increment current len of accept-string. */
  206. lcbb %r9,0(%r5,%r3),6 /* Get loaded byte count of accept-string. */
  207. jo .Lslow_next_acc_onbb /* Jump away if accept-string is
  208. on block-boundary. */
  209. .Lslow_next_acc_notonbb:
  210. vistrfs %v17,%v17 /* Fill with zeros after first zero. */
  211. jo .Lslow_loop_acc /* No zero found -> no preparation needed. */
  212. .Lslow_next_acc_prepare_zero:
  213. /* Zero in accept-part: fill zeros with first-accept-character. */
  214. vlgvf %r8,%v17,0 /* Load first element of acc-part. */
  215. clije %r8,0,.Lslow_add_lbc_end /* End if zero is first character
  216. in this part of accept-string. */
  217. /* r8>0 -> zero found in this acc-part. */
  218. vrepf %v18,%v17,0 /* Replicate first char across all chars. */
  219. vceqf %v22,%v20,%v17 /* Create a mask (v22) of null chars
  220. by comparing with 0 (v20). */
  221. vsel %v17,%v18,%v17,%v22 /* Replace null chars with first char. */
  222. j .Lslow_loop_acc /* Accept part is prepared -> process. */
  223. .Lslow_next_acc_onbb:
  224. nill %r9,65532 /* Recognize only fully loaded characters. */
  225. je .Lslow_next_acc_onbb2 /* Reload vr, if we loaded no full
  226. wchar_t. */
  227. vfenezf %v18,%v17,%v17 /* Find zero in loaded bytes of accept part. */
  228. vlgvb %r8,%v18,7 /* Load byte index of zero. */
  229. clrjl %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes
  230. -> Prepare vreg. */
  231. .Lslow_next_acc_onbb2:
  232. vl %v17,0(%r5,%r3) /* Load over boundary ... */
  233. lghi %r8,0 /* r8=0 -> no zero in this part of acc,
  234. check for zero is in jump-target. */
  235. j .Lslow_next_acc_notonbb /* ... and search for zero in
  236. fully loaded vreg again. */
  237. .Lfallback:
  238. jg WCSSPN_C
  239. END(WCSSPN_Z13)
  240. # if ! HAVE_WCSSPN_IFUNC
  241. strong_alias (WCSSPN_Z13, wcsspn)
  242. # endif
  243. # if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT \
  244. && defined SHARED && IS_IN (libc)
  245. strong_alias (WCSSPN_Z13, __GI_wcsspn)
  246. # endif
  247. #endif