memcpy.S 6.3 KB

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