memcpy.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Optimized memcpy for Xtensa.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sysdep.h>
  16. #include <bits/xtensa-config.h>
  17. .macro src_b r, w0, w1
  18. #ifdef __XTENSA_EB__
  19. src \r, \w0, \w1
  20. #else
  21. src \r, \w1, \w0
  22. #endif
  23. .endm
  24. .macro ssa8 r
  25. #ifdef __XTENSA_EB__
  26. ssa8b \r
  27. #else
  28. ssa8l \r
  29. #endif
  30. .endm
  31. /* If the Xtensa Unaligned Load Exception option is not used, this
  32. code can run a few cycles faster by relying on the low address bits
  33. being ignored. However, if the code is then run with an Xtensa ISS
  34. client that checks for unaligned accesses, it will produce a lot of
  35. warning messages. Set this flag to disable the use of unaligned
  36. accesses and keep the ISS happy. */
  37. #define UNALIGNED_ADDRESSES_CHECKED 1
  38. /* Do not use .literal_position in the ENTRY macro. */
  39. #undef LITERAL_POSITION
  40. #define LITERAL_POSITION
  41. /* void *memcpy (void *dst, const void *src, size_t len)
  42. The algorithm is as follows:
  43. If the destination is unaligned, align it by conditionally
  44. copying 1- and/or 2-byte pieces.
  45. If the source is aligned, copy 16 bytes with a loop, and then finish up
  46. with 8, 4, 2, and 1-byte copies conditional on the length.
  47. Else (if source is unaligned), do the same, but use SRC to align the
  48. source data.
  49. This code tries to use fall-through branches for the common
  50. case of aligned source and destination and multiple of 4 (or 8) length. */
  51. /* Byte by byte copy. */
  52. .text
  53. .align 4
  54. .literal_position
  55. __memcpy_aux:
  56. /* Skip a byte to get 1 mod 4 alignment for LOOPNEZ
  57. (0 mod 4 alignment for LBEG). */
  58. .byte 0
  59. .Lbytecopy:
  60. #if XCHAL_HAVE_LOOPS
  61. loopnez a4, 2f
  62. #else
  63. beqz a4, 2f
  64. add a7, a3, a4 /* a7 = end address for source */
  65. #endif
  66. 1: l8ui a6, a3, 0
  67. addi a3, a3, 1
  68. s8i a6, a5, 0
  69. addi a5, a5, 1
  70. #if !XCHAL_HAVE_LOOPS
  71. blt a3, a7, 1b
  72. #endif
  73. 2: abi_ret
  74. /* Destination is unaligned. */
  75. .align 4
  76. .Ldst1mod2: /* dst is only byte aligned */
  77. /* Do short copies byte-by-byte. */
  78. _bltui a4, 7, .Lbytecopy
  79. /* Copy 1 byte. */
  80. l8ui a6, a3, 0
  81. addi a3, a3, 1
  82. addi a4, a4, -1
  83. s8i a6, a5, 0
  84. addi a5, a5, 1
  85. /* Return to main algorithm if dst is now aligned. */
  86. _bbci.l a5, 1, .Ldstaligned
  87. .Ldst2mod4: /* dst has 16-bit alignment */
  88. /* Do short copies byte-by-byte. */
  89. _bltui a4, 6, .Lbytecopy
  90. /* Copy 2 bytes. */
  91. l8ui a6, a3, 0
  92. l8ui a7, a3, 1
  93. addi a3, a3, 2
  94. addi a4, a4, -2
  95. s8i a6, a5, 0
  96. s8i a7, a5, 1
  97. addi a5, a5, 2
  98. /* dst is now aligned; return to main algorithm. */
  99. j .Ldstaligned
  100. ENTRY (memcpy)
  101. /* a2 = dst, a3 = src, a4 = len */
  102. mov a5, a2 /* copy dst so that a2 is return value */
  103. _bbsi.l a2, 0, .Ldst1mod2
  104. _bbsi.l a2, 1, .Ldst2mod4
  105. .Ldstaligned:
  106. /* Get number of loop iterations with 16B per iteration. */
  107. srli a7, a4, 4
  108. /* Check if source is aligned. */
  109. movi a8, 3
  110. _bany a3, a8, .Lsrcunaligned
  111. /* Destination and source are word-aligned, use word copy. */
  112. #if XCHAL_HAVE_LOOPS
  113. loopnez a7, 2f
  114. #else
  115. beqz a7, 2f
  116. slli a8, a7, 4
  117. add a8, a8, a3 /* a8 = end of last 16B source chunk */
  118. #endif
  119. 1: l32i a6, a3, 0
  120. l32i a7, a3, 4
  121. s32i a6, a5, 0
  122. l32i a6, a3, 8
  123. s32i a7, a5, 4
  124. l32i a7, a3, 12
  125. s32i a6, a5, 8
  126. addi a3, a3, 16
  127. s32i a7, a5, 12
  128. addi a5, a5, 16
  129. #if !XCHAL_HAVE_LOOPS
  130. blt a3, a8, 1b
  131. #endif
  132. /* Copy any leftover pieces smaller than 16B. */
  133. 2: bbci.l a4, 3, 3f
  134. /* Copy 8 bytes. */
  135. l32i a6, a3, 0
  136. l32i a7, a3, 4
  137. addi a3, a3, 8
  138. s32i a6, a5, 0
  139. s32i a7, a5, 4
  140. addi a5, a5, 8
  141. 3: bbsi.l a4, 2, 4f
  142. bbsi.l a4, 1, 5f
  143. bbsi.l a4, 0, 6f
  144. abi_ret
  145. /* Copy 4 bytes. */
  146. 4: l32i a6, a3, 0
  147. addi a3, a3, 4
  148. s32i a6, a5, 0
  149. addi a5, a5, 4
  150. bbsi.l a4, 1, 5f
  151. bbsi.l a4, 0, 6f
  152. abi_ret
  153. /* Copy 2 bytes. */
  154. 5: l16ui a6, a3, 0
  155. addi a3, a3, 2
  156. s16i a6, a5, 0
  157. addi a5, a5, 2
  158. bbsi.l a4, 0, 6f
  159. abi_ret
  160. /* Copy 1 byte. */
  161. 6: l8ui a6, a3, 0
  162. s8i a6, a5, 0
  163. .Ldone:
  164. abi_ret
  165. /* Destination is aligned; source is unaligned. */
  166. .align 4
  167. .Lsrcunaligned:
  168. /* Avoid loading anything for zero-length copies. */
  169. _beqz a4, .Ldone
  170. /* Copy 16 bytes per iteration for word-aligned dst and
  171. unaligned src. */
  172. ssa8 a3 /* set shift amount from byte offset */
  173. #if UNALIGNED_ADDRESSES_CHECKED
  174. and a11, a3, a8 /* save unalignment offset for below */
  175. sub a3, a3, a11 /* align a3 */
  176. #endif
  177. l32i a6, a3, 0 /* load first word */
  178. #if XCHAL_HAVE_LOOPS
  179. loopnez a7, 2f
  180. #else
  181. beqz a7, 2f
  182. slli a10, a7, 4
  183. add a10, a10, a3 /* a10 = end of last 16B source chunk */
  184. #endif
  185. 1: l32i a7, a3, 4
  186. l32i a8, a3, 8
  187. src_b a6, a6, a7
  188. s32i a6, a5, 0
  189. l32i a9, a3, 12
  190. src_b a7, a7, a8
  191. s32i a7, a5, 4
  192. l32i a6, a3, 16
  193. src_b a8, a8, a9
  194. s32i a8, a5, 8
  195. addi a3, a3, 16
  196. src_b a9, a9, a6
  197. s32i a9, a5, 12
  198. addi a5, a5, 16
  199. #if !XCHAL_HAVE_LOOPS
  200. blt a3, a10, 1b
  201. #endif
  202. 2: bbci.l a4, 3, 3f
  203. /* Copy 8 bytes. */
  204. l32i a7, a3, 4
  205. l32i a8, a3, 8
  206. src_b a6, a6, a7
  207. s32i a6, a5, 0
  208. addi a3, a3, 8
  209. src_b a7, a7, a8
  210. s32i a7, a5, 4
  211. addi a5, a5, 8
  212. mov a6, a8
  213. 3: bbci.l a4, 2, 4f
  214. /* Copy 4 bytes. */
  215. l32i a7, a3, 4
  216. addi a3, a3, 4
  217. src_b a6, a6, a7
  218. s32i a6, a5, 0
  219. addi a5, a5, 4
  220. mov a6, a7
  221. 4:
  222. #if UNALIGNED_ADDRESSES_CHECKED
  223. add a3, a3, a11 /* readjust a3 with correct misalignment */
  224. #endif
  225. bbsi.l a4, 1, 5f
  226. bbsi.l a4, 0, 6f
  227. abi_ret
  228. /* Copy 2 bytes. */
  229. 5: l8ui a6, a3, 0
  230. l8ui a7, a3, 1
  231. addi a3, a3, 2
  232. s8i a6, a5, 0
  233. s8i a7, a5, 1
  234. addi a5, a5, 2
  235. bbsi.l a4, 0, 6f
  236. abi_ret
  237. /* Copy 1 byte. */
  238. 6: l8ui a6, a3, 0
  239. s8i a6, a5, 0
  240. abi_ret
  241. libc_hidden_def (memcpy)