memcpy.S 6.3 KB

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