stxncpy.S 11 KB

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