ev6-stxncpy.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * arch/alpha/lib/ev6-stxncpy.S
  3. * 21264 version contributed by Rick Gorton <rick.gorton@api-networks.com>
  4. *
  5. * Copy no more than COUNT bytes of the null-terminated string from
  6. * SRC to DST.
  7. *
  8. * This is an internal routine used by strncpy, stpncpy, and strncat.
  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. * a2 = COUNT
  17. *
  18. * Furthermore, COUNT may not be zero.
  19. *
  20. * On output:
  21. * t0 = last word written
  22. * t10 = bitmask (with one bit set) indicating the byte position of
  23. * the end of the range specified by COUNT
  24. * t12 = bitmask (with one bit set) indicating the last byte written
  25. * a0 = unaligned address of the last *word* written
  26. * a2 = the number of full words left in COUNT
  27. *
  28. * Furthermore, v0, a3-a5, t11, and $at are untouched.
  29. *
  30. * Much of the information about 21264 scheduling/coding comes from:
  31. * Compiler Writer's Guide for the Alpha 21264
  32. * abbreviated as 'CWG' in other comments here
  33. * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
  34. * Scheduling notation:
  35. * E - either cluster
  36. * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
  37. * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
  38. * Try not to change the actual algorithm if possible for consistency.
  39. */
  40. #include <asm/regdef.h>
  41. .set noat
  42. .set noreorder
  43. .text
  44. /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
  45. doesn't like putting the entry point for a procedure somewhere in the
  46. middle of the procedure descriptor. Work around this by putting the
  47. aligned copy in its own procedure descriptor */
  48. .ent stxncpy_aligned
  49. .align 4
  50. stxncpy_aligned:
  51. .frame sp, 0, t9, 0
  52. .prologue 0
  53. /* On entry to this basic block:
  54. t0 == the first destination word for masking back in
  55. t1 == the first source word. */
  56. /* Create the 1st output word and detect 0's in the 1st input word. */
  57. lda t2, -1 # E : build a mask against false zero
  58. mskqh t2, a1, t2 # U : detection in the src word (stall)
  59. mskqh t1, a1, t3 # U :
  60. ornot t1, t2, t2 # E : (stall)
  61. mskql t0, a1, t0 # U : assemble the first output word
  62. cmpbge zero, t2, t8 # E : bits set iff null found
  63. or t0, t3, t0 # E : (stall)
  64. beq a2, $a_eoc # U :
  65. bne t8, $a_eos # U :
  66. nop
  67. nop
  68. nop
  69. /* On entry to this basic block:
  70. t0 == a source word not containing a null. */
  71. /*
  72. * nops here to:
  73. * separate store quads from load quads
  74. * limit of 1 bcond/quad to permit training
  75. */
  76. $a_loop:
  77. stq_u t0, 0(a0) # L :
  78. addq a0, 8, a0 # E :
  79. subq a2, 1, a2 # E :
  80. nop
  81. ldq_u t0, 0(a1) # L :
  82. addq a1, 8, a1 # E :
  83. cmpbge zero, t0, t8 # E :
  84. beq a2, $a_eoc # U :
  85. beq t8, $a_loop # U :
  86. nop
  87. nop
  88. nop
  89. /* Take care of the final (partial) word store. At this point
  90. the end-of-count bit is set in t8 iff it applies.
  91. On entry to this basic block we have:
  92. t0 == the source word containing the null
  93. t8 == the cmpbge mask that found it. */
  94. $a_eos:
  95. negq t8, t12 # E : find low bit set
  96. and t8, t12, t12 # E : (stall)
  97. /* For the sake of the cache, don't read a destination word
  98. if we're not going to need it. */
  99. and t12, 0x80, t6 # E : (stall)
  100. bne t6, 1f # U : (stall)
  101. /* We're doing a partial word store and so need to combine
  102. our source and original destination words. */
  103. ldq_u t1, 0(a0) # L :
  104. subq t12, 1, t6 # E :
  105. or t12, t6, t8 # E : (stall)
  106. zapnot t0, t8, t0 # U : clear src bytes > null (stall)
  107. zap t1, t8, t1 # .. e1 : clear dst bytes <= null
  108. or t0, t1, t0 # e1 : (stall)
  109. nop
  110. nop
  111. 1: stq_u t0, 0(a0) # L :
  112. ret (t9) # L0 : Latency=3
  113. nop
  114. nop
  115. /* Add the end-of-count bit to the eos detection bitmask. */
  116. $a_eoc:
  117. or t10, t8, t8 # E :
  118. br $a_eos # L0 : Latency=3
  119. nop
  120. nop
  121. .end stxncpy_aligned
  122. .align 4
  123. .ent __stxncpy
  124. .globl __stxncpy
  125. __stxncpy:
  126. .frame sp, 0, t9, 0
  127. .prologue 0
  128. /* Are source and destination co-aligned? */
  129. xor a0, a1, t1 # E :
  130. and a0, 7, t0 # E : find dest misalignment
  131. and t1, 7, t1 # E : (stall)
  132. addq a2, t0, a2 # E : bias count by dest misalignment (stall)
  133. subq a2, 1, a2 # E :
  134. and a2, 7, t2 # E : (stall)
  135. srl a2, 3, a2 # U : a2 = loop counter = (count - 1)/8 (stall)
  136. addq zero, 1, t10 # E :
  137. sll t10, t2, t10 # U : t10 = bitmask of last count byte
  138. bne t1, $unaligned # U :
  139. /* We are co-aligned; take care of a partial first word. */
  140. ldq_u t1, 0(a1) # L : load first src word
  141. addq a1, 8, a1 # E :
  142. beq t0, stxncpy_aligned # U : avoid loading dest word if not needed
  143. ldq_u t0, 0(a0) # L :
  144. nop
  145. nop
  146. br stxncpy_aligned # .. e1 :
  147. nop
  148. nop
  149. nop
  150. /* The source and destination are not co-aligned. Align the destination
  151. and cope. We have to be very careful about not reading too much and
  152. causing a SEGV. */
  153. .align 4
  154. $u_head:
  155. /* We know just enough now to be able to assemble the first
  156. full source word. We can still find a zero at the end of it
  157. that prevents us from outputting the whole thing.
  158. On entry to this basic block:
  159. t0 == the first dest word, unmasked
  160. t1 == the shifted low bits of the first source word
  161. t6 == bytemask that is -1 in dest word bytes */
  162. ldq_u t2, 8(a1) # L : Latency=3 load second src word
  163. addq a1, 8, a1 # E :
  164. mskql t0, a0, t0 # U : mask trailing garbage in dst
  165. extqh t2, a1, t4 # U : (3 cycle stall on t2)
  166. or t1, t4, t1 # E : first aligned src word complete (stall)
  167. mskqh t1, a0, t1 # U : mask leading garbage in src (stall)
  168. or t0, t1, t0 # E : first output word complete (stall)
  169. or t0, t6, t6 # E : mask original data for zero test (stall)
  170. cmpbge zero, t6, t8 # E :
  171. beq a2, $u_eocfin # U :
  172. lda t6, -1 # E :
  173. nop
  174. bne t8, $u_final # U :
  175. mskql t6, a1, t6 # U : mask out bits already seen
  176. stq_u t0, 0(a0) # L : store first output word
  177. or t6, t2, t2 # E : (stall)
  178. cmpbge zero, t2, t8 # E : find nulls in second partial
  179. addq a0, 8, a0 # E :
  180. subq a2, 1, a2 # E :
  181. bne t8, $u_late_head_exit # U :
  182. /* Finally, we've got all the stupid leading edge cases taken care
  183. of and we can set up to enter the main loop. */
  184. extql t2, a1, t1 # U : position hi-bits of lo word
  185. beq a2, $u_eoc # U :
  186. ldq_u t2, 8(a1) # L : read next high-order source word
  187. addq a1, 8, a1 # E :
  188. extqh t2, a1, t0 # U : position lo-bits of hi word (stall)
  189. cmpbge zero, t2, t8 # E :
  190. nop
  191. bne t8, $u_eos # U :
  192. /* Unaligned copy main loop. In order to avoid reading too much,
  193. the loop is structured to detect zeros in aligned source words.
  194. This has, unfortunately, effectively pulled half of a loop
  195. iteration out into the head and half into the tail, but it does
  196. prevent nastiness from accumulating in the very thing we want
  197. to run as fast as possible.
  198. On entry to this basic block:
  199. t0 == the shifted low-order bits from the current source word
  200. t1 == the shifted high-order bits from the previous source word
  201. t2 == the unshifted current source word
  202. We further know that t2 does not contain a null terminator. */
  203. .align 4
  204. $u_loop:
  205. or t0, t1, t0 # E : current dst word now complete
  206. subq a2, 1, a2 # E : decrement word count
  207. extql t2, a1, t1 # U : extract low bits for next time
  208. addq a0, 8, a0 # E :
  209. stq_u t0, -8(a0) # U : save the current word
  210. beq a2, $u_eoc # U :
  211. ldq_u t2, 8(a1) # U : Latency=3 load high word for next time
  212. addq a1, 8, a1 # E :
  213. extqh t2, a1, t0 # U : extract low bits (2 cycle stall)
  214. cmpbge zero, t2, t8 # E : test new word for eos
  215. nop
  216. beq t8, $u_loop # U :
  217. /* We've found a zero somewhere in the source word we just read.
  218. If it resides in the lower half, we have one (probably partial)
  219. word to write out, and if it resides in the upper half, we
  220. have one full and one partial word left to write out.
  221. On entry to this basic block:
  222. t0 == the shifted low-order bits from the current source word
  223. t1 == the shifted high-order bits from the previous source word
  224. t2 == the unshifted current source word. */
  225. $u_eos:
  226. or t0, t1, t0 # E : first (partial) source word complete
  227. nop
  228. cmpbge zero, t0, t8 # E : is the null in this first bit? (stall)
  229. bne t8, $u_final # U : (stall)
  230. stq_u t0, 0(a0) # L : the null was in the high-order bits
  231. addq a0, 8, a0 # E :
  232. subq a2, 1, a2 # E :
  233. nop
  234. $u_late_head_exit:
  235. extql t2, a1, t0 # U :
  236. cmpbge zero, t0, t8 # E :
  237. or t8, t10, t6 # E : (stall)
  238. cmoveq a2, t6, t8 # E : Latency=2, extra map slot (stall)
  239. /* Take care of a final (probably partial) result word.
  240. On entry to this basic block:
  241. t0 == assembled source word
  242. t8 == cmpbge mask that found the null. */
  243. $u_final:
  244. negq t8, t6 # E : isolate low bit set
  245. and t6, t8, t12 # E : (stall)
  246. and t12, 0x80, t6 # E : avoid dest word load if we can (stall)
  247. bne t6, 1f # U : (stall)
  248. ldq_u t1, 0(a0) # L :
  249. subq t12, 1, t6 # E :
  250. or t6, t12, t8 # E : (stall)
  251. zapnot t0, t8, t0 # U : kill source bytes > null
  252. zap t1, t8, t1 # U : kill dest bytes <= null
  253. or t0, t1, t0 # E : (stall)
  254. nop
  255. nop
  256. 1: stq_u t0, 0(a0) # L :
  257. ret (t9) # L0 : Latency=3
  258. /* Got to end-of-count before end of string.
  259. On entry to this basic block:
  260. t1 == the shifted high-order bits from the previous source word */
  261. $u_eoc:
  262. and a1, 7, t6 # E : avoid final load if possible
  263. sll t10, t6, t6 # U : (stall)
  264. and t6, 0xff, t6 # E : (stall)
  265. bne t6, 1f # U : (stall)
  266. ldq_u t2, 8(a1) # L : load final src word
  267. nop
  268. extqh t2, a1, t0 # U : extract low bits for last word (stall)
  269. or t1, t0, t1 # E : (stall)
  270. 1: cmpbge zero, t1, t8 # E :
  271. mov t1, t0 # E :
  272. $u_eocfin: # end-of-count, final word
  273. or t10, t8, t8 # E :
  274. br $u_final # L0 : Latency=3
  275. /* Unaligned copy entry point. */
  276. .align 4
  277. $unaligned:
  278. ldq_u t1, 0(a1) # L : load first source word
  279. and a0, 7, t4 # E : find dest misalignment
  280. and a1, 7, t5 # E : find src misalignment
  281. /* Conditionally load the first destination word and a bytemask
  282. with 0xff indicating that the destination byte is sacrosanct. */
  283. mov zero, t0 # E :
  284. mov zero, t6 # E :
  285. beq t4, 1f # U :
  286. ldq_u t0, 0(a0) # L :
  287. lda t6, -1 # E :
  288. mskql t6, a0, t6 # U :
  289. nop
  290. nop
  291. subq a1, t4, a1 # E : sub dest misalignment from src addr
  292. /* If source misalignment is larger than dest misalignment, we need
  293. extra startup checks to avoid SEGV. */
  294. 1: cmplt t4, t5, t12 # E :
  295. extql t1, a1, t1 # U : shift src into place
  296. lda t2, -1 # E : for creating masks later
  297. beq t12, $u_head # U : (stall)
  298. extql t2, a1, t2 # U :
  299. cmpbge zero, t1, t8 # E : is there a zero?
  300. andnot t2, t6, t2 # E : dest mask for a single word copy
  301. or t8, t10, t5 # E : test for end-of-count too
  302. cmpbge zero, t2, t3 # E :
  303. cmoveq a2, t5, t8 # E : Latency=2, extra map slot
  304. nop # E : keep with cmoveq
  305. andnot t8, t3, t8 # E : (stall)
  306. beq t8, $u_head # U :
  307. /* At this point we've found a zero in the first partial word of
  308. the source. We need to isolate the valid source data and mask
  309. it into the original destination data. (Incidentally, we know
  310. that we'll need at least one byte of that original dest word.) */
  311. ldq_u t0, 0(a0) # L :
  312. negq t8, t6 # E : build bitmask of bytes <= zero
  313. mskqh t1, t4, t1 # U :
  314. and t6, t8, t12 # E :
  315. subq t12, 1, t6 # E : (stall)
  316. or t6, t12, t8 # E : (stall)
  317. zapnot t2, t8, t2 # U : prepare source word; mirror changes (stall)
  318. zapnot t1, t8, t1 # U : to source validity mask
  319. andnot t0, t2, t0 # E : zero place for source to reside
  320. or t0, t1, t0 # E : and put it there (stall both t0, t1)
  321. stq_u t0, 0(a0) # L : (stall)
  322. ret (t9) # L0 : Latency=3
  323. nop
  324. nop
  325. nop
  326. .end __stxncpy