memcpy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* A memcpy for CRIS.
  2. Copyright (C) 1994-2008 Axis Communications.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Neither the name of Axis Communications nor the names of its
  10. contributors may be used to endorse or promote products derived
  11. from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
  16. COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  17. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  21. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  22. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE. */
  24. /* FIXME: This file should really only be used for reference, as the
  25. result is somewhat depending on gcc generating what we expect rather
  26. than what we describe. An assembly file should be used instead. */
  27. #include <string.h>
  28. #ifdef __arch_v32
  29. /* For CRISv32, movem is very cheap. */
  30. #define MEMCPY_BY_BLOCK_THRESHOLD (44)
  31. #else
  32. /* Break even between movem and move16 is really at 38.7 * 2, but
  33. modulo 44, so up to the next multiple of 44, we use ordinary code. */
  34. #define MEMCPY_BY_BLOCK_THRESHOLD (44 * 2)
  35. #endif
  36. /* No name ambiguities in this file. */
  37. __asm__ (".syntax no_register_prefix");
  38. void *
  39. memcpy(void *pdst, const void *psrc, size_t pn)
  40. {
  41. /* Now we want the parameters put in special registers.
  42. Make sure the compiler is able to make something useful of this.
  43. As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
  44. If gcc was allright, it really would need no temporaries, and no
  45. stack space to save stuff on. */
  46. register void *return_dst __asm__ ("r10") = pdst;
  47. register unsigned char *dst __asm__ ("r13") = pdst;
  48. register unsigned const char *src __asm__ ("r11") = psrc;
  49. register int n __asm__ ("r12") = pn;
  50. /* When src is aligned but not dst, this makes a few extra needless
  51. cycles. I believe it would take as many to check that the
  52. re-alignment was unnecessary. */
  53. if (((unsigned long) dst & 3) != 0
  54. /* Don't align if we wouldn't copy more than a few bytes; so we
  55. don't have to check further for overflows. */
  56. && n >= 3)
  57. {
  58. if ((unsigned long) dst & 1)
  59. {
  60. n--;
  61. *dst = *src;
  62. src++;
  63. dst++;
  64. }
  65. if ((unsigned long) dst & 2)
  66. {
  67. n -= 2;
  68. *(short *) dst = *(short *) src;
  69. src += 2;
  70. dst += 2;
  71. }
  72. }
  73. /* Decide which copying method to use. */
  74. if (n >= MEMCPY_BY_BLOCK_THRESHOLD)
  75. {
  76. /* It is not optimal to tell the compiler about clobbering any
  77. registers; that will move the saving/restoring of those registers
  78. to the function prologue/epilogue, and make non-movem sizes
  79. suboptimal. */
  80. __asm__ __volatile__
  81. ("\
  82. ;; GCC does promise correct register allocations, but let's \n\
  83. ;; make sure it keeps its promises. \n\
  84. .ifnc %0-%1-%2,$r13-$r11-$r12 \n\
  85. .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\
  86. .endif \n\
  87. \n\
  88. ;; Save the registers we'll use in the movem process \n\
  89. ;; on the stack. \n\
  90. subq 11*4,sp \n\
  91. movem r10,[sp] \n\
  92. \n\
  93. ;; Now we've got this: \n\
  94. ;; r11 - src \n\
  95. ;; r13 - dst \n\
  96. ;; r12 - n \n\
  97. \n\
  98. ;; Update n for the first loop. \n\
  99. subq 44,r12 \n\
  100. 0: \n\
  101. "
  102. #ifdef __arch_common_v10_v32
  103. /* Cater to branch offset difference between v32 and v10. We
  104. assume the branch below has an 8-bit offset. */
  105. " setf\n"
  106. #endif
  107. " movem [r11+],r10 \n\
  108. subq 44,r12 \n\
  109. bge 0b \n\
  110. movem r10,[r13+] \n\
  111. \n\
  112. ;; Compensate for last loop underflowing n. \n\
  113. addq 44,r12 \n\
  114. \n\
  115. ;; Restore registers from stack. \n\
  116. movem [sp+],r10"
  117. /* Outputs. */
  118. : "=r" (dst), "=r" (src), "=r" (n)
  119. /* Inputs. */
  120. : "0" (dst), "1" (src), "2" (n));
  121. }
  122. while (n >= 16)
  123. {
  124. *(long *) dst = *(long *) src; dst += 4; src += 4;
  125. *(long *) dst = *(long *) src; dst += 4; src += 4;
  126. *(long *) dst = *(long *) src; dst += 4; src += 4;
  127. *(long *) dst = *(long *) src; dst += 4; src += 4;
  128. n -= 16;
  129. }
  130. switch (n)
  131. {
  132. case 0:
  133. break;
  134. case 1:
  135. *dst = *src;
  136. break;
  137. case 2:
  138. *(short *) dst = *(short *) src;
  139. break;
  140. case 3:
  141. *(short *) dst = *(short *) src; dst += 2; src += 2;
  142. *dst = *src;
  143. break;
  144. case 4:
  145. *(long *) dst = *(long *) src;
  146. break;
  147. case 5:
  148. *(long *) dst = *(long *) src; dst += 4; src += 4;
  149. *dst = *src;
  150. break;
  151. case 6:
  152. *(long *) dst = *(long *) src; dst += 4; src += 4;
  153. *(short *) dst = *(short *) src;
  154. break;
  155. case 7:
  156. *(long *) dst = *(long *) src; dst += 4; src += 4;
  157. *(short *) dst = *(short *) src; dst += 2; src += 2;
  158. *dst = *src;
  159. break;
  160. case 8:
  161. *(long *) dst = *(long *) src; dst += 4; src += 4;
  162. *(long *) dst = *(long *) src;
  163. break;
  164. case 9:
  165. *(long *) dst = *(long *) src; dst += 4; src += 4;
  166. *(long *) dst = *(long *) src; dst += 4; src += 4;
  167. *dst = *src;
  168. break;
  169. case 10:
  170. *(long *) dst = *(long *) src; dst += 4; src += 4;
  171. *(long *) dst = *(long *) src; dst += 4; src += 4;
  172. *(short *) dst = *(short *) src;
  173. break;
  174. case 11:
  175. *(long *) dst = *(long *) src; dst += 4; src += 4;
  176. *(long *) dst = *(long *) src; dst += 4; src += 4;
  177. *(short *) dst = *(short *) src; dst += 2; src += 2;
  178. *dst = *src;
  179. break;
  180. case 12:
  181. *(long *) dst = *(long *) src; dst += 4; src += 4;
  182. *(long *) dst = *(long *) src; dst += 4; src += 4;
  183. *(long *) dst = *(long *) src;
  184. break;
  185. case 13:
  186. *(long *) dst = *(long *) src; dst += 4; src += 4;
  187. *(long *) dst = *(long *) src; dst += 4; src += 4;
  188. *(long *) dst = *(long *) src; dst += 4; src += 4;
  189. *dst = *src;
  190. break;
  191. case 14:
  192. *(long *) dst = *(long *) src; dst += 4; src += 4;
  193. *(long *) dst = *(long *) src; dst += 4; src += 4;
  194. *(long *) dst = *(long *) src; dst += 4; src += 4;
  195. *(short *) dst = *(short *) src;
  196. break;
  197. case 15:
  198. *(long *) dst = *(long *) src; dst += 4; src += 4;
  199. *(long *) dst = *(long *) src; dst += 4; src += 4;
  200. *(long *) dst = *(long *) src; dst += 4; src += 4;
  201. *(short *) dst = *(short *) src; dst += 2; src += 2;
  202. *dst = *src;
  203. break;
  204. }
  205. return return_dst;
  206. }
  207. libc_hidden_def(memcpy)