strncpy-vx.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Vector optimized 32/64 bit S/390 version of strncpy.
  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-strncpy.h>
  16. #include "sysdep.h"
  17. #include "asm-syntax.h"
  18. .text
  19. #if HAVE_STRNCPY_Z13
  20. /* char * strncpy (const char *dest, const char *src, size_t n)
  21. Copy at most n characters of string src to dest.
  22. Register usage:
  23. -r0=dest pointer for return
  24. -r1=tmp, zero byte index
  25. -r2=dest
  26. -r3=src
  27. -r4=n
  28. -r5=current_len
  29. -r6=tmp, loaded bytes
  30. -r7=tmp, border
  31. -v16=part of src
  32. -v17=index of zero
  33. -v18=part of src
  34. -v31=register save area for r6, r7
  35. */
  36. ENTRY(STRNCPY_Z13)
  37. .machine "z13"
  38. .machinemode "zarch_nohighgprs"
  39. # if !defined __s390x__
  40. llgfr %r4,%r4
  41. # endif /* !defined __s390x__ */
  42. clgfi %r4,0
  43. ber %r14 /* Nothing to do, if n == 0. */
  44. lgr %r0,%r2 /* Save destination pointer for return. */
  45. vlvgp %v31,%r6,%r7 /* Save registers. */
  46. vlbb %v16,0(%r3),6 /* Load s until next 4k-byte boundary. */
  47. lcbb %r6,0(%r3),6 /* Get bytes to 4k-byte boundary or 16. */
  48. llgfr %r6,%r6 /* Convert 32bit to 64bit. */
  49. lghi %r5,0 /* current_len = 0. */
  50. clgrjle %r4,%r6,.Lremaining_v16 /* If n <= loaded-bytes
  51. -> process remaining. */
  52. /* n > loaded-byte-count. */
  53. vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */
  54. vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
  55. clrjl %r1,%r6,.Lfound_v16_store /* Found zero within loaded bytes,
  56. copy and return. */
  57. /* Align s to 16 byte. */
  58. risbgn %r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
  59. lghi %r5,15 /* current_len = 15. */
  60. slr %r5,%r7 /* Compute highest index to 16byte boundary. */
  61. /* Zero not found and n > loaded-byte-count. */
  62. vstl %v16,%r5,0(%r2) /* Copy loaded characters - no zero. */
  63. ahi %r5,1 /* Start loop at next character. */
  64. /* Now we are 16byte aligned, so we can load
  65. a full vreg without page fault. */
  66. lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
  67. aghi %r1,64
  68. clgrjl %r1,%r4,.Lloop64
  69. vl %v16,0(%r5,%r3) /* Load s. */
  70. clgijl %r4,17,.Lremaining_v16 /* If n <= 16, process remaining
  71. bytes. */
  72. .Llt64:
  73. lgr %r7,%r4
  74. slgfi %r7,16 /* border_len = n - 16. */
  75. clgrjhe %r5,%r7,.Lremaining_v16 /* If current_len >= border
  76. then process remaining bytes. */
  77. vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */
  78. je .Lfound_v16 /* Jump away if zero was found. */
  79. vl %v18,16(%r5,%r3) /* Load next part of s. */
  80. vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */
  81. aghi %r5,16
  82. clgrjhe %r5,%r7,.Lremaining_v18
  83. vfenezbs %v17,%v18,%v18
  84. je .Lfound_v18
  85. vl %v16,16(%r5,%r3)
  86. vst %v18,0(%r5,%r2)
  87. aghi %r5,16
  88. clgrjhe %r5,%r7,.Lremaining_v16
  89. vfenezbs %v17,%v16,%v16
  90. je .Lfound_v16
  91. vl %v18,16(%r5,%r3)
  92. vst %v16,0(%r5,%r2)
  93. aghi %r5,16
  94. .Lremaining_v18:
  95. vlr %v16,%v18
  96. .Lremaining_v16:
  97. /* v16 contains the remaining bytes [1...16].
  98. Store remaining bytes and append string-termination. */
  99. vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */
  100. slgrk %r7,%r4,%r5 /* Remaining bytes = maxlen - current_len. */
  101. aghi %r7,-1 /* vstl needs highest index. */
  102. la %r2,0(%r5,%r2) /* vstl has no index register. */
  103. vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */
  104. /* Zero in remaining bytes? -> jump away (zero-index < max-index)
  105. Do not jump away if zero-index == max-index,
  106. but simply copy zero with vstl below. */
  107. clrjl %r1,%r7,.Lfound_v16_store
  108. vstl %v16,%r7,0(%r2) /* Store remaining bytes without null
  109. termination!. */
  110. .Lend:
  111. /* Restore saved registers. */
  112. vlgvg %r6,%v31,0
  113. vlgvg %r7,%v31,1
  114. lgr %r2,%r0 /* Load saved dest-ptr. */
  115. br %r14
  116. .Lfound_v16_32:
  117. aghi %r5,32
  118. j .Lfound_v16
  119. .Lfound_v18_48:
  120. aghi %r5,32
  121. .Lfound_v18_16:
  122. aghi %r5,16
  123. .Lfound_v18:
  124. vlr %v16,%v18
  125. .Lfound_v16:
  126. /* v16 contains a zero. Store remaining bytes to zero. current_len
  127. has not reached border, thus checking for n is not needed! */
  128. vlgvb %r1,%v17,7 /* Load byte index of zero. */
  129. la %r2,0(%r5,%r2) /* vstl has no support for index-register. */
  130. .Lfound_v16_store:
  131. vstl %v16,%r1,0(%r2) /* Copy characters including zero. */
  132. /* Fill remaining bytes with zero - remaining count always > 0. */
  133. algr %r5,%r1 /* Remaining bytes (=%r4) = ... */
  134. slgr %r4,%r5 /* = n - (current_len + zero_index + 1). */
  135. la %r2,0(%r1,%r2) /* Pointer to zero. start filling beyond. */
  136. aghi %r4,-2 /* mvc with exrl needs count - 1.
  137. (additional -1, see remaining bytes above) */
  138. srlg %r6,%r4,8 /* Split into 256 byte blocks. */
  139. ltgr %r6,%r6
  140. je .Lzero_lt256
  141. .Lzero_loop256:
  142. mvc 1(256,%r2),0(%r2) /* Fill 256 zeros at once. */
  143. la %r2,256(%r2)
  144. brctg %r6,.Lzero_loop256 /* Loop until all blocks are processed. */
  145. .Lzero_lt256:
  146. exrl %r4,.Lmvc_lt256
  147. j .Lend
  148. .Lmvc_lt256:
  149. mvc 1(1,%r2),0(%r2)
  150. .Lloop64:
  151. vl %v16,0(%r5,%r3) /* Load s. */
  152. vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */
  153. je .Lfound_v16 /* Jump away if zero was found. */
  154. vl %v18,16(%r5,%r3) /* Load next part of s. */
  155. vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */
  156. vfenezbs %v17,%v18,%v18
  157. je .Lfound_v18_16
  158. vl %v16,32(%r5,%r3)
  159. vst %v18,16(%r5,%r2)
  160. vfenezbs %v17,%v16,%v16
  161. je .Lfound_v16_32
  162. vl %v18,48(%r5,%r3)
  163. vst %v16,32(%r5,%r2)
  164. vfenezbs %v17,%v18,%v18
  165. je .Lfound_v18_48
  166. vst %v18,48(%r5,%r2)
  167. aghi %r5,64
  168. lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
  169. aghi %r1,64
  170. clgrjl %r1,%r4,.Lloop64
  171. vl %v16,0(%r5,%r3) /* Load s. */
  172. j .Llt64
  173. END(STRNCPY_Z13)
  174. # if ! HAVE_STRNCPY_IFUNC
  175. strong_alias (STRNCPY_Z13, strncpy)
  176. # endif
  177. # if ! HAVE_STRNCPY_Z900_G5 && defined SHARED && IS_IN (libc)
  178. strong_alias (STRNCPY_Z13, __GI_strncpy)
  179. # endif
  180. #endif