memmove.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* $OpenBSD: memmove.S,v 1.4 2013/06/13 15:03:08 deraadt Exp $ */
  2. /*-
  3. * Copyright (c) 1991, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ralph Campbell.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "DEFS.h"
  34. /*
  35. * memcpy(to, from, len)
  36. always copy forward
  37. *
  38. * memmove(to, from, len), bcopy(from, to, len)
  39. * both handle overlap
  40. */
  41. LEAF(memcpy, 0)
  42. .set noreorder
  43. move v0, a0 # swap from and to
  44. move a0, a1
  45. move a1, v0
  46. PTR_ADDU t0, a0, a2 # t0 = end of s1 region
  47. sltu t1, a1, t0
  48. sltu t2, a0, a1
  49. j forward # do forward copy
  50. slt t2, a2, 12 # check for small copy
  51. ALEAF(memmove)
  52. .set noreorder
  53. move v0, a0 # swap from and to
  54. move a0, a1
  55. move a1, v0
  56. ALEAF(bcopy)
  57. .set noreorder
  58. PTR_ADDU t0, a0, a2 # t0 = end of s1 region
  59. sltu t1, a1, t0
  60. sltu t2, a0, a1
  61. and t1, t1, t2 # t1 = true if from < to < (from+len)
  62. beq t1, zero, forward # non overlapping, do forward copy
  63. slt t2, a2, 12 # check for small copy
  64. ble a2, zero, 2f
  65. PTR_ADDU t1, a1, a2 # t1 = end of to region
  66. 1:
  67. lb v1, -1(t0) # copy bytes backwards,
  68. PTR_SUBU t0, t0, 1 # doesnt happen often so do slow way
  69. PTR_SUBU t1, t1, 1
  70. bne t0, a0, 1b
  71. sb v1, 0(t1)
  72. 2:
  73. j ra
  74. nop
  75. forward:
  76. bne t2, zero, smallcpy # do a small bcopy
  77. xor v1, a0, a1 # compare low two bits of addresses
  78. and v1, v1, 3
  79. PTR_SUBU a3, zero, a1 # compute # bytes to word align address
  80. beq v1, zero, aligned # addresses can be word aligned
  81. and a3, a3, 3
  82. beq a3, zero, 1f
  83. PTR_SUBU a2, a2, a3 # subtract from remaining count
  84. LWHI v1, 0(a0) # get next 4 bytes (unaligned)
  85. LWLO v1, 3(a0)
  86. PTR_ADDU a0, a0, a3
  87. SWHI v1, 0(a1) # store 1, 2, or 3 bytes to align a1
  88. PTR_ADDU a1, a1, a3
  89. 1:
  90. and v1, a2, 3 # compute number of words left
  91. PTR_SUBU a3, a2, v1
  92. move a2, v1
  93. PTR_ADDU a3, a3, a0 # compute ending address
  94. 2:
  95. LWHI v1, 0(a0) # copy words a0 unaligned, a1 aligned
  96. LWLO v1, 3(a0)
  97. PTR_ADDU a0, a0, 4
  98. sw v1, 0(a1)
  99. PTR_ADDU a1, a1, 4
  100. bne a0, a3, 2b
  101. nop # We have to do this mmu-bug.
  102. b smallcpy
  103. nop
  104. aligned:
  105. beq a3, zero, 1f
  106. PTR_SUBU a2, a2, a3 # subtract from remaining count
  107. LWHI v1, 0(a0) # copy 1, 2, or 3 bytes to align
  108. PTR_ADDU a0, a0, a3
  109. SWHI v1, 0(a1)
  110. PTR_ADDU a1, a1, a3
  111. 1:
  112. and v1, a2, 3 # compute number of whole words left
  113. PTR_SUBU a3, a2, v1
  114. move a2, v1
  115. PTR_ADDU a3, a3, a0 # compute ending address
  116. 2:
  117. lw v1, 0(a0) # copy words
  118. PTR_ADDU a0, a0, 4
  119. sw v1, 0(a1)
  120. bne a0, a3, 2b
  121. PTR_ADDU a1, a1, 4
  122. smallcpy:
  123. ble a2, zero, 2f
  124. PTR_ADDU a3, a2, a0 # compute ending address
  125. 1:
  126. lbu v1, 0(a0) # copy bytes
  127. PTR_ADDU a0, a0, 1
  128. sb v1, 0(a1)
  129. bne a0, a3, 1b
  130. PTR_ADDU a1, a1, 1 # MMU BUG ? can not do -1(a1) at 0x80000000!!
  131. 2:
  132. j ra
  133. nop
  134. END(memcpy)