ev6-stxcpy.S 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * arch/alpha/lib/ev6-stxcpy.S
  3. * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
  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. * Much of the information about 21264 scheduling/coding comes from:
  23. * Compiler Writer's Guide for the Alpha 21264
  24. * abbreviated as 'CWG' in other comments here
  25. * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
  26. * Scheduling notation:
  27. * E - either cluster
  28. * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
  29. * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
  30. * Try not to change the actual algorithm if possible for consistency.
  31. */
  32. #include <asm/regdef.h>
  33. .set noat
  34. .set noreorder
  35. .text
  36. /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
  37. doesn't like putting the entry point for a procedure somewhere in the
  38. middle of the procedure descriptor. Work around this by putting the
  39. aligned copy in its own procedure descriptor */
  40. .ent stxcpy_aligned
  41. .align 4
  42. stxcpy_aligned:
  43. .frame sp, 0, t9
  44. .prologue 0
  45. /* On entry to this basic block:
  46. t0 == the first destination word for masking back in
  47. t1 == the first source word. */
  48. /* Create the 1st output word and detect 0's in the 1st input word. */
  49. lda t2, -1 # E : build a mask against false zero
  50. mskqh t2, a1, t2 # U : detection in the src word (stall)
  51. mskqh t1, a1, t3 # U :
  52. ornot t1, t2, t2 # E : (stall)
  53. mskql t0, a1, t0 # U : assemble the first output word
  54. cmpbge zero, t2, t8 # E : bits set iff null found
  55. or t0, t3, t1 # E : (stall)
  56. bne t8, $a_eos # U : (stall)
  57. /* On entry to this basic block:
  58. t0 == the first destination word for masking back in
  59. t1 == a source word not containing a null. */
  60. /* Nops here to separate store quads from load quads */
  61. $a_loop:
  62. stq_u t1, 0(a0) # L :
  63. addq a0, 8, a0 # E :
  64. nop
  65. nop
  66. ldq_u t1, 0(a1) # L : Latency=3
  67. addq a1, 8, a1 # E :
  68. cmpbge zero, t1, t8 # E : (3 cycle stall)
  69. beq t8, $a_loop # U : (stall for t8)
  70. /* Take care of the final (partial) word store.
  71. On entry to this basic block we have:
  72. t1 == the source word containing the null
  73. t8 == the cmpbge mask that found it. */
  74. $a_eos:
  75. negq t8, t6 # E : find low bit set
  76. and t8, t6, t12 # E : (stall)
  77. /* For the sake of the cache, don't read a destination word
  78. if we're not going to need it. */
  79. and t12, 0x80, t6 # E : (stall)
  80. bne t6, 1f # U : (stall)
  81. /* We're doing a partial word store and so need to combine
  82. our source and original destination words. */
  83. ldq_u t0, 0(a0) # L : Latency=3
  84. subq t12, 1, t6 # E :
  85. zapnot t1, t6, t1 # U : clear src bytes >= null (stall)
  86. or t12, t6, t8 # E : (stall)
  87. zap t0, t8, t0 # E : clear dst bytes <= null
  88. or t0, t1, t1 # E : (stall)
  89. nop
  90. nop
  91. 1: stq_u t1, 0(a0) # L :
  92. ret (t9) # L0 : Latency=3
  93. nop
  94. nop
  95. .end stxcpy_aligned
  96. .align 4
  97. .ent __stxcpy
  98. .globl __stxcpy
  99. __stxcpy:
  100. .frame sp, 0, t9
  101. .prologue 0
  102. /* Are source and destination co-aligned? */
  103. xor a0, a1, t0 # E :
  104. unop # E :
  105. and t0, 7, t0 # E : (stall)
  106. bne t0, $unaligned # U : (stall)
  107. /* We are co-aligned; take care of a partial first word. */
  108. ldq_u t1, 0(a1) # L : load first src word
  109. and a0, 7, t0 # E : take care not to load a word ...
  110. addq a1, 8, a1 # E :
  111. beq t0, stxcpy_aligned # U : ... if we wont need it (stall)
  112. ldq_u t0, 0(a0) # L :
  113. br stxcpy_aligned # L0 : Latency=3
  114. nop
  115. nop
  116. /* The source and destination are not co-aligned. Align the destination
  117. and cope. We have to be very careful about not reading too much and
  118. causing a SEGV. */
  119. .align 4
  120. $u_head:
  121. /* We know just enough now to be able to assemble the first
  122. full source word. We can still find a zero at the end of it
  123. that prevents us from outputting the whole thing.
  124. On entry to this basic block:
  125. t0 == the first dest word, for masking back in, if needed else 0
  126. t1 == the low bits of the first source word
  127. t6 == bytemask that is -1 in dest word bytes */
  128. ldq_u t2, 8(a1) # L :
  129. addq a1, 8, a1 # E :
  130. extql t1, a1, t1 # U : (stall on a1)
  131. extqh t2, a1, t4 # U : (stall on a1)
  132. mskql t0, a0, t0 # U :
  133. or t1, t4, t1 # E :
  134. mskqh t1, a0, t1 # U : (stall on t1)
  135. or t0, t1, t1 # E : (stall on t1)
  136. or t1, t6, t6 # E :
  137. cmpbge zero, t6, t8 # E : (stall)
  138. lda t6, -1 # E : for masking just below
  139. bne t8, $u_final # U : (stall)
  140. mskql t6, a1, t6 # U : mask out the bits we have
  141. or t6, t2, t2 # E : already extracted before (stall)
  142. cmpbge zero, t2, t8 # E : testing eos (stall)
  143. bne t8, $u_late_head_exit # U : (stall)
  144. /* Finally, we've got all the stupid leading edge cases taken care
  145. of and we can set up to enter the main loop. */
  146. stq_u t1, 0(a0) # L : store first output word
  147. addq a0, 8, a0 # E :
  148. extql t2, a1, t0 # U : position ho-bits of lo word
  149. ldq_u t2, 8(a1) # U : read next high-order source word
  150. addq a1, 8, a1 # E :
  151. cmpbge zero, t2, t8 # E : (stall for t2)
  152. nop # E :
  153. bne t8, $u_eos # U : (stall)
  154. /* Unaligned copy main loop. In order to avoid reading too much,
  155. the loop is structured to detect zeros in aligned source words.
  156. This has, unfortunately, effectively pulled half of a loop
  157. iteration out into the head and half into the tail, but it does
  158. prevent nastiness from accumulating in the very thing we want
  159. to run as fast as possible.
  160. On entry to this basic block:
  161. t0 == the shifted high-order bits from the previous source word
  162. t2 == the unshifted current source word
  163. We further know that t2 does not contain a null terminator. */
  164. .align 3
  165. $u_loop:
  166. extqh t2, a1, t1 # U : extract high bits for current word
  167. addq a1, 8, a1 # E : (stall)
  168. extql t2, a1, t3 # U : extract low bits for next time (stall)
  169. addq a0, 8, a0 # E :
  170. or t0, t1, t1 # E : current dst word now complete
  171. ldq_u t2, 0(a1) # L : Latency=3 load high word for next time
  172. stq_u t1, -8(a0) # L : save the current word (stall)
  173. mov t3, t0 # E :
  174. cmpbge zero, t2, t8 # E : test new word for eos
  175. beq t8, $u_loop # U : (stall)
  176. nop
  177. nop
  178. /* We've found a zero somewhere in the source word we just read.
  179. If it resides in the lower half, we have one (probably partial)
  180. word to write out, and if it resides in the upper half, we
  181. have one full and one partial word left to write out.
  182. On entry to this basic block:
  183. t0 == the shifted high-order bits from the previous source word
  184. t2 == the unshifted current source word. */
  185. $u_eos:
  186. extqh t2, a1, t1 # U :
  187. or t0, t1, t1 # E : first (partial) source word complete (stall)
  188. cmpbge zero, t1, t8 # E : is the null in this first bit? (stall)
  189. bne t8, $u_final # U : (stall)
  190. $u_late_head_exit:
  191. stq_u t1, 0(a0) # L : the null was in the high-order bits
  192. addq a0, 8, a0 # E :
  193. extql t2, a1, t1 # U :
  194. cmpbge zero, t1, t8 # E : (stall)
  195. /* Take care of a final (probably partial) result word.
  196. On entry to this basic block:
  197. t1 == assembled source word
  198. t8 == cmpbge mask that found the null. */
  199. $u_final:
  200. negq t8, t6 # E : isolate low bit set
  201. and t6, t8, t12 # E : (stall)
  202. and t12, 0x80, t6 # E : avoid dest word load if we can (stall)
  203. bne t6, 1f # U : (stall)
  204. ldq_u t0, 0(a0) # E :
  205. subq t12, 1, t6 # E :
  206. or t6, t12, t8 # E : (stall)
  207. zapnot t1, t6, t1 # U : kill source bytes >= null (stall)
  208. zap t0, t8, t0 # U : kill dest bytes <= null (2 cycle data stall)
  209. or t0, t1, t1 # E : (stall)
  210. nop
  211. nop
  212. 1: stq_u t1, 0(a0) # L :
  213. ret (t9) # L0 : Latency=3
  214. nop
  215. nop
  216. /* Unaligned copy entry point. */
  217. .align 4
  218. $unaligned:
  219. ldq_u t1, 0(a1) # L : load first source word
  220. and a0, 7, t4 # E : find dest misalignment
  221. and a1, 7, t5 # E : find src misalignment
  222. /* Conditionally load the first destination word and a bytemask
  223. with 0xff indicating that the destination byte is sacrosanct. */
  224. mov zero, t0 # E :
  225. mov zero, t6 # E :
  226. beq t4, 1f # U :
  227. ldq_u t0, 0(a0) # L :
  228. lda t6, -1 # E :
  229. mskql t6, a0, t6 # U :
  230. nop
  231. nop
  232. nop
  233. 1:
  234. subq a1, t4, a1 # E : sub dest misalignment from src addr
  235. /* If source misalignment is larger than dest misalignment, we need
  236. extra startup checks to avoid SEGV. */
  237. cmplt t4, t5, t12 # E :
  238. beq t12, $u_head # U :
  239. lda t2, -1 # E : mask out leading garbage in source
  240. mskqh t2, t5, t2 # U :
  241. ornot t1, t2, t3 # E : (stall)
  242. cmpbge zero, t3, t8 # E : is there a zero? (stall)
  243. beq t8, $u_head # U : (stall)
  244. /* At this point we've found a zero in the first partial word of
  245. the source. We need to isolate the valid source data and mask
  246. it into the original destination data. (Incidentally, we know
  247. that we'll need at least one byte of that original dest word.) */
  248. ldq_u t0, 0(a0) # L :
  249. negq t8, t6 # E : build bitmask of bytes <= zero
  250. and t6, t8, t12 # E : (stall)
  251. and a1, 7, t5 # E :
  252. subq t12, 1, t6 # E :
  253. or t6, t12, t8 # E : (stall)
  254. srl t12, t5, t12 # U : adjust final null return value
  255. zapnot t2, t8, t2 # U : prepare source word; mirror changes (stall)
  256. and t1, t2, t1 # E : to source validity mask
  257. extql t2, a1, t2 # U :
  258. extql t1, a1, t1 # U : (stall)
  259. andnot t0, t2, t0 # .. e1 : zero place for source to reside (stall)
  260. or t0, t1, t1 # e1 : and put it there
  261. stq_u t1, 0(a0) # .. e0 : (stall)
  262. ret (t9) # e1 :
  263. nop
  264. .end __stxcpy