memcpy.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Hartvig Ekner <hartvige@mips.com>, 2002.
  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 <features.h>
  16. #include <sysdep.h>
  17. #include <endian.h>
  18. /* void *memcpy(void *s1, const void *s2, size_t n); */
  19. #ifdef __mips64
  20. #include <sys/asm.h>
  21. #if __BYTE_ORDER == __BIG_ENDIAN
  22. # define LDHI ldl /* high part is left in big-endian */
  23. # define SDHI sdl /* high part is left in big-endian */
  24. # define LDLO ldr /* low part is right in big-endian */
  25. # define SDLO sdr /* low part is right in big-endian */
  26. #else
  27. # define LDHI ldr /* high part is right in little-endian */
  28. # define SDHI sdr /* high part is right in little-endian */
  29. # define LDLO ldl /* low part is left in little-endian */
  30. # define SDLO sdl /* low part is left in little-endian */
  31. #endif
  32. ENTRY (memcpy)
  33. .set noreorder
  34. slti t0, a2, 16 # Less than 16?
  35. bne t0, zero, L(last16)
  36. move v0, a0 # Setup exit value before too late
  37. xor t0, a1, a0 # Find a0/a1 displacement
  38. andi t0, 0x7
  39. bne t0, zero, L(shift) # Go handle the unaligned case
  40. PTR_SUBU t1, zero, a1
  41. andi t1, 0x7 # a0/a1 are aligned, but are we
  42. beq t1, zero, L(chk8w) # starting in the middle of a word?
  43. PTR_SUBU a2, t1
  44. LDHI t0, 0(a1) # Yes we are... take care of that
  45. PTR_ADDU a1, t1
  46. SDHI t0, 0(a0)
  47. PTR_ADDU a0, t1
  48. L(chk8w):
  49. andi t0, a2, 0x3f # 64 or more bytes left?
  50. beq t0, a2, L(chk1w)
  51. PTR_SUBU a3, a2, t0 # Yes
  52. PTR_ADDU a3, a1 # a3 = end address of loop
  53. move a2, t0 # a2 = what will be left after loop
  54. L(lop8w):
  55. ld t0, 0(a1) # Loop taking 8 words at a time
  56. ld t1, 8(a1)
  57. ld t2, 16(a1)
  58. ld t3, 24(a1)
  59. ld ta0, 32(a1)
  60. ld ta1, 40(a1)
  61. ld ta2, 48(a1)
  62. ld ta3, 56(a1)
  63. PTR_ADDIU a0, 64
  64. PTR_ADDIU a1, 64
  65. sd t0, -64(a0)
  66. sd t1, -56(a0)
  67. sd t2, -48(a0)
  68. sd t3, -40(a0)
  69. sd ta0, -32(a0)
  70. sd ta1, -24(a0)
  71. sd ta2, -16(a0)
  72. bne a1, a3, L(lop8w)
  73. sd ta3, -8(a0)
  74. L(chk1w):
  75. andi t0, a2, 0x7 # 8 or more bytes left?
  76. beq t0, a2, L(last16)
  77. PTR_SUBU a3, a2, t0 # Yes, handle them one dword at a time
  78. PTR_ADDU a3, a1 # a3 again end address
  79. move a2, t0
  80. L(lop1w):
  81. ld t0, 0(a1)
  82. PTR_ADDIU a0, 8
  83. PTR_ADDIU a1, 8
  84. bne a1, a3, L(lop1w)
  85. sd t0, -8(a0)
  86. L(last16):
  87. blez a2, L(lst16e) # Handle last 16 bytes, one at a time
  88. PTR_ADDU a3, a2, a1
  89. L(lst16l):
  90. lb t0, 0(a1)
  91. PTR_ADDIU a0, 1
  92. PTR_ADDIU a1, 1
  93. bne a1, a3, L(lst16l)
  94. sb t0, -1(a0)
  95. L(lst16e):
  96. jr ra # Bye, bye
  97. nop
  98. L(shift):
  99. PTR_SUBU a3, zero, a0 # Src and Dest unaligned
  100. andi a3, 0x7 # (unoptimized case...)
  101. beq a3, zero, L(shft1)
  102. PTR_SUBU a2, a3 # a2 = bytes left
  103. LDHI t0, 0(a1) # Take care of first odd part
  104. LDLO t0, 7(a1)
  105. PTR_ADDU a1, a3
  106. SDHI t0, 0(a0)
  107. PTR_ADDU a0, a3
  108. L(shft1):
  109. andi t0, a2, 0x7
  110. PTR_SUBU a3, a2, t0
  111. PTR_ADDU a3, a1
  112. L(shfth):
  113. LDHI t1, 0(a1) # Limp through, dword by dword
  114. LDLO t1, 7(a1)
  115. PTR_ADDIU a0, 8
  116. PTR_ADDIU a1, 8
  117. bne a1, a3, L(shfth)
  118. sd t1, -8(a0)
  119. b L(last16) # Handle anything which may be left
  120. move a2, t0
  121. .set reorder
  122. END (memcpy)
  123. #else /* !__mips64 */
  124. #if __BYTE_ORDER == __BIG_ENDIAN
  125. # define LWHI lwl /* high part is left in big-endian */
  126. # define SWHI swl /* high part is left in big-endian */
  127. # define LWLO lwr /* low part is right in big-endian */
  128. # define SWLO swr /* low part is right in big-endian */
  129. #else
  130. # define LWHI lwr /* high part is right in little-endian */
  131. # define SWHI swr /* high part is right in little-endian */
  132. # define LWLO lwl /* low part is left in little-endian */
  133. # define SWLO swl /* low part is left in little-endian */
  134. #endif
  135. ENTRY (memcpy)
  136. .set noreorder
  137. slti t0, a2, 8 # Less than 8?
  138. bne t0, zero, L(last8)
  139. move v0, a0 # Setup exit value before too late
  140. xor t0, a1, a0 # Find a0/a1 displacement
  141. andi t0, 0x3
  142. bne t0, zero, L(shift) # Go handle the unaligned case
  143. subu t1, zero, a1
  144. andi t1, 0x3 # a0/a1 are aligned, but are we
  145. beq t1, zero, L(chk8w) # starting in the middle of a word?
  146. subu a2, t1
  147. LWHI t0, 0(a1) # Yes we are... take care of that
  148. addu a1, t1
  149. SWHI t0, 0(a0)
  150. addu a0, t1
  151. L(chk8w):
  152. andi t0, a2, 0x1f # 32 or more bytes left?
  153. beq t0, a2, L(chk1w)
  154. subu a3, a2, t0 # Yes
  155. addu a3, a1 # a3 = end address of loop
  156. move a2, t0 # a2 = what will be left after loop
  157. L(lop8w):
  158. lw t0, 0(a1) # Loop taking 8 words at a time
  159. lw t1, 4(a1)
  160. lw t2, 8(a1)
  161. lw t3, 12(a1)
  162. lw t4, 16(a1)
  163. lw t5, 20(a1)
  164. lw t6, 24(a1)
  165. lw t7, 28(a1)
  166. addiu a0, 32
  167. addiu a1, 32
  168. sw t0, -32(a0)
  169. sw t1, -28(a0)
  170. sw t2, -24(a0)
  171. sw t3, -20(a0)
  172. sw t4, -16(a0)
  173. sw t5, -12(a0)
  174. sw t6, -8(a0)
  175. bne a1, a3, L(lop8w)
  176. sw t7, -4(a0)
  177. L(chk1w):
  178. andi t0, a2, 0x3 # 4 or more bytes left?
  179. beq t0, a2, L(last8)
  180. subu a3, a2, t0 # Yes, handle them one word at a time
  181. addu a3, a1 # a3 again end address
  182. move a2, t0
  183. L(lop1w):
  184. lw t0, 0(a1)
  185. addiu a0, 4
  186. addiu a1, 4
  187. bne a1, a3, L(lop1w)
  188. sw t0, -4(a0)
  189. L(last8):
  190. blez a2, L(lst8e) # Handle last 8 bytes, one at a time
  191. addu a3, a2, a1
  192. L(lst8l):
  193. lb t0, 0(a1)
  194. addiu a0, 1
  195. addiu a1, 1
  196. bne a1, a3, L(lst8l)
  197. sb t0, -1(a0)
  198. L(lst8e):
  199. jr ra # Bye, bye
  200. nop
  201. L(shift):
  202. subu a3, zero, a0 # Src and Dest unaligned
  203. andi a3, 0x3 # (unoptimized case...)
  204. beq a3, zero, L(shft1)
  205. subu a2, a3 # a2 = bytes left
  206. LWHI t0, 0(a1) # Take care of first odd part
  207. LWLO t0, 3(a1)
  208. addu a1, a3
  209. SWHI t0, 0(a0)
  210. addu a0, a3
  211. L(shft1):
  212. andi t0, a2, 0x3
  213. subu a3, a2, t0
  214. addu a3, a1
  215. L(shfth):
  216. LWHI t1, 0(a1) # Limp through, word by word
  217. LWLO t1, 3(a1)
  218. addiu a0, 4
  219. addiu a1, 4
  220. bne a1, a3, L(shfth)
  221. sw t1, -4(a0)
  222. b L(last8) # Handle anything which may be left
  223. move a2, t0
  224. .set reorder
  225. END (memcpy)
  226. #endif /* !__mips64 */
  227. libc_hidden_def(memcpy)