memcpy.S 6.1 KB

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