memcpy.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <sysdep.h>*/
  17. #include <endian.h>
  18. #include "sysdep.h"
  19. #ifdef __mips64
  20. #error mips32 code being compiled for mips64!
  21. #endif
  22. /* void *memcpy(void *s1, const void *s2, size_t n); */
  23. #if __BYTE_ORDER == __BIG_ENDIAN
  24. # define LWHI lwl /* high part is left in big-endian */
  25. # define SWHI swl /* high part is left in big-endian */
  26. # define LWLO lwr /* low part is right in big-endian */
  27. # define SWLO swr /* low part is right in big-endian */
  28. #else
  29. # define LWHI lwr /* high part is right in little-endian */
  30. # define SWHI swr /* high part is right in little-endian */
  31. # define LWLO lwl /* low part is left in little-endian */
  32. # define SWLO swl /* low part is left in little-endian */
  33. #endif
  34. ENTRY (memcpy)
  35. .set noreorder
  36. slti t0, a2, 8 # Less than 8?
  37. bne t0, zero, L(last8)
  38. move v0, a0 # Setup exit value before too late
  39. xor t0, a1, a0 # Find a0/a1 displacement
  40. andi t0, 0x3
  41. bne t0, zero, L(shift) # Go handle the unaligned case
  42. subu t1, zero, a1
  43. andi t1, 0x3 # a0/a1 are aligned, but are we
  44. beq t1, zero, L(chk8w) # starting in the middle of a word?
  45. subu a2, t1
  46. LWHI t0, 0(a1) # Yes we are... take care of that
  47. addu a1, t1
  48. SWHI t0, 0(a0)
  49. addu a0, t1
  50. L(chk8w):
  51. andi t0, a2, 0x1f # 32 or more bytes left?
  52. beq t0, a2, L(chk1w)
  53. subu a3, a2, t0 # Yes
  54. addu a3, a1 # a3 = end address of loop
  55. move a2, t0 # a2 = what will be left after loop
  56. L(lop8w):
  57. lw t0, 0(a1) # Loop taking 8 words at a time
  58. lw t1, 4(a1)
  59. lw t2, 8(a1)
  60. lw t3, 12(a1)
  61. lw t4, 16(a1)
  62. lw t5, 20(a1)
  63. lw t6, 24(a1)
  64. lw t7, 28(a1)
  65. addiu a0, 32
  66. addiu a1, 32
  67. sw t0, -32(a0)
  68. sw t1, -28(a0)
  69. sw t2, -24(a0)
  70. sw t3, -20(a0)
  71. sw t4, -16(a0)
  72. sw t5, -12(a0)
  73. sw t6, -8(a0)
  74. bne a1, a3, L(lop8w)
  75. sw t7, -4(a0)
  76. L(chk1w):
  77. andi t0, a2, 0x3 # 4 or more bytes left?
  78. beq t0, a2, L(last8)
  79. subu a3, a2, t0 # Yes, handle them one word at a time
  80. addu a3, a1 # a3 again end address
  81. move a2, t0
  82. L(lop1w):
  83. lw t0, 0(a1)
  84. addiu a0, 4
  85. addiu a1, 4
  86. bne a1, a3, L(lop1w)
  87. sw t0, -4(a0)
  88. L(last8):
  89. blez a2, L(lst8e) # Handle last 8 bytes, one at a time
  90. addu a3, a2, a1
  91. L(lst8l):
  92. lb t0, 0(a1)
  93. addiu a0, 1
  94. addiu a1, 1
  95. bne a1, a3, L(lst8l)
  96. sb t0, -1(a0)
  97. L(lst8e):
  98. jr ra # Bye, bye
  99. nop
  100. L(shift):
  101. subu a3, zero, a0 # Src and Dest unaligned
  102. andi a3, 0x3 # (unoptimized case...)
  103. beq a3, zero, L(shft1)
  104. subu a2, a3 # a2 = bytes left
  105. LWHI t0, 0(a1) # Take care of first odd part
  106. LWLO t0, 3(a1)
  107. addu a1, a3
  108. SWHI t0, 0(a0)
  109. addu a0, a3
  110. L(shft1):
  111. andi t0, a2, 0x3
  112. subu a3, a2, t0
  113. addu a3, a1
  114. L(shfth):
  115. LWHI t1, 0(a1) # Limp through, word by word
  116. LWLO t1, 3(a1)
  117. addiu a0, 4
  118. addiu a1, 4
  119. bne a1, a3, L(shfth)
  120. sw t1, -4(a0)
  121. b L(last8) # Handle anything which may be left
  122. move a2, t0
  123. .set reorder
  124. END (memcpy)
  125. libc_hidden_builtin_def (memcpy)