stxncpy.S 11 KB

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