stxncpy.S 11 KB

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