stxcpy.S 8.7 KB

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