memcpy.S 3.6 KB

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