memmove.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2008-2009 PetaLogix
  4. * Copyright (C) 2007 John Williams
  5. *
  6. * Reasonably optimised generic C-code for memcpy on Microblaze
  7. * This is generic C code to do efficient, alignment-aware memmove.
  8. *
  9. * It is based on demo code originally Copyright 2001 by Intel Corp, taken from
  10. * http://www.embedded.com/showArticle.jhtml?articleID=19205567
  11. *
  12. * Attempts were made, unsuccessfully, to contact the original
  13. * author of this code (Michael Morrow, Intel). Below is the original
  14. * copyright notice.
  15. *
  16. * This software has been developed by Intel Corporation.
  17. * Intel specifically disclaims all warranties, express or
  18. * implied, and all liability, including consequential and
  19. * other indirect damages, for the use of this program, including
  20. * liability for infringement of any proprietary rights,
  21. * and including the warranties of merchantability and fitness
  22. * for a particular purpose. Intel does not assume any
  23. * responsibility for and errors which may appear in this program
  24. * not any responsibility to update it.
  25. */
  26. #include <linux/types.h>
  27. #include <linux/stddef.h>
  28. #include <linux/compiler.h>
  29. #include <linux/module.h>
  30. #include <linux/string.h>
  31. #ifdef __HAVE_ARCH_MEMMOVE
  32. #ifndef CONFIG_OPT_LIB_FUNCTION
  33. void *memmove(void *v_dst, const void *v_src, __kernel_size_t c)
  34. {
  35. const char *src = v_src;
  36. char *dst = v_dst;
  37. if (!c)
  38. return v_dst;
  39. /* Use memcpy when source is higher than dest */
  40. if (v_dst <= v_src)
  41. return memcpy(v_dst, v_src, c);
  42. /* copy backwards, from end to beginning */
  43. src += c;
  44. dst += c;
  45. /* Simple, byte oriented memmove. */
  46. while (c--)
  47. *--dst = *--src;
  48. return v_dst;
  49. }
  50. #else /* CONFIG_OPT_LIB_FUNCTION */
  51. void *memmove(void *v_dst, const void *v_src, __kernel_size_t c)
  52. {
  53. const char *src = v_src;
  54. char *dst = v_dst;
  55. const uint32_t *i_src;
  56. uint32_t *i_dst;
  57. if (!c)
  58. return v_dst;
  59. /* Use memcpy when source is higher than dest */
  60. if (v_dst <= v_src)
  61. return memcpy(v_dst, v_src, c);
  62. /* The following code tries to optimize the copy by using unsigned
  63. * alignment. This will work fine if both source and destination are
  64. * aligned on the same boundary. However, if they are aligned on
  65. * different boundaries shifts will be necessary. This might result in
  66. * bad performance on MicroBlaze systems without a barrel shifter.
  67. */
  68. /* FIXME this part needs more test */
  69. /* Do a descending copy - this is a bit trickier! */
  70. dst += c;
  71. src += c;
  72. if (c >= 4) {
  73. unsigned value, buf_hold;
  74. /* Align the destination to a word boundary. */
  75. /* This is done in an endian independent manner. */
  76. switch ((unsigned long)dst & 3) {
  77. case 3:
  78. *--dst = *--src;
  79. --c;
  80. case 2:
  81. *--dst = *--src;
  82. --c;
  83. case 1:
  84. *--dst = *--src;
  85. --c;
  86. }
  87. i_dst = (void *)dst;
  88. /* Choose a copy scheme based on the source */
  89. /* alignment relative to dstination. */
  90. switch ((unsigned long)src & 3) {
  91. case 0x0: /* Both byte offsets are aligned */
  92. i_src = (const void *)src;
  93. for (; c >= 4; c -= 4)
  94. *--i_dst = *--i_src;
  95. src = (const void *)i_src;
  96. break;
  97. case 0x1: /* Unaligned - Off by 1 */
  98. /* Word align the source */
  99. i_src = (const void *) (((unsigned)src + 4) & ~3);
  100. #ifndef __MICROBLAZEEL__
  101. /* Load the holding buffer */
  102. buf_hold = *--i_src >> 24;
  103. for (; c >= 4; c -= 4) {
  104. value = *--i_src;
  105. *--i_dst = buf_hold << 8 | value;
  106. buf_hold = value >> 24;
  107. }
  108. #else
  109. /* Load the holding buffer */
  110. buf_hold = (*--i_src & 0xFF) << 24;
  111. for (; c >= 4; c -= 4) {
  112. value = *--i_src;
  113. *--i_dst = buf_hold | ((value & 0xFFFFFF00)>>8);
  114. buf_hold = (value & 0xFF) << 24;
  115. }
  116. #endif
  117. /* Realign the source */
  118. src = (const void *)i_src;
  119. src += 1;
  120. break;
  121. case 0x2: /* Unaligned - Off by 2 */
  122. /* Word align the source */
  123. i_src = (const void *) (((unsigned)src + 4) & ~3);
  124. #ifndef __MICROBLAZEEL__
  125. /* Load the holding buffer */
  126. buf_hold = *--i_src >> 16;
  127. for (; c >= 4; c -= 4) {
  128. value = *--i_src;
  129. *--i_dst = buf_hold << 16 | value;
  130. buf_hold = value >> 16;
  131. }
  132. #else
  133. /* Load the holding buffer */
  134. buf_hold = (*--i_src & 0xFFFF) << 16;
  135. for (; c >= 4; c -= 4) {
  136. value = *--i_src;
  137. *--i_dst = buf_hold | ((value & 0xFFFF0000)>>16);
  138. buf_hold = (value & 0xFFFF) << 16;
  139. }
  140. #endif
  141. /* Realign the source */
  142. src = (const void *)i_src;
  143. src += 2;
  144. break;
  145. case 0x3: /* Unaligned - Off by 3 */
  146. /* Word align the source */
  147. i_src = (const void *) (((unsigned)src + 4) & ~3);
  148. #ifndef __MICROBLAZEEL__
  149. /* Load the holding buffer */
  150. buf_hold = *--i_src >> 8;
  151. for (; c >= 4; c -= 4) {
  152. value = *--i_src;
  153. *--i_dst = buf_hold << 24 | value;
  154. buf_hold = value >> 8;
  155. }
  156. #else
  157. /* Load the holding buffer */
  158. buf_hold = (*--i_src & 0xFFFFFF) << 8;
  159. for (; c >= 4; c -= 4) {
  160. value = *--i_src;
  161. *--i_dst = buf_hold | ((value & 0xFF000000)>> 24);
  162. buf_hold = (value & 0xFFFFFF) << 8;
  163. }
  164. #endif
  165. /* Realign the source */
  166. src = (const void *)i_src;
  167. src += 3;
  168. break;
  169. }
  170. dst = (void *)i_dst;
  171. }
  172. /* simple fast copy, ... unless a cache boundary is crossed */
  173. /* Finish off any remaining bytes */
  174. switch (c) {
  175. case 4:
  176. *--dst = *--src;
  177. case 3:
  178. *--dst = *--src;
  179. case 2:
  180. *--dst = *--src;
  181. case 1:
  182. *--dst = *--src;
  183. }
  184. return v_dst;
  185. }
  186. #endif /* CONFIG_OPT_LIB_FUNCTION */
  187. EXPORT_SYMBOL(memmove);
  188. #endif /* __HAVE_ARCH_MEMMOVE */