mymemcpy.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <asm.h>
  24. #include <arch/arm/cores.h>
  25. .text
  26. .align 2
  27. .global mymemcpy
  28. mymemcpy:
  29. // check for zero length copy or the same pointer
  30. cmp r2, #0
  31. cmpne r1, r0
  32. bxeq lr
  33. // save a few registers for use and the return code (input dst)
  34. stmfd sp!, {r0, r4, r5, lr}
  35. // check for forwards overlap (src > dst, distance < len)
  36. subs r3, r0, r1
  37. cmpgt r2, r3
  38. bgt .L_forwardoverlap
  39. // check for a short copy len.
  40. // 20 bytes is enough so that if a 16 byte alignment needs to happen there is at least a
  41. // wordwise copy worth of work to be done.
  42. cmp r2, #(16+4)
  43. blt .L_bytewise
  44. // see if they are similarly aligned on 4 byte boundaries
  45. eor r3, r0, r1
  46. tst r3, #3
  47. bne .L_bytewise // dissimilarly aligned, nothing we can do (for now)
  48. // check for 16 byte alignment on dst.
  49. // this will also catch src being not 4 byte aligned, since it is similarly 4 byte
  50. // aligned with dst at this point.
  51. tst r0, #15
  52. bne .L_not16bytealigned
  53. // check to see if we have at least 32 bytes of data to copy.
  54. // if not, just revert to wordwise copy
  55. cmp r2, #32
  56. blt .L_wordwise
  57. .L_bigcopy:
  58. // copy 32 bytes at a time. src & dst need to be at least 4 byte aligned,
  59. // and we need at least 32 bytes remaining to copy
  60. // save r6-r7 for use in the big copy
  61. stmfd sp!, {r6-r7}
  62. sub r2, r2, #32 // subtract an extra 32 to the len so we can avoid an extra compare
  63. .L_bigcopy_loop:
  64. ldmia r1!, {r4, r5, r6, r7}
  65. stmia r0!, {r4, r5, r6, r7}
  66. ldmia r1!, {r4, r5, r6, r7}
  67. subs r2, r2, #32
  68. stmia r0!, {r4, r5, r6, r7}
  69. bge .L_bigcopy_loop
  70. // restore r6-r7
  71. ldmfd sp!, {r6-r7}
  72. // see if we are done
  73. adds r2, r2, #32
  74. beq .L_done
  75. // less then 4 bytes left?
  76. cmp r2, #4
  77. blt .L_bytewise
  78. .L_wordwise:
  79. // copy 4 bytes at a time.
  80. // src & dst are guaranteed to be word aligned, and at least 4 bytes are left to copy.
  81. subs r2, r2, #4
  82. .L_wordwise_loop:
  83. ldr r3, [r1], #4
  84. subs r2, r2, #4
  85. str r3, [r0], #4
  86. bge .L_wordwise_loop
  87. // correct the remaining len and test for completion
  88. adds r2, r2, #4
  89. beq .L_done
  90. .L_bytewise:
  91. // simple bytewise copy
  92. ldrb r3, [r1], #1
  93. subs r2, r2, #1
  94. strb r3, [r0], #1
  95. bgt .L_bytewise
  96. .L_done:
  97. // load dst for return and restore r4,r5
  98. #if ARM_ARCH_LEVEL >= 5
  99. ldmfd sp!, {r0, r4, r5, pc}
  100. #else
  101. ldmfd sp!, {r0, r4, r5, lr}
  102. bx lr
  103. #endif
  104. .L_not16bytealigned:
  105. // dst is not 16 byte aligned, so we will copy up to 15 bytes to get it aligned.
  106. // src is guaranteed to be similarly word aligned with dst.
  107. // set the condition flags based on the alignment.
  108. lsl r12, r0, #28
  109. rsb r12, r12, #0
  110. msr CPSR_f, r12 // move into NZCV fields in CPSR
  111. // move as many bytes as necessary to get the dst aligned
  112. ldrvsb r3, [r1], #1 // V set
  113. ldrcsh r4, [r1], #2 // C set
  114. ldreq r5, [r1], #4 // Z set
  115. strvsb r3, [r0], #1
  116. strcsh r4, [r0], #2
  117. streq r5, [r0], #4
  118. ldmmiia r1!, {r3-r4} // N set
  119. stmmiia r0!, {r3-r4}
  120. // fix the remaining len
  121. sub r2, r2, r12, lsr #28
  122. // test to see what we should do now
  123. cmp r2, #32
  124. bge .L_bigcopy
  125. b .L_wordwise
  126. // src and dest overlap 'forwards' or dst > src
  127. .L_forwardoverlap:
  128. // do a bytewise reverse copy for now
  129. add r1, r1, r2
  130. add r0, r0, r2
  131. .L_bytewisereverse:
  132. // simple bytewise reverse copy
  133. ldrb r3, [r1], #-1
  134. subs r2, r2, #1
  135. strb r3, [r0], #-1
  136. bgt .L_bytewisereverse
  137. b .L_done