ev6-stxncpy.S 11 KB

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