stxncpy.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /* Copy no more than COUNT bytes of the null-terminated string from
  15. SRC to DST.
  16. This is an internal routine used by strncpy, stpncpy, and strncat.
  17. As such, it uses special linkage conventions to make implementation
  18. of these public functions more efficient.
  19. On input:
  20. t9 = return address
  21. a0 = DST
  22. a1 = SRC
  23. a2 = COUNT
  24. Furthermore, COUNT may not be zero.
  25. On output:
  26. t0 = last word written
  27. t8 = bitmask (with one bit set) indicating the last byte written
  28. t10 = bitmask (with one bit set) indicating the byte position of
  29. the end of the range specified by COUNT
  30. a0 = unaligned address of the last *word* written
  31. a2 = the number of full words left in COUNT
  32. Furthermore, v0, a3-a5, t11, and t12 are untouched.
  33. */
  34. /* This is generally scheduled for the EV5, but should still be pretty
  35. good for the EV4 too. */
  36. #include <sysdep.h>
  37. .set noat
  38. .set noreorder
  39. .text
  40. .type __stxncpy, @function
  41. .globl __stxncpy
  42. .usepv __stxncpy, no
  43. cfi_startproc
  44. cfi_return_column (t9)
  45. /* On entry to this basic block:
  46. t0 == the first destination word for masking back in
  47. t1 == the first source word. */
  48. .align 3
  49. stxncpy_aligned:
  50. /* Create the 1st output word and detect 0's in the 1st input word. */
  51. lda t2, -1 # e1 : build a mask against false zero
  52. mskqh t2, a1, t2 # e0 : detection in the src word
  53. mskqh t1, a1, t3 # e0 :
  54. ornot t1, t2, t2 # .. e1 :
  55. mskql t0, a1, t0 # e0 : assemble the first output word
  56. cmpbge zero, t2, t7 # .. e1 : bits set iff null found
  57. or t0, t3, t0 # e0 :
  58. beq a2, $a_eoc # .. e1 :
  59. bne t7, $a_eos # .. e1 :
  60. /* On entry to this basic block:
  61. t0 == a source word not containing a null. */
  62. $a_loop:
  63. stq_u t0, 0(a0) # e0 :
  64. addq a0, 8, a0 # .. e1 :
  65. ldq_u t0, 0(a1) # e0 :
  66. addq a1, 8, a1 # .. e1 :
  67. subq a2, 1, a2 # e0 :
  68. cmpbge zero, t0, t7 # .. e1 (stall)
  69. beq a2, $a_eoc # e1 :
  70. beq t7, $a_loop # e1 :
  71. /* Take care of the final (partial) word store. At this point
  72. the end-of-count bit is set in t7 iff it applies.
  73. On entry to this basic block we have:
  74. t0 == the source word containing the null
  75. t7 == the cmpbge mask that found it. */
  76. $a_eos:
  77. negq t7, t8 # e0 : find low bit set
  78. and t7, t8, t8 # e1 (stall)
  79. /* For the sake of the cache, don't read a destination word
  80. if we're not going to need it. */
  81. and t8, 0x80, t6 # e0 :
  82. bne t6, 1f # .. e1 (zdb)
  83. /* We're doing a partial word store and so need to combine
  84. our source and original destination words. */
  85. ldq_u t1, 0(a0) # e0 :
  86. subq t8, 1, t6 # .. e1 :
  87. or t8, t6, t7 # e0 :
  88. unop #
  89. zapnot t0, t7, t0 # e0 : clear src bytes > null
  90. zap t1, t7, t1 # .. e1 : clear dst bytes <= null
  91. or t0, t1, t0 # e1 :
  92. 1: stq_u t0, 0(a0) # e0 :
  93. ret (t9) # e1 :
  94. /* Add the end-of-count bit to the eos detection bitmask. */
  95. $a_eoc:
  96. or t10, t7, t7
  97. br $a_eos
  98. .align 3
  99. __stxncpy:
  100. /* Are source and destination co-aligned? */
  101. lda t2, -1
  102. xor a0, a1, t1
  103. srl t2, 1, t2
  104. and a0, 7, t0 # find dest misalignment
  105. cmovlt a2, t2, a2 # bound neg count to LONG_MAX
  106. and t1, 7, t1
  107. addq a2, t0, a2 # bias count by dest misalignment
  108. subq a2, 1, a2
  109. and a2, 7, t2
  110. srl a2, 3, a2 # a2 = loop counter = (count - 1)/8
  111. addq zero, 1, t10
  112. sll t10, t2, t10 # t10 = bitmask of last count byte
  113. bne t1, $unaligned
  114. /* We are co-aligned; take care of a partial first word. */
  115. ldq_u t1, 0(a1) # e0 : load first src word
  116. addq a1, 8, a1 # .. e1 :
  117. beq t0, stxncpy_aligned # avoid loading dest word if not needed
  118. ldq_u t0, 0(a0) # e0 :
  119. br stxncpy_aligned # .. e1 :
  120. /* The source and destination are not co-aligned. Align the destination
  121. and cope. We have to be very careful about not reading too much and
  122. causing a SEGV. */
  123. .align 3
  124. $u_head:
  125. /* We know just enough now to be able to assemble the first
  126. full source word. We can still find a zero at the end of it
  127. that prevents us from outputting the whole thing.
  128. On entry to this basic block:
  129. t0 == the first dest word, unmasked
  130. t1 == the shifted low bits of the first source word
  131. t6 == bytemask that is -1 in dest word bytes */
  132. ldq_u t2, 8(a1) # e0 : load second src word
  133. addq a1, 8, a1 # .. e1 :
  134. mskql t0, a0, t0 # e0 : mask trailing garbage in dst
  135. extqh t2, a1, t4 # e0 :
  136. or t1, t4, t1 # e1 : first aligned src word complete
  137. mskqh t1, a0, t1 # e0 : mask leading garbage in src
  138. or t0, t1, t0 # e0 : first output word complete
  139. or t0, t6, t6 # e1 : mask original data for zero test
  140. cmpbge zero, t6, t7 # e0 :
  141. beq a2, $u_eocfin # .. e1 :
  142. lda t6, -1 # e0 :
  143. bne t7, $u_final # .. e1 :
  144. mskql t6, a1, t6 # e0 : mask out bits already seen
  145. nop # .. e1 :
  146. stq_u t0, 0(a0) # e0 : store first output word
  147. or t6, t2, t2 # .. e1 :
  148. cmpbge zero, t2, t7 # e0 : find nulls in second partial
  149. addq a0, 8, a0 # .. e1 :
  150. subq a2, 1, a2 # e0 :
  151. bne t7, $u_late_head_exit # .. e1 :
  152. /* Finally, we've got all the stupid leading edge cases taken care
  153. of and we can set up to enter the main loop. */
  154. extql t2, a1, t1 # e0 : position hi-bits of lo word
  155. beq a2, $u_eoc # .. e1 :
  156. ldq_u t2, 8(a1) # e0 : read next high-order source word
  157. addq a1, 8, a1 # .. e1 :
  158. extqh t2, a1, t0 # e0 : position lo-bits of hi word
  159. cmpbge zero, t2, t7 # .. e1 : test new word for eos
  160. nop # e0 :
  161. bne t7, $u_eos # .. e1 :
  162. /* Unaligned copy main loop. In order to avoid reading too much,
  163. the loop is structured to detect zeros in aligned source words.
  164. This has, unfortunately, effectively pulled half of a loop
  165. iteration out into the head and half into the tail, but it does
  166. prevent nastiness from accumulating in the very thing we want
  167. to run as fast as possible.
  168. On entry to this basic block:
  169. t0 == the shifted low-order bits from the current source word
  170. t1 == the shifted high-order bits from the previous source word
  171. t2 == the unshifted current source word
  172. We further know that t2 does not contain a null terminator. */
  173. .align 3
  174. $u_loop:
  175. or t0, t1, t0 # e0 : current dst word now complete
  176. subq a2, 1, a2 # .. e1 : decrement word count
  177. stq_u t0, 0(a0) # e0 : save the current word
  178. addq a0, 8, a0 # .. e1 :
  179. extql t2, a1, t1 # e0 : extract high bits for next time
  180. beq a2, $u_eoc # .. e1 :
  181. ldq_u t2, 8(a1) # e0 : load high word for next time
  182. addq a1, 8, a1 # .. e1 :
  183. nop # e0 :
  184. cmpbge zero, t2, t7 # .. e1 : test new word for eos
  185. extqh t2, a1, t0 # e0 : extract low bits for current word
  186. beq t7, $u_loop # .. e1 :
  187. /* We've found a zero somewhere in the source word we just read.
  188. If it resides in the lower half, we have one (probably partial)
  189. word to write out, and if it resides in the upper half, we
  190. have one full and one partial word left to write out.
  191. On entry to this basic block:
  192. t0 == the shifted low-order bits from the current source word
  193. t1 == the shifted high-order bits from the previous source word
  194. t2 == the unshifted current source word. */
  195. $u_eos:
  196. or t0, t1, t0 # e0 : first (partial) source word complete
  197. cmpbge zero, t0, t7 # e0 : is the null in this first bit?
  198. bne t7, $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 # e0 :
  202. $u_late_head_exit:
  203. extql t2, a1, t0 # e0 :
  204. cmpbge zero, t0, t7 # e0 :
  205. or t7, t10, t6 # e1 :
  206. cmoveq a2, t6, t7 # e0 :
  207. /* Take care of a final (probably partial) result word.
  208. On entry to this basic block:
  209. t0 == assembled source word
  210. t7 == cmpbge mask that found the null. */
  211. $u_final:
  212. negq t7, t6 # e0 : isolate low bit set
  213. and t6, t7, t8 # e1 :
  214. and t8, 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 t8, 1, t6 # .. e1 :
  218. or t6, t8, t7 # e0 :
  219. zapnot t0, t7, t0 # .. e1 : kill source bytes > null
  220. zap t1, t7, 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 : avoid src word load if we can
  232. ldq_u t2, 8(a1) # e0 : load final src word
  233. nop # .. e1 :
  234. extqh t2, a1, t0 # e0 : extract high bits for last word
  235. or t1, t0, t1 # e1 :
  236. 1: cmpbge zero, t1, t7
  237. mov t1, t0
  238. $u_eocfin: # end-of-count, final word
  239. or t10, t7, t7
  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. 1:
  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. cmplt t4, t5, t8 # e1 :
  260. extql t1, a1, t1 # .. e0 : shift src into place
  261. lda t2, -1 # e0 : for creating masks later
  262. beq t8, $u_head # e1 :
  263. mskqh t2, t5, t2 # e0 : begin src byte validity mask
  264. cmpbge zero, t1, t7 # .. e1 : is there a zero?
  265. extql t2, a1, t2 # e0 :
  266. or t7, t10, t5 # .. e1 : test for end-of-count too
  267. cmpbge zero, t2, t3 # e0 :
  268. cmoveq a2, t5, t7 # .. e1 :
  269. andnot t7, t3, t7 # e0 :
  270. beq t7, $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 t7, t6 # .. e1 : build bitmask of bytes <= zero
  277. mskqh t1, t4, t1 # e0 :
  278. and t6, t7, t8 # .. e1 :
  279. subq t8, 1, t6 # e0 :
  280. or t6, t8, t7 # e1 :
  281. zapnot t2, t7, t2 # e0 : prepare source word; mirror changes
  282. zapnot t1, t7, 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. cfi_endproc