stxcpy.S 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * arch/alpha/lib/stxcpy.S
  4. * Contributed by Richard Henderson ([email protected])
  5. *
  6. * Copy a null-terminated string from SRC to DST.
  7. *
  8. * This is an internal routine used by strcpy, stpcpy, and strcat.
  9. * As such, it uses special linkage conventions to make implementation
  10. * of these public functions more efficient.
  11. *
  12. * On input:
  13. * t9 = return address
  14. * a0 = DST
  15. * a1 = SRC
  16. *
  17. * On output:
  18. * t12 = bitmask (with one bit set) indicating the last byte written
  19. * a0 = unaligned address of the last *word* written
  20. *
  21. * Furthermore, v0, a3-a5, t11, and t12 are untouched.
  22. */
  23. #include <asm/regdef.h>
  24. .set noat
  25. .set noreorder
  26. .text
  27. /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
  28. doesn't like putting the entry point for a procedure somewhere in the
  29. middle of the procedure descriptor. Work around this by putting the
  30. aligned copy in its own procedure descriptor */
  31. .ent stxcpy_aligned
  32. .align 3
  33. stxcpy_aligned:
  34. .frame sp, 0, t9
  35. .prologue 0
  36. /* On entry to this basic block:
  37. t0 == the first destination word for masking back in
  38. t1 == the first source word. */
  39. /* Create the 1st output word and detect 0's in the 1st input word. */
  40. lda t2, -1 # e1 : build a mask against false zero
  41. mskqh t2, a1, t2 # e0 : detection in the src word
  42. mskqh t1, a1, t3 # e0 :
  43. ornot t1, t2, t2 # .. e1 :
  44. mskql t0, a1, t0 # e0 : assemble the first output word
  45. cmpbge zero, t2, t8 # .. e1 : bits set iff null found
  46. or t0, t3, t1 # e0 :
  47. bne t8, $a_eos # .. e1 :
  48. /* On entry to this basic block:
  49. t0 == the first destination word for masking back in
  50. t1 == a source word not containing a null. */
  51. $a_loop:
  52. stq_u t1, 0(a0) # e0 :
  53. addq a0, 8, a0 # .. e1 :
  54. ldq_u t1, 0(a1) # e0 :
  55. addq a1, 8, a1 # .. e1 :
  56. cmpbge zero, t1, t8 # e0 (stall)
  57. beq t8, $a_loop # .. e1 (zdb)
  58. /* Take care of the final (partial) word store.
  59. On entry to this basic block we have:
  60. t1 == the source word containing the null
  61. t8 == the cmpbge mask that found it. */
  62. $a_eos:
  63. negq t8, t6 # e0 : find low bit set
  64. and t8, t6, t12 # e1 (stall)
  65. /* For the sake of the cache, don't read a destination word
  66. if we're not going to need it. */
  67. and t12, 0x80, t6 # e0 :
  68. bne t6, 1f # .. e1 (zdb)
  69. /* We're doing a partial word store and so need to combine
  70. our source and original destination words. */
  71. ldq_u t0, 0(a0) # e0 :
  72. subq t12, 1, t6 # .. e1 :
  73. zapnot t1, t6, t1 # e0 : clear src bytes >= null
  74. or t12, t6, t8 # .. e1 :
  75. zap t0, t8, t0 # e0 : clear dst bytes <= null
  76. or t0, t1, t1 # e1 :
  77. 1: stq_u t1, 0(a0) # e0 :
  78. ret (t9) # .. e1 :
  79. .end stxcpy_aligned
  80. .align 3
  81. .ent __stxcpy
  82. .globl __stxcpy
  83. __stxcpy:
  84. .frame sp, 0, t9
  85. .prologue 0
  86. /* Are source and destination co-aligned? */
  87. xor a0, a1, t0 # e0 :
  88. unop # :
  89. and t0, 7, t0 # e0 :
  90. bne t0, $unaligned # .. e1 :
  91. /* We are co-aligned; take care of a partial first word. */
  92. ldq_u t1, 0(a1) # e0 : load first src word
  93. and a0, 7, t0 # .. e1 : take care not to load a word ...
  94. addq a1, 8, a1 # e0 :
  95. beq t0, stxcpy_aligned # .. e1 : ... if we wont need it
  96. ldq_u t0, 0(a0) # e0 :
  97. br stxcpy_aligned # .. e1 :
  98. /* The source and destination are not co-aligned. Align the destination
  99. and cope. We have to be very careful about not reading too much and
  100. causing a SEGV. */
  101. .align 3
  102. $u_head:
  103. /* We know just enough now to be able to assemble the first
  104. full source word. We can still find a zero at the end of it
  105. that prevents us from outputting the whole thing.
  106. On entry to this basic block:
  107. t0 == the first dest word, for masking back in, if needed else 0
  108. t1 == the low bits of the first source word
  109. t6 == bytemask that is -1 in dest word bytes */
  110. ldq_u t2, 8(a1) # e0 :
  111. addq a1, 8, a1 # .. e1 :
  112. extql t1, a1, t1 # e0 :
  113. extqh t2, a1, t4 # e0 :
  114. mskql t0, a0, t0 # e0 :
  115. or t1, t4, t1 # .. e1 :
  116. mskqh t1, a0, t1 # e0 :
  117. or t0, t1, t1 # e1 :
  118. or t1, t6, t6 # e0 :
  119. cmpbge zero, t6, t8 # .. e1 :
  120. lda t6, -1 # e0 : for masking just below
  121. bne t8, $u_final # .. e1 :
  122. mskql t6, a1, t6 # e0 : mask out the bits we have
  123. or t6, t2, t2 # e1 : already extracted before
  124. cmpbge zero, t2, t8 # e0 : testing eos
  125. bne t8, $u_late_head_exit # .. e1 (zdb)
  126. /* Finally, we've got all the stupid leading edge cases taken care
  127. of and we can set up to enter the main loop. */
  128. stq_u t1, 0(a0) # e0 : store first output word
  129. addq a0, 8, a0 # .. e1 :
  130. extql t2, a1, t0 # e0 : position ho-bits of lo word
  131. ldq_u t2, 8(a1) # .. e1 : read next high-order source word
  132. addq a1, 8, a1 # e0 :
  133. cmpbge zero, t2, t8 # .. e1 :
  134. nop # e0 :
  135. bne t8, $u_eos # .. e1 :
  136. /* Unaligned copy main loop. In order to avoid reading too much,
  137. the loop is structured to detect zeros in aligned source words.
  138. This has, unfortunately, effectively pulled half of a loop
  139. iteration out into the head and half into the tail, but it does
  140. prevent nastiness from accumulating in the very thing we want
  141. to run as fast as possible.
  142. On entry to this basic block:
  143. t0 == the shifted high-order bits from the previous source word
  144. t2 == the unshifted current source word
  145. We further know that t2 does not contain a null terminator. */
  146. .align 3
  147. $u_loop:
  148. extqh t2, a1, t1 # e0 : extract high bits for current word
  149. addq a1, 8, a1 # .. e1 :
  150. extql t2, a1, t3 # e0 : extract low bits for next time
  151. addq a0, 8, a0 # .. e1 :
  152. or t0, t1, t1 # e0 : current dst word now complete
  153. ldq_u t2, 0(a1) # .. e1 : load high word for next time
  154. stq_u t1, -8(a0) # e0 : save the current word
  155. mov t3, t0 # .. e1 :
  156. cmpbge zero, t2, t8 # e0 : test new word for eos
  157. beq t8, $u_loop # .. e1 :
  158. /* We've found a zero somewhere in the source word we just read.
  159. If it resides in the lower half, we have one (probably partial)
  160. word to write out, and if it resides in the upper half, we
  161. have one full and one partial word left to write out.
  162. On entry to this basic block:
  163. t0 == the shifted high-order bits from the previous source word
  164. t2 == the unshifted current source word. */
  165. $u_eos:
  166. extqh t2, a1, t1 # e0 :
  167. or t0, t1, t1 # e1 : first (partial) source word complete
  168. cmpbge zero, t1, t8 # e0 : is the null in this first bit?
  169. bne t8, $u_final # .. e1 (zdb)
  170. $u_late_head_exit:
  171. stq_u t1, 0(a0) # e0 : the null was in the high-order bits
  172. addq a0, 8, a0 # .. e1 :
  173. extql t2, a1, t1 # e0 :
  174. cmpbge zero, t1, t8 # .. e1 :
  175. /* Take care of a final (probably partial) result word.
  176. On entry to this basic block:
  177. t1 == assembled source word
  178. t8 == cmpbge mask that found the null. */
  179. $u_final:
  180. negq t8, t6 # e0 : isolate low bit set
  181. and t6, t8, t12 # e1 :
  182. and t12, 0x80, t6 # e0 : avoid dest word load if we can
  183. bne t6, 1f # .. e1 (zdb)
  184. ldq_u t0, 0(a0) # e0 :
  185. subq t12, 1, t6 # .. e1 :
  186. or t6, t12, t8 # e0 :
  187. zapnot t1, t6, t1 # .. e1 : kill source bytes >= null
  188. zap t0, t8, t0 # e0 : kill dest bytes <= null
  189. or t0, t1, t1 # e1 :
  190. 1: stq_u t1, 0(a0) # e0 :
  191. ret (t9) # .. e1 :
  192. /* Unaligned copy entry point. */
  193. .align 3
  194. $unaligned:
  195. ldq_u t1, 0(a1) # e0 : load first source word
  196. and a0, 7, t4 # .. e1 : find dest misalignment
  197. and a1, 7, t5 # e0 : find src misalignment
  198. /* Conditionally load the first destination word and a bytemask
  199. with 0xff indicating that the destination byte is sacrosanct. */
  200. mov zero, t0 # .. e1 :
  201. mov zero, t6 # e0 :
  202. beq t4, 1f # .. e1 :
  203. ldq_u t0, 0(a0) # e0 :
  204. lda t6, -1 # .. e1 :
  205. mskql t6, a0, t6 # e0 :
  206. 1:
  207. subq a1, t4, a1 # .. e1 : sub dest misalignment from src addr
  208. /* If source misalignment is larger than dest misalignment, we need
  209. extra startup checks to avoid SEGV. */
  210. cmplt t4, t5, t12 # e0 :
  211. beq t12, $u_head # .. e1 (zdb)
  212. lda t2, -1 # e1 : mask out leading garbage in source
  213. mskqh t2, t5, t2 # e0 :
  214. nop # e0 :
  215. ornot t1, t2, t3 # .. e1 :
  216. cmpbge zero, t3, t8 # e0 : is there a zero?
  217. beq t8, $u_head # .. e1 (zdb)
  218. /* At this point we've found a zero in the first partial word of
  219. the source. We need to isolate the valid source data and mask
  220. it into the original destination data. (Incidentally, we know
  221. that we'll need at least one byte of that original dest word.) */
  222. ldq_u t0, 0(a0) # e0 :
  223. negq t8, t6 # .. e1 : build bitmask of bytes <= zero
  224. and t6, t8, t12 # e0 :
  225. and a1, 7, t5 # .. e1 :
  226. subq t12, 1, t6 # e0 :
  227. or t6, t12, t8 # e1 :
  228. srl t12, t5, t12 # e0 : adjust final null return value
  229. zapnot t2, t8, t2 # .. e1 : prepare source word; mirror changes
  230. and t1, t2, t1 # e1 : to source validity mask
  231. extql t2, a1, t2 # .. e0 :
  232. extql t1, a1, t1 # e0 :
  233. andnot t0, t2, t0 # .. e1 : zero place for source to reside
  234. or t0, t1, t1 # e1 : and put it there
  235. stq_u t1, 0(a0) # .. e0 :
  236. ret (t9) # e1 :
  237. .end __stxcpy