ev6-stxcpy.S 9.4 KB

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