memcpy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /* Experimentally off - libc_hidden_proto(memcpy) */
  39. void *
  40. memcpy(void *pdst, const void *psrc, size_t pn)
  41. {
  42. /* Now we want the parameters put in special registers.
  43. Make sure the compiler is able to make something useful of this.
  44. As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
  45. If gcc was allright, it really would need no temporaries, and no
  46. stack space to save stuff on. */
  47. register void *return_dst __asm__ ("r10") = pdst;
  48. register unsigned char *dst __asm__ ("r13") = pdst;
  49. register unsigned const char *src __asm__ ("r11") = psrc;
  50. register int n __asm__ ("r12") = pn;
  51. /* When src is aligned but not dst, this makes a few extra needless
  52. cycles. I believe it would take as many to check that the
  53. re-alignment was unnecessary. */
  54. if (((unsigned long) dst & 3) != 0
  55. /* Don't align if we wouldn't copy more than a few bytes; so we
  56. don't have to check further for overflows. */
  57. && n >= 3)
  58. {
  59. if ((unsigned long) dst & 1)
  60. {
  61. n--;
  62. *dst = *src;
  63. src++;
  64. dst++;
  65. }
  66. if ((unsigned long) dst & 2)
  67. {
  68. n -= 2;
  69. *(short *) dst = *(short *) src;
  70. src += 2;
  71. dst += 2;
  72. }
  73. }
  74. /* Decide which copying method to use. */
  75. if (n >= MEMCPY_BY_BLOCK_THRESHOLD)
  76. {
  77. /* It is not optimal to tell the compiler about clobbering any
  78. registers; that will move the saving/restoring of those registers
  79. to the function prologue/epilogue, and make non-movem sizes
  80. suboptimal. */
  81. __asm__ __volatile__
  82. ("\
  83. ;; GCC does promise correct register allocations, but let's \n\
  84. ;; make sure it keeps its promises. \n\
  85. .ifnc %0-%1-%2,$r13-$r11-$r12 \n\
  86. .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\
  87. .endif \n\
  88. \n\
  89. ;; Save the registers we'll use in the movem process \n\
  90. ;; on the stack. \n\
  91. subq 11*4,sp \n\
  92. movem r10,[sp] \n\
  93. \n\
  94. ;; Now we've got this: \n\
  95. ;; r11 - src \n\
  96. ;; r13 - dst \n\
  97. ;; r12 - n \n\
  98. \n\
  99. ;; Update n for the first loop. \n\
  100. subq 44,r12 \n\
  101. 0: \n\
  102. "
  103. #ifdef __arch_common_v10_v32
  104. /* Cater to branch offset difference between v32 and v10. We
  105. assume the branch below has an 8-bit offset. */
  106. " setf\n"
  107. #endif
  108. " movem [r11+],r10 \n\
  109. subq 44,r12 \n\
  110. bge 0b \n\
  111. movem r10,[r13+] \n\
  112. \n\
  113. ;; Compensate for last loop underflowing n. \n\
  114. addq 44,r12 \n\
  115. \n\
  116. ;; Restore registers from stack. \n\
  117. movem [sp+],r10"
  118. /* Outputs. */
  119. : "=r" (dst), "=r" (src), "=r" (n)
  120. /* Inputs. */
  121. : "0" (dst), "1" (src), "2" (n));
  122. }
  123. while (n >= 16)
  124. {
  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. *(long *) dst = *(long *) src; dst += 4; src += 4;
  129. n -= 16;
  130. }
  131. switch (n)
  132. {
  133. case 0:
  134. break;
  135. case 1:
  136. *dst = *src;
  137. break;
  138. case 2:
  139. *(short *) dst = *(short *) src;
  140. break;
  141. case 3:
  142. *(short *) dst = *(short *) src; dst += 2; src += 2;
  143. *dst = *src;
  144. break;
  145. case 4:
  146. *(long *) dst = *(long *) src;
  147. break;
  148. case 5:
  149. *(long *) dst = *(long *) src; dst += 4; src += 4;
  150. *dst = *src;
  151. break;
  152. case 6:
  153. *(long *) dst = *(long *) src; dst += 4; src += 4;
  154. *(short *) dst = *(short *) src;
  155. break;
  156. case 7:
  157. *(long *) dst = *(long *) src; dst += 4; src += 4;
  158. *(short *) dst = *(short *) src; dst += 2; src += 2;
  159. *dst = *src;
  160. break;
  161. case 8:
  162. *(long *) dst = *(long *) src; dst += 4; src += 4;
  163. *(long *) dst = *(long *) src;
  164. break;
  165. case 9:
  166. *(long *) dst = *(long *) src; dst += 4; src += 4;
  167. *(long *) dst = *(long *) src; dst += 4; src += 4;
  168. *dst = *src;
  169. break;
  170. case 10:
  171. *(long *) dst = *(long *) src; dst += 4; src += 4;
  172. *(long *) dst = *(long *) src; dst += 4; src += 4;
  173. *(short *) dst = *(short *) src;
  174. break;
  175. case 11:
  176. *(long *) dst = *(long *) src; dst += 4; src += 4;
  177. *(long *) dst = *(long *) src; dst += 4; src += 4;
  178. *(short *) dst = *(short *) src; dst += 2; src += 2;
  179. *dst = *src;
  180. break;
  181. case 12:
  182. *(long *) dst = *(long *) src; dst += 4; src += 4;
  183. *(long *) dst = *(long *) src; dst += 4; src += 4;
  184. *(long *) dst = *(long *) src;
  185. break;
  186. case 13:
  187. *(long *) dst = *(long *) src; dst += 4; src += 4;
  188. *(long *) dst = *(long *) src; dst += 4; src += 4;
  189. *(long *) dst = *(long *) src; dst += 4; src += 4;
  190. *dst = *src;
  191. break;
  192. case 14:
  193. *(long *) dst = *(long *) src; dst += 4; src += 4;
  194. *(long *) dst = *(long *) src; dst += 4; src += 4;
  195. *(long *) dst = *(long *) src; dst += 4; src += 4;
  196. *(short *) dst = *(short *) src;
  197. break;
  198. case 15:
  199. *(long *) dst = *(long *) src; dst += 4; src += 4;
  200. *(long *) dst = *(long *) src; dst += 4; src += 4;
  201. *(long *) dst = *(long *) src; dst += 4; src += 4;
  202. *(short *) dst = *(short *) src; dst += 2; src += 2;
  203. *dst = *src;
  204. break;
  205. }
  206. return return_dst;
  207. }
  208. libc_hidden_def(memcpy)